From cafc27302d7fdf69934f3b5007ddf3f5e0fc1cd4 Mon Sep 17 00:00:00 2001 From: dakkar Date: Thu, 19 Dec 2024 15:16:56 +0000 Subject: add another package to better show the pattern --- business/business.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ cmd/main/main.go | 4 ++-- config/config.go | 30 ++++++++++++++++++++++++------ factory/factory.go | 24 +++++++++++++++++++----- something/something.go | 10 +++------- 5 files changed, 93 insertions(+), 20 deletions(-) create mode 100644 business/business.go diff --git a/business/business.go b/business/business.go new file mode 100644 index 0000000..ecb54ad --- /dev/null +++ b/business/business.go @@ -0,0 +1,45 @@ +package business + +import ( + "fmt" + + "www.thenautilus.net/cgit/go-example/config" + "www.thenautilus.net/cgit/go-example/something" +) + +type OverError struct { + Value, Threshold int +} + +func (e OverError) Error() string { + return fmt.Sprintf("value %d is above threshold %d", e.Value, e.Threshold) +} + +type Logic struct { + somethingClient *something.Something + threshold int +} + +func New(conf *config.BusinessLogicConfig, client *something.Something) Logic { + return Logic{ + somethingClient: client, + threshold: conf.Threshold, + } +} + +func (bl *Logic) DoSomething() error { + value, err := bl.somethingClient.FetchValue() + if err != nil { + return fmt.Errorf("failed to fetch value: %w", err) + } + + if value > bl.threshold { + fmt.Printf("OVER! %d > %d\n", value, bl.threshold) + + return OverError{value, bl.threshold} + } + + fmt.Printf("all good") + + return nil +} diff --git a/cmd/main/main.go b/cmd/main/main.go index 7ad978b..3169f09 100644 --- a/cmd/main/main.go +++ b/cmd/main/main.go @@ -7,9 +7,9 @@ import ( func main() { factory := factorypkg.NewFromConfig() - something := factory.Something() + logic := factory.BusinessLogic() - err := something.DoSomething() + err := logic.DoSomething() if err != nil { log := factory.Logger() log.Error().Err(err).Msg("Can't do the thing") diff --git a/config/config.go b/config/config.go index c28fa14..c4755f6 100644 --- a/config/config.go +++ b/config/config.go @@ -24,18 +24,28 @@ func (c *LoggerConfig) MarshalZerologObject(e *zerolog.Event) { Str("format", c.Format) } -type SomethingConfig struct { +type SomethingClientConfig struct { Value int `mapstructure:"value"` } -func (c *SomethingConfig) MarshalZerologObject(event *zerolog.Event) { +func (c *SomethingClientConfig) MarshalZerologObject(event *zerolog.Event) { event. Int("value", c.Value) } +type BusinessLogicConfig struct { + Threshold int `mapstructure:"threshold"` +} + +func (c *BusinessLogicConfig) MarshalZerologObject(event *zerolog.Event) { + event. + Int("threshold", c.Threshold) +} + type MainConfig struct { - Something SomethingConfig `mapstructure:"something"` - Logger LoggerConfig `mapstructure:"logger"` + Something SomethingClientConfig `mapstructure:"something"` + BusinessLogic BusinessLogicConfig `mapstructure:"bl"` + Logger LoggerConfig `mapstructure:"logger"` configFile string } @@ -44,7 +54,8 @@ func (c *MainConfig) MarshalZerologObject(event *zerolog.Event) { event. Str("config-file", c.configFile). Object("logger", &c.Logger). - Object("something", &c.Something) + Object("something", &c.Something). + Object("bl", &c.BusinessLogic) } func addLoggerOptions() { @@ -55,11 +66,17 @@ func addLoggerOptions() { } func addSomethingOptions() { - pflag.Int("something-value", 123, "value used to do something") + pflag.Int("something-value", 123, "value of something") viper.BindPFlag("something.value", pflag.Lookup("something-value")) //nolint:errcheck viper.BindEnv("something.value", "SOMETHING_VALUE") //nolint:errcheck } +func addBLOptions() { + pflag.Int("bl-threshold", 100, "threshold for a business logic action") + viper.BindPFlag("bl.threshold", pflag.Lookup("bl-threshold")) //nolint:errcheck + viper.BindEnv("bl.threshold", "BUSINESS_THRESHOLD") //nolint:errcheck +} + func addConfigFromFile(configRelPath string) error { mode := os.Getenv("RUN_MODE") if mode == "" { @@ -118,6 +135,7 @@ func loadConfigFromFile() (MainConfig, error) { func GetMainConfig() (MainConfig, error) { addLoggerOptions() addSomethingOptions() + addBLOptions() pflag.Parse() viper.BindPFlags(pflag.CommandLine) //nolint:errcheck diff --git a/factory/factory.go b/factory/factory.go index 2acac39..ad3262e 100644 --- a/factory/factory.go +++ b/factory/factory.go @@ -6,13 +6,15 @@ import ( "github.com/rs/zerolog" + "www.thenautilus.net/cgit/go-example/business" "www.thenautilus.net/cgit/go-example/config" "www.thenautilus.net/cgit/go-example/logging" "www.thenautilus.net/cgit/go-example/something" ) type Factory struct { - something *something.Something + somethingClient *something.Something + businessLogic *business.Logic config *config.MainConfig logger zerolog.Logger @@ -43,13 +45,25 @@ func (f *Factory) Logger() zerolog.Logger { return f.logger } -func (f *Factory) Something() *something.Something { - if f.something == nil { +func (f *Factory) SomethingClient() *something.Something { + if f.somethingClient == nil { something := something.New( &f.config.Something, ) - f.something = &something + f.somethingClient = &something } - return f.something + return f.somethingClient +} + +func (f *Factory) BusinessLogic() *business.Logic { + if f.businessLogic == nil { + logic := business.New( + &f.config.BusinessLogic, + f.SomethingClient(), + ) + f.businessLogic = &logic + } + + return f.businessLogic } diff --git a/something/something.go b/something/something.go index 2516b2d..3412886 100644 --- a/something/something.go +++ b/something/something.go @@ -1,8 +1,6 @@ package something import ( - "fmt" - "www.thenautilus.net/cgit/go-example/config" ) @@ -10,14 +8,12 @@ type Something struct { value int } -func New(conf *config.SomethingConfig) Something { +func New(conf *config.SomethingClientConfig) Something { return Something{ value: conf.Value, } } -func (s *Something) DoSomething() error { - fmt.Printf("the value is %d", s.value) - - return nil +func (s *Something) FetchValue() (int, error) { + return s.value, nil } -- cgit v1.2.3