This commit is contained in:
66
provider/json/provider.go
Normal file
66
provider/json/provider.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package json
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/key"
|
||||
"gitoa.ru/go-4devs/config/value"
|
||||
)
|
||||
|
||||
var _ config.Provider = (*Provider)(nil)
|
||||
|
||||
func New(json []byte, opts ...Option) *Provider {
|
||||
provider := Provider{
|
||||
key: key.Name,
|
||||
data: json,
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(&provider)
|
||||
}
|
||||
|
||||
return &provider
|
||||
}
|
||||
|
||||
func NewFile(path string, opts ...Option) (*Provider, error) {
|
||||
file, err := ioutil.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), nil
|
||||
}
|
||||
|
||||
type Option func(*Provider)
|
||||
|
||||
type Provider struct {
|
||||
data []byte
|
||||
key config.KeyFactory
|
||||
}
|
||||
|
||||
func (p *Provider) IsSupport(ctx context.Context, key config.Key) bool {
|
||||
return p.key(ctx, key) != ""
|
||||
}
|
||||
|
||||
func (p *Provider) Name() string {
|
||||
return "json"
|
||||
}
|
||||
|
||||
func (p *Provider) Read(ctx context.Context, key config.Key) (config.Variable, error) {
|
||||
path := p.key(ctx, key)
|
||||
|
||||
if val := gjson.GetBytes(p.data, path); val.Exists() {
|
||||
return config.Variable{
|
||||
Name: path,
|
||||
Provider: p.Name(),
|
||||
Value: value.JString(val.String()),
|
||||
}, nil
|
||||
}
|
||||
|
||||
return config.Variable{}, config.ErrVariableNotFound
|
||||
}
|
||||
27
provider/json/provider_test.go
Normal file
27
provider/json/provider_test.go
Normal file
@@ -0,0 +1,27 @@
|
||||
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("app.name.title", "config title"),
|
||||
test.NewRead("app.name.timeout", time.Minute),
|
||||
test.NewReadUnmarshal("app.name.var", &[]string{"name"}, &sl),
|
||||
test.NewReadConfig("cfg"),
|
||||
test.NewRead("app.name.success", true),
|
||||
}
|
||||
|
||||
test.Run(t, prov, read)
|
||||
}
|
||||
Reference in New Issue
Block a user