add definition config
This commit is contained in:
@@ -3,13 +3,18 @@ package toml
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/pelletier/go-toml"
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/key"
|
||||
"gitoa.ru/go-4devs/config/value"
|
||||
)
|
||||
|
||||
const (
|
||||
Name = "toml"
|
||||
Separator = "."
|
||||
)
|
||||
|
||||
var _ config.Provider = (*Provider)(nil)
|
||||
|
||||
func NewFile(file string, opts ...Option) (*Provider, error) {
|
||||
@@ -26,7 +31,9 @@ type Option func(*Provider)
|
||||
func configure(tree *toml.Tree, opts ...Option) *Provider {
|
||||
prov := &Provider{
|
||||
tree: tree,
|
||||
key: key.Name,
|
||||
key: func(s []string) string {
|
||||
return strings.Join(s, Separator)
|
||||
},
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
@@ -47,25 +54,18 @@ func New(data []byte, opts ...Option) (*Provider, error) {
|
||||
|
||||
type Provider struct {
|
||||
tree *toml.Tree
|
||||
key config.KeyFactory
|
||||
}
|
||||
|
||||
func (p *Provider) IsSupport(ctx context.Context, key config.Key) bool {
|
||||
return p.key(ctx, key) != ""
|
||||
key func([]string) string
|
||||
name string
|
||||
}
|
||||
|
||||
func (p *Provider) Name() string {
|
||||
return "toml"
|
||||
return p.name
|
||||
}
|
||||
|
||||
func (p *Provider) Read(ctx context.Context, key config.Key) (config.Variable, error) {
|
||||
if k := p.key(ctx, key); p.tree.Has(k) {
|
||||
return config.Variable{
|
||||
Name: k,
|
||||
Provider: p.Name(),
|
||||
Value: Value{Value: value.Value{Val: p.tree.Get(k)}},
|
||||
}, nil
|
||||
func (p *Provider) Value(ctx context.Context, path ...string) (config.Value, error) {
|
||||
if k := p.key(path); p.tree.Has(k) {
|
||||
return Value{Value: value.Value{Val: p.tree.Get(k)}}, nil
|
||||
}
|
||||
|
||||
return config.Variable{}, config.ErrVariableNotFound
|
||||
return nil, config.ErrValueNotFound
|
||||
}
|
||||
|
||||
@@ -17,12 +17,12 @@ func TestProvider(t *testing.T) {
|
||||
m := []int{}
|
||||
|
||||
read := []test.Read{
|
||||
test.NewRead("database.server", "192.168.1.1"),
|
||||
test.NewRead("title", "TOML Example"),
|
||||
test.NewRead("servers.alpha.ip", "10.0.0.1"),
|
||||
test.NewRead("database.enabled", true),
|
||||
test.NewRead("database.connection_max", 5000),
|
||||
test.NewReadUnmarshal("database.ports", &[]int{8001, 8001, 8002}, &m),
|
||||
test.NewRead("192.168.1.1", "database.server"),
|
||||
test.NewRead("TOML Example", "title"),
|
||||
test.NewRead("10.0.0.1", "servers.alpha.ip"),
|
||||
test.NewRead(true, "database.enabled"),
|
||||
test.NewRead(5000, "database.connection_max"),
|
||||
test.NewReadUnmarshal(&[]int{8001, 8001, 8002}, &m, "database", "ports"),
|
||||
}
|
||||
|
||||
test.Run(t, prov, read)
|
||||
|
||||
Reference in New Issue
Block a user