Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions splitio/link/client/types/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -24,3 +24,9 @@ type Result struct {
}

type Results = map[string]Result

type Options struct {
EvaluationOptions *dtos.EvaluationOptions
}

type OptFn = func(o *Options)
44 changes: 34 additions & 10 deletions splitio/link/client/v1/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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),
}
}

Expand All @@ -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
Expand Down Expand Up @@ -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),
}
}

Expand Down Expand Up @@ -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)
9 changes: 8 additions & 1 deletion splitio/link/client/v1/impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -155,6 +160,7 @@ func TestClientGetTreatmentWithImpression(t *testing.T) {
Label: "l1",
ChangeNumber: 1234,
Time: 123,
Properties: "{\"pleassssse\":\"holaaaaa\"}",
}, res.Impression)

}
Expand Down Expand Up @@ -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)

}
10 changes: 5 additions & 5 deletions splitio/sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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 ""
}
Expand Down