Skip to content

Commit 892e9bc

Browse files
NamyalgPijush Chakraborty
andauthored
Part III - Refactors
* chore: minor refactors and updates to comments, address review #686 (comment) --------- Co-authored-by: Pijush Chakraborty <[email protected]>
1 parent fd5d9c2 commit 892e9bc

8 files changed

+374
-103
lines changed

remoteconfig/condition_evaluator.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,13 @@ func (ce *conditionEvaluator) evaluatePercentCondition(percentCondition *percent
148148
}
149149

150150
func computeInstanceMicroPercentile(seed string, randomizationID string) uint32 {
151-
seedPrefix := ""
151+
var sb strings.Builder
152152
if len(seed) > 0 {
153-
seedPrefix = fmt.Sprintf("%s.", seed)
153+
sb.WriteString(seed)
154+
sb.WriteRune('.')
154155
}
155-
stringToHash := fmt.Sprintf("%s%s", seedPrefix, randomizationID)
156+
sb.WriteString(randomizationID)
157+
stringToHash := sb.String()
156158

157159
hash := sha256.New()
158160
hash.Write([]byte(stringToHash))
@@ -192,7 +194,7 @@ func (ce *conditionEvaluator) evaluateCustomSignalCondition(customSignalConditio
192194
return result
193195
})
194196

195-
// For numeric operators only one target value is allowed
197+
// For numeric operators only one target value is allowed.
196198
case numericLessThan:
197199
return compareNumbers(customSignalCondition.TargetCustomSignalValues[0], actualValue, func(result int) bool { return result < 0 })
198200
case numericLessThanEqual:

remoteconfig/condition_evaluator_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -296,41 +296,41 @@ func TestPercentConditionMicroPercent(t *testing.T) {
296296
}{
297297
{
298298
description: "Evaluate LESS_OR_EQUAL to true when MicroPercent is max",
299-
operator: "LESS_OR_EQUAL",
299+
operator: lessThanOrEqual,
300300
microPercent: 100_000_000,
301301
outcome: true,
302302
},
303303
{
304304
description: "Evaluate LESS_OR_EQUAL to false when MicroPercent is min",
305-
operator: "LESS_OR_EQUAL",
305+
operator: lessThanOrEqual,
306306
microPercent: 0,
307307
outcome: false,
308308
},
309309
{
310310
description: "Evaluate LESS_OR_EQUAL to false when MicroPercent is not set (MicroPercent should use zero)",
311-
operator: "LESS_OR_EQUAL",
311+
operator: lessThanOrEqual,
312312
outcome: false,
313313
},
314314
{
315315
description: "Evaluate GREATER_THAN to true when MicroPercent is not set (MicroPercent should use zero)",
316-
operator: "GREATER_THAN",
316+
operator: greaterThan,
317317
outcome: true,
318318
},
319319
{
320320
description: "Evaluate GREATER_THAN max to false",
321-
operator: "GREATER_THAN",
321+
operator: greaterThan,
322322
outcome: false,
323323
microPercent: 100_000_000,
324324
},
325325
{
326326
description: "Evaluate LESS_OR_EQUAL to 9571542 to true",
327-
operator: "LESS_OR_EQUAL",
327+
operator: lessThanOrEqual,
328328
microPercent: 9_571_542, // instanceMicroPercentile of abcdef.123 (testSeed.testRandomizationID) is 9_571_542
329329
outcome: true,
330330
},
331331
{
332332
description: "Evaluate greater than 9571542 to true",
333-
operator: "GREATER_THAN",
333+
operator: greaterThan,
334334
microPercent: 9_571_541, // instanceMicroPercentile of abcdef.123 (testSeed.testRandomizationID) is 9_571_542
335335
outcome: true,
336336
},
@@ -361,40 +361,40 @@ func TestPercentConditionMicroPercentRange(t *testing.T) {
361361
}{
362362
{
363363
description: "Evaluate to false when microPercentRange is not set",
364-
operator: "BETWEEN",
364+
operator: between,
365365
outcome: false,
366366
},
367367
{
368368
description: "Evaluate to false when upper bound is not set",
369369
microPercentLb: 0,
370-
operator: "BETWEEN",
370+
operator: between,
371371
outcome: false,
372372
},
373373
{
374374
description: "Evaluate to true when lower bound is not set and upper bound is max",
375375
microPercentUb: 100_000_000,
376-
operator: "BETWEEN",
376+
operator: between,
377377
outcome: true,
378378
},
379379
{
380380
description: "Evaluate to true when between lower and upper bound", // instanceMicroPercentile of abcdef.123 (testSeed.testRandomizationID) is 9_571_542
381381
microPercentLb: 9_000_000,
382382
microPercentUb: 9_571_542, // interval is (9_000_000, 9_571_542]
383-
operator: "BETWEEN",
383+
operator: between,
384384
outcome: true,
385385
},
386386
{
387387
description: "Evaluate to false when lower and upper bounds are equal",
388388
microPercentLb: 98_000_000,
389389
microPercentUb: 98_000_000,
390-
operator: "BETWEEN",
390+
operator: between,
391391
outcome: false,
392392
},
393393
{
394394
description: "Evaluate to false when not between 9_400_000 and 9_500_000", // instanceMicroPercentile of abcdef.123 (testSeed.testRandomizationID) is 9_571_542
395395
microPercentLb: 9_400_000,
396396
microPercentUb: 9_500_000,
397-
operator: "BETWEEN",
397+
operator: between,
398398
outcome: false,
399399
},
400400
}

remoteconfig/remoteconfig.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
// Package remoteconfig allows clients to use Firebase Remote Config with Go.
15+
// Package remoteconfig provides functions to fetch and evaluate a server-side Remote Config template.
1616
package remoteconfig
1717

1818
import (
@@ -34,7 +34,7 @@ type Client struct {
3434
*rcClient
3535
}
3636

37-
// NewClient initializes a RemoteConfigClient with app-specific detail and a returns a
37+
// NewClient initializes a RemoteConfigClient with app-specific detail and returns a
3838
// client to be used by the user.
3939
func NewClient(ctx context.Context, c *internal.RemoteConfigClientConfig) (*Client, error) {
4040
if c.ProjectID == "" {
@@ -67,6 +67,7 @@ func newRcClient(client *internal.HTTPClient, conf *internal.RemoteConfigClientC
6767
internal.WithHeader("x-goog-api-client", internal.GetMetricsHeader(conf.Version)),
6868
}
6969

70+
// Handles errors for non-success HTTP status codes from Remote Config servers.
7071
client.CreateErrFn = handleRemoteConfigError
7172

7273
return &rcClient{
@@ -77,7 +78,7 @@ func newRcClient(client *internal.HTTPClient, conf *internal.RemoteConfigClientC
7778
}
7879
}
7980

80-
// GetServerTemplate Initializes a new ServerTemplate instance and fetches the server template.
81+
// GetServerTemplate initializes a new ServerTemplate instance and fetches the server template.
8182
func (c *rcClient) GetServerTemplate(ctx context.Context,
8283
defaultConfig map[string]any) (*ServerTemplate, error) {
8384
template, err := c.InitServerTemplate(defaultConfig, "")
@@ -97,10 +98,10 @@ func (c *rcClient) InitServerTemplate(defaultConfig map[string]any,
9798
template, err := newServerTemplate(c, defaultConfig)
9899

99100
if templateDataJSON != "" && err == nil {
100-
template.Set(templateDataJSON)
101+
err = template.Set(templateDataJSON)
101102
}
102103

103-
return template, nil
104+
return template, err
104105
}
105106

106107
func handleRemoteConfigError(resp *internal.Response) error {

remoteconfig/server_config.go

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,20 @@
1515
package remoteconfig
1616

1717
import (
18+
"slices"
1819
"strconv"
1920
"strings"
2021
)
2122

22-
// ValueSource represents the source of a value
23+
// ValueSource represents the source of a value.
2324
type ValueSource int
2425

25-
// Constants for value source
26+
// Constants for value source.
2627
const (
2728
sourceUnspecified ValueSource = iota
2829
Static // Static represents a statically defined value.
29-
Remote // Default represents a default value.
30-
Default // Remote represents a value fetched from a remote source.
30+
Remote // Remote represents a value fetched from a remote source.
31+
Default // Default represents a default value.
3132
)
3233

3334
// Value defines the interface for configuration values.
@@ -36,7 +37,7 @@ type value struct {
3637
value string
3738
}
3839

39-
// Default Values for config parameters.
40+
// Default values for different parameter types.
4041
const (
4142
DefaultValueForBoolean = false
4243
DefaultValueForString = ""
@@ -47,30 +48,41 @@ var booleanTruthyValues = []string{"1", "true", "t", "yes", "y", "on"}
4748

4849
// ServerConfig is the implementation of the ServerConfig interface.
4950
type ServerConfig struct {
50-
ConfigValues map[string]value
51+
configValues map[string]value
5152
}
5253

5354
// NewServerConfig creates a new ServerConfig instance.
54-
func NewServerConfig(configValues map[string]value) *ServerConfig {
55-
return &ServerConfig{ConfigValues: configValues}
55+
func newServerConfig(configValues map[string]value) *ServerConfig {
56+
return &ServerConfig{configValues: configValues}
5657
}
5758

5859
// GetBoolean returns the boolean value associated with the given key.
60+
//
61+
// It returns true if the string value is "1", "true", "t", "yes", "y", or "on" (case-insensitive).
62+
// Otherwise, or if the key is not found, it returns the default boolean value (false).
5963
func (s *ServerConfig) GetBoolean(key string) bool {
6064
return s.getValue(key).asBoolean()
6165
}
6266

6367
// GetInt returns the integer value associated with the given key.
68+
//
69+
// If the parameter value cannot be parsed as an integer, or if the key is not found,
70+
// it returns the default numeric value (0).
6471
func (s *ServerConfig) GetInt(key string) int {
6572
return s.getValue(key).asInt()
6673
}
6774

6875
// GetFloat returns the float value associated with the given key.
76+
//
77+
// If the parameter value cannot be parsed as a float64, or if the key is not found,
78+
// it returns the default float value (0).
6979
func (s *ServerConfig) GetFloat(key string) float64 {
7080
return s.getValue(key).asFloat()
7181
}
7282

7383
// GetString returns the string value associated with the given key.
84+
//
85+
// If the key is not found, it returns the default string value ("").
7486
func (s *ServerConfig) GetString(key string) string {
7587
return s.getValue(key).asString()
7688
}
@@ -82,10 +94,10 @@ func (s *ServerConfig) GetValueSource(key string) ValueSource {
8294

8395
// getValue returns the value associated with the given key.
8496
func (s *ServerConfig) getValue(key string) *value {
85-
if val, ok := s.ConfigValues[key]; ok {
97+
if val, ok := s.configValues[key]; ok {
8698
return &val
8799
}
88-
return newValue(Static, "")
100+
return newValue(Static, DefaultValueForString)
89101
}
90102

91103
// newValue creates a new value instance.
@@ -107,13 +119,7 @@ func (v *value) asBoolean() bool {
107119
return DefaultValueForBoolean
108120
}
109121

110-
for _, truthyValue := range booleanTruthyValues {
111-
if strings.ToLower(v.value) == truthyValue {
112-
return true
113-
}
114-
}
115-
116-
return false
122+
return slices.Contains(booleanTruthyValues, strings.ToLower(v.value))
117123
}
118124

119125
// asInt returns the value as an integer.
@@ -130,12 +136,12 @@ func (v *value) asInt() int {
130136
return num
131137
}
132138

133-
// asFloat returns the value as an integer.
139+
// asFloat returns the value as a float.
134140
func (v *value) asFloat() float64 {
135141
if v.source == Static {
136142
return DefaultValueForNumber
137143
}
138-
num, err := strconv.ParseFloat(v.value, 64)
144+
num, err := strconv.ParseFloat(v.value, doublePrecision)
139145

140146
if err != nil {
141147
return DefaultValueForNumber

0 commit comments

Comments
 (0)