Skip to content

Commit

Permalink
Implement global configs storage functions
Browse files Browse the repository at this point in the history
  • Loading branch information
petyos committed Jan 19, 2024
1 parent 94c992f commit a6e9f20
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
3 changes: 3 additions & 0 deletions core/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ type Storage interface {
RegisterStorageListener(listener storage.Listener)
PerformTransaction(func(context storage.TransactionContext) error, int64) error

FindGlobalConfig(context storage.TransactionContext, key string) (*model.GlobalConfigEntry, error)
SaveGlobalConfig(context storage.TransactionContext, globalConfig model.GlobalConfigEntry) error

FindConfig(configType string, appID string, orgID string) (*model.Config, error)
FindConfigByID(id string) (*model.Config, error)
FindConfigs(configType *string) ([]model.Config, error)
Expand Down
3 changes: 2 additions & 1 deletion core/model/globalConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package model

// GlobalConfigEntry represents global config entry.
type GlobalConfigEntry struct {
ID string `bson:"_id"`
Key string `bson:"key"`
Text map[string]interface{} `bson:"data"`
Data map[string]interface{} `bson:"data"`
}
27 changes: 27 additions & 0 deletions driven/storage/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,33 @@ func (a *Adapter) loadConfigs() ([]model.Config, error) {
return configs, nil
}

// FindGlobalConfig finds global config by key
func (a *Adapter) FindGlobalConfig(context TransactionContext, key string) (*model.GlobalConfigEntry, error) {
var err error

filter := bson.D{
bson.E{Key: "key", Value: key},
}

var globalConfig model.GlobalConfigEntry
err = a.db.globalConfigs.FindOneWithContext(context, filter, &globalConfig, nil)
if err != nil {
return nil, err
}

return &globalConfig, nil
}

// FindGlobalConfig saves global config

Check failure on line 215 in driven/storage/adapter.go

View workflow job for this annotation

GitHub Actions / build

comment on exported method Adapter.SaveGlobalConfig should be of the form "SaveGlobalConfig ..."
func (a *Adapter) SaveGlobalConfig(context TransactionContext, globalConfig model.GlobalConfigEntry) error {
filter := bson.D{primitive.E{Key: "_id", Value: globalConfig.ID}}
err := a.db.globalConfigs.ReplaceOneWithContext(context, filter, globalConfig, nil)
if err != nil {
return err
}
return nil
}

// FindConfig finds the config for the specified type, appID, and orgID
func (a *Adapter) FindConfig(configType string, appID string, orgID string) (*model.Config, error) {
return a.getCachedConfig("", configType, appID, orgID)
Expand Down
2 changes: 1 addition & 1 deletion driven/storage/adapter_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (a *Adapter) FindExample(orgID string, appID string, id string) (*model.Exa
filter := bson.M{"org_id": orgID, "app_id": appID, "_id": id}

var data *model.Example
err := a.db.examples.FindOne(a.context, filter, &data, nil)
err := a.db.examples.FindOneWithContext(a.context, filter, &data, nil)
if err != nil {
return nil, errors.WrapErrorAction(logutils.ActionFind, model.TypeExample, filterArgs(filter), err)
}
Expand Down
12 changes: 10 additions & 2 deletions driven/storage/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ func (collWrapper *collectionWrapper) Find(ctx context.Context, filter interface
return err
}

func (collWrapper *collectionWrapper) FindOne(ctx context.Context, filter interface{}, result interface{}, findOptions *options.FindOneOptions) error {
func (collWrapper *collectionWrapper) FindOne(filter interface{}, result interface{}, findOptions *options.FindOneOptions) error {
return collWrapper.FindOneWithContext(context.Background(), filter, result, findOptions)
}

func (collWrapper *collectionWrapper) FindOneWithContext(ctx context.Context, filter interface{}, result interface{}, findOptions *options.FindOneOptions) error {
if ctx == nil {
ctx = context.Background()
}
Expand All @@ -76,7 +80,11 @@ func (collWrapper *collectionWrapper) FindOne(ctx context.Context, filter interf
return nil
}

func (collWrapper *collectionWrapper) ReplaceOne(ctx context.Context, filter interface{}, replacement interface{}, replaceOptions *options.ReplaceOptions) error {
func (collWrapper *collectionWrapper) ReplaceOne(filter interface{}, replacement interface{}, replaceOptions *options.ReplaceOptions) error {
return collWrapper.ReplaceOneWithContext(context.Background(), filter, replacement, replaceOptions)
}

func (collWrapper *collectionWrapper) ReplaceOneWithContext(ctx context.Context, filter interface{}, replacement interface{}, replaceOptions *options.ReplaceOptions) error {
if ctx == nil {
ctx = context.Background()
}
Expand Down

0 comments on commit a6e9f20

Please sign in to comment.