Skip to content

Commit

Permalink
chore: abstract out the two types of DistributionRequests
Browse files Browse the repository at this point in the history
  • Loading branch information
yquansah committed Mar 29, 2023
1 parent 5ca6d24 commit 75cb62c
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions internal/storage/sql/common/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,28 +430,33 @@ func (s *Store) orderRules(ctx context.Context, runner sq.BaseRunner, namespaceK
return nil
}

func (s *Store) distributionValidationHelper(ctx context.Context, ruleId, variantId, flagKey, namespaceKey string) error {
func (s *Store) distributionValidationHelper(ctx context.Context, distributionRequest interface {
GetFlagKey() string
GetNamespaceKey() string
GetVariantId() string
GetRuleId() string
}) error {
var ruleNamespace, variantNamespace, flagNamespace string

if err := s.builder.Select("namespace_key").
From("rules").
Where(sq.Eq{"id": ruleId}).
Where(sq.Eq{"id": distributionRequest.GetRuleId()}).
QueryRowContext(ctx).
Scan(&ruleNamespace); err != nil {
return err
}

if err := s.builder.Select("namespace_key").
From("variants").
Where(sq.Eq{"id": variantId}).
Where(sq.Eq{"id": distributionRequest.GetVariantId()}).
QueryRowContext(ctx).
Scan(&variantNamespace); err != nil {
return err
}

if err := s.builder.Select("namespace_key").
From("flags").
Where(sq.And{sq.Eq{"\"key\"": flagKey}, sq.Eq{"namespace_key": namespaceKey}}).
Where(sq.And{sq.Eq{"\"key\"": distributionRequest.GetFlagKey()}, sq.Eq{"namespace_key": distributionRequest.GetNamespaceKey()}}).
QueryRowContext(ctx).
Scan(&flagNamespace); err != nil {
return err
Expand Down Expand Up @@ -486,7 +491,7 @@ func (s *Store) CreateDistribution(ctx context.Context, r *flipt.CreateDistribut
}
)

err := s.distributionValidationHelper(ctx, r.RuleId, r.VariantId, r.FlagKey, r.NamespaceKey)
err := s.distributionValidationHelper(ctx, r)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, errs.ErrNotFoundf("variant %q, rule %q, flag %q in namespace %q", r.VariantId, r.RuleId, r.FlagKey, r.NamespaceKey)
Expand Down Expand Up @@ -517,7 +522,7 @@ func (s *Store) UpdateDistribution(ctx context.Context, r *flipt.UpdateDistribut
r.NamespaceKey = storage.DefaultNamespace
}

err := s.distributionValidationHelper(ctx, r.RuleId, r.VariantId, r.FlagKey, r.NamespaceKey)
err := s.distributionValidationHelper(ctx, r)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, errs.ErrNotFoundf("variant %q, rule %q, flag %q in namespace %q", r.VariantId, r.RuleId, r.FlagKey, r.NamespaceKey)
Expand Down

0 comments on commit 75cb62c

Please sign in to comment.