Skip to content

Commit 0f6d828

Browse files
authored
style: replace usage of interface{} with any (#362)
Signed-off-by: Sahid Velji <sahidvelji@gmail.com>
1 parent 965e766 commit 0f6d828

25 files changed

+155
-155
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,23 +128,23 @@ If the flag management system you're using supports targeting, you can provide t
128128
```go
129129
// set a value to the global context
130130
openfeature.SetEvaluationContext(openfeature.NewTargetlessEvaluationContext(
131-
map[string]interface{}{
131+
map[string]any{
132132
"region": "us-east-1-iah-1a",
133133
},
134134
))
135135

136136
// set a value to the client context
137137
client := openfeature.NewClient("my-app")
138138
client.SetEvaluationContext(openfeature.NewTargetlessEvaluationContext(
139-
map[string]interface{}{
139+
map[string]any{
140140
"version": "1.4.6",
141141
},
142142
))
143143

144144
// set a value to the invocation context
145145
evalCtx := openfeature.NewEvaluationContext(
146146
"user-123",
147-
map[string]interface{}{
147+
map[string]any{
148148
"company": "Initech",
149149
},
150150
)
@@ -371,7 +371,7 @@ func (i MyFeatureProvider) IntEvaluation(ctx context.Context, flag string, defau
371371
}
372372

373373
// ObjectEvaluation returns an object flag
374-
func (i MyFeatureProvider) ObjectEvaluation(ctx context.Context, flag string, defaultValue interface{}, evalCtx openfeature.FlattenedContext) openfeature.InterfaceResolutionDetail {
374+
func (i MyFeatureProvider) ObjectEvaluation(ctx context.Context, flag string, defaultValue any, evalCtx openfeature.FlattenedContext) openfeature.InterfaceResolutionDetail {
375375
// code to evaluate object
376376
}
377377

e2e/common_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
// ctxFunction is a context based evaluation callback
99
var ctxFunction = func(this memprovider.InMemoryFlag, evalCtx openfeature.FlattenedContext) (
10-
interface{}, openfeature.ProviderResolutionDetail) {
10+
any, openfeature.ProviderResolutionDetail) {
1111

1212
defaultValue := this.Variants[this.DefaultVariant]
1313
defaultResolution := openfeature.ProviderResolutionDetail{
@@ -39,7 +39,7 @@ var memoryFlags = map[string]memprovider.InMemoryFlag{
3939
Key: "boolean-flag",
4040
State: memprovider.Enabled,
4141
DefaultVariant: "on",
42-
Variants: map[string]interface{}{
42+
Variants: map[string]any{
4343
"on": true,
4444
"off": false,
4545
},
@@ -49,7 +49,7 @@ var memoryFlags = map[string]memprovider.InMemoryFlag{
4949
Key: "string-flag",
5050
State: memprovider.Enabled,
5151
DefaultVariant: "greeting",
52-
Variants: map[string]interface{}{
52+
Variants: map[string]any{
5353
"greeting": "hi",
5454
"parting": "bye",
5555
},
@@ -59,7 +59,7 @@ var memoryFlags = map[string]memprovider.InMemoryFlag{
5959
Key: "integer-flag",
6060
State: memprovider.Enabled,
6161
DefaultVariant: "ten",
62-
Variants: map[string]interface{}{
62+
Variants: map[string]any{
6363
"one": 1,
6464
"ten": 10,
6565
},
@@ -69,7 +69,7 @@ var memoryFlags = map[string]memprovider.InMemoryFlag{
6969
Key: "float-flag",
7070
State: memprovider.Enabled,
7171
DefaultVariant: "half",
72-
Variants: map[string]interface{}{
72+
Variants: map[string]any{
7373
"tenth": 0.1,
7474
"half": 0.5,
7575
},
@@ -79,9 +79,9 @@ var memoryFlags = map[string]memprovider.InMemoryFlag{
7979
Key: "object-flag",
8080
State: memprovider.Enabled,
8181
DefaultVariant: "template",
82-
Variants: map[string]interface{}{
83-
"empty": map[string]interface{}{},
84-
"template": map[string]interface{}{
82+
Variants: map[string]any{
83+
"empty": map[string]any{},
84+
"template": map[string]any{
8585
"showImages": true,
8686
"title": "Check out these pics!",
8787
"imagesPerPage": 100,
@@ -93,7 +93,7 @@ var memoryFlags = map[string]memprovider.InMemoryFlag{
9393
Key: "wrong-flag",
9494
State: memprovider.Enabled,
9595
DefaultVariant: "one",
96-
Variants: map[string]interface{}{
96+
Variants: map[string]any{
9797
"one": "uno",
9898
"two": "dos",
9999
},
@@ -103,7 +103,7 @@ var memoryFlags = map[string]memprovider.InMemoryFlag{
103103
Key: "context-aware",
104104
State: memprovider.Enabled,
105105
DefaultVariant: "external",
106-
Variants: map[string]interface{}{
106+
Variants: map[string]any{
107107
"internal": "INTERNAL",
108108
"external": "EXTERNAL",
109109
},

e2e/evaluation_fuzz_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func FuzzObjectEvaluation(f *testing.F) {
111111
f.Add("FoO", "true")
112112
f.Add("FoO234", "-1.23")
113113
f.Add("FoO2\b34", "1")
114-
f.Fuzz(func(t *testing.T, flagKey string, defaultValue string) { // interface{} is not supported, using a string
114+
f.Fuzz(func(t *testing.T, flagKey string, defaultValue string) { // any is not supported, using a string
115115
res, err := client.ObjectValueDetails(context.Background(), flagKey, defaultValue, openfeature.EvaluationContext{})
116116
if err != nil {
117117
if res.ErrorCode == openfeature.FlagNotFoundCode {

e2e/evaluation_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ func anObjectFlagWithKeyIsEvaluatedWithANullDefaultValue(ctx context.Context, fl
219219
func theResolvedObjectValueShouldBeContainFieldsAndWithValuesAndRespectively(
220220
ctx context.Context, field1, field2, field3, value1, value2 string, value3 int) error {
221221

222-
got, ok := ctx.Value(ctxStorageKey{}).(map[string]interface{})
222+
got, ok := ctx.Value(ctxStorageKey{}).(map[string]any)
223223
if !ok {
224224
return errors.New("no flag resolution result")
225225
}
@@ -454,7 +454,7 @@ func theResolvedObjectDetailsValueShouldBeContainFieldsAndWithValuesAndRespectiv
454454
return err
455455
}
456456

457-
content, ok := gotResDetail.Value.(map[string]interface{})
457+
content, ok := gotResDetail.Value.(map[string]any)
458458
if !ok {
459459
return errors.New("unexpected value format")
460460
}
@@ -516,7 +516,7 @@ func theVariantShouldBeAndTheReasonShouldBe(ctx context.Context, variant, reason
516516
func contextContainsKeysWithValues(
517517
ctx context.Context, ctxKey1, ctxKey2, ctxKey3, ctxKey4, ctxValue1, ctxValue2 string, ctxValue3 int64, ctxValue4 string,
518518
) (context.Context, error) {
519-
evalCtx := openfeature.NewEvaluationContext("", map[string]interface{}{
519+
evalCtx := openfeature.NewEvaluationContext("", map[string]any{
520520
ctxKey1: boolOrString(ctxValue1),
521521
ctxKey2: boolOrString(ctxValue2),
522522
ctxKey3: ctxValue3,
@@ -778,7 +778,7 @@ func getFirstInterfaceEvaluationDetails(ctx context.Context) (openfeature.Interf
778778
return openfeature.InterfaceEvaluationDetails{}, errors.New("no evaluation detail found in context")
779779
}
780780

781-
func boolOrString(str string) interface{} {
781+
func boolOrString(str string) any {
782782
boolean, err := strconv.ParseBool(str)
783783
if err != nil {
784784
return str

openfeature/client.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ type IntEvaluationDetails struct {
169169
}
170170

171171
type InterfaceEvaluationDetails struct {
172-
Value interface{}
172+
Value any
173173
EvaluationDetails
174174
}
175175

@@ -184,7 +184,7 @@ type ResolutionDetail struct {
184184
// FlagMetadata is a structure which supports definition of arbitrary properties, with keys of type string, and values
185185
// of type boolean, string, int64 or float64. This structure is populated by a provider for use by an Application
186186
// Author (via the Evaluation API) or an Application Integrator (via hooks).
187-
type FlagMetadata map[string]interface{}
187+
type FlagMetadata map[string]any
188188

189189
// GetString fetch string value from FlagMetadata.
190190
// Returns an error if the key does not exist, or, the value is of the wrong type
@@ -365,7 +365,7 @@ func (c *Client) IntValue(ctx context.Context, flag string, defaultValue int64,
365365
// - defaultValue is returned if an error occurs
366366
// - evalCtx is the evaluation context used in a flag evaluation (not to be confused with ctx)
367367
// - options are optional additional evaluation options e.g. WithHooks & WithHookHints
368-
func (c *Client) ObjectValue(ctx context.Context, flag string, defaultValue interface{}, evalCtx EvaluationContext, options ...Option) (interface{}, error) {
368+
func (c *Client) ObjectValue(ctx context.Context, flag string, defaultValue any, evalCtx EvaluationContext, options ...Option) (any, error) {
369369
details, err := c.ObjectValueDetails(ctx, flag, defaultValue, evalCtx, options...)
370370
if err != nil {
371371
return defaultValue, err
@@ -558,7 +558,7 @@ func (c *Client) IntValueDetails(ctx context.Context, flag string, defaultValue
558558
// - defaultValue is returned if an error occurs
559559
// - evalCtx is the evaluation context used in a flag evaluation (not to be confused with ctx)
560560
// - options are optional additional evaluation options e.g. WithHooks & WithHookHints
561-
func (c *Client) ObjectValueDetails(ctx context.Context, flag string, defaultValue interface{}, evalCtx EvaluationContext, options ...Option) (InterfaceEvaluationDetails, error) {
561+
func (c *Client) ObjectValueDetails(ctx context.Context, flag string, defaultValue any, evalCtx EvaluationContext, options ...Option) (InterfaceEvaluationDetails, error) {
562562
c.mx.RLock()
563563
defer c.mx.RUnlock()
564564

@@ -644,7 +644,7 @@ func (c *Client) Int(ctx context.Context, flag string, defaultValue int64, evalC
644644
// - defaultValue is returned if an error occurs
645645
// - evalCtx is the evaluation context used in a flag evaluation (not to be confused with ctx)
646646
// - options are optional additional evaluation options e.g. WithHooks & WithHookHints
647-
func (c *Client) Object(ctx context.Context, flag string, defaultValue interface{}, evalCtx EvaluationContext, options ...Option) interface{} {
647+
func (c *Client) Object(ctx context.Context, flag string, defaultValue any, evalCtx EvaluationContext, options ...Option) any {
648648
value, _ := c.ObjectValue(ctx, flag, defaultValue, evalCtx, options...)
649649

650650
return value
@@ -680,7 +680,7 @@ func (c *Client) forTracking(ctx context.Context, evalCtx EvaluationContext) (Tr
680680
}
681681

682682
func (c *Client) evaluate(
683-
ctx context.Context, flag string, flagType Type, defaultValue interface{}, evalCtx EvaluationContext, options EvaluationOptions,
683+
ctx context.Context, flag string, flagType Type, defaultValue any, evalCtx EvaluationContext, options EvaluationOptions,
684684
) (InterfaceEvaluationDetails, error) {
685685
evalDetails := InterfaceEvaluationDetails{
686686
Value: defaultValue,

openfeature/client_test.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ func TestRequirements_1_3(t *testing.T) {
9393
StringValue(ctx context.Context, flag string, defaultValue string, evalCtx EvaluationContext, options ...Option) (string, error)
9494
FloatValue(ctx context.Context, flag string, defaultValue float64, evalCtx EvaluationContext, options ...Option) (float64, error)
9595
IntValue(ctx context.Context, flag string, defaultValue int64, evalCtx EvaluationContext, options ...Option) (int64, error)
96-
ObjectValue(ctx context.Context, flag string, defaultValue interface{}, evalCtx EvaluationContext, options ...Option) (interface{}, error)
96+
ObjectValue(ctx context.Context, flag string, defaultValue any, evalCtx EvaluationContext, options ...Option) (any, error)
9797
}
9898

99-
var clientI interface{} = client
99+
var clientI any = client
100100
if _, ok := clientI.(requirements); !ok {
101101
t.Error("client returned by NewClient doesn't implement the 1.3.* requirements interface")
102102
}
@@ -114,10 +114,10 @@ func TestRequirement_1_4_1(t *testing.T) {
114114
StringValueDetails(ctx context.Context, flag string, defaultValue string, evalCtx EvaluationContext, options ...Option) (StringEvaluationDetails, error)
115115
FloatValueDetails(ctx context.Context, flag string, defaultValue float64, evalCtx EvaluationContext, options ...Option) (FloatEvaluationDetails, error)
116116
IntValueDetails(ctx context.Context, flag string, defaultValue int64, evalCtx EvaluationContext, options ...Option) (IntEvaluationDetails, error)
117-
ObjectValueDetails(ctx context.Context, flag string, defaultValue interface{}, evalCtx EvaluationContext, options ...Option) (InterfaceEvaluationDetails, error)
117+
ObjectValueDetails(ctx context.Context, flag string, defaultValue any, evalCtx EvaluationContext, options ...Option) (InterfaceEvaluationDetails, error)
118118
}
119119

120-
var clientI interface{} = client
120+
var clientI any = client
121121
if _, ok := clientI.(requirements); !ok {
122122
t.Error("client returned by NewClient doesn't implement the 1.4.1 requirements interface")
123123
}
@@ -142,7 +142,7 @@ const (
142142
incorrectReason = "Incorrect reason returned!"
143143
)
144144

145-
var objectValue = map[string]interface{}{"foo": 1, "bar": true, "baz": "buz"}
145+
var objectValue = map[string]any{"foo": 1, "bar": true, "baz": "buz"}
146146

147147
// Requirement_1_4_2
148148
// The `evaluation details` structure's `value` field MUST contain the evaluated flag value.
@@ -780,7 +780,7 @@ func TestRequirement_6_1(t *testing.T) {
780780
Track(ctx context.Context, trackingEventName string, evalCtx EvaluationContext, details TrackingEventDetails)
781781
}
782782

783-
var clientI interface{} = client
783+
var clientI any = client
784784
if _, ok := clientI.(requirements); !ok {
785785
t.Error("client returned by NewClient doesn't implement the 1.6.* requirements interface")
786786
}
@@ -823,34 +823,34 @@ func TestTrack(t *testing.T) {
823823
eventName: "example-event",
824824
inCtx: inputCtx{
825825
api: EvaluationContext{
826-
attributes: map[string]interface{}{
826+
attributes: map[string]any{
827827
"1": "api",
828828
"2": "api",
829829
"3": "api",
830830
"4": "api",
831831
},
832832
},
833833
txn: EvaluationContext{
834-
attributes: map[string]interface{}{
834+
attributes: map[string]any{
835835
"2": "txn",
836836
"3": "txn",
837837
"4": "txn",
838838
},
839839
},
840840
client: EvaluationContext{
841-
attributes: map[string]interface{}{
841+
attributes: map[string]any{
842842
"3": "client",
843843
"4": "client",
844844
},
845845
},
846846
invocation: EvaluationContext{
847-
attributes: map[string]interface{}{
847+
attributes: map[string]any{
848848
"4": "invocation",
849849
},
850850
},
851851
},
852852
outCtx: EvaluationContext{
853-
attributes: map[string]interface{}{
853+
attributes: map[string]any{
854854
"1": "api",
855855
"2": "txn",
856856
"3": "client",
@@ -907,7 +907,7 @@ func TestFlattenContext(t *testing.T) {
907907
}{
908908
"happy path": {
909909
inCtx: EvaluationContext{
910-
attributes: map[string]interface{}{
910+
attributes: map[string]any{
911911
"1": "string",
912912
"2": 0.01,
913913
"3": false,
@@ -923,7 +923,7 @@ func TestFlattenContext(t *testing.T) {
923923
},
924924
"no targeting key": {
925925
inCtx: EvaluationContext{
926-
attributes: map[string]interface{}{
926+
attributes: map[string]any{
927927
"1": "string",
928928
"2": 0.01,
929929
"3": false,
@@ -938,7 +938,7 @@ func TestFlattenContext(t *testing.T) {
938938
"duplicated key": {
939939
inCtx: EvaluationContext{
940940
targetingKey: "user",
941-
attributes: map[string]interface{}{
941+
attributes: map[string]any{
942942
TargetingKey: "not user",
943943
"1": "string",
944944
"2": 0.01,
@@ -987,7 +987,7 @@ func TestBeforeHookNilContext(t *testing.T) {
987987
mocks := hydratedMocksForClientTests(t, 1)
988988
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
989989

990-
attributes := map[string]interface{}{"should": "persist"}
990+
attributes := map[string]any{"should": "persist"}
991991
evalCtx := EvaluationContext{attributes: attributes}
992992
mocks.providerAPI.EXPECT().BooleanEvaluation(gomock.Any(), gomock.Any(), gomock.Any(), attributes)
993993

@@ -1034,7 +1034,7 @@ func TestObjectEvaluationShouldSupportNilValue(t *testing.T) {
10341034

10351035
variant := "variant1"
10361036
reason := TargetingMatchReason
1037-
var value interface{} = nil
1037+
var value any = nil
10381038

10391039
mocks := hydratedMocksForClientTests(t, 1)
10401040
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
@@ -1192,7 +1192,7 @@ func TestRequirement_1_7_1(t *testing.T) {
11921192
State() State
11931193
}
11941194

1195-
var clientI interface{} = client
1195+
var clientI any = client
11961196
if _, ok := clientI.(requirements); !ok {
11971197
t.Fatal("client des not define a status accessor which indicates the readiness of the associated provider")
11981198
}

openfeature/evaluation_context.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import (
1212
// https://openfeature.dev/specification/sections/evaluation-context
1313
type EvaluationContext struct {
1414
targetingKey string // uniquely identifying the subject (end-user, or client service) of a flag evaluation
15-
attributes map[string]interface{}
15+
attributes map[string]any
1616
}
1717

1818
// Attribute retrieves the attribute with the given key
19-
func (e EvaluationContext) Attribute(key string) interface{} {
19+
func (e EvaluationContext) Attribute(key string) any {
2020
return e.attributes[key]
2121
}
2222

@@ -26,9 +26,9 @@ func (e EvaluationContext) TargetingKey() string {
2626
}
2727

2828
// Attributes returns a copy of the EvaluationContext's attributes
29-
func (e EvaluationContext) Attributes() map[string]interface{} {
29+
func (e EvaluationContext) Attributes() map[string]any {
3030
// copy attributes to new map to prevent mutation (maps are passed by reference)
31-
attrs := make(map[string]interface{}, len(e.attributes))
31+
attrs := make(map[string]any, len(e.attributes))
3232
for key, value := range e.attributes {
3333
attrs[key] = value
3434
}
@@ -40,9 +40,9 @@ func (e EvaluationContext) Attributes() map[string]interface{} {
4040
//
4141
// targetingKey - uniquely identifying the subject (end-user, or client service) of a flag evaluation
4242
// attributes - contextual data used in flag evaluation
43-
func NewEvaluationContext(targetingKey string, attributes map[string]interface{}) EvaluationContext {
43+
func NewEvaluationContext(targetingKey string, attributes map[string]any) EvaluationContext {
4444
// copy attributes to new map to avoid reference being externally available, thereby enforcing immutability
45-
attrs := make(map[string]interface{}, len(attributes))
45+
attrs := make(map[string]any, len(attributes))
4646
for key, value := range attributes {
4747
attrs[key] = value
4848
}
@@ -56,7 +56,7 @@ func NewEvaluationContext(targetingKey string, attributes map[string]interface{}
5656
// NewTargetlessEvaluationContext constructs an EvaluationContext with an empty targeting key
5757
//
5858
// attributes - contextual data used in flag evaluation
59-
func NewTargetlessEvaluationContext(attributes map[string]interface{}) EvaluationContext {
59+
func NewTargetlessEvaluationContext(attributes map[string]any) EvaluationContext {
6060
return NewEvaluationContext("", attributes)
6161
}
6262

0 commit comments

Comments
 (0)