From 82723c8c80e7fcbd39e9e2759f318d2cd93fe2b1 Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Thu, 1 Jun 2023 12:11:28 +0200 Subject: [PATCH] provide Config interface with basic implementation --- config/config.go | 32 ++++++++++++++++++++++++++++++++ examples/demo/etc/etc.go | 8 +++----- examples/demo/main.go | 7 ++++--- scopes.go | 7 +++---- tests/etc/etc.go | 8 +++----- tests/scopes_test.go | 6 ++++-- 6 files changed, 49 insertions(+), 19 deletions(-) create mode 100644 config/config.go diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..10a8fcb --- /dev/null +++ b/config/config.go @@ -0,0 +1,32 @@ +package config + +type Config interface { + Name() string + Children() []Config + AddChild(Config) +} + +type BaseConfig struct { + name string + children []Config +} + +func (cfg *BaseConfig) Name() string { + return cfg.name +} + +func (cfg *BaseConfig) Children() []Config { + return cfg.children +} + +func (cfg *BaseConfig) AddChild(child Config) { + cfg.children = append(cfg.children, child) +} + +func MakeConfig(name string) Config { + return &BaseConfig{name: name} +} + +func Setup(cfg Config) Config { + return cfg +} diff --git a/examples/demo/etc/etc.go b/examples/demo/etc/etc.go index 1e6bf62..aa8ff3c 100644 --- a/examples/demo/etc/etc.go +++ b/examples/demo/etc/etc.go @@ -1,11 +1,9 @@ package etc -import "git.sr.ht/~cco/go-scopes" +import "git.sr.ht/~cco/go-scopes/config" -func Config() *scopes.Config { - return &scopes.Config{ - Name: "dummy", - } +func Config() config.Config { + return config.MakeConfig("dummy") } const ( diff --git a/examples/demo/main.go b/examples/demo/main.go index 9175b3c..e57bef9 100644 --- a/examples/demo/main.go +++ b/examples/demo/main.go @@ -6,11 +6,12 @@ import ( "demo/etc" "git.sr.ht/~cco/go-scopes" + "git.sr.ht/~cco/go-scopes/config" ) func main() { - fmt.Println("vim-go") - scopes.Setup() - fmt.Println(etc.Config().Name) + fmt.Println(etc.Config().Name()) fmt.Println(etc.Override()["name"]) + cfg := config.Setup(etc.Config()) + scopes.RunApp(cfg) } diff --git a/scopes.go b/scopes.go index 96bbd51..1934eff 100644 --- a/scopes.go +++ b/scopes.go @@ -1,8 +1,7 @@ package scopes -type Config struct { - Name string -} +import "git.sr.ht/~cco/go-scopes/config" -func Setup() { +func RunApp(cfg config.Config) { + _ = cfg } diff --git a/tests/etc/etc.go b/tests/etc/etc.go index 60274b0..a1b0494 100644 --- a/tests/etc/etc.go +++ b/tests/etc/etc.go @@ -1,11 +1,9 @@ package etc -import "git.sr.ht/~cco/go-scopes" +import "git.sr.ht/~cco/go-scopes/config" -func Config() *scopes.Config { - return &scopes.Config{ - Name: "dummy", - } +func Config() config.Config { + return config.MakeConfig("dummy") } // collect here the names of fields that may be overridden via diff --git a/tests/scopes_test.go b/tests/scopes_test.go index 3886b76..882cfad 100644 --- a/tests/scopes_test.go +++ b/tests/scopes_test.go @@ -4,6 +4,7 @@ import ( tbase "testing" "git.sr.ht/~cco/go-scopes" + "git.sr.ht/~cco/go-scopes/config" "git.sr.ht/~cco/go-scopes/testing" "git.sr.ht/~cco/go-scopes/tests/etc" ) @@ -14,7 +15,8 @@ func TestConfig(tb *tbase.T) { } func ConfigTest(t *testing.T) { - scopes.Setup() - t.AssertEqual(etc.Config().Name, "dummy") + t.AssertEqual(etc.Config().Name(), "dummy") t.AssertEqual(etc.Override()[etc.NAME], "overridden") + cfg := config.Setup(etc.Config()) + scopes.RunApp(cfg) }