andrey
12 months ago
6 changed files with 0 additions and 344 deletions
@ -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}
|
|||
} |
@ -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) |
|||
} |
@ -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) |
|||
} |
@ -1,17 +0,0 @@ |
|||
{ |
|||
"app": { |
|||
"name": { |
|||
"var": [ |
|||
"name" |
|||
], |
|||
"title": "config title", |
|||
"timeout": "1m", |
|||
"success": true |
|||
} |
|||
}, |
|||
"cfg": { |
|||
"duration": 1260000000000, |
|||
"enabled": true, |
|||
"type":"json" |
|||
} |
|||
} |
Loading…
Reference in new issue