Compare commits

...

2 Commits

Author SHA1 Message Date
d5b5103f40 remove yaml provider
Some checks failed
continuous-integration/drone/push Build is failing
2024-01-25 19:43:56 +03:00
11c3c3e5f8 remove json provider 2024-01-25 19:13:01 +03:00
15 changed files with 31 additions and 573 deletions

View File

@@ -1,226 +0,0 @@
package config_test
import (
"context"
"fmt"
"log"
"os"
"sync"
"time"
"gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/provider/arg"
"gitoa.ru/go-4devs/config/provider/env"
"gitoa.ru/go-4devs/config/provider/json"
"gitoa.ru/go-4devs/config/provider/watcher"
"gitoa.ru/go-4devs/config/provider/yaml"
"gitoa.ru/go-4devs/config/test"
)
func ExampleClient_Value() {
const (
namespace = "fdevs"
appName = "config"
)
ctx := context.Background()
_ = os.Setenv("FDEVS_CONFIG_LISTEN", "8080")
_ = os.Setenv("FDEVS_CONFIG_HOST", "localhost")
args := os.Args
defer func() {
os.Args = args
}()
os.Args = []string{"main.go", "--host=gitoa.ru"}
// read json config
jsonConfig := test.ReadFile("config.json")
config, err := config.New(
arg.New(),
env.New(test.Namespace, test.AppName),
json.New(jsonConfig),
)
if err != nil {
log.Print(err)
return
}
port, err := config.Value(ctx, "listen")
if err != nil {
log.Print("listen", err)
return
}
title, err := config.Value(ctx, "app.name.title")
if err != nil {
log.Print("app.name.title", err)
return
}
cfgValue, err := config.Value(ctx, "cfg")
if err != nil {
log.Print("cfg ", err)
return
}
hostValue, err := config.Value(ctx, "host")
if err != nil {
log.Print("host ", err)
return
}
cfg := test.Config{}
_ = cfgValue.Unmarshal(&cfg)
fmt.Printf("listen from env: %d\n", port.Int())
fmt.Printf("title from json: %v\n", title.String())
fmt.Printf("struct from json: %+v\n", cfg)
fmt.Printf("replace env host by args: %v\n", hostValue.String())
// Output:
// listen from env: 8080
// title from json: config title
// struct from json: {Duration:21m0s Enabled:true}
// replace env host by args: gitoa.ru
}
func ExampleClient_Watch() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_ = os.Setenv("FDEVS_CONFIG_EXAMPLE_ENABLE", "true")
watcher, err := config.New(
watcher.New(time.Microsecond, env.New(test.Namespace, test.AppName)),
watcher.New(time.Microsecond, yaml.NewWatch("test/fixture/config.yaml")),
)
if err != nil {
log.Print(err)
return
}
wg := sync.WaitGroup{}
wg.Add(1)
err = watcher.Watch(ctx, func(ctx context.Context, oldVar, newVar config.Value) {
fmt.Println("update example_enable old: ", oldVar.Bool(), " new:", newVar.Bool())
wg.Done()
}, "example_enable")
if err != nil {
log.Print(err)
return
}
_ = os.Setenv("FDEVS_CONFIG_EXAMPLE_ENABLE", "false")
err = watcher.Watch(ctx, func(ctx context.Context, oldVar, newVar config.Value) {
fmt.Println("update example_db_dsn old: ", oldVar.String(), " new:", newVar.String())
wg.Done()
}, "example_db_dsn")
if err != nil {
log.Print(err)
return
}
wg.Wait()
// Output:
// update example_enable old: true new: false
}
func ExampleClient_Value_factory() {
ctx := context.Background()
_ = os.Setenv("FDEVS_CONFIG_LISTEN", "8080")
_ = os.Setenv("FDEVS_CONFIG_HOST", "localhost")
args := os.Args
defer func() {
os.Args = args
}()
os.Args = []string{"main.go", "--config-json=config.json", "--config-yaml=test/fixture/config.yaml"}
config, err := config.New(
arg.New(),
env.New(test.Namespace, test.AppName),
config.Factory(func(ctx context.Context, cfg config.Provider) (config.Provider, error) {
val, err := cfg.Value(ctx, "config-json")
if err != nil {
return nil, fmt.Errorf("failed read config file:%w", err)
}
jsonConfig := test.ReadFile(val.String())
return json.New(jsonConfig), nil
}),
config.Factory(func(ctx context.Context, cfg config.Provider) (config.Provider, error) {
val, err := cfg.Value(ctx, "config-yaml")
if err != nil {
return nil, fmt.Errorf("failed read config file:%w", err)
}
provader, err := yaml.NewFile(val.String())
if err != nil {
return nil, fmt.Errorf("failed init by file %v:%w", val.String(), err)
}
return provader, nil
}),
)
if err != nil {
log.Print(err)
return
}
port, err := config.Value(ctx, "listen")
if err != nil {
log.Print(err)
return
}
title, err := config.Value(ctx, "app", "name", "title")
if err != nil {
log.Print(err)
return
}
yamlTitle, err := config.Value(ctx, "app", "title")
if err != nil {
log.Print(err)
return
}
cfgValue, err := config.Value(ctx, "cfg")
if err != nil {
log.Print(err)
return
}
cfg := test.Config{}
_ = cfgValue.Unmarshal(&cfg)
fmt.Printf("listen from env: %d\n", port.Int())
fmt.Printf("title from json: %v\n", title.String())
fmt.Printf("yaml title: %v\n", yamlTitle.String())
fmt.Printf("struct from json: %+v\n", cfg)
// Output:
// listen from env: 8080
// title from json: config title
// yaml title: yaml title
// struct from json: {Duration:21m0s Enabled:true}
}

5
go.mod
View File

@@ -5,15 +5,12 @@ go 1.18
require (
github.com/iancoleman/strcase v0.3.0
github.com/stretchr/testify v1.7.0
github.com/tidwall/gjson v1.7.5
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/tidwall/match v1.0.3 // indirect
github.com/tidwall/pretty v1.1.0 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)

6
go.sum
View File

@@ -13,12 +13,6 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tidwall/gjson v1.7.5 h1:zmAN/xmX7OtpAkv4Ovfso60r/BiCi5IErCDYGNJu+uc=
github.com/tidwall/gjson v1.7.5/go.mod h1:5/xDoumyyDNerp2U36lyolv46b3uF/9Bu6OfyQ9GImk=
github.com/tidwall/match v1.0.3 h1:FQUVvBImDutD8wJLN6c5eMzWtjgONK9MwIBCOrUJKeE=
github.com/tidwall/match v1.0.3/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.1.0 h1:K3hMW5epkdAVwibsQEfR/7Zj0Qgt4DxtNumTq/VloO8=
github.com/tidwall/pretty v1.1.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

View File

@@ -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
}
}

View File

@@ -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
}

View File

@@ -1,65 +0,0 @@
package json
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/tidwall/gjson"
"gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/value"
)
const (
Name = "json"
Separator = "."
)
var _ config.Provider = (*Provider)(nil)
func New(json []byte, opts ...Option) *Provider {
provider := Provider{
key: func(s ...string) string {
return strings.Join(s, Separator)
},
data: json,
}
for _, opt := range opts {
opt(&provider)
}
return &provider
}
func NewFile(path string, opts ...Option) (*Provider, error) {
file, err := os.ReadFile(filepath.Clean(path))
if err != nil {
return nil, fmt.Errorf("%w: unable to read config file %#q: file not found or unreadable", err, path)
}
return New(file, opts...), nil
}
type Option func(*Provider)
type Provider struct {
data []byte
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) {
key := p.key(path...)
if val := gjson.GetBytes(p.data, key); val.Exists() {
return value.JString(val.String()), nil
}
return nil, fmt.Errorf("%v:%w", p.Name(), config.ErrValueNotFound)
}

View File

@@ -1,27 +0,0 @@
package json_test
import (
"testing"
"time"
provider "gitoa.ru/go-4devs/config/provider/json"
"gitoa.ru/go-4devs/config/test"
)
func TestProvider(t *testing.T) {
t.Parallel()
js := test.ReadFile("config.json")
prov := provider.New(js)
sl := []string{}
read := []test.Read{
test.NewRead("config title", "app.name.title"),
test.NewRead(time.Minute, "app.name.timeout"),
test.NewReadUnmarshal(&[]string{"name"}, &sl, "app.name.var"),
test.NewReadConfig("cfg"),
test.NewRead(true, "app", "name", "success"),
}
test.Run(t, prov, read)
}

View File

@@ -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
}

View File

@@ -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)
}

View File

@@ -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...)
}

View File

@@ -1,17 +0,0 @@
{
"app": {
"name": {
"var": [
"name"
],
"title": "config title",
"timeout": "1m",
"success": true
}
},
"cfg": {
"duration": 1260000000000,
"enabled": true,
"type":"json"
}
}

View File

@@ -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

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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