From d41fa88996d4f541282ccd984ecbdc8c8f46d6c7 Mon Sep 17 00:00:00 2001 From: Nadia Mayor Date: Thu, 22 Jan 2026 15:48:37 -0300 Subject: [PATCH] [FME-12564] Added impression properties in link package --- splitio/link/client/types/interfaces.go | 14 +++++--- splitio/link/client/v1/impl.go | 44 +++++++++++++++++++------ splitio/link/client/v1/impl_test.go | 9 ++++- splitio/sdk/sdk.go | 10 +++--- 4 files changed, 57 insertions(+), 20 deletions(-) diff --git a/splitio/link/client/types/interfaces.go b/splitio/link/client/types/interfaces.go index 4e11855..a3da0bc 100644 --- a/splitio/link/client/types/interfaces.go +++ b/splitio/link/client/types/interfaces.go @@ -6,10 +6,10 @@ import ( ) type ClientInterface interface { - Treatment(key string, bucketingKey string, feature string, attrs map[string]interface{}) (*Result, error) - Treatments(key string, bucketingKey string, features []string, attrs map[string]interface{}) (Results, error) - TreatmentWithConfig(key string, bucketingKey string, feature string, attrs map[string]interface{}) (*Result, error) - TreatmentsWithConfig(key string, bucketingKey string, features []string, attrs map[string]interface{}) (Results, error) + Treatment(key string, bucketingKey string, feature string, attrs map[string]interface{}, optFns ...OptFn) (*Result, error) + Treatments(key string, bucketingKey string, features []string, attrs map[string]interface{}, optFns ...OptFn) (Results, error) + TreatmentWithConfig(key string, bucketingKey string, feature string, attrs map[string]interface{}, optFns ...OptFn) (*Result, error) + TreatmentsWithConfig(key string, bucketingKey string, features []string, attrs map[string]interface{}, optFns ...OptFn) (Results, error) Track(key string, trafficType string, eventType string, value *float64, properties map[string]interface{}) error SplitNames() ([]string, error) Split(name string) (*sdk.SplitView, error) @@ -24,3 +24,9 @@ type Result struct { } type Results = map[string]Result + +type Options struct { + EvaluationOptions *dtos.EvaluationOptions +} + +type OptFn = func(o *Options) diff --git a/splitio/link/client/v1/impl.go b/splitio/link/client/v1/impl.go index fba91ce..e33d070 100644 --- a/splitio/link/client/v1/impl.go +++ b/splitio/link/client/v1/impl.go @@ -26,6 +26,16 @@ type Impl struct { listenerFeedback bool } +func (c *Impl) WithEvaluationOptions(e *dtos.EvaluationOptions) types.OptFn { + return func(o *types.Options) { o.EvaluationOptions = e } +} + +func defaultOpts() types.Options { + return types.Options{ + EvaluationOptions: nil, + } +} + func New(id string, logger logging.LoggerInterface, conn transfer.RawConn, serializer serializer.Interface, listenerFeedback bool) (*Impl, error) { i := &Impl{ logger: logger, @@ -43,23 +53,27 @@ func New(id string, logger logging.LoggerInterface, conn transfer.RawConn, seria } // Treatment implements Interface -func (c *Impl) Treatment(key string, bucketingKey string, feature string, attrs map[string]interface{}) (*types.Result, error) { - return c.treatment(key, bucketingKey, feature, attrs, false) +func (c *Impl) Treatment(key string, bucketingKey string, feature string, attrs map[string]interface{}, optFns ...types.OptFn) (*types.Result, error) { + options := getOptions(optFns...) + return c.treatment(key, bucketingKey, feature, attrs, false, options.EvaluationOptions) } // TreatmentWithConfig implements types.ClientInterface -func (c *Impl) TreatmentWithConfig(key string, bucketingKey string, feature string, attrs map[string]interface{}) (*types.Result, error) { - return c.treatment(key, bucketingKey, feature, attrs, true) +func (c *Impl) TreatmentWithConfig(key string, bucketingKey string, feature string, attrs map[string]interface{}, optFns ...types.OptFn) (*types.Result, error) { + options := getOptions(optFns...) + return c.treatment(key, bucketingKey, feature, attrs, true, options.EvaluationOptions) } // Treatment implements Interface -func (c *Impl) Treatments(key string, bucketingKey string, features []string, attrs map[string]interface{}) (types.Results, error) { - return c.treatments(key, bucketingKey, features, attrs, false) +func (c *Impl) Treatments(key string, bucketingKey string, features []string, attrs map[string]interface{}, optFns ...types.OptFn) (types.Results, error) { + options := getOptions(optFns...) + return c.treatments(key, bucketingKey, features, attrs, false, options.EvaluationOptions) } // TreatmentsWithConfig implements types.ClientInterface -func (c *Impl) TreatmentsWithConfig(key string, bucketingKey string, features []string, attrs map[string]interface{}) (types.Results, error) { - return c.treatments(key, bucketingKey, features, attrs, true) +func (c *Impl) TreatmentsWithConfig(key string, bucketingKey string, features []string, attrs map[string]interface{}, optFns ...types.OptFn) (types.Results, error) { + options := getOptions(optFns...) + return c.treatments(key, bucketingKey, features, attrs, true, options.EvaluationOptions) } // Track implements types.ClientInterface @@ -139,7 +153,7 @@ func (c *Impl) Splits() ([]sdk.SplitView, error) { return views, nil } -func (c *Impl) treatment(key string, bucketingKey string, feature string, attrs map[string]interface{}, withConfig bool) (*types.Result, error) { +func (c *Impl) treatment(key string, bucketingKey string, feature string, attrs map[string]interface{}, withConfig bool, evaluationOptions *dtos.EvaluationOptions) (*types.Result, error) { var bkp *string if bucketingKey != "" { bkp = &bucketingKey @@ -174,6 +188,7 @@ func (c *Impl) treatment(key string, bucketingKey string, feature string, attrs ChangeNumber: resp.Payload.ListenerData.ChangeNumber, Label: resp.Payload.ListenerData.Label, BucketingKey: bucketingKey, + Properties: sdk.SerializeProperties(evaluationOptions), } } @@ -185,7 +200,7 @@ func (c *Impl) treatment(key string, bucketingKey string, feature string, attrs return toRet, nil } -func (c *Impl) treatments(key string, bucketingKey string, features []string, attrs map[string]interface{}, withConfig bool) (types.Results, error) { +func (c *Impl) treatments(key string, bucketingKey string, features []string, attrs map[string]interface{}, withConfig bool, evaluationOptions *dtos.EvaluationOptions) (types.Results, error) { var bkp *string if bucketingKey != "" { bkp = &bucketingKey @@ -221,6 +236,7 @@ func (c *Impl) treatments(key string, bucketingKey string, features []string, at ChangeNumber: resp.Payload.Results[idx].ListenerData.ChangeNumber, Label: resp.Payload.Results[idx].ListenerData.Label, BucketingKey: bucketingKey, + Properties: sdk.SerializeProperties(evaluationOptions), } } @@ -286,4 +302,12 @@ func (c *Impl) Shutdown() error { return c.conn.Shutdown() } +func getOptions(optFns ...types.OptFn) types.Options { + options := defaultOpts() + for _, optFn := range optFns { + optFn(&options) + } + return options +} + var _ types.ClientInterface = (*Impl)(nil) diff --git a/splitio/link/client/v1/impl_test.go b/splitio/link/client/v1/impl_test.go index 2bdbee6..d9460af 100644 --- a/splitio/link/client/v1/impl_test.go +++ b/splitio/link/client/v1/impl_test.go @@ -144,7 +144,12 @@ func TestClientGetTreatmentWithImpression(t *testing.T) { assert.NotNil(t, client) assert.Nil(t, err) - res, err := client.Treatment("key1", "buck1", "feat1", map[string]interface{}{"a": 1}) + opts := dtos.EvaluationOptions{ + Properties: map[string]interface{}{ + "pleassssse": "holaaaaa", + }, + } + res, err := client.Treatment("key1", "buck1", "feat1", map[string]interface{}{"a": 1}, client.WithEvaluationOptions(&opts)) assert.Nil(t, err) assert.Equal(t, "on", res.Treatment) validateImpression(t, &dtos.Impression{ @@ -155,6 +160,7 @@ func TestClientGetTreatmentWithImpression(t *testing.T) { Label: "l1", ChangeNumber: 1234, Time: 123, + Properties: "{\"pleassssse\":\"holaaaaa\"}", }, res.Impression) } @@ -435,5 +441,6 @@ func validateImpression(t *testing.T, expected *dtos.Impression, actual *dtos.Im assert.Equal(t, expected.Time, actual.Time) assert.Equal(t, expected.Treatment, actual.Treatment) assert.Equal(t, expected.Label, actual.Label) + assert.Equal(t, expected.Properties, actual.Properties) } diff --git a/splitio/sdk/sdk.go b/splitio/sdk/sdk.go index 64febce..af8e6bb 100644 --- a/splitio/sdk/sdk.go +++ b/splitio/sdk/sdk.go @@ -156,7 +156,7 @@ func (i *Impl) Treatment(cfg *types.ClientConfig, key string, bk *string, featur return nil, fmt.Errorf("nil result") } - imp := i.handleImpression(key, bk, feature, res, cfg.Metadata, serializeProperties(options.evaluationOptions)) + imp := i.handleImpression(key, bk, feature, res, cfg.Metadata, SerializeProperties(options.evaluationOptions)) return &EvaluationResult{ Treatment: res.Treatment, Impression: imp, @@ -188,7 +188,7 @@ func (i *Impl) Treatments(cfg *types.ClientConfig, key string, bk *string, featu var eres EvaluationResult eres.Treatment = curr.Treatment - eres.Impression = i.handleImpression(key, bk, feature, &curr, cfg.Metadata, serializeProperties(options.evaluationOptions)) + eres.Impression = i.handleImpression(key, bk, feature, &curr, cfg.Metadata, SerializeProperties(options.evaluationOptions)) eres.Config = curr.Config toRet[feature] = eres } @@ -205,7 +205,7 @@ func (i *Impl) TreatmentsByFlagSet(cfg *types.ClientConfig, key string, bk *stri for feature, curr := range res.Evaluations { var eres EvaluationResult eres.Treatment = curr.Treatment - eres.Impression = i.handleImpression(key, bk, feature, &curr, cfg.Metadata, serializeProperties(options.evaluationOptions)) + eres.Impression = i.handleImpression(key, bk, feature, &curr, cfg.Metadata, SerializeProperties(options.evaluationOptions)) eres.Config = curr.Config toRet[feature] = eres } @@ -222,7 +222,7 @@ func (i *Impl) TreatmentsByFlagSets(cfg *types.ClientConfig, key string, bk *str for feature, curr := range res.Evaluations { var eres EvaluationResult eres.Treatment = curr.Treatment - eres.Impression = i.handleImpression(key, bk, feature, &curr, cfg.Metadata, serializeProperties(options.evaluationOptions)) + eres.Impression = i.handleImpression(key, bk, feature, &curr, cfg.Metadata, SerializeProperties(options.evaluationOptions)) eres.Config = curr.Config toRet[feature] = eres } @@ -367,7 +367,7 @@ func createFallbackTreatmentCalculator(fallbackTreatmentConfig *dtos.FallbackTre return dtos.NewFallbackTreatmentCalculatorImp(&fallbackTreatmentConf) } -func serializeProperties(opts *dtos.EvaluationOptions) string { +func SerializeProperties(opts *dtos.EvaluationOptions) string { if opts == nil { return "" }