provide Config interface with basic implementation

This commit is contained in:
Helmut Merz 2023-06-01 12:11:28 +02:00
parent 87e9bb5fc4
commit 82723c8c80
6 changed files with 49 additions and 19 deletions

32
config/config.go Normal file
View file

@ -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
}

View file

@ -1,11 +1,9 @@
package etc package etc
import "git.sr.ht/~cco/go-scopes" import "git.sr.ht/~cco/go-scopes/config"
func Config() *scopes.Config { func Config() config.Config {
return &scopes.Config{ return config.MakeConfig("dummy")
Name: "dummy",
}
} }
const ( const (

View file

@ -6,11 +6,12 @@ import (
"demo/etc" "demo/etc"
"git.sr.ht/~cco/go-scopes" "git.sr.ht/~cco/go-scopes"
"git.sr.ht/~cco/go-scopes/config"
) )
func main() { func main() {
fmt.Println("vim-go") fmt.Println(etc.Config().Name())
scopes.Setup()
fmt.Println(etc.Config().Name)
fmt.Println(etc.Override()["name"]) fmt.Println(etc.Override()["name"])
cfg := config.Setup(etc.Config())
scopes.RunApp(cfg)
} }

View file

@ -1,8 +1,7 @@
package scopes package scopes
type Config struct { import "git.sr.ht/~cco/go-scopes/config"
Name string
}
func Setup() { func RunApp(cfg config.Config) {
_ = cfg
} }

View file

@ -1,11 +1,9 @@
package etc package etc
import "git.sr.ht/~cco/go-scopes" import "git.sr.ht/~cco/go-scopes/config"
func Config() *scopes.Config { func Config() config.Config {
return &scopes.Config{ return config.MakeConfig("dummy")
Name: "dummy",
}
} }
// collect here the names of fields that may be overridden via // collect here the names of fields that may be overridden via

View file

@ -4,6 +4,7 @@ import (
tbase "testing" tbase "testing"
"git.sr.ht/~cco/go-scopes" "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/testing"
"git.sr.ht/~cco/go-scopes/tests/etc" "git.sr.ht/~cco/go-scopes/tests/etc"
) )
@ -14,7 +15,8 @@ func TestConfig(tb *tbase.T) {
} }
func ConfigTest(t *testing.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") t.AssertEqual(etc.Override()[etc.NAME], "overridden")
cfg := config.Setup(etc.Config())
scopes.RunApp(cfg)
} }