Skip to content

Commit 1dd9e5f

Browse files
Merge pull request #8 from subiradhikari/FFM-744
FFM-744: Changes to send target in authentication request
2 parents 3dace62 + a144d95 commit 1dd9e5f

File tree

7 files changed

+67
-43
lines changed

7 files changed

+67
-43
lines changed

client/client.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type CfClient struct {
3636
mux sync.RWMutex
3737
api rest.ClientWithResponsesInterface
3838
sdkKey string
39+
auth rest.AuthenticationRequest
3940
config *config
4041
environmentID string
4142
token string
@@ -81,7 +82,7 @@ func NewCfClient(sdkKey string, options ...ConfigOption) (*CfClient, error) {
8182
}
8283
}
8384

84-
go client.authenticate(ctx)
85+
go client.authenticate(ctx, client.config.target)
8586

8687
go client.retrieve(ctx)
8788

@@ -162,8 +163,19 @@ func (c *CfClient) streamConnect() {
162163
}
163164
}
164165

165-
func (c *CfClient) authenticate(ctx context.Context) {
166-
166+
func (c *CfClient) authenticate(ctx context.Context, target evaluation.Target) {
167+
t := struct {
168+
Anonymous *bool `json:"anonymous,omitempty"`
169+
Attributes *map[string]interface{} `json:"attributes,omitempty"`
170+
Identifier string `json:"identifier"`
171+
Name *string `json:"name,omitempty"`
172+
}{
173+
target.Anonymous,
174+
target.Attributes,
175+
target.Identifier,
176+
target.Name,
177+
}
178+
c.auth.Target = &t
167179
c.mux.RLock()
168180
defer c.mux.RUnlock()
169181

@@ -176,6 +188,7 @@ func (c *CfClient) authenticate(ctx context.Context) {
176188

177189
response, err := httpClient.AuthenticateWithResponse(ctx, rest.AuthenticateJSONRequestBody{
178190
ApiKey: c.sdkKey,
191+
Target: c.auth.Target,
179192
})
180193
if err != nil {
181194
c.config.Logger.Error(err)

dto/target_builder.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,18 @@ func (b *targetBuilder) Name(name string) TargetBuilderInterface {
6969

7070
// Anonymous target object
7171
func (b *targetBuilder) Anonymous(value bool) TargetBuilderInterface {
72-
b.Target.Anonymous = value
72+
b.Target.Anonymous = &value
7373
return b
7474
}
7575

7676
// Custom object
7777
func (b *targetBuilder) Custom(key string, value interface{}) TargetBuilderInterface {
78+
m := make(map[string]interface{})
7879
if b.Target.Attributes == nil {
79-
b.Target.Attributes = make(map[string]interface{})
80+
b.Target.Attributes = &m
8081
}
81-
82-
b.Target.Attributes[key] = value
82+
m[key] = value
83+
b.Target.Attributes = &m
8384
return b
8485
}
8586

evaluation/feature_test.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -435,12 +435,13 @@ func TestFeatureConfig_IntVariation(t *testing.T) {
435435
}
436436

437437
func TestServingRules_GetVariationName(t *testing.T) {
438-
438+
f := false
439439
dev := "dev"
440440
harness := "Harness"
441441
onVariationIdentifier := "v1"
442442
offVariationIdentifier := "v2"
443-
443+
m := make(map[string]interface{})
444+
m["email"] = "[email protected]"
444445
segment := &Segment{
445446
Identifier: "beta",
446447
Name: "beta",
@@ -450,12 +451,11 @@ func TestServingRules_GetVariationName(t *testing.T) {
450451
Tags: nil,
451452
Version: 1,
452453
}
453-
454454
target := &Target{
455455
Identifier: harness,
456456
Name: &harness,
457-
Anonymous: false,
458-
Attributes: nil,
457+
Anonymous: &f,
458+
Attributes: &m,
459459
}
460460
type args struct {
461461
target *Target
@@ -532,14 +532,17 @@ func TestServingRules_GetVariationName(t *testing.T) {
532532
}
533533

534534
func TestFeatureConfig_Evaluate(t *testing.T) {
535+
f := false
535536
harness := "Harness"
536537
v1 := "v1"
537538
v2 := "v2"
539+
m := make(map[string]interface{})
540+
m["email"] = "[email protected]"
538541
target := Target{
539542
Identifier: harness,
540543
Name: nil,
541-
Anonymous: false,
542-
Attributes: nil,
544+
Anonymous: &f,
545+
Attributes: &m,
543546
}
544547
type fields struct {
545548
DefaultServe Serve
@@ -620,6 +623,7 @@ func TestFeatureConfig_Evaluate(t *testing.T) {
620623
}
621624

622625
func TestClause_Evaluate(t *testing.T) {
626+
f := false
623627
type fields struct {
624628
Attribute string
625629
ID string
@@ -632,14 +636,13 @@ func TestClause_Evaluate(t *testing.T) {
632636
segments Segments
633637
operator types.ValueType
634638
}
635-
639+
m := make(map[string]interface{})
640+
m["email"] = "[email protected]"
636641
target := Target{
637642
Identifier: "john",
638643
Name: nil,
639-
Anonymous: false,
640-
Attributes: map[string]interface{}{
641-
"email": "[email protected]",
642-
},
644+
Anonymous: &f,
645+
Attributes: &m,
643646
}
644647
tests := []struct {
645648
name string

evaluation/reflection_test.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ import (
66
)
77

88
func TestGetStructFieldValue(t *testing.T) {
9+
m := make(map[string]interface{})
10+
m["email"] = "[email protected]"
911
identifier := "john"
1012
name := "John"
13+
f := false
1114
target := Target{
1215
Identifier: identifier,
1316
Name: &name,
14-
Anonymous: false,
15-
Attributes: map[string]interface{}{
16-
"email": "[email protected]",
17-
},
17+
Anonymous: &f,
18+
Attributes: &m,
1819
}
1920
type args struct {
2021
target interface{}
@@ -55,13 +56,14 @@ func TestGetStructFieldValue(t *testing.T) {
5556
func Test_caseInsensitiveFieldByName(t *testing.T) {
5657
identifier := "john"
5758
name := "John"
59+
m := make(map[string]interface{})
60+
m["email"] = "[email protected]"
61+
f := false
5862
target := Target{
5963
Identifier: identifier,
6064
Name: &name,
61-
Anonymous: false,
62-
Attributes: map[string]interface{}{
63-
"email": "[email protected]",
64-
},
65+
Anonymous: &f,
66+
Attributes: &m,
6567
}
6668
type args struct {
6769
v reflect.Value

evaluation/segment_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ func TestSegment_Evaluate(t *testing.T) {
2222
type args struct {
2323
target *Target
2424
}
25-
25+
f := false
26+
m := make(map[string]interface{})
27+
m["email"] = "[email protected]"
2628
target := Target{
2729
Identifier: "john",
2830
Name: nil,
29-
Anonymous: false,
30-
Attributes: map[string]interface{}{
31-
"email": "[email protected]",
32-
},
31+
Anonymous: &f,
32+
Attributes: &m,
3333
}
3434

3535
tests := []struct {

evaluation/target.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ import (
1010
type Target struct {
1111
Identifier string
1212
Name *string
13-
Anonymous bool
14-
Attributes map[string]interface{}
13+
Anonymous *bool
14+
Attributes *map[string]interface{}
1515
}
1616

1717
// GetAttrValue returns value from target with specified attribute
1818
func (t Target) GetAttrValue(attr string) reflect.Value {
1919
var value reflect.Value
20-
attrVal, ok := t.Attributes[attr] // first check custom attributes
20+
attrs := *t.Attributes
21+
attrVal, ok := attrs[attr] // first check custom attributes
2122
if ok {
2223
value = reflect.ValueOf(attrVal)
2324
} else {

evaluation/target_test.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99

1010
func TestTarget_GetOperator(t1 *testing.T) {
1111
harness := "Harness"
12+
m := make(map[string]interface{})
13+
m["anonymous"] = false
1214
type fields struct {
1315
Identifier string
1416
Name *string
@@ -29,7 +31,7 @@ func TestTarget_GetOperator(t1 *testing.T) {
2931
Name *string
3032
Anonymous bool
3133
Attributes map[string]interface{}
32-
}{Identifier: "harness", Name: &harness, Anonymous: false, Attributes: nil},
34+
}{Identifier: "harness", Name: &harness, Anonymous: false, Attributes: m},
3335
args: struct{ attr string }{attr: "anonymous"}, want: types.Boolean(false)},
3436
{name: "string operator", fields: struct {
3537
Identifier string
@@ -63,8 +65,8 @@ func TestTarget_GetOperator(t1 *testing.T) {
6365
t := Target{
6466
Identifier: val.fields.Identifier,
6567
Name: val.fields.Name,
66-
Anonymous: val.fields.Anonymous,
67-
Attributes: val.fields.Attributes,
68+
Anonymous: &val.fields.Anonymous,
69+
Attributes: &val.fields.Attributes,
6870
}
6971
if got := t.GetOperator(val.args.attr); !reflect.DeepEqual(got, val.want) {
7072
t1.Errorf("GetOperator() = %v, want %v", got, val.want)
@@ -115,8 +117,8 @@ func TestTarget_GetAttrValue(t1 *testing.T) {
115117
t := Target{
116118
Identifier: val.fields.Identifier,
117119
Name: val.fields.Name,
118-
Anonymous: val.fields.Anonymous,
119-
Attributes: val.fields.Attributes,
120+
Anonymous: &val.fields.Anonymous,
121+
Attributes: &val.fields.Attributes,
120122
}
121123
if got := t.GetAttrValue(val.args.attr); !reflect.DeepEqual(got.Interface(), val.want.Interface()) {
122124
t1.Errorf("GetAttrValue() = %v, want %v", got, val.want)
@@ -126,6 +128,8 @@ func TestTarget_GetAttrValue(t1 *testing.T) {
126128
}
127129

128130
func TestTarget_GetOperator1(t1 *testing.T) {
131+
m := make(map[string]interface{})
132+
m["anonymous"] = false
129133
type fields struct {
130134
Identifier string
131135
Name *string
@@ -148,7 +152,7 @@ func TestTarget_GetOperator1(t1 *testing.T) {
148152
Name *string
149153
Anonymous bool
150154
Attributes map[string]interface{}
151-
}{Identifier: "john", Name: &name, Anonymous: false, Attributes: types.JSON{}},
155+
}{Identifier: "john", Name: &name, Anonymous: false, Attributes: types.JSON{"anonymous": false}},
152156
args: struct{ attr string }{attr: "anonymous"}, want: types.Boolean(false)},
153157
{name: "string operator", fields: struct {
154158
Identifier string
@@ -182,8 +186,8 @@ func TestTarget_GetOperator1(t1 *testing.T) {
182186
t := Target{
183187
Identifier: val.fields.Identifier,
184188
Name: val.fields.Name,
185-
Anonymous: val.fields.Anonymous,
186-
Attributes: val.fields.Attributes,
189+
Anonymous: &val.fields.Anonymous,
190+
Attributes: &val.fields.Attributes,
187191
}
188192
if got := t.GetOperator(val.args.attr); !reflect.DeepEqual(got, val.want) {
189193
t1.Errorf("GetOperator() = %v, want %v", got, val.want)

0 commit comments

Comments
 (0)