Skip to content

Commit 98754ac

Browse files
committed
apply @genesor suggestions
Signed-off-by: Eliott Bouhana <[email protected]>
1 parent a4b6096 commit 98754ac

File tree

6 files changed

+30
-29
lines changed

6 files changed

+30
-29
lines changed

openfeature/doc.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@
226226
// defer tracer.Stop()
227227
//
228228
// // Create OpenFeature provider
229-
// provider, err := openfeature.NewDatadogProvider()
229+
// provider, err := openfeature.NewDatadogProvider(openfeature.ProviderConfig{})
230230
// if err != nil {
231231
// log.Fatalf("Failed to create provider: %v", err)
232232
// }
@@ -278,7 +278,7 @@
278278
// provider := openfeature.newDatadogProvider()
279279
//
280280
// // Manually set test configuration
281-
// config := &serverConfiguration{
281+
// config := &universalFlagConfiguration{
282282
// Format: "SERVER",
283283
// Flags: map[string]*flag{
284284
// "test-flag": {

openfeature/integration_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -814,20 +814,20 @@ func TestEndToEnd_JSONSerialization(t *testing.T) {
814814
originalConfig := createE2EBooleanConfig()
815815

816816
// Serialize to JSON
817-
data, err := json.Marshal(serverConfiguration{originalConfig})
817+
data, err := json.Marshal(originalConfig)
818818
if err != nil {
819819
t.Fatalf("failed to marshal config: %v", err)
820820
}
821821

822822
// Deserialize from JSON
823-
var parsedConfig serverConfiguration
823+
var parsedConfig universalFlagsConfiguration
824824
if err := json.Unmarshal(data, &parsedConfig); err != nil {
825825
t.Fatalf("failed to unmarshal config: %v", err)
826826
}
827827

828828
// Use the parsed config
829829
provider := newDatadogProvider()
830-
provider.updateConfiguration(&parsedConfig.FlagConfiguration)
830+
provider.updateConfiguration(&parsedConfig)
831831

832832
err = of.SetProviderAndWait(provider)
833833
if err != nil {

openfeature/provider.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ type DatadogProvider struct {
3838
configChange sync.Cond
3939
}
4040

41+
type ProviderConfig struct {
42+
// Add any configuration fields if needed in the future
43+
}
44+
4145
// NewDatadogProvider creates a new Datadog OpenFeature provider.
4246
// It subscribes to Remote Config updates and automatically updates the provider's configuration
4347
// when new flag configurations are received.
@@ -47,9 +51,10 @@ type DatadogProvider struct {
4751
//
4852
// Returns an error if the default configuration of the Remote Config client is NOT working
4953
// In this case, please call tracer.Start before creating the provider.
50-
func NewDatadogProvider() (*DatadogProvider, error) {
54+
func NewDatadogProvider(ProviderConfig) (openfeature.FeatureProvider, error) {
5155
if !internal.BoolEnv(ffeProductEnvVar, false) {
52-
return nil, fmt.Errorf("experimental flagging provider is not enabled: set %s=true to enable it", ffeProductEnvVar)
56+
log.Error("openfeature: experimental flagging provider is not enabled, please set %s=true to enable it", ffeProductEnvVar)
57+
return &openfeature.NoopProvider{}, nil
5358
}
5459

5560
return startWithRemoteConfig()

openfeature/remoteconfig.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func processConfigUpdate(provider *DatadogProvider, path string, data []byte) rc
7272
// Parse the configuration
7373
log.Debug("openfeature: remote config: processing configuration update %q: %s", path, string(data))
7474

75-
var config serverConfiguration
75+
var config universalFlagsConfiguration
7676
if err := json.Unmarshal(data, &config); err != nil {
7777
log.Error("openfeature: remote config: failed to unmarshal configuration %q: %v", path, err.Error())
7878
return rc.ApplyStatus{
@@ -82,7 +82,7 @@ func processConfigUpdate(provider *DatadogProvider, path string, data []byte) rc
8282
}
8383

8484
// Validate the configuration
85-
err := validateConfiguration(&config.FlagConfiguration)
85+
err := validateConfiguration(&config)
8686
if err != nil {
8787
log.Error("openfeature: remote config: invalid configuration %q: %v", path, err.Error())
8888
return rc.ApplyStatus{
@@ -92,8 +92,8 @@ func processConfigUpdate(provider *DatadogProvider, path string, data []byte) rc
9292
}
9393

9494
// Update the provider with the new configuration
95-
provider.updateConfiguration(&config.FlagConfiguration)
96-
log.Debug("openfeature: remote config: successfully applied configuration %q with %d flags", path, len(config.FlagConfiguration.Flags))
95+
provider.updateConfiguration(&config)
96+
log.Debug("openfeature: remote config: successfully applied configuration %q with %d flags", path, len(config.Flags))
9797

9898
return rc.ApplyStatus{
9999
State: rc.ApplyStateAcknowledged,
@@ -102,13 +102,11 @@ func processConfigUpdate(provider *DatadogProvider, path string, data []byte) rc
102102

103103
// validateConfiguration performs basic validation on a serverConfiguration.
104104
func validateConfiguration(config *universalFlagsConfiguration) error {
105-
if config == nil {
106-
return fmt.Errorf("configuration is nil")
105+
if config.Format != "SERVER" {
106+
return fmt.Errorf("unsupported format %q, expected SERVER (Is the remote config payload the right format ?)", config.Format)
107107
}
108108

109-
if config.Format != "SERVER" && config.Format != "" {
110-
return fmt.Errorf("unsupported format %q, expected SERVER", config.Format)
111-
}
109+
hasFlags := len(config.Flags) > 0
112110

113111
// Validate each flag and delete invalid ones from the map
114112
// Collect errors for reporting
@@ -119,6 +117,10 @@ func validateConfiguration(config *universalFlagsConfiguration) error {
119117
return err != nil
120118
})
121119

120+
if hasFlags && len(config.Flags) == 0 {
121+
errs = append(errs, errors.New("all flags are invalid"))
122+
}
123+
122124
return errors.Join(errs...)
123125
}
124126

openfeature/remoteconfig_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ func TestProcessConfigUpdate(t *testing.T) {
434434
},
435435
}
436436

437-
data, err := json.Marshal(serverConfiguration{config})
437+
data, err := json.Marshal(config)
438438
if err != nil {
439439
t.Fatalf("failed to marshal config: %v", err)
440440
}
@@ -508,7 +508,7 @@ func TestProcessConfigUpdate(t *testing.T) {
508508
Flags: map[string]*flag{},
509509
}
510510

511-
data, _ := json.Marshal(serverConfiguration{config})
511+
data, _ := json.Marshal(config)
512512
status := processConfigUpdate(provider, "test-path", data)
513513

514514
if status.State != rc.ApplyStateError {
@@ -553,7 +553,7 @@ func TestCreateRemoteConfigCallback(t *testing.T) {
553553
},
554554
}
555555

556-
data, _ := json.Marshal(serverConfiguration{config})
556+
data, _ := json.Marshal(config)
557557

558558
// Simulate Remote Config update with multiple paths
559559
update := remoteconfig.ProductUpdate{
@@ -613,8 +613,8 @@ func TestRemoteConfigIntegration(t *testing.T) {
613613
},
614614
}
615615

616-
data1, _ := json.Marshal(serverConfiguration{config1})
617-
data2, _ := json.Marshal(serverConfiguration{config2})
616+
data1, _ := json.Marshal(config1)
617+
data2, _ := json.Marshal(config2)
618618

619619
// First update
620620
update1 := remoteconfig.ProductUpdate{
@@ -663,7 +663,7 @@ func TestRemoteConfigIntegration(t *testing.T) {
663663
},
664664
}
665665

666-
validData, _ := json.Marshal(serverConfiguration{validConfig})
666+
validData, _ := json.Marshal(validConfig)
667667
invalidData := []byte("{invalid")
668668

669669
update := remoteconfig.ProductUpdate{
@@ -721,7 +721,7 @@ func TestConfigurationPersistence(t *testing.T) {
721721

722722
// Apply configurations sequentially
723723
for i, config := range configs {
724-
data, _ := json.Marshal(serverConfiguration{config})
724+
data, _ := json.Marshal(config)
725725
update := remoteconfig.ProductUpdate{
726726
"config": data,
727727
}

openfeature/types.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@ package openfeature
77

88
import "time"
99

10-
// serverConfiguration represents the top-level configuration received from Remote Config.
11-
// It contains the feature flags configuration for a specific environment.
12-
type serverConfiguration struct {
13-
FlagConfiguration universalFlagsConfiguration `json:"flag_configuration"`
14-
}
15-
1610
// universalFlagsConfiguration represents the universal feature flags configuration structure.
1711
// of the openfeature standard for server-side flag configurations.
1812
type universalFlagsConfiguration struct {

0 commit comments

Comments
 (0)