This commit is contained in:
57
provider/yaml/file.go
Normal file
57
provider/yaml/file.go
Normal file
@@ -0,0 +1,57 @@
|
||||
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)
|
||||
}
|
||||
88
provider/yaml/provider.go
Normal file
88
provider/yaml/provider.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package yaml
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/value"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
var _ config.Provider = (*Provider)(nil)
|
||||
|
||||
func keyFactory(ctx context.Context, key config.Key) []string {
|
||||
return strings.Split(key.Name, "/")
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
p := Provider{
|
||||
key: keyFactory,
|
||||
data: node{Node: &data},
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(&p)
|
||||
}
|
||||
|
||||
return &p, nil
|
||||
}
|
||||
|
||||
type Option func(*Provider)
|
||||
|
||||
type Provider struct {
|
||||
data node
|
||||
key func(context.Context, config.Key) []string
|
||||
}
|
||||
|
||||
func (p *Provider) Name() string {
|
||||
return "yaml"
|
||||
}
|
||||
|
||||
func (p *Provider) Read(ctx context.Context, key config.Key) (config.Variable, error) {
|
||||
k := p.key(ctx, key)
|
||||
|
||||
return p.data.read(p.Name(), k)
|
||||
}
|
||||
|
||||
type node struct {
|
||||
*yaml.Node
|
||||
}
|
||||
|
||||
func (n *node) read(name string, k []string) (config.Variable, error) {
|
||||
val, err := getData(n.Node.Content[0].Content, k)
|
||||
if err != nil {
|
||||
if errors.Is(err, config.ErrVariableNotFound) {
|
||||
return config.Variable{}, fmt.Errorf("%w: %s", config.ErrVariableNotFound, name)
|
||||
}
|
||||
|
||||
return config.Variable{}, fmt.Errorf("%w: %s", err, name)
|
||||
}
|
||||
|
||||
return config.Variable{
|
||||
Name: strings.Join(k, "."),
|
||||
Provider: name,
|
||||
Value: value.Decode(val),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func getData(node []*yaml.Node, keys []string) (func(interface{}) error, error) {
|
||||
for i := len(node) - 1; i > 0; i -= 2 {
|
||||
if node[i-1].Value == keys[0] {
|
||||
if len(keys) > 1 {
|
||||
return getData(node[i].Content, keys[1:])
|
||||
}
|
||||
|
||||
return node[i].Decode, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, config.ErrVariableNotFound
|
||||
}
|
||||
26
provider/yaml/provider_test.go
Normal file
26
provider/yaml/provider_test.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package yaml_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
provider "gitoa.ru/go-4devs/config/provider/yaml"
|
||||
"gitoa.ru/go-4devs/config/test"
|
||||
)
|
||||
|
||||
func TestProvider(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
prov, err := provider.New(test.ReadFile("config.yaml"))
|
||||
require.Nil(t, err)
|
||||
|
||||
read := []test.Read{
|
||||
test.NewRead("duration_var", 21*time.Minute),
|
||||
test.NewRead("app/name/bool_var", true),
|
||||
test.NewRead("time_var", test.Time("2020-01-02T15:04:05Z")),
|
||||
test.NewReadConfig("cfg"),
|
||||
}
|
||||
|
||||
test.Run(t, prov, read)
|
||||
}
|
||||
Reference in New Issue
Block a user