Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
change Config from interface to struct
Browse files Browse the repository at this point in the history
  • Loading branch information
imantung committed Oct 28, 2019
1 parent fb20fc2 commit ff4b753
Show file tree
Hide file tree
Showing 9 changed files with 12 additions and 38 deletions.
4 changes: 2 additions & 2 deletions EXPERIMENTAL/typicmd/prebuilder/prebuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func (p *prebuilder) Initiate(ctx *typictx.Context) (err error) {
log.Debug("Create configs")
for _, cfg := range ctx.Configs() {
p.Configs = append(p.Configs, config{
Key: fmtConfigKey(cfg.Prefix()),
Typ: fmtConfigTyp(cfg.Spec()),
Key: fmtConfigKey(cfg.Prefix),
Typ: fmtConfigTyp(cfg.Spec),
})
}
return
Expand Down
27 changes: 3 additions & 24 deletions EXPERIMENTAL/typictx/configurer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,7 @@ type Configurer interface {
}

// Config represent the configuration
type Config interface {
Prefix() string
Spec() interface{}
}

// NewConfig return new instance of Config
func NewConfig(prefix string, spec interface{}) Config {
return config{
prefix: prefix,
spec: spec,
}
}

type config struct {
prefix string
spec interface{}
}

func (c config) Prefix() string {
return c.prefix
}

func (c config) Spec() interface{} {
return c.spec
type Config struct {
Prefix string
Spec interface{}
}
5 changes: 1 addition & 4 deletions EXPERIMENTAL/typictx/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ func (c *Context) Invoke(function interface{}) (err error) {

// Configs return config list
func (c *Context) Configs() (cfgs []Config) {
cfg := c.Application.Configure()
if cfg != nil {
cfgs = append(cfgs, c.Application.Configure())
}
cfgs = append(cfgs, c.Application.Configure())
cfgs = append(cfgs, c.Modules.Configs()...)
return
}
Expand Down
4 changes: 1 addition & 3 deletions EXPERIMENTAL/typictx/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ type Modules slice.Interfaces
func (m Modules) Configs() (cfgs []Config) {
for _, module := range m {
if configurer, ok := module.(Configurer); ok {
if cfg := configurer.Configure(); cfg != nil {
cfgs = append(cfgs, cfg)
}
cfgs = append(cfgs, configurer.Configure())
}
}
return
Expand Down
2 changes: 1 addition & 1 deletion EXPERIMENTAL/typienv/env_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func PrepareEnvFile(ctx *typictx.Context) (err error) {
}
defer buf.Close()
for _, cfg := range ctx.Configs() {
envconfig.Usagef(cfg.Prefix(), cfg.Spec(), buf, envTemplate)
envconfig.Usagef(cfg.Prefix, cfg.Spec, buf, envTemplate)
}
return
}
2 changes: 1 addition & 1 deletion pkg/module/typpostgres/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
func Module() *typictx.Module {
return &typictx.Module{
Name: "Postgres Database",
Config: typictx.NewConfig("PG", &Config{}),
Config: typictx.Config{Prefix: "PG", Spec: &Config{}},
OpenFunc: openConnection,
CloseFunc: closeConnection,
// Command: &typictx.Command{
Expand Down
2 changes: 1 addition & 1 deletion pkg/module/typredis/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
func Module() *typictx.Module {
return &typictx.Module{
Name: "Redis",
Config: typictx.NewConfig("REDIS", &Config{}),
Config: typictx.Config{Prefix: "REDIS", Spec: &Config{}},
OpenFunc: Connect,
// Command: &typictx.Command{
// Name: "redis",
Expand Down
2 changes: 1 addition & 1 deletion pkg/module/typserver/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ func Module() *typictx.Module {
Name: "Server",
OpenFunc: Create,
CloseFunc: Shutdown,
Config: typictx.NewConfig("SERVER", &Config{}),
Config: typictx.Config{"SERVER", &Config{}},
}
}
2 changes: 1 addition & 1 deletion typical/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var Context = &typictx.Context{
Description: "Example of typical and scalable RESTful API Server for Go",
Application: typictx.Application{
StartFunc: app.Start,
Config: typictx.NewConfig("APP", &config.Config{}),
Config: typictx.Config{Prefix: "APP", Spec: &config.Config{}},
Initiations: []interface{}{
app.Middlewares,
app.Routes,
Expand Down

0 comments on commit ff4b753

Please sign in to comment.