Skip to content

Commit 819c22b

Browse files
authored
chore: simplify featuregate interface (#4454)
1 parent 0ade1b7 commit 819c22b

5 files changed

Lines changed: 20 additions & 112 deletions

File tree

openmeter/billing/worker/subscriptionsync/service/reconciler/patch.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,15 @@ func (c patchCollectionRouterConfig) Validate() error {
8080
if c.capacity <= 0 {
8181
return fmt.Errorf("capacity is required")
8282
}
83+
8384
if c.invoices == nil {
8485
return fmt.Errorf("invoices is required")
8586
}
87+
88+
if c.featureGate == nil {
89+
return fmt.Errorf("feature gate is required")
90+
}
91+
8692
return nil
8793
}
8894

@@ -133,11 +139,7 @@ func (c patchCollectionRouter) isCreditsEnabled(ns string) (bool, error) {
133139
if c.creditsFlag == "" {
134140
return true, nil
135141
}
136-
gate, err := c.featureGate.WithOrg(featuregate.NamespaceOrg(ns))
137-
if err != nil {
138-
return false, err
139-
}
140-
return gate.EvaluateBool(c.creditsFlag, false)
142+
return c.featureGate.EvaluateBool(ns, c.creditsFlag, false)
141143
}
142144

143145
func (c patchCollectionRouter) ResolveDefaultCollection(target targetstate.StateItem) (PatchCollection, error) {

openmeter/billing/worker/subscriptionsync/service/reconciler/patch_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,18 +143,14 @@ func TestIsCreditEnabled(t *testing.T) {
143143
})
144144

145145
t.Run("no_feature_gate_client", func(t *testing.T) {
146-
router, err := newPatchCollectionRouter(patchCollectionRouterConfig{
146+
_, err := newPatchCollectionRouter(patchCollectionRouterConfig{
147147
capacity: 1,
148148
invoices: persistedstate.Invoices{},
149149
creditThenInvoiceEnabled: false,
150150
creditsEnabled: true,
151-
featureGate: nil, // If feature gate is nil, it should default to enabled.
151+
featureGate: nil,
152152
})
153-
require.NoError(t, err)
154-
155-
enabled, err := router.isCreditsEnabled("test")
156-
require.NoError(t, err)
157-
require.True(t, enabled)
153+
require.Error(t, err)
158154
})
159155

160156
t.Run("credit_flag_disabled", func(t *testing.T) {

openmeter/billing/worker/subscriptionsync/service/reconciler/reconciler.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ func (c Config) Validate() error {
4949
return fmt.Errorf("charges service is required when credit then invoice is enabled")
5050
}
5151

52+
if c.FeatureGate == nil {
53+
return fmt.Errorf("feature gate is required")
54+
}
55+
5256
return nil
5357
}
5458

openmeter/billing/worker/subscriptionsync/service/service.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ func (c Config) Validate() error {
5959
return fmt.Errorf("tracer is required")
6060
}
6161

62+
if c.FeatureGate == nil {
63+
return fmt.Errorf("feature gate is required")
64+
}
65+
6266
return nil
6367
}
6468

pkg/featuregate/featuregate.go

Lines changed: 2 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,7 @@
11
package featuregate
22

3-
import (
4-
"encoding/json"
5-
6-
"github.com/google/uuid"
7-
"github.com/samber/lo"
8-
)
9-
10-
type Org interface {
11-
Context
12-
13-
ID() *uuid.UUID
14-
PortalID() *uuid.UUID
15-
OrgName() *string
16-
FeatureSet() *string
17-
Tier() *string
18-
}
19-
20-
type Context interface {
21-
Key() string
22-
Kind() string
23-
Anonymous() bool
24-
GetCustomAttributes() map[string]any
25-
AddCustomAttribute(name string, value any)
26-
}
27-
283
type Gate interface {
29-
EvaluateBool(flag string, defaultValue bool) (bool, error)
30-
EvaluateInt(flag string, defaultValue int) (int, error)
31-
EvaluateFloat64(flag string, defaultValue float64) (float64, error)
32-
EvaluateString(flag string, defaultValue string) (string, error)
33-
EvaluateJSON(flag string, defaultValue json.RawMessage) (json.RawMessage, error)
34-
35-
WithOrg(org Org) (Gate, error)
36-
WithFFContext(custom ...Context) (Gate, error)
4+
EvaluateBool(namespace, flag string, defaultValue bool) (bool, error)
375
}
386

397
func NewNoop() Gate {
@@ -42,72 +10,6 @@ func NewNoop() Gate {
4210

4311
type Noop struct{}
4412

45-
func (n Noop) EvaluateBool(string, bool) (bool, error) {
13+
func (n Noop) EvaluateBool(string, string, bool) (bool, error) {
4614
return true, nil
4715
}
48-
49-
func (n Noop) EvaluateInt(string, int) (int, error) {
50-
return 0, nil
51-
}
52-
53-
func (n Noop) EvaluateFloat64(string, float64) (float64, error) {
54-
return 0, nil
55-
}
56-
57-
func (n Noop) EvaluateString(string, string) (string, error) {
58-
return "", nil
59-
}
60-
61-
func (n Noop) EvaluateJSON(string, json.RawMessage) (json.RawMessage, error) {
62-
return json.RawMessage(`{}`), nil
63-
}
64-
65-
func (n Noop) WithFFContext(custom ...Context) (Gate, error) {
66-
return Noop{}, nil
67-
}
68-
69-
func (n Noop) WithOrg(org Org) (Gate, error) {
70-
return n.WithFFContext(org)
71-
}
72-
73-
type NamespaceOrg string
74-
75-
var _ Org = NamespaceOrg("")
76-
77-
func (n NamespaceOrg) AddCustomAttribute(name string, value any) {}
78-
79-
func (n NamespaceOrg) Anonymous() bool {
80-
return false
81-
}
82-
83-
func (n NamespaceOrg) FeatureSet() *string {
84-
return nil
85-
}
86-
87-
func (n NamespaceOrg) GetCustomAttributes() map[string]any {
88-
return nil
89-
}
90-
91-
func (n NamespaceOrg) ID() *uuid.UUID {
92-
return lo.ToPtr(uuid.NewSHA1(uuid.NameSpaceURL, []byte(n)))
93-
}
94-
95-
func (n NamespaceOrg) Key() string {
96-
return string(n)
97-
}
98-
99-
func (n NamespaceOrg) Kind() string {
100-
return "namespace"
101-
}
102-
103-
func (n NamespaceOrg) OrgName() *string {
104-
return lo.ToPtr(string(n))
105-
}
106-
107-
func (n NamespaceOrg) PortalID() *uuid.UUID {
108-
return n.ID()
109-
}
110-
111-
func (n NamespaceOrg) Tier() *string {
112-
return nil
113-
}

0 commit comments

Comments
 (0)