Skip to content

Commit

Permalink
feat: add viper provider for xload (#32)
Browse files Browse the repository at this point in the history
* feat: add viper provider for xload

* introduce ValueMapper to give more control in public API

* add examples
  • Loading branch information
ajatprabha authored Jan 11, 2024
1 parent 6f7f235 commit 26d32ad
Show file tree
Hide file tree
Showing 8 changed files with 526 additions and 0 deletions.
2 changes: 2 additions & 0 deletions xload/providers/viper/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package viper provides a xload.Loader implementation that uses github.com/spf13/viper.
package viper
91 changes: 91 additions & 0 deletions xload/providers/viper/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package viper_test

import (
"context"
"fmt"

vpr "github.com/spf13/viper"

"github.com/gojekfarm/xtools/xload"
"github.com/gojekfarm/xtools/xload/providers/viper"
)

type Config struct {
Log LogConfig `env:",prefix=LOG_"`
}

type LogConfig struct {
Level string `env:"LEVEL"`
}

func ExampleNew() {
v := vpr.New()
v.Set("LOG_LEVEL", "debug")

vl, err := viper.New(viper.From(v))
if err != nil {
panic(err)
}

cfg := &Config{}
if err := xload.Load(context.Background(), cfg, xload.WithLoader(vl)); err != nil {
panic(err)
}

fmt.Println(cfg.Log.Level)

// Output:
// debug
}

func ExampleFrom() {
_, err := viper.New(viper.From(vpr.New()))
if err != nil {
panic(err)
}
}

func ExampleLoader_ConfigFileUsed() {
_, err := viper.New(viper.ConfigFile("<path-to-config-file>.ext"))
if err != nil {
panic(err)
}

fmt.Println(vpr.ConfigFileUsed()) // <path-to-config-file>.ext
}

func ExampleNew_fileOptions() {
_, err := viper.New(
viper.ConfigName("config"),
viper.ConfigType("toml"),
viper.ConfigPaths([]string{"./", "/etc/<program/"}),
)
if err != nil {
panic(err)
}
}

func ExampleAutoEnv_disable() {
_, err := viper.New(viper.AutoEnv(false))
if err != nil {
panic(err)
}
}

func ExampleValueMapper() {
_, err := viper.New(viper.ValueMapper(func(m map[string]any) map[string]string {
return xload.FlattenMap(m, "__")
}))
if err != nil {
panic(err)
}
}

func ExampleTransformer() {
_, err := viper.New(viper.Transformer(func(v *vpr.Viper, next xload.Loader) xload.Loader {
return xload.PrefixLoader("ENV_", next)
}))
if err != nil {
panic(err)
}
}
35 changes: 35 additions & 0 deletions xload/providers/viper/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module github.com/gojekfarm/xtools/xload/providers/viper

go 1.21.3

replace github.com/gojekfarm/xtools/xload => ../..

require (
github.com/gojekfarm/xtools/xload v0.5.0
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.8.4
)

require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
71 changes: 71 additions & 0 deletions xload/providers/viper/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/gotidy/ptr v1.4.0 h1:7++suUs+HNHMnyz6/AW3SE+4EnBhupPSQTSI7QNijVc=
github.com/gotidy/ptr v1.4.0/go.mod h1:MjRBG6/IETiiZGWI8LrRtISXEji+8b/jigmj2q0mEyM=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ=
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY=
github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0=
github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.18.2 h1:LUXCnvUvSM6FXAsj6nnfc8Q2tp1dIgUfY9Kc8GsSOiQ=
github.com/spf13/viper v1.18.2/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMVB+yk=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
101 changes: 101 additions & 0 deletions xload/providers/viper/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package viper

import (
"context"
"strings"

"github.com/spf13/viper"

"github.com/gojekfarm/xtools/xload"
)

// Option allows configuring the Loader.
type Option interface{ apply(*options) }

type optionFunc func(*options)

func (f optionFunc) apply(o *options) { f(o) }

// From allows passing a pre-configured Viper instance.
func From(v *viper.Viper) Option { return optionFunc(func(o *options) { o.viper = v }) }

// ValueMapper allows specifying a custom value mapper function that will be used to flatten the config
// for xload.Loader from Viper.
type ValueMapper func(map[string]any) map[string]string

func (m ValueMapper) apply(o *options) { o.mapValues = m }

// ConfigFile allows specifying the config file to be used.
type ConfigFile string

func (p ConfigFile) apply(o *options) { o.file.absPath = string(p) }

// ConfigName allows specifying the config file name.
type ConfigName string

func (n ConfigName) apply(o *options) { o.file.name = string(n) }

// ConfigType allows specifying the config file type.
type ConfigType string

func (t ConfigType) apply(o *options) { o.file.ext = string(t) }

// ConfigPaths allows specifying the config file search paths.
type ConfigPaths []string

// AutoEnv allows enabling/disabling automatic environment variable loading.
type AutoEnv bool

func (b AutoEnv) apply(o *options) { o.autoEnv = bool(b) }

func (p ConfigPaths) apply(o *options) {
for _, path := range p {
o.file.paths = append(o.file.paths, path)
}
}

// Transformer allows specifying a custom transformer function.
type Transformer func(v *viper.Viper, next xload.Loader) xload.Loader

func (t Transformer) apply(o *options) { o.transform = t }

type options struct {
viper *viper.Viper
file fileOpts
transform Transformer
mapValues ValueMapper
autoEnv bool
}

type fileOpts struct {
absPath string
name string
ext string
paths []string
}

func def() *options {
return &options{
viper: viper.New(),
file: fileOpts{
name: "application",
ext: "yaml",
paths: []string{"./", "../"},
},
autoEnv: true,
mapValues: func(in map[string]any) map[string]string {
out := make(map[string]string)

for key, value := range xload.FlattenMap(in, "_") {
out[key] = value
}

return out
},
transform: func(_ *viper.Viper, next xload.Loader) xload.Loader {
return xload.LoaderFunc(func(ctx context.Context, key string) (string, error) {
return next.Load(ctx, strings.ToLower(key))
})
},
}
}
81 changes: 81 additions & 0 deletions xload/providers/viper/options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package viper

import (
"fmt"
"testing"

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"

"github.com/gojekfarm/xtools/xload"
)

func TestOptions(t *testing.T) {
testcases := []struct {
name string
opts []Option
want *options
}{
{
name: "ConfigFile",
opts: []Option{ConfigFile("/tmp/config.yaml")},
want: &options{file: fileOpts{absPath: "/tmp/config.yaml"}},
},
{
name: "ConfigName",
opts: []Option{ConfigName("config")},
want: &options{file: fileOpts{name: "config"}},
},
{
name: "ConfigType",
opts: []Option{ConfigType("json")},
want: &options{file: fileOpts{ext: "json"}},
},
{
name: "ConfigPaths",
opts: []Option{ConfigPaths{"/tmp", "/tmp/config"}},
want: &options{file: fileOpts{paths: []string{"/tmp", "/tmp/config"}}},
},
{
name: "AutoEnv",
opts: []Option{AutoEnv(true)},
want: &options{autoEnv: true},
},
}

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
opts := &options{}
for _, o := range tc.opts {
o.apply(opts)
}

assert.Equal(t, tc.want, opts)
})
}

t.Run("Transformer", func(t *testing.T) {
opts := &options{}
f := Transformer(func(_ *viper.Viper, _ xload.Loader) xload.Loader { return nil })
f.apply(opts)
assert.NotNil(t, opts.transform)
assert.True(t, fmt.Sprintf("%p", f) == fmt.Sprintf("%p", opts.transform))
})

t.Run("ValueMapper", func(t *testing.T) {
opts := &options{}
f := ValueMapper(func(_ map[string]any) map[string]string { return nil })
f.apply(opts)
assert.NotNil(t, opts.mapValues)
assert.True(t, fmt.Sprintf("%p", f) == fmt.Sprintf("%p", opts.mapValues))
})
}

func Test_def(t *testing.T) {
opts := def()

assert.Equal(t, fileOpts{name: "application", ext: "yaml", paths: []string{"./", "../"}}, opts.file)
assert.NotNil(t, opts.viper)
assert.NotNil(t, opts.mapValues)
assert.NotNil(t, opts.transform)
}
Loading

0 comments on commit 26d32ad

Please sign in to comment.