remove toml provider
This commit is contained in:
@@ -1,71 +0,0 @@
|
||||
package toml
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/pelletier/go-toml"
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/value"
|
||||
)
|
||||
|
||||
const (
|
||||
Name = "toml"
|
||||
Separator = "."
|
||||
)
|
||||
|
||||
var _ config.Provider = (*Provider)(nil)
|
||||
|
||||
func NewFile(file string, opts ...Option) (*Provider, error) {
|
||||
tree, err := toml.LoadFile(file)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("toml: failed load file: %w", err)
|
||||
}
|
||||
|
||||
return configure(tree, opts...), nil
|
||||
}
|
||||
|
||||
type Option func(*Provider)
|
||||
|
||||
func configure(tree *toml.Tree, opts ...Option) *Provider {
|
||||
prov := &Provider{
|
||||
tree: tree,
|
||||
key: func(s []string) string {
|
||||
return strings.Join(s, Separator)
|
||||
},
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(prov)
|
||||
}
|
||||
|
||||
return prov
|
||||
}
|
||||
|
||||
func New(data []byte, opts ...Option) (*Provider, error) {
|
||||
tree, err := toml.LoadBytes(data)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("toml failed load data: %w", err)
|
||||
}
|
||||
|
||||
return configure(tree, opts...), nil
|
||||
}
|
||||
|
||||
type Provider struct {
|
||||
tree *toml.Tree
|
||||
key func([]string) string
|
||||
name string
|
||||
}
|
||||
|
||||
func (p *Provider) Name() string {
|
||||
return p.name
|
||||
}
|
||||
|
||||
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 nil, config.ErrValueNotFound
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
package toml_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"gitoa.ru/go-4devs/config/provider/toml"
|
||||
"gitoa.ru/go-4devs/config/test"
|
||||
)
|
||||
|
||||
func TestProvider(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
prov, err := toml.NewFile(test.FixturePath("config.toml"))
|
||||
require.NoError(t, err)
|
||||
|
||||
m := []int{}
|
||||
|
||||
read := []test.Read{
|
||||
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)
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
package toml
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/value"
|
||||
)
|
||||
|
||||
type Value struct {
|
||||
value.Value
|
||||
}
|
||||
|
||||
func (s Value) Int() int {
|
||||
v, _ := s.ParseInt()
|
||||
|
||||
return v
|
||||
}
|
||||
|
||||
func (s Value) ParseInt() (int, error) {
|
||||
v, err := s.ParseInt64()
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("toml failed parce int: %w", err)
|
||||
}
|
||||
|
||||
return int(v), nil
|
||||
}
|
||||
|
||||
func (s Value) Unmarshal(target interface{}) error {
|
||||
b, err := json.Marshal(s.Raw())
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: %w", config.ErrInvalidValue, err)
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(b, target); err != nil {
|
||||
return fmt.Errorf("%w: %w", config.ErrInvalidValue, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user