From d5b5103f40176a2e63c38f5da1b6732a51c7c1fa Mon Sep 17 00:00:00 2001 From: andrey Date: Thu, 25 Jan 2024 19:43:56 +0300 Subject: [PATCH] remove yaml provider --- go.mod | 2 +- provider/arg/provider.go | 15 +++-- provider/arg/provider_test.go | 22 ++++++- provider/yaml/provider.go | 101 --------------------------------- provider/yaml/provider_test.go | 26 --------- provider/yaml/watch.go | 46 --------------- test/fixture/config.yaml | 14 ----- test/helpers.go | 17 ------ test/json.go | 15 ----- test/provider_suite.go | 2 - 10 files changed, 31 insertions(+), 229 deletions(-) delete mode 100644 provider/yaml/provider.go delete mode 100644 provider/yaml/provider_test.go delete mode 100644 provider/yaml/watch.go delete mode 100644 test/fixture/config.yaml delete mode 100644 test/helpers.go delete mode 100644 test/json.go diff --git a/go.mod b/go.mod index b5767fb..5af0f3c 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,6 @@ go 1.18 require ( github.com/iancoleman/strcase v0.3.0 github.com/stretchr/testify v1.7.0 - gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b ) require ( @@ -13,4 +12,5 @@ require ( github.com/kr/pretty v0.1.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect + gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect ) diff --git a/provider/arg/provider.go b/provider/arg/provider.go index 620ea8d..3503a26 100644 --- a/provider/arg/provider.go +++ b/provider/arg/provider.go @@ -2,13 +2,14 @@ package arg import ( "context" + "encoding/json" "fmt" + "log" "os" "strings" "gitoa.ru/go-4devs/config" "gitoa.ru/go-4devs/config/value" - "gopkg.in/yaml.v3" ) const Name = "arg" @@ -117,13 +118,15 @@ func (p *Provider) Value(ctx context.Context, path ...string) (config.Value, err case len(val) == 1: return value.JString(val[0]), nil default: - var yNode yaml.Node - - if err := yaml.Unmarshal([]byte("["+strings.Join(val, ",")+"]"), &yNode); err != nil { - return nil, fmt.Errorf("arg: failed unmarshal yaml:%w", err) + data, jerr := json.Marshal(val) + if jerr != nil { + return nil, fmt.Errorf("failed load data:%w", jerr) } - return value.Decode(yNode.Decode), nil + return value.Decode(func(v interface{}) error { + log.Println(string(data)) + return json.Unmarshal(data, v) + }), nil } } diff --git a/provider/arg/provider_test.go b/provider/arg/provider_test.go index a27cf2e..2e759ea 100644 --- a/provider/arg/provider_test.go +++ b/provider/arg/provider_test.go @@ -1,7 +1,9 @@ package arg_test import ( + "fmt" "os" + "strings" "testing" "time" @@ -35,7 +37,7 @@ func TestProvider(t *testing.T) { test.NewRead("config.hcl", "config"), test.NewRead(test.Time("2010-01-02T15:04:05Z"), "start-at"), test.NewReadUnmarshal(&[]string{"http://4devs.io", "https://4devs.io"}, &[]string{}, "url"), - test.NewReadUnmarshal(&[]time.Duration{time.Minute, time.Hour}, &[]time.Duration{}, "timeout"), + test.NewReadUnmarshal(&[]Duration{{time.Minute}, {time.Hour}}, &[]Duration{}, "timeout"), test.NewReadUnmarshal(&[]time.Time{ test.Time("2009-01-02T15:04:05Z"), test.Time("2008-01-02T15:04:05+03:00"), @@ -46,3 +48,21 @@ func TestProvider(t *testing.T) { test.Run(t, prov, read) } + +type Duration struct { + time.Duration +} + +func (d *Duration) UnmarshalJSON(in []byte) error { + o, err := time.ParseDuration(strings.Trim(string(in), `"`)) + if err != nil { + return fmt.Errorf("parse:%w", err) + } + d.Duration = o + + return nil +} + +func (d *Duration) MarshalJSON() ([]byte, error) { + return []byte(fmt.Sprintf("%q", d)), nil +} diff --git a/provider/yaml/provider.go b/provider/yaml/provider.go deleted file mode 100644 index eefc5f0..0000000 --- a/provider/yaml/provider.go +++ /dev/null @@ -1,101 +0,0 @@ -package yaml - -import ( - "context" - "errors" - "fmt" - "os" - - "gitoa.ru/go-4devs/config" - "gitoa.ru/go-4devs/config/value" - "gopkg.in/yaml.v3" -) - -const ( - Name = "yaml" -) - -var _ config.Provider = (*Provider)(nil) - -func NewFile(name string, opts ...Option) (*Provider, error) { - in, err := os.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 { - prov := Provider{ - name: Name, - } - - for _, opt := range opts { - opt(&prov) - } - - return &prov -} - -type Option func(*Provider) - -type Provider struct { - data node - name string -} - -func (p *Provider) Name() string { - return p.name -} - -func (p *Provider) Value(_ context.Context, path ...string) (config.Value, error) { - - return p.data.read(p.Name(), path) -} - -func (p *Provider) With(data *yaml.Node) *Provider { - return &Provider{ - data: node{Node: data}, - } -} - -type node struct { - *yaml.Node -} - -func (n *node) read(name string, keys []string) (config.Value, error) { - val, err := getData(n.Node.Content[0].Content, keys) - if err != nil { - if errors.Is(err, config.ErrValueNotFound) { - return nil, fmt.Errorf("%w: %s", config.ErrValueNotFound, name) - } - - return nil, fmt.Errorf("%w: %s", err, name) - } - - return value.Decode(val), nil -} - -func getData(node []*yaml.Node, keys []string) (func(interface{}) error, error) { - for idx := len(node) - 1; idx > 0; idx -= 2 { - if node[idx-1].Value == keys[0] { - if len(keys) > 1 { - return getData(node[idx].Content, keys[1:]) - } - - return node[idx].Decode, nil - } - } - - return nil, config.ErrValueNotFound -} diff --git a/provider/yaml/provider_test.go b/provider/yaml/provider_test.go deleted file mode 100644 index e5105db..0000000 --- a/provider/yaml/provider_test.go +++ /dev/null @@ -1,26 +0,0 @@ -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(21*time.Minute, "duration_var"), - test.NewRead(true, "app", "name", "bool_var"), - test.NewRead(test.Time("2020-01-02T15:04:05Z"), "time_var"), - test.NewReadConfig("cfg"), - } - - test.Run(t, prov, read) -} diff --git a/provider/yaml/watch.go b/provider/yaml/watch.go deleted file mode 100644 index a88fc67..0000000 --- a/provider/yaml/watch.go +++ /dev/null @@ -1,46 +0,0 @@ -package yaml - -import ( - "context" - "fmt" - "os" - - "gitoa.ru/go-4devs/config" - "gopkg.in/yaml.v3" -) - -const NameWatch = "yaml_watch" - -func NewWatch(name string, opts ...Option) *Watch { - f := Watch{ - file: name, - prov: create(opts...), - name: NameWatch, - } - - return &f -} - -type Watch struct { - file string - prov *Provider - name string -} - -func (p *Watch) Name() string { - return p.name -} - -func (p *Watch) Value(ctx context.Context, path ...string) (config.Value, error) { - in, err := os.ReadFile(p.file) - if err != nil { - return nil, fmt.Errorf("yaml_file: read error: %w", err) - } - - var yNode yaml.Node - if err = yaml.Unmarshal(in, &yNode); err != nil { - return nil, fmt.Errorf("yaml_file: unmarshal error: %w", err) - } - - return p.prov.With(&yNode).Value(ctx, path...) -} diff --git a/test/fixture/config.yaml b/test/fixture/config.yaml deleted file mode 100644 index 0d99de8..0000000 --- a/test/fixture/config.yaml +++ /dev/null @@ -1,14 +0,0 @@ -app: - title: yaml title - name: - var: - - test - bool_var: true -duration_var: 21m -empty_var: -url_var: "http://google.com/" -time_var: "2020-01-02T15:04:05Z" -cfg: - duration: 21m - enabled: true - type: yaml diff --git a/test/helpers.go b/test/helpers.go deleted file mode 100644 index 979b354..0000000 --- a/test/helpers.go +++ /dev/null @@ -1,17 +0,0 @@ -package test - -import ( - "path/filepath" - "runtime" -) - -func FixturePath(file string) string { - path := "fixture/" - - _, filename, _, ok := runtime.Caller(0) - if ok { - path = filepath.Dir(filename) + "/" + path - } - - return path + file -} diff --git a/test/json.go b/test/json.go deleted file mode 100644 index dbed18f..0000000 --- a/test/json.go +++ /dev/null @@ -1,15 +0,0 @@ -package test - -import ( - "io/ioutil" - "log" -) - -func ReadFile(file string) []byte { - data, err := ioutil.ReadFile(FixturePath(file)) - if err != nil { - log.Fatal(err) - } - - return data -} diff --git a/test/provider_suite.go b/test/provider_suite.go index c71ae92..d0fef3d 100644 --- a/test/provider_suite.go +++ b/test/provider_suite.go @@ -39,8 +39,6 @@ type Read struct { Assert func(t *testing.T, v config.Value) } -const ConfigJSON = `{"duration":1260000000000,"enabled":true}` - type Config struct { Duration time.Duration Enabled bool