Skip to content

Commit

Permalink
feat: cleans up the interface and readme (#18)
Browse files Browse the repository at this point in the history
Signed-off-by: Tronje Krop <[email protected]>
  • Loading branch information
Tronje Krop authored Nov 8, 2024
1 parent 4bcb980 commit 7531ccc
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 19 deletions.
26 changes: 17 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ allows creating multiple configs while applying the setup mechanisms for
defaults using the following convenience functions:

```go
reader := config.New("<prefix>", "<app-name>", &Config{}).
reader := config.New[config.Config]("<prefix>", "<app-name>").
SetDefaults(func(c *config.ConfigReader[config.Config]{
c.SetDefault("int", 32)
}).ReadConfig("main")
Expand Down Expand Up @@ -116,17 +116,20 @@ still possible to customize the reader arbitrarily, e.g. with flag support, and
setup any other config structure by using the original [Viper][viper] interface
functions.

A special feature provided by [`go-config`][go-config] is to set up the
defaults using a partial or complete config prototype. While you must provide a
complete prototype in the `New` constructor, you can provide any sub-prototype
in the `SetSubDefaults` method as follows:
A special feature provided by [`go-config`][go-config] is to set up defaults
using a partial or complete config prototype. While in the `New` constructor
automatically an empty prototype is constructed and parsed for `default`-tags,
you can use th `SetDefaultConfig` method to provide any pre-filled (sub-)config
to updated and extend default values.

```go
reader := config.New("<prefix>", "<app-name>", &config.Config{
reader := config.New("<prefix>", "<app-name>")).
SetDefaultConfig("", &config.Config{
Env: "prod",
}).SetSubDefaults("<sub-path>", &log.Config{
Level: "debug",
}, false).
SetDefaultConfig("log", &log.Config{
Level: "debug",
}, false)
```


Expand All @@ -137,11 +140,16 @@ The [`go-config`][go-config] framework supports to set up a `Logger` in
out-of-the-box as follows:

```go
logger := config.Log.Setup[Rus|Zero](log.New(...)|nil)
logger := config.Log.Setup[Rus|Zero](writer[, logger])
```

If no logger is provided, the standard logger is configured and returned.

**Note:** While the config supports [zerolog][zerolog], there is currently no
real benefit of using it aside of its having a modern interface. Performance
wise, the necessary transformations for pretty printing logs are a heavy burden
that likely eats up all performance advantages compared to [logrus][logrus].

[zerolog]: <https://github.com/ra/zerolog>
[logrus]: <https://github.com/sirupsen/logrus>

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.10
0.0.11
14 changes: 9 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func New[C any](
r.SetConfigName(GetEnvName(prefix, name))
r.SetConfigType("yaml")
r.AddConfigPath(".")
r.SetSubDefaults("", new(C), true)
r.SetDefaultConfig("", new(C), true)
r.SetDefaults(setup...)

return r
Expand All @@ -89,12 +89,16 @@ func (r *Reader[C]) SetDefaults(
return r
}

// SetSubDefaults is a convenience method to update the default values of a
// sub-section configured in the reader by using the given config struct. The
// config struct is scanned for `default`-tags and values to set the defaults.
// SetDefaultConfig is a convenience method to update the default values of
// config in the reader by using the given config struct. The config struct is
// scanned for `default`-tags and non-zero values to set the defaults using the
// given key as prefix for constructing the config key-value pairs. This way
// the default config of a whole config struct as well as any sub-config can be
// updated.
//
// Depending on the `zero` flag the default values are either include setting
// zero values or ignoring them.
func (r *Reader[C]) SetSubDefaults(
func (r *Reader[C]) SetDefaultConfig(
key string, config any, zero bool,
) *Reader[C] {
info := info.GetDefault()
Expand Down
4 changes: 2 additions & 2 deletions log/logrus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestSetupRus(t *testing.T) {
// Given
logger := logrus.New()
config := config.New[config.Config]("TEST", "test").
SetSubDefaults("log", param.config, false).
SetDefaultConfig("log", param.config, false).
GetConfig(t.Name())

// When
Expand Down Expand Up @@ -503,7 +503,7 @@ func TestPrettyLogRus(t *testing.T) {
Run(func(t test.Test, param testPrettyLogRusParam) {
// Given
config := config.New[config.Config]("X", "app").
SetSubDefaults("log", param.config, true).
SetDefaultConfig("log", param.config, true).
SetDefaults(func(r *config.Reader[config.Config]) {
r.SetDefault("log.level", "trace")
}).GetConfig("logrus")
Expand Down
4 changes: 2 additions & 2 deletions log/zerolog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestSetupZero(t *testing.T) {
Run(func(t test.Test, param setupParams) {
// Given
config := config.New[config.Config]("TEST", "test").
SetSubDefaults("log", param.config, false).
SetDefaultConfig("log", param.config, false).
GetConfig(t.Name())

// When
Expand Down Expand Up @@ -477,7 +477,7 @@ func TestZeroLog(t *testing.T) {
// Given
buffer := &bytes.Buffer{}
config := config.New[config.Config]("X", "app").
SetSubDefaults("log", param.config, true).
SetDefaultConfig("log", param.config, true).
SetDefaults(func(r *config.Reader[config.Config]) {
r.SetDefault("log.level", "trace")
}).GetConfig("zerolog")
Expand Down

0 comments on commit 7531ccc

Please sign in to comment.