Skip to content

Commit

Permalink
fix: use ID instead of Id
Browse files Browse the repository at this point in the history
  • Loading branch information
nityanandagohain committed Feb 4, 2025
1 parent 2a14f57 commit e6cc5b5
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 36 deletions.
2 changes: 1 addition & 1 deletion ee/featureflag/zeus/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func New(ctx context.Context, providerSettings factory.ProviderSettings, config
return &Provider{}, nil
}

func (p *Provider) GetFeatures(orgId string) []featureflag.Feature {
func (p *Provider) GetFeatures(orgID string) []featureflag.Feature {
// TODO : get the features from zeus
return basePlanFeatures
}
2 changes: 1 addition & 1 deletion pkg/featureflag/base/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ func New(ctx context.Context, providerSettings factory.ProviderSettings, config
return &Provider{}, nil
}

func (p *Provider) GetFeatures(orgId string) []featureflag.Feature {
func (p *Provider) GetFeatures(orgID string) []featureflag.Feature {
return defaultFeatures
}
6 changes: 3 additions & 3 deletions pkg/featureflag/featureflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import (
// FeatureFlag is the interface that all feature flag providers must implement
type FeatureFlag interface {
//pass context
GetFeatures(orgId string) []Feature
GetFeatures(orgID string) []Feature
}

// Feature is the struct that holds the feature flag data
type Feature struct {
bun.BaseModel `bun:"table:feature_flag"` // This specifies the table name as "features"

OrgId string `bun:"org_id"`
OrgID string `bun:"org_id"`
Name Flag `bun:"name"`
Description string `bun:"description"`
Stage Stage `bun:"stage"`
Expand All @@ -31,7 +31,7 @@ type Feature struct {
// Add a method to Feature for comparison
func (f Feature) Equals(other Feature) bool {
return f.Name == other.Name &&
f.OrgId == other.OrgId &&
f.OrgID == other.OrgID &&
f.Description == other.Description &&
f.Stage == other.Stage &&
f.IsActive == other.IsActive &&
Expand Down
28 changes: 14 additions & 14 deletions pkg/featureflag/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,20 @@ func (fm *FeatureFlagManager) Stop(ctx context.Context) error {

func (fm *FeatureFlagManager) RefreshFeatureFlags() {
var wg sync.WaitGroup
orgIds, err := fm.storage.ListOrgIDs(context.Background())
orgIDs, err := fm.storage.ListOrgIDs(context.Background())
if err != nil {
fm.logger.Error("Error listing orgIds", "error", err)
fm.logger.Error("Error listing orgIDs", "error", err)
return
}
for _, orgId := range orgIds {
for _, orgID := range orgIDs {
wg.Add(1)
go func(orgId string) {
go func(orgID string) {
defer wg.Done()
// Create a map to store features by their flag or identifier
featureMap := make(map[string]Feature)

for _, provider := range fm.providers {
features := provider.GetFeatures(orgId)
features := provider.GetFeatures(orgID)
for _, feature := range features {
// Assuming feature has a method or field `Flag` to identify it
featureMap[feature.Name.String()] = feature
Expand All @@ -93,26 +93,26 @@ func (fm *FeatureFlagManager) RefreshFeatureFlags() {
}

// Save the merged features
err := fm.storage.SaveFeatureFlags(context.Background(), orgId, mergedFeatures)
err := fm.storage.SaveFeatureFlags(context.Background(), orgID, mergedFeatures)
if err != nil {
fm.logger.Error("Failed to save features", "orgId", orgId, "error", err)
fm.logger.Error("Failed to save features", "orgID", orgID, "error", err)
}
}(orgId)
}(orgID)
}
wg.Wait()
}

// GetFeatureFlags returns all features for an org
func (fm *FeatureFlagManager) ListFeatureFlags(ctx context.Context, orgId string) ([]Feature, error) {
return fm.storage.ListFeatureFlags(ctx, orgId)
func (fm *FeatureFlagManager) ListFeatureFlags(ctx context.Context, orgID string) ([]Feature, error) {
return fm.storage.ListFeatureFlags(ctx, orgID)
}

// GetFeatureFlag returns a specific feature by flag
func (fm *FeatureFlagManager) GetFeatureFlag(ctx context.Context, orgId string, flag Flag) (Feature, error) {
return fm.storage.GetFeatureFlag(ctx, orgId, flag)
func (fm *FeatureFlagManager) GetFeatureFlag(ctx context.Context, orgID string, flag Flag) (Feature, error) {
return fm.storage.GetFeatureFlag(ctx, orgID, flag)
}

// UpdateFeatureFlag updates a specific feature by flag for an org
func (fm *FeatureFlagManager) UpdateFeatureFlag(ctx context.Context, orgId string, flag Flag, feature Feature) error {
return fm.storage.UpdateFeatureFlag(ctx, orgId, flag, feature)
func (fm *FeatureFlagManager) UpdateFeatureFlag(ctx context.Context, orgID string, flag Flag, feature Feature) error {
return fm.storage.UpdateFeatureFlag(ctx, orgID, flag, feature)
}
34 changes: 17 additions & 17 deletions pkg/featureflag/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (

type Store interface {
ListOrgIDs(ctx context.Context) ([]string, error)
GetFeatureFlag(ctx context.Context, orgId string, name Flag) (Feature, error)
ListFeatureFlags(ctx context.Context, orgId string) ([]Feature, error)
UpdateFeatureFlag(ctx context.Context, orgId string, name Flag, feature Feature) error
SaveFeatureFlags(ctx context.Context, orgId string, features []Feature) error
GetFeatureFlag(ctx context.Context, orgID string, name Flag) (Feature, error)
ListFeatureFlags(ctx context.Context, orgID string) ([]Feature, error)
UpdateFeatureFlag(ctx context.Context, orgID string, name Flag, feature Feature) error
SaveFeatureFlags(ctx context.Context, orgID string, features []Feature) error
}

type FeatureStorage struct {
Expand All @@ -24,20 +24,20 @@ func NewFeatureStorage(db *bun.DB) Store {
}

func (s *FeatureStorage) ListOrgIDs(ctx context.Context) ([]string, error) {
var orgIds []string
var orgIDs []string
err := s.db.NewSelect().
Table("organizations").
Model(&orgIds).
Model(&orgIDs).
Column("id").
Scan(ctx)
return orgIds, err
return orgIDs, err
}

func (s *FeatureStorage) GetFeatureFlag(ctx context.Context, orgId string, name Flag) (Feature, error) {
func (s *FeatureStorage) GetFeatureFlag(ctx context.Context, orgID string, name Flag) (Feature, error) {
var dbFeature Feature
err := s.db.NewSelect().
Model(&dbFeature).
Where("org_id = ?", orgId).
Where("org_id = ?", orgID).
Where("name = ?", name.String()).
Scan(ctx)
if err != nil {
Expand All @@ -47,11 +47,11 @@ func (s *FeatureStorage) GetFeatureFlag(ctx context.Context, orgId string, name
}

// will return all features for an org
func (s *FeatureStorage) ListFeatureFlags(ctx context.Context, orgId string) ([]Feature, error) {
func (s *FeatureStorage) ListFeatureFlags(ctx context.Context, orgID string) ([]Feature, error) {
var features []Feature
err := s.db.NewSelect().
Model(&features).
Where("org_id = ?", orgId).
Where("org_id = ?", orgID).
Scan(ctx)
if err != nil {
return nil, err
Expand All @@ -60,11 +60,11 @@ func (s *FeatureStorage) ListFeatureFlags(ctx context.Context, orgId string) ([]
}

// UpdateFeature updates a specific feature by flag for an org
func (s *FeatureStorage) UpdateFeatureFlag(ctx context.Context, orgId string, name Flag, feature Feature) error {
func (s *FeatureStorage) UpdateFeatureFlag(ctx context.Context, orgID string, name Flag, feature Feature) error {
_, err := s.db.NewUpdate().
Model(&feature).
Set("description = ?, stage = ?, is_active = ?, is_changed = true", feature.Description, feature.Stage.String(), feature.IsActive).
Where("org_id = ?", orgId).
Where("org_id = ?", orgID).
Where("name = ?", name.String()).
Where("is_changeable = true").
Exec(ctx)
Expand All @@ -74,7 +74,7 @@ func (s *FeatureStorage) UpdateFeatureFlag(ctx context.Context, orgId string, na
// SaveFeatureFlags saves a list of features to the database
// we write all the features to the db first time.
// if any update for zeus/constants it will update the flags as they will have IsChangeable = false
func (s *FeatureStorage) SaveFeatureFlags(ctx context.Context, orgId string, features []Feature) error {
func (s *FeatureStorage) SaveFeatureFlags(ctx context.Context, orgID string, features []Feature) error {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return fmt.Errorf("failed to begin transaction: %w", err)
Expand All @@ -95,7 +95,7 @@ func (s *FeatureStorage) SaveFeatureFlags(ctx context.Context, orgId string, fea
var existingFeatures []Feature
err = tx.NewSelect().
Model(&existingFeatures).
Where("org_id = ?", orgId).
Where("org_id = ?", orgID).
Scan(ctx)
if err != nil {
return err
Expand All @@ -108,7 +108,7 @@ func (s *FeatureStorage) SaveFeatureFlags(ctx context.Context, orgId string, fea
}

for _, feature := range features {
feature.OrgId = orgId
feature.OrgID = orgID
dbFeature, exists := existingFeaturesMap[feature.Name.String()]
if !exists {
// Feature not present, create it
Expand All @@ -127,7 +127,7 @@ func (s *FeatureStorage) SaveFeatureFlags(ctx context.Context, orgId string, fea
// Update the feature
_, err = tx.NewUpdate().
Model(&feature).
Where("org_id = ? AND name = ?", feature.OrgId, feature.Name.String()).
Where("org_id = ? AND name = ?", feature.OrgID, feature.Name.String()).
Exec(ctx)
if err != nil {
return err
Expand Down

0 comments on commit e6cc5b5

Please # to comment.