-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Maya Sergeeva
committed
Dec 10, 2021
1 parent
31d9466
commit 44ebcfa
Showing
8 changed files
with
141 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package commands | ||
|
||
type CommandContextKey string | ||
|
||
const ( | ||
CommandContextCfgKey CommandContextKey = "cfg" | ||
CommandContextCfgKeyDB = CommandContextCfgKey + ".db" | ||
CommandContextCfgKeyLog = CommandContextCfgKey + ".log" | ||
CommandContextCfgKeyAppInfo = CommandContextCfgKey + ".appInfo" | ||
CommandContextCfgKeyStage = CommandContextCfgKey + ".stage" | ||
) | ||
|
||
const ( | ||
CommandContextObjectKey CommandContextKey = "obj" | ||
CommandContextObjectKeySeeder = CommandContextObjectKey + ".seeder" | ||
CommandContextObjectKeyConfig = CommandContextObjectKey + ".config" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package commands | ||
|
||
import "errors" | ||
|
||
var ( | ||
ErrNoMethodFound = errors.New("seed method is not exists") | ||
ErrSeedIsDisabled = errors.New("seed method is disabled in config") | ||
ErrSeedClassNameNotRegistered = errors.New("seed class name not registered") | ||
ErrSeedClassIsNotValid = errors.New("seed class name not valid") | ||
ErrSeedClassNotImplementInterface = errors.New("seed class name not implements commands.SeedInterface") | ||
ErrBadContextValue = errors.New("context value is empty or has wrong type") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package commands | ||
|
||
import ( | ||
cfgstructs "github.com/spacetab-io/configuration-structs-go" | ||
) | ||
|
||
type SeedInterface interface { | ||
Enabled() bool | ||
Name() string | ||
Seed() error | ||
SetRepo(r interface{}) | ||
SetCfg(c cfgstructs.SeedInfo) | ||
} | ||
|
||
type SeederInterface interface { | ||
GetMethods() map[string]SeedInterface | ||
GetMethod(name string) (SeedInterface, error) | ||
SeedsList() []string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package commands | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
|
||
cfgstructs "github.com/spacetab-io/configuration-structs-go" | ||
) | ||
|
||
type Seeder struct { | ||
seeds []cfgstructs.SeedInfo | ||
methods map[string]SeedInterface | ||
} | ||
|
||
// SeedMethodType checks SeedInterface implementation and returns Seed object reflect.Type. | ||
func SeedMethodType(seed SeedInterface) reflect.Type { | ||
return reflect.ValueOf(seed).Elem().Type() | ||
} | ||
|
||
// NewSeeder initiates new Seeder object. | ||
func NewSeeder(cfg cfgstructs.SeedingCfg, seeds map[string]reflect.Type, repo interface{}) (SeederInterface, error) { | ||
s := Seeder{seeds: cfg.Seeds, methods: make(map[string]SeedInterface)} | ||
|
||
for _, seedCfg := range s.seeds { | ||
if !seedCfg.Enabled { | ||
continue | ||
} | ||
|
||
seedType, ok := seeds[seedCfg.ClassName] | ||
if !ok { | ||
return nil, fmt.Errorf("%w: %s", ErrSeedClassNameNotRegistered, seedCfg.ClassName) | ||
} | ||
|
||
seedValue := reflect.New(seedType) | ||
if !seedValue.IsValid() { | ||
return nil, fmt.Errorf("%w: %s", ErrSeedClassIsNotValid, seedCfg.ClassName) | ||
} | ||
|
||
seed, ok := seedValue.Interface().(SeedInterface) | ||
if !ok || seed == nil { | ||
return nil, fmt.Errorf("%w: %s", ErrSeedClassNotImplementInterface, seedCfg.ClassName) | ||
} | ||
|
||
seed.SetCfg(seedCfg) | ||
seed.SetRepo(repo) | ||
|
||
s.methods[seedCfg.Name] = seed | ||
} | ||
|
||
return s, nil | ||
} | ||
|
||
func (s Seeder) GetMethods() map[string]SeedInterface { | ||
return s.methods | ||
} | ||
|
||
func (s Seeder) GetMethod(name string) (SeedInterface, error) { | ||
m, ok := s.methods[name] | ||
if !ok { | ||
return nil, fmt.Errorf("Seeder.GetMethod %s error: %w", name, ErrNoMethodFound) | ||
} | ||
|
||
if !m.Enabled() { | ||
return nil, fmt.Errorf("Seeder.GetMethod %s error: %w", name, ErrSeedIsDisabled) | ||
} | ||
|
||
return m, nil | ||
} | ||
|
||
func (s Seeder) SeedsList() []string { | ||
result := make([]string, 0) | ||
|
||
for _, seed := range s.seeds { | ||
status := "+" | ||
if !seed.Enabled { | ||
status = "-" | ||
} | ||
|
||
result = append(result, fmt.Sprintf("[%s] %s - %s", status, seed.Name, seed.Description)) | ||
} | ||
|
||
return result | ||
} |