Skip to content

Commit 2ee8cfb

Browse files
author
Dave Johnston
authored
Merge pull request #40 from davejohnston/FFM-1355b
[FFM-1355]: SDK doesn't support multiple custom attributes and name no…
2 parents 0db9ea1 + 8435134 commit 2ee8cfb

File tree

8 files changed

+44
-39
lines changed

8 files changed

+44
-39
lines changed

analyticsservice/analytics.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ func (as *AnalyticsService) sendDataAndResetCache(ctx context.Context) {
151151
}
152152

153153
targetName := analytic.target.Identifier
154-
if analytic.target.Name != nil {
155-
targetName = *analytic.target.Name
154+
if analytic.target.Name != "" {
155+
targetName = analytic.target.Name
156156
}
157157

158158
td := metricsclient.TargetData{

client/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ func (c *CfClient) authenticate(ctx context.Context, target evaluation.Target) {
191191
target.Anonymous,
192192
target.Attributes,
193193
target.Identifier,
194-
target.Name,
194+
&target.Name,
195195
}
196196
c.auth.Target = &t
197197
c.mux.RLock()

dto/target_builder.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (b *targetBuilder) Lastname(lastname string) TargetBuilderInterface {
6363

6464
// Name target name object
6565
func (b *targetBuilder) Name(name string) TargetBuilderInterface {
66-
b.Custom("name", name)
66+
b.Target.Name = name
6767
return b
6868
}
6969

@@ -76,8 +76,8 @@ func (b *targetBuilder) Anonymous(value bool) TargetBuilderInterface {
7676
// Custom object
7777
func (b *targetBuilder) Custom(key string, value interface{}) TargetBuilderInterface {
7878
m := make(map[string]interface{})
79-
if b.Target.Attributes == nil {
80-
b.Target.Attributes = &m
79+
if b.Target.Attributes != nil {
80+
m = *b.Target.Attributes
8181
}
8282
m[key] = value
8383
b.Target.Attributes = &m

evaluation/feature_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ func TestServingRules_GetVariationName(t *testing.T) {
298298
}
299299
target := &Target{
300300
Identifier: harness,
301-
Name: &harness,
301+
Name: harness,
302302
Anonymous: &f,
303303
Attributes: &m,
304304
}
@@ -482,7 +482,6 @@ func TestFeatureConfig_Evaluate(t *testing.T) {
482482

483483
target := Target{
484484
Identifier: harness,
485-
Name: nil,
486485
Anonymous: &f,
487486
Attributes: &m,
488487
}
@@ -554,7 +553,6 @@ func TestClause_Evaluate(t *testing.T) {
554553
m["email"] = "[email protected]"
555554
target := Target{
556555
Identifier: "john",
557-
Name: nil,
558556
Anonymous: &f,
559557
Attributes: &m,
560558
}

evaluation/reflection_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func TestGetStructFieldValue(t *testing.T) {
1313
f := false
1414
target := Target{
1515
Identifier: identifier,
16-
Name: &name,
16+
Name: name,
1717
Anonymous: &f,
1818
Attributes: &m,
1919
}
@@ -61,7 +61,7 @@ func Test_caseInsensitiveFieldByName(t *testing.T) {
6161
f := false
6262
target := Target{
6363
Identifier: identifier,
64-
Name: &name,
64+
Name: name,
6565
Anonymous: &f,
6666
Attributes: &m,
6767
}

evaluation/segment_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ func TestSegment_Evaluate(t *testing.T) {
1717
m["email"] = "[email protected]"
1818
target := Target{
1919
Identifier: "john",
20-
Name: nil,
2120
Anonymous: &f,
2221
Attributes: &m,
2322
}

evaluation/target.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package evaluation
22

33
import (
44
"fmt"
5+
"strings"
56

67
"github.com/drone/ff-golang-server-sdk/types"
78

@@ -11,7 +12,7 @@ import (
1112
// Target object
1213
type Target struct {
1314
Identifier string
14-
Name *string
15+
Name string
1516
Anonymous *bool
1617
Attributes *map[string]interface{}
1718
}
@@ -24,7 +25,14 @@ func (t Target) GetAttrValue(attr string) reflect.Value {
2425
if ok {
2526
value = reflect.ValueOf(attrVal)
2627
} else {
27-
value = GetStructFieldValue(t, attr)
28+
// We only have two fields here, so we will access the fields directly, and use reflection if we start adding
29+
// more in the future
30+
switch strings.ToLower(attr) {
31+
case "identifier":
32+
value = reflect.ValueOf(t.Identifier)
33+
case "name":
34+
value = reflect.ValueOf(t.Name)
35+
}
2836
}
2937
return value
3038
}

evaluation/target_test.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func TestTarget_GetOperator(t1 *testing.T) {
1313
m["anonymous"] = false
1414
type fields struct {
1515
Identifier string
16-
Name *string
16+
Name string
1717
Anonymous bool
1818
Attributes map[string]interface{}
1919
}
@@ -29,42 +29,42 @@ func TestTarget_GetOperator(t1 *testing.T) {
2929
}{
3030
{name: "boolean operator", fields: struct {
3131
Identifier string
32-
Name *string
32+
Name string
3333
Anonymous bool
3434
Attributes map[string]interface{}
35-
}{Identifier: "harness", Name: &harness, Anonymous: false, Attributes: m},
35+
}{Identifier: "harness", Name: harness, Anonymous: false, Attributes: m},
3636
args: struct{ attr string }{attr: "anonymous"}, want: types.Boolean(false)},
3737
{name: "string operator", fields: struct {
3838
Identifier string
39-
Name *string
39+
Name string
4040
Anonymous bool
4141
Attributes map[string]interface{}
42-
}{Identifier: "harness", Name: &harness, Anonymous: false, Attributes: nil},
42+
}{Identifier: "harness", Name: harness, Anonymous: false, Attributes: nil},
4343
args: struct{ attr string }{attr: "identifier"}, want: types.String("harness")},
4444
{name: "int operator", fields: struct {
4545
Identifier string
46-
Name *string
46+
Name string
4747
Anonymous bool
4848
Attributes map[string]interface{}
49-
}{Identifier: "harness", Name: &harness, Anonymous: false, Attributes: map[string]interface{}{
49+
}{Identifier: "harness", Name: harness, Anonymous: false, Attributes: map[string]interface{}{
5050
"order": 1,
5151
}},
5252
args: struct{ attr string }{attr: "order"}, want: types.Integer(1)},
5353
{name: "number operator", fields: struct {
5454
Identifier string
55-
Name *string
55+
Name string
5656
Anonymous bool
5757
Attributes map[string]interface{}
58-
}{Identifier: "harness", Name: &harness, Anonymous: false, Attributes: map[string]interface{}{
58+
}{Identifier: "harness", Name: harness, Anonymous: false, Attributes: map[string]interface{}{
5959
"weight": 99.99,
6060
}},
6161
args: struct{ attr string }{attr: "weight"}, want: types.Number(99.99)},
6262
{name: "empty operator", fields: struct {
6363
Identifier string
64-
Name *string
64+
Name string
6565
Anonymous bool
6666
Attributes map[string]interface{}
67-
}{Identifier: "harness", Name: nil, Anonymous: false, Attributes: map[string]interface{}{}},
67+
}{Identifier: "harness", Anonymous: false, Attributes: map[string]interface{}{}},
6868
args: struct{ attr string }{attr: ""}, want: nil},
6969
}
7070
for _, tt := range tests {
@@ -94,7 +94,7 @@ func TestTarget_GetAttrValue(t1 *testing.T) {
9494
email := "[email protected]"
9595
type fields struct {
9696
Identifier string
97-
Name *string
97+
Name string
9898
Anonymous bool
9999
Attributes map[string]interface{}
100100
}
@@ -109,17 +109,17 @@ func TestTarget_GetAttrValue(t1 *testing.T) {
109109
}{
110110
{name: "check identifier", fields: struct {
111111
Identifier string
112-
Name *string
112+
Name string
113113
Anonymous bool
114114
Attributes map[string]interface{}
115-
}{Identifier: identifier, Name: &name, Anonymous: false, Attributes: types.JSON{}},
115+
}{Identifier: identifier, Name: name, Anonymous: false, Attributes: types.JSON{}},
116116
args: struct{ attr string }{attr: "identifier"}, want: reflect.ValueOf(identifier)},
117117
{name: "check attributes", fields: struct {
118118
Identifier string
119-
Name *string
119+
Name string
120120
Anonymous bool
121121
Attributes map[string]interface{}
122-
}{Identifier: "john", Name: &name, Anonymous: false, Attributes: types.JSON{
122+
}{Identifier: "john", Name: name, Anonymous: false, Attributes: types.JSON{
123123
"email": email,
124124
}},
125125
args: struct{ attr string }{attr: "email"}, want: reflect.ValueOf(email)},
@@ -145,7 +145,7 @@ func TestTarget_GetOperator1(t1 *testing.T) {
145145
m["anonymous"] = false
146146
type fields struct {
147147
Identifier string
148-
Name *string
148+
Name string
149149
Anonymous bool
150150
Attributes map[string]interface{}
151151
}
@@ -163,33 +163,33 @@ func TestTarget_GetOperator1(t1 *testing.T) {
163163
}{
164164
{name: "bool operator", fields: struct {
165165
Identifier string
166-
Name *string
166+
Name string
167167
Anonymous bool
168168
Attributes map[string]interface{}
169-
}{Identifier: "john", Name: &name, Anonymous: false, Attributes: types.JSON{"anonymous": false}},
169+
}{Identifier: "john", Name: name, Anonymous: false, Attributes: types.JSON{"anonymous": false}},
170170
args: struct{ attr string }{attr: "anonymous"}, want: types.Boolean(false)},
171171
{name: "string operator", fields: struct {
172172
Identifier string
173-
Name *string
173+
Name string
174174
Anonymous bool
175175
Attributes map[string]interface{}
176-
}{Identifier: "john", Name: &name, Anonymous: false, Attributes: types.JSON{}},
176+
}{Identifier: "john", Name: name, Anonymous: false, Attributes: types.JSON{}},
177177
args: struct{ attr string }{attr: "identifier"}, want: types.String("john")},
178178
{name: "number operator", fields: struct {
179179
Identifier string
180-
Name *string
180+
Name string
181181
Anonymous bool
182182
Attributes map[string]interface{}
183-
}{Identifier: "john", Name: &name, Anonymous: false, Attributes: types.JSON{
183+
}{Identifier: "john", Name: name, Anonymous: false, Attributes: types.JSON{
184184
"height": 186.5,
185185
}},
186186
args: struct{ attr string }{attr: "height"}, want: types.Number(186.5)},
187187
{name: "integer operator", fields: struct {
188188
Identifier string
189-
Name *string
189+
Name string
190190
Anonymous bool
191191
Attributes map[string]interface{}
192-
}{Identifier: "john", Name: &name, Anonymous: false, Attributes: types.JSON{
192+
}{Identifier: "john", Name: name, Anonymous: false, Attributes: types.JSON{
193193
"zip": 90210,
194194
}},
195195
args: struct{ attr string }{attr: "zip"}, want: types.Integer(90210)},

0 commit comments

Comments
 (0)