feat: add helper and update viper

This commit is contained in:
Kevin Franklin Kim 2022-06-11 13:16:11 +02:00
parent 9045fbd6e5
commit 903dde7ebc
4 changed files with 171 additions and 28 deletions

View File

@ -10,6 +10,7 @@ func WithRemoteConfig(c *viper.Viper, provider, endpoint string, path string) er
if err := c.AddRemoteProvider(provider, endpoint, path); err != nil {
return err
}
if err := c.ReadRemoteConfig(); err != nil {
return errors.Wrap(err, "failed to read remote config")
}

View File

@ -5,36 +5,179 @@ import (
"time"
)
// Watch callback
func WatchBool(ctx context.Context, fn func() bool, callback func(bool)) {
go func(ctx context.Context, fn func() bool, callback func(bool)) {
current := fn()
for {
select {
case <-time.After(time.Second):
if value := fn(); value != current {
current = value
callback(current)
}
case <-ctx.Done():
return
}
current := fn()
watch(ctx, func() {
if value := fn(); value != current {
current = value
callback(current)
}
}(ctx, fn, callback)
})
}
func WatchTime(ctx context.Context, fn func() time.Time, callback func(time.Time)) {
current := fn()
watch(ctx, func() {
if value := fn(); value != current {
current = value
callback(current)
}
})
}
func WatchDuration(ctx context.Context, fn func() time.Duration, callback func(time.Duration)) {
current := fn()
watch(ctx, func() {
if value := fn(); value != current {
current = value
callback(current)
}
})
}
func WatchInt(ctx context.Context, fn func() int, callback func(int)) {
current := fn()
watch(ctx, func() {
if value := fn(); value != current {
current = value
callback(current)
}
})
}
func WatchInt32(ctx context.Context, fn func() int32, callback func(int32)) {
current := fn()
watch(ctx, func() {
if value := fn(); value != current {
current = value
callback(current)
}
})
}
func WatchInt64(ctx context.Context, fn func() int64, callback func(int64)) {
current := fn()
watch(ctx, func() {
if value := fn(); value != current {
current = value
callback(current)
}
})
}
func WatchFloat64(ctx context.Context, fn func() float64, callback func(float64)) {
current := fn()
watch(ctx, func() {
if value := fn(); value != current {
current = value
callback(current)
}
})
}
func WatchString(ctx context.Context, fn func() string, callback func(string)) {
go func(ctx context.Context, fn func() string, callback func(string)) {
current := fn()
current := fn()
watch(ctx, func() {
if value := fn(); value != current {
current = value
callback(current)
}
})
}
// Watch channel
func WatchBoolChan(ctx context.Context, fn func() bool, ch chan bool) {
current := fn()
watch(ctx, func() {
if value := fn(); value != current {
current = value
ch <- current
}
})
}
func WatchTimeChan(ctx context.Context, fn func() time.Time, ch chan time.Time) {
current := fn()
watch(ctx, func() {
if value := fn(); value != current {
current = value
ch <- current
}
})
}
func WatchDurationChan(ctx context.Context, fn func() time.Duration, ch chan time.Duration) {
current := fn()
watch(ctx, func() {
if value := fn(); value != current {
current = value
ch <- current
}
})
}
func WatchIntChan(ctx context.Context, fn func() int, ch chan int) {
current := fn()
watch(ctx, func() {
if value := fn(); value != current {
current = value
ch <- current
}
})
}
func WatchInt32Chan(ctx context.Context, fn func() int32, ch chan int32) {
current := fn()
watch(ctx, func() {
if value := fn(); value != current {
current = value
ch <- current
}
})
}
func WatchInt64Chan(ctx context.Context, fn func() int64, ch chan int64) {
current := fn()
watch(ctx, func() {
if value := fn(); value != current {
current = value
ch <- current
}
})
}
func WatchFloat64Chan(ctx context.Context, fn func() float64, ch chan float64) {
current := fn()
watch(ctx, func() {
if value := fn(); value != current {
current = value
ch <- current
}
})
}
func WatchStringChan(ctx context.Context, fn func() string, ch chan string) {
current := fn()
watch(ctx, func() {
if value := fn(); value != current {
current = value
ch <- current
}
})
}
func watch(ctx context.Context, fn func()) {
go func(ctx context.Context, fn func()) {
for {
select {
case <-time.After(time.Second):
if value := fn(); value != current {
current = value
callback(current)
}
fn()
case <-ctx.Done():
return
}
}
}(ctx, fn, callback)
}(ctx, fn)
}

2
go.mod
View File

@ -40,6 +40,6 @@ require (
)
replace (
github.com/spf13/viper v1.12.0 => github.com/franklinkim/viper v1.12.1-0.20220601073657-b47725e72d4c
github.com/spf13/viper v1.12.0 => github.com/franklinkim/viper v1.12.1-0.20220611111410-2d69ce7c2fe8
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.32.0 => github.com/foomo/opentelemetry-go-contrib/instrumentation/net/http/otelhttp v0.32.1-0.20220517120905-10e2553b9bac
)

13
go.sum
View File

@ -144,8 +144,8 @@ github.com/foomo/opentelemetry-go-contrib/instrumentation/net/http/otelhttp v0.3
github.com/foomo/opentelemetry-go-contrib/instrumentation/net/http/otelhttp v0.32.1-0.20220517120905-10e2553b9bac/go.mod h1:5eCOqeGphOyz6TsY3ZDNjE33SM/TFAK3RGuCL2naTgY=
github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE=
github.com/frankban/quicktest v1.14.3/go.mod h1:mgiwOwqx65TmIk1wJ6Q7wvnVMocbUorkibMOrVTHZps=
github.com/franklinkim/viper v1.12.1-0.20220601073657-b47725e72d4c h1:on8xQzCalZbBCh/MkHgmPkIFg/ohHCZ9/9YpA1DG0OU=
github.com/franklinkim/viper v1.12.1-0.20220601073657-b47725e72d4c/go.mod h1:b6COn30jlNxbm/V2IqWiNWkJ+vZNiMNksliPCiuKtSI=
github.com/franklinkim/viper v1.12.1-0.20220611111410-2d69ce7c2fe8 h1:N9JhVRMc8z++6a8UuA7OB5P60V9cmwfeiow1ohkSRgA=
github.com/franklinkim/viper v1.12.1-0.20220611111410-2d69ce7c2fe8/go.mod h1:VE2mylSsEG0PLvujqWRd4qEnbfdctwRfURTHOnx2blk=
github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwVZI=
github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
@ -478,8 +478,8 @@ github.com/pborman/uuid v1.2.1 h1:+ZZIw58t/ozdjRaXh/3awHfmWRbzYxJoAdNJxe/3pvw=
github.com/pborman/uuid v1.2.1/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pelletier/go-toml/v2 v2.0.1 h1:8e3L2cCQzLFi2CR4g7vGFuFxX7Jl1kKX8gW+iV0GUKU=
github.com/pelletier/go-toml/v2 v2.0.1/go.mod h1:r9LEWfGN8R5k0VXJ+0BkIe7MYkRdwZOjgMj2KwnJFUo=
github.com/pelletier/go-toml/v2 v2.0.2 h1:+jQXlF3scKIcSEKkdHzXhCTDLPFi5r1wnK6yPS+49Gw=
github.com/pelletier/go-toml/v2 v2.0.2/go.mod h1:MovirKjgVRESsAvNZlAjtFwV867yGuwRkXbG66OzopI=
github.com/philhofer/fwd v1.1.1 h1:GdGcTjf5RNAxwS4QLsiMzJYj5KEvPJD3Abr261yRQXQ=
github.com/philhofer/fwd v1.1.1/go.mod h1:gk3iGcWd9+svBvR0sR+KPcfE+RNWozjowpeBVG3ZVNU=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
@ -567,8 +567,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/subosito/gotenv v1.3.0 h1:mjC+YW8QpAdXibNi+vNWgzmgBH4+5l5dCXv8cNysBLI=
github.com/subosito/gotenv v1.3.0/go.mod h1:YzJjq/33h7nrwdY+iHMhEOEEbW0ovIz0tB6t6PwAXzs=
github.com/subosito/gotenv v1.4.0 h1:yAzM1+SmVcz5R4tXGsNMu1jUl2aOJXoiWUCEwwnGrvs=
github.com/subosito/gotenv v1.4.0/go.mod h1:mZd6rFysKEcUhUHXJk0C/08wAgyDBFuwEYL7vWWGaGo=
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tinylib/msgp v1.1.6 h1:i+SbKraHhnrf9M5MYmvQhFnbLhAXSDWF8WWsuyRdocw=
@ -1208,7 +1208,6 @@ gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0/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=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=