11 Commits

Author SHA1 Message Date
b9af510e71 update version golangci-lint
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2024-01-25 21:06:29 +03:00
4ef55a0d93 update version go for slog
Some checks failed
continuous-integration/drone/push Build is failing
2024-01-25 21:01:28 +03:00
f094e4b48c remove race from test
Some checks failed
continuous-integration/drone/push Build is failing
2024-01-25 20:57:43 +03:00
126ec4cb88 fix lint
Some checks failed
continuous-integration/drone/push Build is failing
2024-01-25 20:56:21 +03:00
085589a938 add stop watch
Some checks failed
continuous-integration/drone/push Build is failing
2024-01-25 20:49:37 +03:00
f4446378d4 update drone
Some checks failed
continuous-integration/drone/push Build is failing
2024-01-25 20:36:51 +03:00
b866158d9b udate watcher
Some checks failed
continuous-integration/drone/push Build is failing
2024-01-25 20:33:10 +03:00
d864996848 update go mod
Some checks failed
continuous-integration/drone/push Build is failing
2024-01-25 20:08:57 +03:00
e2a2db01bb remove definition
Some checks failed
continuous-integration/drone/push Build is failing
2024-01-25 19:44:58 +03:00
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
50 changed files with 176 additions and 1677 deletions

View File

@@ -1,17 +1,6 @@
kind: pipeline
name: default
services:
- name: vault
image: vault:1.13.3
environment:
VAULT_DEV_ROOT_TOKEN_ID: dev
VAULT_DEV_LISTEN_ADDRESS: 0.0.0.0:8200
- name: etcd
image: bitnami/etcd:3.5.11
environment:
ALLOW_NONE_AUTHENTICATION: yes
environment:
VAULT_DEV_LISTEN_ADDRESS: http://vault:8200
VAULT_DEV_ROOT_TOKEN_ID: dev
@@ -21,9 +10,11 @@ steps:
- name: test
image: golang
commands:
- go test -parallel 10 -race ./...
# - go test -parallel 10 -race ./...
# - go test -race ./...
- go test ./...
- name: golangci-lint
image: golangci/golangci-lint:v1.53
image: golangci/golangci-lint:v1.55
commands:
- golangci-lint run

View File

@@ -43,6 +43,8 @@ linters:
- ifshort
- nosnakecase
- ireturn # implement provider interface
issues:
# Excluding configuration per-path, per-linter, per-text and per-source
exclude-rules:

View File

@@ -124,7 +124,6 @@ func (c *Client) Value(ctx context.Context, path ...string) (Value, error) {
}
func (c *Client) Watch(ctx context.Context, callback WatchCallback, path ...string) error {
for idx, prov := range c.providers {
provider, ok := prov.(WatchProvider)
if !ok {

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

View File

@@ -1,29 +0,0 @@
package definition
import (
"fmt"
)
func New() Definition {
return Definition{}
}
type Definition struct {
options Options
}
func (d *Definition) Add(opts ...Option) *Definition {
d.options = append(d.options, opts...)
return d
}
func (d *Definition) View(handle func(Option) error) error {
for idx, opt := range d.options {
if err := handle(opt); err != nil {
return fmt.Errorf("%s[%d]:%w", opt.Kind(), idx, err)
}
}
return nil
}

View File

@@ -1,70 +0,0 @@
package generate
import (
"fmt"
"io"
"gitoa.ru/go-4devs/config/definition"
)
type Generator struct {
pkg string
ViewOption
Imp Imports
errs []error
defaultErrors []string
}
func (g Generator) Pkg() string {
return g.pkg
}
func (g Generator) Imports() []Import {
return g.Imp.Imports()
}
func (g Generator) Handle(w io.Writer, data Handler, opt definition.Option) error {
handle := get(opt.Kind())
return handle(w, data, opt)
}
func (g Generator) StructName() string {
return FuncName(g.Prefix + "_" + g.Struct + "_" + g.Suffix)
}
func (g Generator) Options() ViewOption {
return g.ViewOption
}
func (g Generator) Keys() []string {
return nil
}
func (g Generator) DefaultErrors() []string {
if len(g.defaultErrors) > 0 {
return g.defaultErrors
}
if len(g.ViewOption.Errors.Default) > 0 {
g.Imp.Adds("errors")
}
g.defaultErrors = make([]string, len(g.ViewOption.Errors.Default))
for idx, name := range g.ViewOption.Errors.Default {
short, err := g.AddType(name)
if err != nil {
g.errs = append(g.errs, fmt.Errorf("add default error[%d]:%w", idx, err))
return nil
}
g.defaultErrors[idx] = short
}
return g.defaultErrors
}
func (g *Generator) AddType(pkg string) (string, error) {
return g.Imp.AddType(pkg)
}

View File

@@ -1,16 +0,0 @@
package generate
import (
"errors"
"github.com/iancoleman/strcase"
)
var (
ErrNotFound = errors.New("not found")
ErrAlreadyExist = errors.New("already exist")
)
func FuncName(in string) string {
return strcase.ToCamel(in)
}

View File

@@ -1,87 +0,0 @@
package generate
import (
"fmt"
"strconv"
"strings"
)
func NewImports() Imports {
return Imports{
data: make(map[string]string),
}
}
type Imports struct {
data map[string]string
}
func (i Imports) Imports() []Import {
imports := make([]Import, 0, len(i.data))
for name, alias := range i.data {
imports = append(imports, Import{
Package: name,
Alias: alias,
})
}
return imports
}
func (i *Imports) Short(fullType string) (string, error) {
idx := strings.LastIndexByte(fullType, '.')
if idx == -1 {
return "", fmt.Errorf("unexpected")
}
if alias, ok := i.data[fullType[:idx]]; ok {
return alias + fullType[idx:], nil
}
return "", fmt.Errorf("%w alias for pkg %v", ErrNotFound, fullType[:idx])
}
func (i *Imports) AddType(fullType string) (string, error) {
idx := strings.LastIndexByte(fullType, '.')
if idx == -1 {
return "", fmt.Errorf("unexpected")
}
imp := i.Add(fullType[:idx])
return imp.Alias + fullType[idx:], nil
}
func (i *Imports) Adds(pkgs ...string) {
for _, pkg := range pkgs {
i.Add(pkg)
}
}
func (i *Imports) Add(pkg string) Import {
alias := pkg
idx := strings.LastIndexByte(pkg, '/')
if idx != -1 {
alias = pkg[idx+1:]
}
if al, ok := i.data[pkg]; ok {
return Import{Package: pkg, Alias: al}
}
for _, al := range i.data {
if al == alias {
alias += strconv.Itoa(len(i.data))
}
}
i.data[pkg] = alias
return Import{
Alias: alias,
Package: pkg,
}
}
type Import struct {
Alias string
Package string
}

View File

@@ -1,38 +0,0 @@
package generate
import (
"bytes"
"fmt"
"io"
"gitoa.ru/go-4devs/config/definition"
)
func Run(w io.Writer, pkgName string, defs definition.Definition, viewOpt ViewOption) error {
gen := Generator{
pkg: pkgName,
ViewOption: viewOpt,
Imp: NewImports(),
}
gen.Imp.Adds("gitoa.ru/go-4devs/config", "fmt", "context")
var view bytes.Buffer
err := defs.View(func(o definition.Option) error {
return gen.Handle(&view, &gen, o)
})
if err != nil {
return fmt.Errorf("render options:%w", err)
}
if err := tpl.Execute(w, gen); err != nil {
return fmt.Errorf("render base:%w", err)
}
_, cerr := io.Copy(w, &view)
if cerr != nil {
return fmt.Errorf("copy error:%w", cerr)
}
return nil
}

View File

@@ -1,41 +0,0 @@
package generate
import "text/template"
var tpl = template.Must(template.New("tpls").Parse(baseTemplate))
var baseTemplate = `// Code generated gitoa.ru/go-4devs/config DO NOT EDIT.
package {{.Pkg}}
import (
{{range .Imports}}
{{- .Alias }}"{{ .Package }}"
{{end}}
)
func With{{.StructName}}Log(log func(context.Context, string, ...any)) func(*{{.StructName}}) {
return func(ci *{{.StructName}}) {
ci.log = log
}
}
func New{{.StructName}}(prov config.Provider, opts ...func(*{{.StructName}})) {{.StructName}} {
i := {{.StructName}}{
Provider: prov,
log: func(_ context.Context, format string, args ...any) {
fmt.Printf(format, args...)
},
}
for _, opt := range opts {
opt(&i)
}
return i
}
type {{.StructName}} struct {
config.Provider
log func(context.Context, string, ...any)
}
`

View File

@@ -1,60 +0,0 @@
package generate
import (
"fmt"
"io"
"sync"
"gitoa.ru/go-4devs/config/definition"
)
var handlers = sync.Map{}
func Add(kind string, h Handle) error {
_, ok := handlers.Load(kind)
if ok {
return fmt.Errorf("kind %v: %w", kind, ErrAlreadyExist)
}
handlers.Store(kind, h)
return nil
}
func get(kind string) Handle {
h, ok := handlers.Load(kind)
if !ok {
return func(w io.Writer, h Handler, o definition.Option) error {
return fmt.Errorf("handler by %v:%w", kind, ErrNotFound)
}
}
return h.(Handle)
}
func MustAdd(kind string, h Handle) {
if err := Add(kind, h); err != nil {
panic(err)
}
}
type Handle func(io.Writer, Handler, definition.Option) error
type Handler interface {
StructName() string
Handle(io.Writer, Handler, definition.Option) error
Options() ViewOption
Keys() []string
AddType(fullName string) (string, error)
DefaultErrors() []string
}
type ViewOption struct {
Prefix, Suffix string
Context bool
Struct string
Errors ViewErrors
}
type ViewErrors struct {
Default []string
}

View File

@@ -1,27 +0,0 @@
package group
import (
"gitoa.ru/go-4devs/config/definition"
)
const Kind = "group"
var _ definition.Option = Group{}
func New(name, desc string, opts ...definition.Option) Group {
return Group{
Name: name,
Description: desc,
Options: opts,
}
}
type Group struct {
Options definition.Options
Name string
Description string
}
func (o Group) Kind() string {
return Kind
}

View File

@@ -1,85 +0,0 @@
package group
import (
"fmt"
"io"
"text/template"
"gitoa.ru/go-4devs/config/definition"
"gitoa.ru/go-4devs/config/definition/generate"
)
func init() {
generate.MustAdd(Kind, handle)
}
func handle(w io.Writer, data generate.Handler, option definition.Option) error {
group, ok := option.(Group)
if !ok {
return fmt.Errorf("uexepected type:%T", option)
}
viewData := View{
Group: group,
ParentName: data.StructName(),
ViewOption: data.Options(),
}
err := tpl.Execute(w, viewData)
if err != nil {
return fmt.Errorf("render group:%w", err)
}
childData := ChildData{
Handler: data,
structName: viewData.StructName(),
keys: append(data.Keys(), group.Name),
}
for idx, child := range group.Options {
if cerr := data.Handle(w, childData, child); cerr != nil {
return fmt.Errorf("render group child[%d]:%w", idx, cerr)
}
}
return nil
}
type ChildData struct {
generate.Handler
structName string
keys []string
}
func (c ChildData) StructName() string {
return c.structName
}
func (v ChildData) Keys() []string {
return v.keys
}
type View struct {
Group
ParentName string
generate.ViewOption
}
func (v View) FuncName() string {
return generate.FuncName(v.Name)
}
func (v View) StructName() string {
return generate.FuncName(v.Prefix + v.Name + v.Suffix)
}
var tpl = template.Must(template.New("tpls").Parse(tplw))
var tplw = `type {{.StructName}} struct {
{{.ParentName}}
}
// {{.FuncName}} {{.Description}}.
func (i {{.ParentName}}) {{.FuncName}}() {{.StructName}} {
return {{.StructName}}{i}
}
`

View File

@@ -1,27 +0,0 @@
package definition
type Option interface {
Kind() string
}
type Options []Option
func (s Options) Len() int { return len(s) }
func (s Options) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
type Params []Param
func (p Params) Get(name string) (any, bool) {
for _, param := range p {
if param.Name == name {
return param.Value, true
}
}
return nil, false
}
type Param struct {
Name string
Value any
}

View File

@@ -1,100 +0,0 @@
package option
import (
"gitoa.ru/go-4devs/config/definition"
)
var _ definition.Option = Option{}
const (
Kind = "option"
)
const (
TypeString = "string"
TypeInt = "int"
TypeInt64 = "int64"
TypeUint = "uint"
TypeUint64 = "uint64"
TypeFloat64 = "float64"
TypeBool = "bool"
TypeTime = "time.Time"
TypeDuration = "time.Duration"
)
func Default(v any) func(*Option) {
return func(o *Option) {
o.Default = v
}
}
func New(name, desc string, vtype any, opts ...func(*Option)) Option {
option := Option{
Name: name,
Description: desc,
Type: vtype,
}
for _, opt := range opts {
opt(&option)
}
return option
}
type Option struct {
Name string
Description string
Type any
Default any
Params definition.Params
}
func (o Option) WithParams(params ...definition.Param) Option {
return Option{
Name: o.Name,
Description: o.Description,
Type: o.Type,
Params: append(params, o.Params...),
}
}
func (o Option) Kind() string {
return Kind
}
func Time(name, desc string, opts ...func(*Option)) Option {
return New(name, desc, TypeTime, opts...)
}
func Duration(name, desc string, opts ...func(*Option)) Option {
return New(name, desc, TypeDuration, opts...)
}
func String(name, desc string, opts ...func(*Option)) Option {
return New(name, desc, TypeString, opts...)
}
func Int(name, desc string, opts ...func(*Option)) Option {
return New(name, desc, TypeInt, opts...)
}
func Int64(name, desc string, opts ...func(*Option)) Option {
return New(name, desc, TypeInt64, opts...)
}
func Uint(name, desc string, opts ...func(*Option)) Option {
return New(name, desc, TypeUint, opts...)
}
func Uint64(name, desc string, opts ...func(*Option)) Option {
return New(name, desc, TypeUint64, opts...)
}
func Float64(name, desc string, opts ...func(*Option)) Option {
return New(name, desc, TypeFloat64, opts...)
}
func Bool(name, desc string, opts ...func(*Option)) Option {
return New(name, desc, TypeBool, opts...)
}

View File

@@ -1,33 +0,0 @@
// read{{.FuncName}} {{.Description}}.
func (i {{.StructName}}) read{{.FuncName}}(ctx context.Context) (v {{.Type}},e error) {
val, err := i.Value(ctx, {{ .ParentKeys }}"{{ .Name }}")
if err != nil {
{{if .HasDefault}}
{{$default := .Default}}
{{range .DefaultErrors}}
if errors.Is(err,{{.}}){
return {{$default}}
}
{{end}}
{{end}}
return v, fmt.Errorf("read {{.Keys}}:%w",err)
}
{{.Parse "val" "v" .Keys }}
}
// Read{{.FuncName}} {{.Description}}.
func (i {{.StructName}}) Read{{.FuncName}}(ctx context.Context) ({{.Type}}, error) {
return i.read{{.FuncName}}(ctx)
}
// {{.FuncName}} {{.Description}}.
func (i {{.StructName}}) {{.FuncName}}({{if .Context}} ctx context.Context {{end}}) {{.Type}} {
{{if not .Context}} ctx := context.Background() {{end}}
val, err := i.read{{.FuncName}}(ctx)
if err != nil {
i.log(ctx, "get {{.Keys}}: %v", err)
}
return val
}

View File

@@ -1,3 +0,0 @@
{{block "Parse" .}}
return {{.ValName}}.Parse{{ .FuncType}}()
{{end}}

View File

@@ -1,8 +0,0 @@
{{block "UnmarshalJSON" . }}
pval, perr := {{.ValName}}.ParseString()
if perr != nil {
return {{.Value}}, fmt.Errorf("read {{.Keys}}:%w", perr)
}
return {{.Value}}, {{.Value}}.UnmarshalJSON([]byte(pval))
{{end}}

View File

@@ -1,8 +0,0 @@
{{block "UnmarshalText" . }}
pval, perr := {{.ValName}}.ParseString()
if perr != nil {
return {{.Value}}, fmt.Errorf("read {{.Keys}}:%w", perr)
}
return {{.Value}}, {{.Value}}.UnmarshalText([]byte(pval))
{{end}}

View File

@@ -1,226 +0,0 @@
package option
import (
"bytes"
"embed"
"encoding"
"encoding/json"
"fmt"
"io"
"reflect"
"strings"
"text/template"
"time"
"gitoa.ru/go-4devs/config/definition"
"gitoa.ru/go-4devs/config/definition/generate"
)
//go:embed tpl/*
var tpls embed.FS
var tpl = template.Must(template.New("tpls").ParseFS(tpls, "tpl/*.tmpl"))
func init() {
generate.MustAdd(Kind, Handle(tpl.Lookup("option.tmpl")))
}
func Handle(tpl *template.Template) generate.Handle {
return func(w io.Writer, h generate.Handler, o definition.Option) error {
opt, _ := o.(Option)
if err := tpl.Execute(w, View{Option: opt, Handler: h}); err != nil {
return fmt.Errorf("option tpl:%w", err)
}
return nil
}
}
type View struct {
Option
generate.Handler
}
func (v View) Context() bool {
return v.Options().Context
}
func (v View) FuncName() string {
if funcName, ok := v.Option.Params.Get(ViewParamFunctName); ok {
return funcName.(string)
}
return generate.FuncName(v.Name)
}
func (v View) Description() string {
if desc, ok := v.Option.Params.Get(ViewParamDescription); ok {
return desc.(string)
}
return v.Option.Description
}
func (v View) Default() string {
switch data := v.Option.Default.(type) {
case time.Time:
return fmt.Sprintf("time.Parse(%q,time.RFC3339Nano)", data.Format(time.RFC3339Nano))
case time.Duration:
return fmt.Sprintf("time.ParseDuration(%q)", data)
default:
return fmt.Sprintf("%#v, nil", data)
}
}
func (v View) HasDefault() bool {
return v.Option.Default != nil
}
func (v View) ParentKeys() string {
if len(v.Handler.Keys()) > 0 {
return `"` + strings.Join(v.Handler.Keys(), `","`) + `",`
}
return ""
}
func (v View) Type() string {
slice := ""
if vtype, ok := v.Option.Type.(string); ok {
if strings.Contains(vtype, ".") {
if name, err := v.AddType(vtype); err == nil {
return slice + name
}
}
return vtype
}
rtype := reflect.TypeOf(v.Option.Type)
if rtype.PkgPath() == "" {
return rtype.String()
}
if rtype.Kind() == reflect.Slice {
slice = "[]"
}
short, err := v.AddType(rtype.PkgPath() + "." + rtype.Name())
if err != nil {
return err.Error()
}
return slice + short
}
func (v View) FuncType() string {
return generate.FuncName(v.Type())
}
func (v View) Parse(valName string, value string, keys []string) string {
h := parser(v.Option.Type)
data, err := h(ParseData{
Value: value,
ValName: valName,
Keys: keys,
View: v,
})
if err != nil {
return err.Error()
}
return data
}
var parses = map[string]func(data ParseData) (string, error){
typesIntreface[0].Name(): func(data ParseData) (string, error) {
var b bytes.Buffer
err := tpl.ExecuteTemplate(&b, "unmarshal_text.tmpl", data)
if err != nil {
return "", err
}
return b.String(), nil
},
typesIntreface[1].Name(): func(data ParseData) (string, error) {
var b bytes.Buffer
err := tpl.ExecuteTemplate(&b, "unmarshal_json.tmpl", data)
if err != nil {
return "", err
}
return b.String(), nil
},
TypeInt: internal,
TypeInt64: internal,
TypeBool: internal,
TypeString: internal,
TypeFloat64: internal,
TypeUint: internal,
TypeUint64: internal,
"time.Duration": func(data ParseData) (string, error) {
return fmt.Sprintf("return %s.ParseDuration()", data.ValName), nil
},
"time.Time": func(data ParseData) (string, error) {
return fmt.Sprintf("return %s.ParseTime()", data.ValName), nil
},
"any": func(data ParseData) (string, error) {
return fmt.Sprintf("return %[2]s, %[1]s.Unmarshal(&%[2]s)", data.ValName, data.Value), nil
},
}
func internal(data ParseData) (string, error) {
var b bytes.Buffer
err := tpl.ExecuteTemplate(&b, "parse.tmpl", data)
if err != nil {
return "", err
}
return b.String(), nil
}
var (
typesIntreface = [...]reflect.Type{
reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem(),
reflect.TypeOf((*json.Unmarshaler)(nil)).Elem(),
}
)
func parser(data any) func(ParseData) (string, error) {
vtype := reflect.TypeOf(data)
name := vtype.Name()
if v, ok := data.(string); ok {
name = v
}
if vtype.Kind() == reflect.Slice {
return parses["any"]
}
if h, ok := parses[name]; ok {
return h
}
for _, extypes := range typesIntreface {
if vtype.Implements(extypes) {
return parses[extypes.Name()]
}
if vtype.Kind() != reflect.Ptr && reflect.PointerTo(vtype).Implements(extypes) {
return parses[extypes.Name()]
}
}
return parses["any"]
}
type ParseData struct {
Value string
ValName string
Keys []string
View
}

View File

@@ -1,6 +0,0 @@
package option
const (
ViewParamFunctName = "view.funcName"
ViewParamDescription = "view.description"
)

View File

@@ -1,27 +0,0 @@
package proto
import (
"gitoa.ru/go-4devs/config/definition"
)
const Kind = "proto"
func New(name, desc string, opt definition.Option, opts ...func(*Proto)) Proto {
pr := Proto{
Name: name,
Description: desc,
Option: opt,
}
return pr
}
type Proto struct {
Name string
Description string
Option definition.Option
}
func (p Proto) Kind() string {
return Kind
}

View File

@@ -1,76 +0,0 @@
package proto
import (
"fmt"
"io"
"strings"
"text/template"
"gitoa.ru/go-4devs/config/definition"
"gitoa.ru/go-4devs/config/definition/generate"
"gitoa.ru/go-4devs/config/definition/option"
)
func init() {
generate.MustAdd(Kind, handle)
}
func handle(w io.Writer, data generate.Handler, opt definition.Option) error {
proto, ok := opt.(Proto)
if !ok {
return fmt.Errorf("uexepected type:%T", opt)
}
if viewOpt, ok := proto.Option.(option.Option); ok {
viewOpt = viewOpt.WithParams(
definition.Param{
Name: option.ViewParamFunctName,
Value: generate.FuncName(proto.Name) + generate.FuncName(viewOpt.Name),
},
definition.Param{
Name: option.ViewParamDescription,
Value: proto.Description + " " + viewOpt.Description,
},
)
return option.Handle(tpl)(w, data, viewOpt)
}
return fmt.Errorf("not support option type")
}
var tpl = template.Must(template.New("tpls").Funcs(template.FuncMap{"join": strings.Join}).Parse(templateOption))
var templateOption = `// read{{.FuncName}} {{.Description}}.
func (i {{.StructName}}) read{{.FuncName}}(ctx context.Context, key string) (v {{.Type}},e error) {
val, err := i.Value(ctx, {{ .ParentKeys }} key, "{{.Name}}")
if err != nil {
{{if .HasDefault}}
{{$default := .Default}}
{{range .DefaultErrors}}
if errors.Is(err,{{.}}){
return {{$default}}
}
{{end}}
{{end}}
return v, fmt.Errorf("read {{.Keys}}:%w",err)
}
{{.Parse "val" "v" .Keys }}
}
// Read{{.FuncName}} {{.Description}}.
func (i {{.StructName}}) Read{{.FuncName}}(ctx context.Context, key string) ({{.Type}}, error) {
return i.read{{.FuncName}}(ctx, key)
}
// {{.FuncName}} {{.Description}}.
func (i {{.StructName}}) {{.FuncName}}({{if .Context}} ctx context.Context, {{end}} key string) {{.Type}} {
{{if not .Context}} ctx := context.Background() {{end}}
val, err := i.read{{.FuncName}}(ctx, key)
if err != nil {
i.log(ctx, "get {{.Keys}}: %v", err)
}
return val
}
`

View File

@@ -7,4 +7,5 @@ var (
ErrInvalidValue = errors.New("invalid value")
ErrUnknowType = errors.New("unknow type")
ErrInitFactory = errors.New("init factory")
ErrStopWatch = errors.New("stop watch")
)

18
go.mod
View File

@@ -1,19 +1,3 @@
module gitoa.ru/go-4devs/config
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
)
go 1.21

27
go.sum
View File

@@ -1,27 +0,0 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI=
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
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=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

15
key.go
View File

@@ -1,15 +0,0 @@
package config
import "context"
type Key struct {
Name string
AppName string
Namespace string
}
type KeyFactory func(ctx context.Context, key Key) string
func (k Key) String() string {
return k.Namespace + "_" + k.AppName + "_" + k.Name
}

View File

@@ -11,7 +11,7 @@ type NamedProvider interface {
Provider
}
type WatchCallback func(ctx context.Context, oldVar, newVar Value)
type WatchCallback func(ctx context.Context, oldVar, newVar Value) error
type WatchProvider interface {
Watch(ctx context.Context, callback WatchCallback, path ...string) error

View File

@@ -2,13 +2,13 @@ package arg
import (
"context"
"encoding/json"
"fmt"
"os"
"strings"
"gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/value"
"gopkg.in/yaml.v3"
)
const Name = "arg"
@@ -106,7 +106,7 @@ func (p *Provider) Name() string {
return p.name
}
func (p *Provider) Value(ctx context.Context, path ...string) (config.Value, error) {
func (p *Provider) Value(_ context.Context, path ...string) (config.Value, error) {
if err := p.parse(); err != nil {
return nil, err
}
@@ -117,13 +117,18 @@ 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 {
if err := json.Unmarshal(data, v); err != nil {
return fmt.Errorf("unmarshal:%w", err)
}
return nil
}), 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,22 @@ 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

@@ -45,7 +45,7 @@ func (p *Provider) Name() string {
return p.name
}
func (p *Provider) Value(ctx context.Context, path ...string) (config.Value, error) {
func (p *Provider) Value(_ context.Context, path ...string) (config.Value, error) {
name := p.prefix + p.key(path...)
if val, ok := os.LookupEnv(name); ok {
return value.JString(val), 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

@@ -2,8 +2,9 @@ package watcher
import (
"context"
"errors"
"fmt"
"log"
"log/slog"
"time"
"gitoa.ru/go-4devs/config"
@@ -14,13 +15,11 @@ var (
_ config.WatchProvider = (*Provider)(nil)
)
func New(duration time.Duration, provider config.NamedProvider, opts ...Option) *Provider {
func New(duration time.Duration, provider config.Provider, opts ...Option) *Provider {
prov := &Provider{
NamedProvider: provider,
ticker: time.NewTicker(duration),
logger: func(_ context.Context, msg string) {
log.Print(msg)
},
Provider: provider,
duration: duration,
logger: slog.ErrorContext,
}
for _, opt := range opts {
@@ -30,7 +29,7 @@ func New(duration time.Duration, provider config.NamedProvider, opts ...Option)
return prov
}
func WithLogger(l func(context.Context, string)) Option {
func WithLogger(l func(context.Context, string, ...any)) Option {
return func(p *Provider) {
p.logger = l
}
@@ -39,33 +38,43 @@ func WithLogger(l func(context.Context, string)) Option {
type Option func(*Provider)
type Provider struct {
config.NamedProvider
ticker *time.Ticker
logger func(context.Context, string)
config.Provider
duration time.Duration
logger func(context.Context, string, ...any)
}
func (p *Provider) Watch(ctx context.Context, callback config.WatchCallback, key ...string) error {
oldVar, err := p.NamedProvider.Value(ctx, key...)
old, err := p.Provider.Value(ctx, key...)
if err != nil {
return fmt.Errorf("failed watch variable: %w", err)
}
go func() {
go func(oldVar config.Value) {
ticker := time.NewTicker(p.duration)
defer func() {
ticker.Stop()
}()
for {
select {
case <-p.ticker.C:
newVar, err := p.NamedProvider.Value(ctx, key...)
case <-ticker.C:
newVar, err := p.Provider.Value(ctx, key...)
if err != nil {
p.logger(ctx, err.Error())
p.logger(ctx, "get value%v:%v", key, err.Error())
} else if !newVar.IsEquals(oldVar) {
callback(ctx, oldVar, newVar)
if err := callback(ctx, oldVar, newVar); err != nil {
if errors.Is(err, config.ErrStopWatch) {
return
}
p.logger(ctx, "callback %v:%v", key, err)
}
oldVar = newVar
}
case <-ctx.Done():
return
}
}
}()
}(old)
return nil
}

View File

@@ -2,18 +2,20 @@ package watcher_test
import (
"context"
"fmt"
"strconv"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/require"
"gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/provider/watcher"
"gitoa.ru/go-4devs/config/test/require"
"gitoa.ru/go-4devs/config/value"
)
var _ config.Provider = (*provider)(nil)
type provider struct {
cnt int32
}
@@ -25,13 +27,17 @@ func (p *provider) Name() string {
func (p *provider) Value(context.Context, ...string) (config.Value, error) {
p.cnt++
return value.JString(fmt.Sprint(p.cnt)), nil
return value.JString(strconv.Itoa(int(p.cnt))), nil
}
func TestWatcher(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
defer func() {
cancel()
}()
prov := &provider{}
w := watcher.New(time.Second, prov)
@@ -42,14 +48,20 @@ func TestWatcher(t *testing.T) {
err := w.Watch(
ctx,
func(ctx context.Context, oldVar, newVar config.Value) {
func(ctx context.Context, oldVar, newVar config.Value) error {
atomic.AddInt32(&cnt, 1)
wg.Done()
if atomic.LoadInt32(&cnt) == 2 {
return config.ErrStopWatch
}
return nil
},
"tmpname",
)
require.NoError(t, err)
wg.Wait()
require.NoError(t, err)
require.Equal(t, int32(2), cnt)
}

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

@@ -2,14 +2,12 @@ package test
import (
"context"
"io/ioutil"
"path/filepath"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/test/require"
)
const (
@@ -21,17 +19,15 @@ const (
func Run(t *testing.T, provider config.Provider, read []Read) {
t.Helper()
prov := &ProviderSuite{
provider: provider,
read: read,
}
suite.Run(t, prov)
}
ctx := context.Background()
type ProviderSuite struct {
suite.Suite
provider config.Provider
read []Read
for idx, read := range read {
t.Run(fmt.Sprintf("%v:%v", idx, read.Key), func(t *testing.T) {
val, err := provider.Value(ctx, read.Key...)
require.NoError(t, err, read.Key)
read.Assert(t, val)
})
}
}
type Read struct {
@@ -39,8 +35,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
@@ -112,7 +106,7 @@ func NewRead(expected interface{}, key ...string) Read {
val, err = v.ParseTime()
short = v.Time()
default:
require.Fail(t, "unexpected type", "type:%+T", expected)
require.Fail(t, "unexpected type:%+T", expected)
}
require.Equalf(t, val, short, "type:%T", expected)
@@ -121,22 +115,3 @@ func NewRead(expected interface{}, key ...string) Read {
},
}
}
func (ps *ProviderSuite) TestReadKeys() {
ctx := context.Background()
for _, read := range ps.read {
val, err := ps.provider.Value(ctx, read.Key...)
require.NoError(ps.T(), err, read.Key)
read.Assert(ps.T(), val)
}
}
func LoadConfig(t *testing.T, path string) []byte {
t.Helper()
file, err := ioutil.ReadFile(filepath.Clean(path))
require.NoError(t, err)
return file
}

28
test/require/equal.go Normal file
View File

@@ -0,0 +1,28 @@
package require
import (
"reflect"
"testing"
)
func Equal(t *testing.T, expected interface{}, actual interface{}, msgAndArgs ...interface{}) {
t.Helper()
if reflect.DeepEqual(expected, actual) {
return
}
t.Error(msgAndArgs...)
t.FailNow()
}
func Equalf(t *testing.T, expected interface{}, actual interface{}, msg string, args ...interface{}) {
t.Helper()
if reflect.DeepEqual(expected, actual) {
return
}
t.Errorf(msg, args...)
t.FailNow()
}

23
test/require/error.go Normal file
View File

@@ -0,0 +1,23 @@
package require
import (
"testing"
)
func NoError(t *testing.T, err error, msgAndArgs ...interface{}) {
t.Helper()
if err != nil {
t.Error(msgAndArgs...)
t.FailNow()
}
}
func NoErrorf(t *testing.T, err error, msg string, args ...interface{}) {
t.Helper()
if err != nil {
t.Errorf(msg, args...)
t.FailNow()
}
}

9
test/require/fail.go Normal file
View File

@@ -0,0 +1,9 @@
package require
import "testing"
func Fail(t *testing.T, msg string, args ...interface{}) {
t.Helper()
t.Errorf(msg, args...)
t.FailNow()
}

14
test/require/true.go Normal file
View File

@@ -0,0 +1,14 @@
package require
import (
"testing"
)
func Truef(t *testing.T, value bool, msg string, args ...interface{}) {
t.Helper()
if !value {
t.Errorf(msg, args...)
t.FailNow()
}
}

View File

@@ -8,7 +8,7 @@ type Value interface {
ReadValue
ParseValue
UnmarshalValue
IsEquals(Value) bool
IsEquals(in Value) bool
}
type UnmarshalValue interface {

View File

@@ -5,8 +5,8 @@ import (
"testing"
"time"
"github.com/stretchr/testify/require"
"gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/test/require"
"gitoa.ru/go-4devs/config/value"
)

View File

@@ -1,11 +0,0 @@
package config
type Variable struct {
Name string
Provider string
Value Value
}
func (v Variable) IsEquals(n Variable) bool {
return n.Name == v.Name && n.Provider == v.Provider && n.Value.String() == v.Value.String()
}