Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ jobs:
exit 1
fi

- name: Install mockgen
run: go install go.uber.org/mock/mockgen@latest

- name: Run go generate
run: go generate ./...

- name: Run golangci-lint
uses: golangci/golangci-lint-action@v9

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/danielgtaylor/huma/v2 v2.34.1
github.com/go-chi/chi/v5 v5.2.3
github.com/stretchr/testify v1.11.1
go.uber.org/mock v0.6.0
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y=
go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
14 changes: 9 additions & 5 deletions internal/flags/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,23 @@ func NewServiceWithMatcher(repo Repository, matcher RuleMatcher) *Service {
}
}

func (s *Service) Create(ctx context.Context, flag *Flag) error {
func (s *Service) Create(ctx context.Context, flag Flag) (Flag, error) {
flag.UpdatedAt = time.Now()

return s.repo.Create(ctx, flag)
if err := s.repo.Create(ctx, &flag); err != nil {
return Flag{}, err
}

return flag, nil
}

func (s *Service) Evaluate(ctx context.Context, key string, evalCtx EvalContext) (*EvalResult, error) {
func (s *Service) Evaluate(ctx context.Context, key string, evalCtx EvalContext) (EvalResult, error) {
flag, err := s.repo.Get(ctx, key)
if err != nil {
return nil, err
return EvalResult{}, err
}

result := &EvalResult{
result := EvalResult{
FlagKey: key,
EvaluatedAt: time.Now(),
}
Expand Down
69 changes: 41 additions & 28 deletions internal/flags/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@ func TestService_Create(t *testing.T) {
svc := flags.NewService(repo)
ctx := context.Background()

flag := &flags.Flag{
input := flags.Flag{
Key: "new-feature",
Type: flags.FlagBool,
Enabled: true,
DefaultValue: flags.BoolValue(false),
}

err := svc.Create(ctx, flag)
created, err := svc.Create(ctx, input)
require.NoError(t, err)

assert.False(t, flag.UpdatedAt.IsZero())
assert.Equal(t, "new-feature", created.Key)
assert.False(t, created.UpdatedAt.IsZero())
}

func TestService_Create_Duplicate(t *testing.T) {
Expand All @@ -36,15 +37,16 @@ func TestService_Create_Duplicate(t *testing.T) {
svc := flags.NewService(repo)
ctx := context.Background()

flag := &flags.Flag{
input := flags.Flag{
Key: "new-feature",
Type: flags.FlagBool,
Enabled: true,
}

require.NoError(t, svc.Create(ctx, flag))
err := svc.Create(ctx, flag)
_, err := svc.Create(ctx, input)
require.NoError(t, err)

_, err = svc.Create(ctx, input)
assert.ErrorIs(t, err, flags.ErrFlagExists)
}

Expand All @@ -55,13 +57,14 @@ func TestService_Evaluate_ReturnsDefault(t *testing.T) {
svc := flags.NewService(repo)
ctx := context.Background()

flag := &flags.Flag{
flag := flags.Flag{
Key: "my-flag",
Type: flags.FlagBool,
Enabled: true,
DefaultValue: flags.BoolValue(true),
}
require.NoError(t, svc.Create(ctx, flag))
_, err := svc.Create(ctx, flag)
require.NoError(t, err)

result, err := svc.Evaluate(ctx, "my-flag", flags.EvalContext{})
require.NoError(t, err)
Expand All @@ -79,13 +82,14 @@ func TestService_Evaluate_DisabledFlag(t *testing.T) {
svc := flags.NewService(repo)
ctx := context.Background()

flag := &flags.Flag{
flag := flags.Flag{
Key: "disabled-flag",
Type: flags.FlagBool,
Enabled: false,
DefaultValue: flags.BoolValue(false),
}
require.NoError(t, svc.Create(ctx, flag))
_, err := svc.Create(ctx, flag)
require.NoError(t, err)

result, err := svc.Evaluate(ctx, "disabled-flag", flags.EvalContext{})
require.NoError(t, err)
Expand Down Expand Up @@ -113,7 +117,7 @@ func TestService_Evaluate_RuleMatch_Equals(t *testing.T) {
svc := flags.NewService(repo)
ctx := context.Background()

flag := &flags.Flag{
flag := flags.Flag{
Key: "premium-feature",
Type: flags.FlagBool,
Enabled: true,
Expand All @@ -128,7 +132,8 @@ func TestService_Evaluate_RuleMatch_Equals(t *testing.T) {
},
},
}
require.NoError(t, svc.Create(ctx, flag))
_, err := svc.Create(ctx, flag)
require.NoError(t, err)

result, err := svc.Evaluate(ctx, "premium-feature", flags.EvalContext{
Attrs: map[string]any{"plan": "premium"},
Expand All @@ -147,7 +152,7 @@ func TestService_Evaluate_RuleMatch_NoMatch(t *testing.T) {
svc := flags.NewService(repo)
ctx := context.Background()

flag := &flags.Flag{
flag := flags.Flag{
Key: "premium-feature",
Type: flags.FlagBool,
Enabled: true,
Expand All @@ -162,7 +167,8 @@ func TestService_Evaluate_RuleMatch_NoMatch(t *testing.T) {
},
},
}
require.NoError(t, svc.Create(ctx, flag))
_, err := svc.Create(ctx, flag)
require.NoError(t, err)

result, err := svc.Evaluate(ctx, "premium-feature", flags.EvalContext{
Attrs: map[string]any{"plan": "free"},
Expand All @@ -180,7 +186,7 @@ func TestService_Evaluate_RuleMatch_In(t *testing.T) {
svc := flags.NewService(repo)
ctx := context.Background()

flag := &flags.Flag{
flag := flags.Flag{
Key: "beta-feature",
Type: flags.FlagBool,
Enabled: true,
Expand All @@ -195,7 +201,8 @@ func TestService_Evaluate_RuleMatch_In(t *testing.T) {
},
},
}
require.NoError(t, svc.Create(ctx, flag))
_, err := svc.Create(ctx, flag)
require.NoError(t, err)

result, err := svc.Evaluate(ctx, "beta-feature", flags.EvalContext{
TenantID: "tenant-2",
Expand All @@ -213,7 +220,7 @@ func TestService_Evaluate_RuleMatch_MultipleConditions(t *testing.T) {
svc := flags.NewService(repo)
ctx := context.Background()

flag := &flags.Flag{
flag := flags.Flag{
Key: "geo-feature",
Type: flags.FlagBool,
Enabled: true,
Expand All @@ -229,7 +236,8 @@ func TestService_Evaluate_RuleMatch_MultipleConditions(t *testing.T) {
},
},
}
require.NoError(t, svc.Create(ctx, flag))
_, err := svc.Create(ctx, flag)
require.NoError(t, err)

result, err := svc.Evaluate(ctx, "geo-feature", flags.EvalContext{
Attrs: map[string]any{"plan": "premium", "country": "US"},
Expand All @@ -251,7 +259,7 @@ func TestService_Evaluate_RuleMatch_FirstMatchWins(t *testing.T) {
svc := flags.NewService(repo)
ctx := context.Background()

flag := &flags.Flag{
flag := flags.Flag{
Key: "tiered-feature",
Type: flags.FlagString,
Enabled: true,
Expand All @@ -273,7 +281,8 @@ func TestService_Evaluate_RuleMatch_FirstMatchWins(t *testing.T) {
},
},
}
require.NoError(t, svc.Create(ctx, flag))
_, err := svc.Create(ctx, flag)
require.NoError(t, err)

result, err := svc.Evaluate(ctx, "tiered-feature", flags.EvalContext{
Attrs: map[string]any{"plan": "enterprise"},
Expand All @@ -291,7 +300,7 @@ func TestService_Evaluate_RuleMatch_StartsWith(t *testing.T) {
svc := flags.NewService(repo)
ctx := context.Background()

flag := &flags.Flag{
flag := flags.Flag{
Key: "internal-feature",
Type: flags.FlagBool,
Enabled: true,
Expand All @@ -306,7 +315,8 @@ func TestService_Evaluate_RuleMatch_StartsWith(t *testing.T) {
},
},
}
require.NoError(t, svc.Create(ctx, flag))
_, err := svc.Create(ctx, flag)
require.NoError(t, err)

result, err := svc.Evaluate(ctx, "internal-feature", flags.EvalContext{
Attrs: map[string]any{"email": "admin@company.com"},
Expand All @@ -328,7 +338,7 @@ func TestService_Evaluate_StringValue(t *testing.T) {
svc := flags.NewService(repo)
ctx := context.Background()

flag := &flags.Flag{
flag := flags.Flag{
Key: "welcome-message",
Type: flags.FlagString,
Enabled: true,
Expand All @@ -343,7 +353,8 @@ func TestService_Evaluate_StringValue(t *testing.T) {
},
},
}
require.NoError(t, svc.Create(ctx, flag))
_, err := svc.Create(ctx, flag)
require.NoError(t, err)

result, err := svc.Evaluate(ctx, "welcome-message", flags.EvalContext{
Attrs: map[string]any{"tier": "vip"},
Expand Down Expand Up @@ -374,13 +385,14 @@ func TestService_WithCustomMatcher(t *testing.T) {
svc := flags.NewServiceWithMatcher(repo, customMatcher)
ctx := context.Background()

flag := &flags.Flag{
flag := flags.Flag{
Key: "test-flag",
Type: flags.FlagString,
Enabled: true,
DefaultValue: flags.StringValue("default"),
}
require.NoError(t, svc.Create(ctx, flag))
_, err := svc.Create(ctx, flag)
require.NoError(t, err)

result, err := svc.Evaluate(ctx, "test-flag", flags.EvalContext{})
require.NoError(t, err)
Expand All @@ -397,7 +409,7 @@ func TestService_Evaluate_NumberValue(t *testing.T) {
svc := flags.NewService(repo)
ctx := context.Background()

flag := &flags.Flag{
flag := flags.Flag{
Key: "rate-limit",
Type: flags.FlagNumber,
Enabled: true,
Expand All @@ -419,7 +431,8 @@ func TestService_Evaluate_NumberValue(t *testing.T) {
},
},
}
require.NoError(t, svc.Create(ctx, flag))
_, err := svc.Create(ctx, flag)
require.NoError(t, err)

result, err := svc.Evaluate(ctx, "rate-limit", flags.EvalContext{
Attrs: map[string]any{"plan": "enterprise"},
Expand Down
15 changes: 10 additions & 5 deletions internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,23 @@ import (
"github.com/serroba/features/internal/flags"
)

//go:generate mockgen -destination=mock_service_test.go -package=handler_test . FlagService

type FlagService interface {
Create(ctx context.Context, flag flags.Flag) (flags.Flag, error)
Evaluate(ctx context.Context, key string, evalCtx flags.EvalContext) (flags.EvalResult, error)
}

type Handler struct {
service *flags.Service
service FlagService
}

func New(service *flags.Service) *Handler {
func New(service FlagService) *Handler {
return &Handler{service: service}
}

func (h *Handler) CreateFlag(ctx context.Context, req *CreateFlagRequest) (*CreateFlagResponse, error) {
flag := ToFlag(req.Body)

err := h.service.Create(ctx, flag)
flag, err := h.service.Create(ctx, ToFlag(req.Body))
if err != nil {
if errors.Is(err, flags.ErrFlagExists) {
return nil, huma.Error409Conflict("flag already exists")
Expand Down
Loading