add definition config
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone/pr Build is failing

This commit is contained in:
andrey
2024-01-25 18:17:27 +03:00
parent 303433a336
commit 3945d61f17
52 changed files with 1382 additions and 377 deletions

View File

@@ -14,10 +14,10 @@ var (
_ config.WatchProvider = (*Provider)(nil)
)
func New(duration time.Duration, provider config.Provider, opts ...Option) *Provider {
func New(duration time.Duration, provider config.NamedProvider, opts ...Option) *Provider {
prov := &Provider{
Provider: provider,
ticker: time.NewTicker(duration),
NamedProvider: provider,
ticker: time.NewTicker(duration),
logger: func(_ context.Context, msg string) {
log.Print(msg)
},
@@ -39,13 +39,13 @@ func WithLogger(l func(context.Context, string)) Option {
type Option func(*Provider)
type Provider struct {
config.Provider
config.NamedProvider
ticker *time.Ticker
logger func(context.Context, string)
}
func (p *Provider) Watch(ctx context.Context, key config.Key, callback config.WatchCallback) error {
oldVar, err := p.Provider.Read(ctx, key)
func (p *Provider) Watch(ctx context.Context, callback config.WatchCallback, key ...string) error {
oldVar, err := p.NamedProvider.Value(ctx, key...)
if err != nil {
return fmt.Errorf("failed watch variable: %w", err)
}
@@ -54,7 +54,7 @@ func (p *Provider) Watch(ctx context.Context, key config.Key, callback config.Wa
for {
select {
case <-p.ticker.C:
newVar, err := p.Provider.Read(ctx, key)
newVar, err := p.NamedProvider.Value(ctx, key...)
if err != nil {
p.logger(ctx, err.Error())
} else if !newVar.IsEquals(oldVar) {

View File

@@ -22,14 +22,10 @@ func (p *provider) Name() string {
return "test"
}
func (p *provider) Read(context.Context, config.Key) (config.Variable, error) {
func (p *provider) Value(context.Context, ...string) (config.Value, error) {
p.cnt++
return config.Variable{
Name: "tmpname",
Provider: p.Name(),
Value: value.JString(fmt.Sprint(p.cnt)),
}, nil
return value.JString(fmt.Sprint(p.cnt)), nil
}
func TestWatcher(t *testing.T) {
@@ -46,11 +42,11 @@ func TestWatcher(t *testing.T) {
err := w.Watch(
ctx,
config.Key{Name: "tmpname"},
func(ctx context.Context, oldVar, newVar config.Variable) {
func(ctx context.Context, oldVar, newVar config.Value) {
atomic.AddInt32(&cnt, 1)
wg.Done()
},
"tmpname",
)
require.NoError(t, err)
wg.Wait()