add configure provider by preview providers

This commit is contained in:
2023-06-03 14:31:12 +03:00
parent 3ba6b4c715
commit 83c5ab5959
10 changed files with 259 additions and 99 deletions

View File

@@ -47,7 +47,7 @@ type Provider struct {
func (p *Provider) Watch(ctx context.Context, key config.Key, callback config.WatchCallback) error {
oldVar, err := p.Provider.Read(ctx, key)
if err != nil {
return fmt.Errorf("%s: failed watch variable: %w", p.Provider.Name(), err)
return fmt.Errorf("failed watch variable: %w", err)
}
go func() {

View File

@@ -1,57 +0,0 @@
package yaml
import (
"context"
"fmt"
"io/ioutil"
"gitoa.ru/go-4devs/config"
"gopkg.in/yaml.v3"
)
func WithFileKeyFactory(f func(context.Context, config.Key) []string) FileOption {
return func(p *File) {
p.key = f
}
}
type FileOption func(*File)
func NewFile(name string, opts ...FileOption) *File {
f := File{
file: name,
key: keyFactory,
}
for _, opt := range opts {
opt(&f)
}
return &f
}
type File struct {
file string
key func(context.Context, config.Key) []string
}
func (p *File) Name() string {
return "yaml_file"
}
func (p *File) Read(ctx context.Context, key config.Key) (config.Variable, error) {
in, err := ioutil.ReadFile(p.file)
if err != nil {
return config.Variable{}, fmt.Errorf("yaml_file: read error: %w", err)
}
var n yaml.Node
if err = yaml.Unmarshal(in, &n); err != nil {
return config.Variable{}, fmt.Errorf("yaml_file: unmarshal error: %w", err)
}
data := node{Node: &n}
k := p.key(ctx, key)
return data.read(p.Name(), k)
}

View File

@@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"strings"
"gitoa.ru/go-4devs/config"
@@ -17,22 +18,34 @@ func keyFactory(ctx context.Context, key config.Key) []string {
return strings.Split(key.Name, "/")
}
func NewFile(name string, opts ...Option) (*Provider, error) {
in, err := ioutil.ReadFile(name)
if err != nil {
return nil, fmt.Errorf("yaml_file: read error: %w", err)
}
return New(in, opts...)
}
func New(yml []byte, opts ...Option) (*Provider, error) {
var data yaml.Node
if err := yaml.Unmarshal(yml, &data); err != nil {
return nil, fmt.Errorf("yaml: unmarshal err: %w", err)
}
return create(opts...).With(&data), nil
}
func create(opts ...Option) *Provider {
p := Provider{
key: keyFactory,
data: node{Node: &data},
key: keyFactory,
}
for _, opt := range opts {
opt(&p)
}
return &p, nil
return &p
}
type Option func(*Provider)
@@ -52,6 +65,13 @@ func (p *Provider) Read(ctx context.Context, key config.Key) (config.Variable, e
return p.data.read(p.Name(), k)
}
func (p *Provider) With(data *yaml.Node) *Provider {
return &Provider{
key: p.key,
data: node{Node: data},
}
}
type node struct {
*yaml.Node
}

42
provider/yaml/watch.go Normal file
View File

@@ -0,0 +1,42 @@
package yaml
import (
"context"
"fmt"
"io/ioutil"
"gitoa.ru/go-4devs/config"
"gopkg.in/yaml.v3"
)
func NewWatch(name string, opts ...Option) *Watch {
f := Watch{
file: name,
prov: create(opts...),
}
return &f
}
type Watch struct {
file string
prov *Provider
}
func (p *Watch) Name() string {
return "yaml_watch"
}
func (p *Watch) Read(ctx context.Context, key config.Key) (config.Variable, error) {
in, err := ioutil.ReadFile(p.file)
if err != nil {
return config.Variable{}, fmt.Errorf("yaml_file: read error: %w", err)
}
var n yaml.Node
if err = yaml.Unmarshal(in, &n); err != nil {
return config.Variable{}, fmt.Errorf("yaml_file: unmarshal error: %w", err)
}
return p.prov.With(&n).Read(ctx, key)
}