From ff4b753fc64076075122853c0d51614c02d44d8e Mon Sep 17 00:00:00 2001 From: iman tung Date: Mon, 28 Oct 2019 14:40:16 +0700 Subject: [PATCH] change Config from interface to struct --- EXPERIMENTAL/typicmd/prebuilder/prebuilder.go | 4 +-- EXPERIMENTAL/typictx/configurer.go | 27 +++---------------- EXPERIMENTAL/typictx/context.go | 5 +--- EXPERIMENTAL/typictx/modules.go | 4 +-- EXPERIMENTAL/typienv/env_file.go | 2 +- pkg/module/typpostgres/module.go | 2 +- pkg/module/typredis/module.go | 2 +- pkg/module/typserver/module.go | 2 +- typical/context.go | 2 +- 9 files changed, 12 insertions(+), 38 deletions(-) diff --git a/EXPERIMENTAL/typicmd/prebuilder/prebuilder.go b/EXPERIMENTAL/typicmd/prebuilder/prebuilder.go index 1761e6df..38e778c4 100644 --- a/EXPERIMENTAL/typicmd/prebuilder/prebuilder.go +++ b/EXPERIMENTAL/typicmd/prebuilder/prebuilder.go @@ -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 diff --git a/EXPERIMENTAL/typictx/configurer.go b/EXPERIMENTAL/typictx/configurer.go index 5c236f80..b4e69c19 100644 --- a/EXPERIMENTAL/typictx/configurer.go +++ b/EXPERIMENTAL/typictx/configurer.go @@ -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{} } diff --git a/EXPERIMENTAL/typictx/context.go b/EXPERIMENTAL/typictx/context.go index f5b894fa..e5cbb18f 100644 --- a/EXPERIMENTAL/typictx/context.go +++ b/EXPERIMENTAL/typictx/context.go @@ -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 } diff --git a/EXPERIMENTAL/typictx/modules.go b/EXPERIMENTAL/typictx/modules.go index fdae1cb7..f1ee5ed8 100644 --- a/EXPERIMENTAL/typictx/modules.go +++ b/EXPERIMENTAL/typictx/modules.go @@ -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 diff --git a/EXPERIMENTAL/typienv/env_file.go b/EXPERIMENTAL/typienv/env_file.go index 1496ecc5..f7b1f81d 100644 --- a/EXPERIMENTAL/typienv/env_file.go +++ b/EXPERIMENTAL/typienv/env_file.go @@ -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 } diff --git a/pkg/module/typpostgres/module.go b/pkg/module/typpostgres/module.go index 3414366a..12ac2026 100644 --- a/pkg/module/typpostgres/module.go +++ b/pkg/module/typpostgres/module.go @@ -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{ diff --git a/pkg/module/typredis/module.go b/pkg/module/typredis/module.go index 616671f0..33b5dfe0 100644 --- a/pkg/module/typredis/module.go +++ b/pkg/module/typredis/module.go @@ -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", diff --git a/pkg/module/typserver/module.go b/pkg/module/typserver/module.go index 405a99bd..5da1246b 100644 --- a/pkg/module/typserver/module.go +++ b/pkg/module/typserver/module.go @@ -10,6 +10,6 @@ func Module() *typictx.Module { Name: "Server", OpenFunc: Create, CloseFunc: Shutdown, - Config: typictx.NewConfig("SERVER", &Config{}), + Config: typictx.Config{"SERVER", &Config{}}, } } diff --git a/typical/context.go b/typical/context.go index a3609ecb..bceb8ff9 100644 --- a/typical/context.go +++ b/typical/context.go @@ -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,