|
| 1 | +// Package koanfcontrib provides a koanf provider backed by an OpenDecree |
| 2 | +// configclient. It implements the koanf.Provider interface so that decree |
| 3 | +// configuration values can be loaded directly into a koanf instance. |
| 4 | +// |
| 5 | +// Usage: |
| 6 | +// |
| 7 | +// provider := koanfcontrib.New(client, "my-tenant") |
| 8 | +// k := koanf.New(".") |
| 9 | +// if err := k.Load(provider, nil); err != nil { |
| 10 | +// log.Fatal(err) |
| 11 | +// } |
| 12 | +package koanfcontrib |
| 13 | + |
| 14 | +import ( |
| 15 | + "context" |
| 16 | + "fmt" |
| 17 | + "time" |
| 18 | + |
| 19 | + "github.com/knadh/koanf/v2" |
| 20 | + "github.com/opendecree/decree/sdk/configclient" |
| 21 | +) |
| 22 | + |
| 23 | +// Provider is a koanf provider backed by a decree configclient. |
| 24 | +// It fetches all configuration values for a tenant via [configclient.Client.GetAll] |
| 25 | +// and exposes them as a flat map of field-path keys. |
| 26 | +type Provider struct { |
| 27 | + client *configclient.Client |
| 28 | + tenantID string |
| 29 | + timeout time.Duration |
| 30 | +} |
| 31 | + |
| 32 | +// New creates a Provider for the given tenant. |
| 33 | +// opts can be used to override defaults such as the per-call timeout. |
| 34 | +func New(client *configclient.Client, tenantID string, opts ...Option) *Provider { |
| 35 | + p := &Provider{client: client, tenantID: tenantID, timeout: 5 * time.Second} |
| 36 | + for _, o := range opts { |
| 37 | + o(p) |
| 38 | + } |
| 39 | + return p |
| 40 | +} |
| 41 | + |
| 42 | +// Option configures a Provider. |
| 43 | +type Option func(*Provider) |
| 44 | + |
| 45 | +// WithTimeout sets the per-call context timeout used by Read. |
| 46 | +// The default timeout is 5 seconds. |
| 47 | +func WithTimeout(d time.Duration) Option { |
| 48 | + return func(p *Provider) { p.timeout = d } |
| 49 | +} |
| 50 | + |
| 51 | +// Read fetches all configuration values for the tenant from OpenDecree and |
| 52 | +// returns them as a flat map[string]interface{} keyed by field path. |
| 53 | +// Implements koanf.Provider. |
| 54 | +func (p *Provider) Read() (map[string]interface{}, error) { |
| 55 | + ctx, cancel := context.WithTimeout(context.Background(), p.timeout) |
| 56 | + defer cancel() |
| 57 | + all, err := p.client.GetAll(ctx, p.tenantID) |
| 58 | + if err != nil { |
| 59 | + return nil, err |
| 60 | + } |
| 61 | + m := make(map[string]interface{}, len(all)) |
| 62 | + for k, v := range all { |
| 63 | + m[k] = v |
| 64 | + } |
| 65 | + return m, nil |
| 66 | +} |
| 67 | + |
| 68 | +// ReadBytes is not supported by this provider. Use Read instead (pass nil as |
| 69 | +// the Parser argument to koanf.Load). |
| 70 | +// Implements koanf.Provider. |
| 71 | +func (p *Provider) ReadBytes() ([]byte, error) { |
| 72 | + return nil, fmt.Errorf("koanfcontrib: ReadBytes not supported; use Read with a nil parser") |
| 73 | +} |
| 74 | + |
| 75 | +// Watch polls for configuration changes at a 30-second interval by invoking |
| 76 | +// the callback, which causes koanf to re-load the provider. |
| 77 | +// The callback receives a nil event and nil error on each tick. |
| 78 | +// |
| 79 | +// Watch launches a background goroutine and returns immediately. |
| 80 | +// This is a convenience method — it is not part of the koanf.Provider interface. |
| 81 | +func (p *Provider) Watch(cb func(event interface{}, err error)) error { |
| 82 | + go func() { |
| 83 | + ticker := time.NewTicker(30 * time.Second) |
| 84 | + defer ticker.Stop() |
| 85 | + for range ticker.C { |
| 86 | + cb(nil, nil) |
| 87 | + } |
| 88 | + }() |
| 89 | + return nil |
| 90 | +} |
| 91 | + |
| 92 | +// Ensure *Provider implements koanf.Provider at compile time. |
| 93 | +var _ koanf.Provider = (*Provider)(nil) |
0 commit comments