Created
February 27, 2024 07:56
-
-
Save dezren39/e57ce6e3e3d9f850bff84419c6fc0d9d to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"fmt" | |
) | |
type Empty interface { | |
IsEmpty() bool, error | |
} | |
type Config interface { | |
Get(key string) string | |
Set(key string, value string) | |
GetAll() map[string]string | |
} | |
type UsesConfig interface { | |
SetConfig(cfg Config) | |
} | |
type Configurable interface { | |
UsesConfig | |
GetConfig() *map[string]string | |
} | |
type Configuration struct { | |
cfg map[string]string | |
} | |
func NewTestConfig() Config { | |
cfg := &Configuration{ | |
cfg: make(map[string]string), | |
} | |
cfg.Set("test", "test") | |
return cfg | |
} | |
func (c *Configuration) Get(key string) string { | |
return c.cfg[key] | |
} | |
func (c *Configuration) Set(key string, value string) { | |
c.cfg[key] = value | |
} | |
func (c *Configuration) GetAll() map[string]string { | |
return c.cfg | |
} | |
type Service interface { | |
Start() error | |
Stop() error | |
HealthCheck() error | |
} | |
type Repo interface { | |
Service | |
UsesConfig | |
Configurable | |
} | |
type testRepo struct { | |
Configuration | |
} | |
func (r *testRepo) GetConfig() *map[string]string { | |
return &r.cfg | |
} | |
func (r *testRepo) SetConfig(cfg Config) { | |
r.cfg = cfg.GetAll() | |
} | |
func (r *testRepo) Start() error { | |
fmt.Println("TestRepo Start") | |
return nil | |
} | |
func (r *testRepo) Stop() error { | |
fmt.Println("TestRepo Stop") | |
return nil | |
} | |
func (r *testRepo) HealthCheck() error { | |
fmt.Println("TestRepo HealthCheck") | |
return nil | |
} | |
func NewTestRepo() (Repo, error) { | |
repo := &testRepo{} | |
repo.SetConfig(NewTestConfig()) | |
return repo, nil | |
} | |
func main() { | |
repo, err := NewTestRepo() | |
if err != nil { | |
fmt.Println("Error creating repo:", err) | |
return | |
} | |
repo.Start() | |
repo.HealthCheck() | |
fmt.Println("Config:", repo.GetConfig()) | |
repo.Stop() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment