20 Commits

Author SHA1 Message Date
3aa8a30f3f Merge pull request 'update pos argument' (#25) from arg into master
All checks were successful
Go Action / goaction (push) Successful in 51s
Reviewed-on: #25
2025-12-27 19:31:02 +03:00
1fb591bb22 update pos argument
All checks were successful
Go Action / goaction (pull_request) Successful in 52s
2025-12-27 19:29:30 +03:00
4547adab23 Merge pull request 'update dasel provider' (#24) from dasel into master
All checks were successful
Go Action / goaction (push) Successful in 45s
Reviewed-on: #24
2025-12-27 16:31:34 +03:00
7ca860c127 update dasel provider
All checks were successful
Go Action / goaction (pull_request) Successful in 38s
2025-12-27 16:30:24 +03:00
b6aa83b53e Merge pull request 'add dasel provider' (#23) from dasel into master
All checks were successful
Go Action / goaction (push) Successful in 53s
Reviewed-on: #23
2025-12-27 16:12:16 +03:00
e5637b2a49 add dasel provider
All checks were successful
Go Action / goaction (pull_request) Successful in 58s
2025-12-27 16:10:10 +03:00
1be25e67a3 Merge pull request 'update duration jbytes' (#22) from jbytes into master
All checks were successful
Go Action / goaction (push) Successful in 46s
Reviewed-on: #22
2025-12-27 15:05:58 +03:00
c81d6ee010 update duration jbytes
All checks were successful
Go Action / goaction (pull_request) Successful in 58s
2025-12-27 15:04:07 +03:00
5e0e1f5c65 Merge pull request 'update yaml provider' (#21) from yaml into master
All checks were successful
Go Action / goaction (push) Successful in 33s
Reviewed-on: #21
2025-12-27 12:12:22 +03:00
3cdadd465b update yaml provider
All checks were successful
Go Action / goaction (pull_request) Successful in 40s
2025-12-27 12:10:58 +03:00
be629c70f1 Merge pull request 'update workflows' (#20) from workflow into master
All checks were successful
Go Action / goaction (push) Successful in 32s
Reviewed-on: #20
2025-12-27 11:53:55 +03:00
bf901485ce add ignore change workflow provider
All checks were successful
Go Action / goaction (pull_request) Successful in 31s
2025-12-27 11:53:12 +03:00
7990d1b4ff update workflows
All checks were successful
Go Action / goaction (pull_request) Successful in 33s
2025-12-27 11:48:55 +03:00
d5250f2c4e Merge pull request 'update provider vault' (#19) from vault into master
All checks were successful
Go Action / goaction (push) Successful in 36s
Reviewed-on: #19
2025-12-27 11:47:01 +03:00
3fc7f6259b update provider vault
All checks were successful
Go Action / goaction (push) Successful in 29s
Go Action / goaction (pull_request) Successful in 1m21s
2025-12-27 11:38:43 +03:00
de35df477b Merge pull request 'update toml' (#18) from toml into master
All checks were successful
Go Action / goaction (push) Successful in 29s
Reviewed-on: #18
2025-12-27 11:25:57 +03:00
0e7f303c2d update toml workflows
All checks were successful
Go Action / goaction (push) Successful in 26s
Go Action / goaction (pull_request) Successful in 35s
2025-12-27 11:21:52 +03:00
90f6f65c5f update toml
All checks were successful
Go Action / goaction (push) Successful in 28s
Go Action / goaction (pull_request) Successful in 26s
2025-12-27 10:50:32 +03:00
cc252cc858 Merge pull request 'update test' (#17) from toml into master
All checks were successful
Go Action / goaction (push) Successful in 28s
Reviewed-on: #17
2025-12-27 10:44:11 +03:00
1e0f4490ba update test
All checks were successful
Go Action / goaction (push) Successful in 27s
Go Action / goaction (pull_request) Successful in 28s
2025-12-27 10:43:31 +03:00
32 changed files with 661 additions and 179 deletions

View File

@@ -0,0 +1,38 @@
name: Go Action
on:
push:
branches:
- master
paths:
- 'provider/dasel/**'
- '.gitea/workflows/dasel.yml'
pull_request:
paths:
- 'provider/dasel/**'
- '.gitea/workflows/dasel.yml'
jobs:
goaction:
runs-on: ubuntu-latest # Use a Gitea Actions runner label
steps:
- name: Check out repository code
uses: actions/checkout@v4 # Action to clone the repo
- name: Set up Go
uses: actions/setup-go@v5 # Action to install a specific Go version
with:
go-version: '1.25.5' # Specify your required Go version
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v8 # Use the golangci-lint action
with:
version: v2.7.2 # Specify the linter version
# Optional: additional arguments
args: --verbose
working-directory: ./provider/dasel
- name: Run go test
run: go test ./...
working-directory: ./provider/dasel

View File

@@ -1,6 +1,16 @@
name: Go Action
on: [push, pull_request]
on:
push:
branches:
- master
paths:
- 'provider/etcd/**'
- '.gitea/workflows/etcd.yml'
pull_request:
paths:
- 'provider/etcd/**'
- '.gitea/workflows/etcd.yml'
jobs:
goaction:

View File

@@ -1,6 +1,27 @@
name: Go Action
on: [push, pull_request]
on:
push:
branches:
- master
pull_request:
paths-ignore:
- 'provider/etcd/**'
- '.gitea/workflows/etcd.yml'
- 'provider/ini/**'
- '.gitea/workflows/ini.yml'
- 'provider/json/**'
- '.gitea/workflows/json.yml'
- 'provider/toml/**'
- '.gitea/workflows/toml.yml'
- 'provider/vault/**'
- '.gitea/workflows/vault.yml'
- 'provider/yaml/**'
- '.gitea/workflows/yaml.yml'
- 'provider/dasel/**'
- '.gitea/workflows/dasel.yml'
jobs:
goaction:

View File

@@ -1,6 +1,16 @@
name: Go Action
on: [push, pull_request]
on:
push:
branches:
- master
paths:
- 'provider/ini/**'
- '.gitea/workflows/ini.yml'
pull_request:
paths:
- 'provider/ini/**'
- '.gitea/workflows/ini.yml'
jobs:
goaction:

View File

@@ -1,6 +1,16 @@
name: Go Action
on: [push, pull_request]
on:
push:
branches:
- master
paths:
- 'provider/json/**'
- '.gitea/workflows/json.yml'
pull_request:
paths:
- 'provider/json/**'
- '.gitea/workflows/json.yml'
jobs:
goaction:

38
.gitea/workflows/toml.yml Normal file
View File

@@ -0,0 +1,38 @@
name: Go Action
on:
push:
branches:
- master
paths:
- 'provider/toml/**'
- '.gitea/workflows/toml.yml'
pull_request:
paths:
- 'provider/toml/**'
- '.gitea/workflows/toml.yml'
jobs:
goaction:
runs-on: ubuntu-latest # Use a Gitea Actions runner label
steps:
- name: Check out repository code
uses: actions/checkout@v4 # Action to clone the repo
- name: Set up Go
uses: actions/setup-go@v5 # Action to install a specific Go version
with:
go-version: '1.25.5' # Specify your required Go version
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v8 # Use the golangci-lint action
with:
version: v2.7.2 # Specify the linter version
# Optional: additional arguments
args: --verbose
working-directory: ./provider/toml
- name: Run go test
run: go test ./...
working-directory: ./provider/toml

View File

@@ -0,0 +1,46 @@
name: Go Action
on:
push:
branches:
- master
paths:
- 'provider/vault/**'
- '.gitea/workflows/vault.yml'
pull_request:
paths:
- 'provider/vault/**'
- '.gitea/workflows/vault.yml'
jobs:
goaction:
services:
vault-server:
image: vault:1.13.3
env:
VAULT_DEV_ROOT_TOKEN_ID: "dev"
env:
VAULT_DEV_LISTEN_ADDRESS: 'http://vault-server:8200'
runs-on: ubuntu-latest # Use a Gitea Actions runner label
steps:
- name: Check out repository code
uses: actions/checkout@v4 # Action to clone the repo
- name: Set up Go
uses: actions/setup-go@v5 # Action to install a specific Go version
with:
go-version: '1.25.5' # Specify your required Go version
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v8 # Use the golangci-lint action
with:
version: v2.7.2 # Specify the linter version
# Optional: additional arguments
args: --verbose
working-directory: ./provider/vault
- name: Run go test
run: go test ./...
working-directory: ./provider/vault

38
.gitea/workflows/yaml.yml Normal file
View File

@@ -0,0 +1,38 @@
name: Go Action
on:
push:
branches:
- master
paths:
- 'provider/yaml/**'
- '.gitea/workflows/yaml.yml'
pull_request:
paths:
- 'provider/yaml/**'
- '.gitea/workflows/yaml.yml'
jobs:
goaction:
runs-on: ubuntu-latest # Use a Gitea Actions runner label
steps:
- name: Check out repository code
uses: actions/checkout@v4 # Action to clone the repo
- name: Set up Go
uses: actions/setup-go@v5 # Action to install a specific Go version
with:
go-version: '1.25.5' # Specify your required Go version
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v8 # Use the golangci-lint action
with:
version: v2.7.2 # Specify the linter version
# Optional: additional arguments
args: --verbose
working-directory: ./provider/yaml
- name: Run go test
run: go test ./...
working-directory: ./provider/yaml

View File

@@ -16,7 +16,7 @@ const (
var argNum uint64
func Argument(v param.Params) param.Params {
return param.With(v, paramArgument, atomic.AddUint64(&argNum, 1))
return param.With(v, paramArgument, atomic.AddUint64(&argNum, 1)-1)
}
func ParamArgument(fn param.Params) (uint64, bool) {

View File

@@ -70,7 +70,7 @@ func (i *Argv) Value(ctx context.Context, key ...string) (config.Value, error) {
data, err := i.Map.Value(ctx, key...)
if err != nil {
return nil, fmt.Errorf("%w", err)
return nil, fmt.Errorf("map: %w", err)
}
return data, nil
@@ -101,12 +101,12 @@ func (i *Argv) Bind(ctx context.Context, def config.Variables) error {
}
if err != nil {
return fmt.Errorf("%w", err)
return fmt.Errorf("arg bind:%w", err)
}
}
if err := i.Map.Bind(ctx, def); err != nil {
return fmt.Errorf("%w", err)
return fmt.Errorf("arg map:%w", err)
}
return nil

21
provider/dasel/go.mod Normal file
View File

@@ -0,0 +1,21 @@
module gitoa.ru/go-4devs/config/provider/dasel
go 1.25.5
require (
github.com/tomwright/dasel/v3 v3.2.0
gitoa.ru/go-4devs/config v0.0.6
)
require (
github.com/agext/levenshtein v1.2.1 // indirect
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/google/go-cmp v0.7.0 // indirect
github.com/hashicorp/hcl/v2 v2.24.0 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/zclconf/go-cty v1.17.0 // indirect
golang.org/x/mod v0.26.0 // indirect
golang.org/x/sync v0.16.0 // indirect
golang.org/x/text v0.28.0 // indirect
golang.org/x/tools v0.35.0 // indirect
)

30
provider/dasel/go.sum Normal file
View File

@@ -0,0 +1,30 @@
github.com/agext/levenshtein v1.2.1 h1:QmvMAjj2aEICytGiWzmxoE0x2KZvE0fvmqMOfy2tjT8=
github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
github.com/apparentlymart/go-textseg/v15 v15.0.0 h1:uYvfpb3DyLSCGWnctWKGj857c6ew1u1fNQOlOtuGxQY=
github.com/apparentlymart/go-textseg/v15 v15.0.0/go.mod h1:K8XmNZdhEBkdlyDdvbmmsvpAG721bKi0joRfFdHIWJ4=
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/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68=
github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/hashicorp/hcl/v2 v2.24.0 h1:2QJdZ454DSsYGoaE6QheQZjtKZSUs9Nh2izTWiwQxvE=
github.com/hashicorp/hcl/v2 v2.24.0/go.mod h1:oGoO1FIQYfn/AgyOhlg9qLC6/nOJPX3qGbkZpYAcqfM=
github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0=
github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0=
github.com/tomwright/dasel/v3 v3.2.0 h1:wyF2A4jVADx10E0kjbzPTiaF26D0OkK2OPuQRPJRDCo=
github.com/tomwright/dasel/v3 v3.2.0/go.mod h1:XyAl6LidZuWOISIeUmKlCqJDz4IWEDp83epNngZgOQA=
github.com/zclconf/go-cty v1.17.0 h1:seZvECve6XX4tmnvRzWtJNHdscMtYEx5R7bnnVyd/d0=
github.com/zclconf/go-cty v1.17.0/go.mod h1:wqFzcImaLTI6A5HfsRwB0nj5n0MRZFwmey8YoFPPs3U=
github.com/zclconf/go-cty-debug v0.0.0-20240509010212-0d6042c53940 h1:4r45xpDWB6ZMSMNJFMOjqrGHynW3DIBuR2H9j0ug+Mo=
github.com/zclconf/go-cty-debug v0.0.0-20240509010212-0d6042c53940/go.mod h1:CmBdvvj3nqzfzJ6nTCIwDTPZ56aVGvDrmztiO5g3qrM=
gitoa.ru/go-4devs/config v0.0.6 h1:BbOH2KHBRMWtSwcFyHKzMXzxYbJpSwZpA8LGnnzBwk8=
gitoa.ru/go-4devs/config v0.0.6/go.mod h1:UINWnObZA0nLiJro+TtavUBBvN0cSt17aRHOk20pP74=
golang.org/x/mod v0.26.0 h1:EGMPT//Ezu+ylkCijjPc+f4Aih7sZvaAr+O3EHBxvZg=
golang.org/x/mod v0.26.0/go.mod h1:/j6NAhSk8iQ723BGAUyoAcn7SlD7s15Dp9Nd/SfeaFQ=
golang.org/x/sync v0.16.0 h1:ycBJEhp9p4vXvUZNszeOq0kGTPghopOL8q0fq3vstxw=
golang.org/x/sync v0.16.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
golang.org/x/text v0.28.0 h1:rhazDwis8INMIwQ4tpjLDzUhx6RlXqZNPEM0huQojng=
golang.org/x/text v0.28.0/go.mod h1:U8nCwOR8jO/marOQ0QbDiOngZVEBB7MAiitBuMjXiNU=
golang.org/x/tools v0.35.0 h1:mBffYraMEf7aa0sB+NuKnuCy8qI/9Bughn8dC2Gu5r0=
golang.org/x/tools v0.35.0/go.mod h1:NKdj5HkL/73byiZSJjqJgKn3ep7KjFkBOkR/Hps3VPw=

View File

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

View File

@@ -0,0 +1,13 @@
package hcl
import (
"github.com/tomwright/dasel/v3/parsing/hcl"
"gitoa.ru/go-4devs/config/provider/dasel"
)
const Name = "dasel:hcl"
//nolint:wrapcheck
func New(data []byte) (dasel.Provider, error) {
return dasel.New(data, hcl.HCL, dasel.WithName(Name))
}

View File

@@ -0,0 +1,35 @@
package hcl_test
import (
"embed"
"testing"
"time"
dhcl "gitoa.ru/go-4devs/config/provider/dasel/hcl"
"gitoa.ru/go-4devs/config/test"
"gitoa.ru/go-4devs/config/test/require"
)
//go:embed fixture/*
var fixture embed.FS
func TestProvider(t *testing.T) {
t.Parallel()
js, err := fixture.ReadFile("fixture/config.hcl")
require.NoError(t, err)
prov, derr := dhcl.New(js)
require.NoError(t, derr)
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

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

View File

@@ -0,0 +1,13 @@
package json //nolint:revive
import (
"github.com/tomwright/dasel/v3/parsing/json"
"gitoa.ru/go-4devs/config/provider/dasel"
)
const Name = "dasel:json"
//nolint:wrapcheck
func New(data []byte) (dasel.Provider, error) {
return dasel.New(data, json.JSON, dasel.WithName(Name))
}

View File

@@ -0,0 +1,35 @@
package json_test
import (
"embed"
"testing"
"time"
djson "gitoa.ru/go-4devs/config/provider/dasel/json"
"gitoa.ru/go-4devs/config/test"
"gitoa.ru/go-4devs/config/test/require"
)
//go:embed fixture/*
var fixture embed.FS
func TestProvider(t *testing.T) {
t.Parallel()
js, err := fixture.ReadFile("fixture/config.json")
require.NoError(t, err)
prov, derr := djson.New(js)
require.NoError(t, derr)
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

@@ -0,0 +1,91 @@
package dasel
import (
"context"
"encoding/json"
"fmt"
"strings"
dasel "github.com/tomwright/dasel/v3"
"github.com/tomwright/dasel/v3/model"
"github.com/tomwright/dasel/v3/parsing"
"gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/value"
)
var _ config.Provider = Provider{} //nolint:exhaustruct
const Name = "dasel"
type Option func(*Provider)
func WithName(in string) Option {
return func(p *Provider) {
p.name = in
}
}
func New(in []byte, format parsing.Format, opts ...Option) (Provider, error) {
reader, err := format.NewReader(parsing.DefaultReaderOptions())
if err != nil {
return Provider{}, fmt.Errorf("%w:%w", config.ErrInitFactory, err)
}
data, verr := reader.Read(in)
if verr != nil {
return Provider{}, fmt.Errorf("%w:%w", config.ErrInitFactory, verr)
}
prov := Provider{
data: data,
key: func(path ...string) string {
return strings.Join(path, ".")
},
name: Name,
}
for _, opt := range opts {
opt(&prov)
}
return prov, nil
}
type Provider struct {
data *model.Value
key func(path ...string) string
name string
}
func (p Provider) Value(ctx context.Context, path ...string) (config.Value, error) {
selector := p.key(path...)
data, cnt, err := dasel.Query(ctx, p.data, selector)
if err != nil {
return nil, fmt.Errorf("query: %w:%w", config.ErrInvalidValue, err)
}
if cnt > 1 {
return nil, fmt.Errorf("count: %v:%w", cnt, config.ErrToManyArgs)
}
if cnt == 0 {
return value.EmptyValue(), nil
}
val, verr := data[0].GoValue()
if verr != nil {
return nil, fmt.Errorf("go value: %w:%w", config.ErrInvalidValue, verr)
}
res, merr := json.Marshal(val)
if merr != nil {
return nil, fmt.Errorf("marshal: %w:%w", config.ErrInvalidValue, merr)
}
return value.JBytes(res), nil
}
func (p Provider) Name() string {
return p.name
}

View File

@@ -1,8 +1,8 @@
module gitoa.ru/go-4devs/config/provider/toml
go 1.22
go 1.23
require (
github.com/pelletier/go-toml v1.9.5
gitoa.ru/go-4devs/config v0.0.1
github.com/pelletier/go-toml/v2 v2.2.4
gitoa.ru/go-4devs/config v0.0.5
)

View File

@@ -1,4 +1,6 @@
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
gitoa.ru/go-4devs/config v0.0.1 h1:9KrOO09YbIMO8qL8aVn/G74DurGdOIW5y3O02bays4I=
gitoa.ru/go-4devs/config v0.0.1/go.mod h1:xfEC2Al9xnMLJUuekYs3KhJ5BIzWAseNwkMwbN6/xss=
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
gitoa.ru/go-4devs/config v0.0.4 h1:mTD0w34PcmVHWaOr/4+zkhYSvcuHiJaBa/bJBuUHcLI=
gitoa.ru/go-4devs/config v0.0.4/go.mod h1:UINWnObZA0nLiJro+TtavUBBvN0cSt17aRHOk20pP74=
gitoa.ru/go-4devs/config v0.0.5 h1:9GnQ1G6ebpkXe1tAeb3va/GIx7eBrMuJBYra47OXl+w=
gitoa.ru/go-4devs/config v0.0.5/go.mod h1:UINWnObZA0nLiJro+TtavUBBvN0cSt17aRHOk20pP74=

View File

@@ -2,10 +2,10 @@ package toml
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/pelletier/go-toml"
toml "github.com/pelletier/go-toml/v2"
"gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/value"
)
@@ -17,23 +17,22 @@ const (
var _ config.Provider = (*Provider)(nil)
func NewFile(file string, opts ...Option) (*Provider, error) {
tree, err := toml.LoadFile(file)
if err != nil {
return nil, fmt.Errorf("toml: failed load file: %w", err)
}
return configure(tree, opts...), nil
}
type Option func(*Provider)
func configure(tree *toml.Tree, opts ...Option) *Provider {
func WithName(in string) Option {
return func(p *Provider) {
p.name = in
}
}
func New(in []byte, opts ...Option) (*Provider, error) {
var data Data
if err := toml.Unmarshal(in, &data); err != nil {
return nil, fmt.Errorf("toml failed load data: %w", err)
}
prov := &Provider{
tree: tree,
key: func(s []string) string {
return strings.Join(s, Separator)
},
data: data,
name: Name,
}
@@ -41,21 +40,11 @@ func configure(tree *toml.Tree, opts ...Option) *Provider {
opt(prov)
}
return prov
}
func New(data []byte, opts ...Option) (*Provider, error) {
tree, err := toml.LoadBytes(data)
if err != nil {
return nil, fmt.Errorf("toml failed load data: %w", err)
}
return configure(tree, opts...), nil
return prov, nil
}
type Provider struct {
tree *toml.Tree
key func([]string) string
data Data
name string
}
@@ -64,9 +53,39 @@ func (p *Provider) Name() string {
}
func (p *Provider) Value(_ context.Context, path ...string) (config.Value, error) {
if k := p.key(path); p.tree.Has(k) {
return Value{Value: value.Value{Val: p.tree.Get(k)}}, nil
val, err := p.data.Value(path...)
if err != nil {
return nil, fmt.Errorf("%w", err)
}
return nil, config.ErrValueNotFound
data, merr := json.Marshal(val)
if merr != nil {
return nil, fmt.Errorf("toml:%w", merr)
}
return value.JBytes(data), nil
}
type Data map[string]any
func (d Data) Value(path ...string) (any, error) {
if len(path) == 1 {
val, ok := d[path[0]]
if !ok {
return "", config.ErrValueNotFound
}
return val, nil
}
key, path := path[0], path[1:]
val, ok := d[key]
if !ok {
return nil, config.ErrValueNotFound
}
data, _ := val.(map[string]any)
return Data(data).Value(path...)
}

View File

@@ -4,6 +4,7 @@ import (
"embed"
"testing"
"gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/provider/toml"
"gitoa.ru/go-4devs/config/test"
"gitoa.ru/go-4devs/config/test/require"
@@ -24,12 +25,14 @@ func TestProvider(t *testing.T) {
m := []int{}
read := []test.Read{
test.NewRead("192.168.1.1", "database.server"),
test.NewRead("192.168.1.1", "database", "server"),
test.NewRead("TOML Example", "title"),
test.NewRead("10.0.0.1", "servers.alpha.ip"),
test.NewRead(true, "database.enabled"),
test.NewRead(5000, "database.connection_max"),
test.NewRead("10.0.0.1", "servers", "alpha", "ip"),
test.NewRead(true, "database", "enabled"),
test.NewRead(5000, "database", "connection_max"),
test.NewReadUnmarshal(&[]int{8001, 8001, 8002}, &m, "database", "ports"),
test.NewErrorIs(config.ErrValueNotFound, "typo"),
test.NewErrorIs(config.ErrValueNotFound, "database.server"),
}
test.Run(t, prov, read)

View File

@@ -1,41 +0,0 @@
package toml
import (
"encoding/json"
"fmt"
"gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/value"
)
type Value struct {
value.Value
}
func (s Value) Int() int {
v, _ := s.ParseInt()
return v
}
func (s Value) ParseInt() (int, error) {
v, err := s.ParseInt64()
if err != nil {
return 0, fmt.Errorf("toml failed parce int: %w", err)
}
return int(v), nil
}
func (s Value) Unmarshal(target any) error {
b, err := json.Marshal(s.Raw())
if err != nil {
return fmt.Errorf("%w: %w", config.ErrInvalidValue, err)
}
if err := json.Unmarshal(b, target); err != nil {
return fmt.Errorf("%w: %w", config.ErrInvalidValue, err)
}
return nil
}

View File

@@ -1,29 +1,29 @@
module gitoa.ru/go-4devs/config/provider/vault
go 1.22
go 1.23.0
require (
github.com/hashicorp/vault/api v1.11.0
gitoa.ru/go-4devs/config v0.0.2
github.com/hashicorp/vault/api v1.22.0
gitoa.ru/go-4devs/config v0.0.5
)
require (
github.com/cenkalti/backoff/v3 v3.0.0 // indirect
github.com/go-jose/go-jose/v3 v3.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/go-jose/go-jose/v4 v4.1.1 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-retryablehttp v0.6.6 // indirect
github.com/hashicorp/go-retryablehttp v0.7.8 // indirect
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
github.com/hashicorp/go-secure-stdlib/parseutil v0.1.6 // indirect
github.com/hashicorp/go-secure-stdlib/parseutil v0.2.0 // indirect
github.com/hashicorp/go-secure-stdlib/strutil v0.1.2 // indirect
github.com/hashicorp/go-sockaddr v1.0.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/go-sockaddr v1.0.7 // indirect
github.com/hashicorp/hcl v1.0.1-vault-7 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/ryanuber/go-glob v1.0.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1 // indirect
golang.org/x/crypto v0.40.0 // indirect
golang.org/x/net v0.42.0 // indirect
golang.org/x/text v0.27.0 // indirect
golang.org/x/time v0.12.0 // indirect
)

View File

@@ -1,92 +1,61 @@
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/cenkalti/backoff/v3 v3.0.0 h1:ske+9nBpD9qZsTBoF41nW5L+AIuFBKMeze18XQ3eG1c=
github.com/cenkalti/backoff/v3 v3.0.0/go.mod h1:cIeZDE3IrqwwJl6VUwCN6trj1oXrTS4rc0ij+ULvLYs=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
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/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/go-jose/go-jose/v3 v3.0.1 h1:pWmKFVtt+Jl0vBZTIpz/eAKwsm6LkIxDVVbFHKkchhA=
github.com/go-jose/go-jose/v3 v3.0.1/go.mod h1:RNkWWRld676jZEYoV3+XK8L2ZnNSvIsxFMht0mSX+u8=
github.com/go-test/deep v1.0.2 h1:onZX1rnHT3Wv6cqNgYyFOOlgVKJrksuCMCRvJStbMYw=
github.com/go-test/deep v1.0.2/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
github.com/go-jose/go-jose/v4 v4.1.1 h1:JYhSgy4mXXzAdF3nUx3ygx347LRXJRrpgyU3adRmkAI=
github.com/go-jose/go-jose/v4 v4.1.1/go.mod h1:BdsZGqgdO3b6tTc6LSE56wcDbMMLuPsw5d4ZD5f94kA=
github.com/go-test/deep v1.1.1 h1:0r/53hagsehfO4bzD2Pgr/+RgHqhmf+k1Bpse2cTu1U=
github.com/go-test/deep v1.1.1/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
github.com/hashicorp/go-hclog v0.16.2 h1:K4ev2ib4LdQETX5cSZBG0DVLk1jwGqSPXBjdah3veNs=
github.com/hashicorp/go-hclog v0.16.2/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ=
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
github.com/hashicorp/go-hclog v1.6.3 h1:Qr2kF+eVWjTiYmU7Y31tYlP1h0q/X3Nl3tPGdaB11/k=
github.com/hashicorp/go-hclog v1.6.3/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/hashicorp/go-retryablehttp v0.6.6 h1:HJunrbHTDDbBb/ay4kxa1n+dLmttUlnP3V9oNE4hmsM=
github.com/hashicorp/go-retryablehttp v0.6.6/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
github.com/hashicorp/go-retryablehttp v0.7.8 h1:ylXZWnqa7Lhqpk0L1P1LzDtGcCR0rPVUrx/c8Unxc48=
github.com/hashicorp/go-retryablehttp v0.7.8/go.mod h1:rjiScheydd+CxvumBsIrFKlx3iS0jrZ7LvzFGFmuKbw=
github.com/hashicorp/go-rootcerts v1.0.2 h1:jzhAVGtqPKbwpyCPELlgNWhE1znq+qwJtW5Oi2viEzc=
github.com/hashicorp/go-rootcerts v1.0.2/go.mod h1:pqUvnprVnM5bf7AOirdbb01K4ccR319Vf4pU3K5EGc8=
github.com/hashicorp/go-secure-stdlib/parseutil v0.1.6 h1:om4Al8Oy7kCm/B86rLCLah4Dt5Aa0Fr5rYBG60OzwHQ=
github.com/hashicorp/go-secure-stdlib/parseutil v0.1.6/go.mod h1:QmrqtbKuxxSWTN3ETMPuB+VtEiBJ/A9XhoYGv8E1uD8=
github.com/hashicorp/go-secure-stdlib/strutil v0.1.1/go.mod h1:gKOamz3EwoIoJq7mlMIRBpVTAUn8qPCrEclOKKWhD3U=
github.com/hashicorp/go-secure-stdlib/parseutil v0.2.0 h1:U+kC2dOhMFQctRfhK0gRctKAPTloZdMU5ZJxaesJ/VM=
github.com/hashicorp/go-secure-stdlib/parseutil v0.2.0/go.mod h1:Ll013mhdmsVDuoIXVfBtvgGJsXDYkTw1kooNcoCXuE0=
github.com/hashicorp/go-secure-stdlib/strutil v0.1.2 h1:kes8mmyCpxJsI7FTwtzRqEy9CdjCtrXrXGuOpxEA7Ts=
github.com/hashicorp/go-secure-stdlib/strutil v0.1.2/go.mod h1:Gou2R9+il93BqX25LAKCLuM+y9U2T4hlwvT1yprcna4=
github.com/hashicorp/go-sockaddr v1.0.2 h1:ztczhD1jLxIRjVejw8gFomI1BQZOe2WoVOu0SyteCQc=
github.com/hashicorp/go-sockaddr v1.0.2/go.mod h1:rB4wwRAUzs07qva3c5SdrY/NEtAUjGlgmH/UkBUC97A=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hashicorp/vault/api v1.11.0 h1:AChWByeHf4/P9sX3Y1B7vFsQhZO2BgQiCMQ2SA1P1UY=
github.com/hashicorp/vault/api v1.11.0/go.mod h1:si+lJCYO7oGkIoNPAN8j3azBLTn9SjMGS+jFaHd1Cck=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-colorable v0.1.6 h1:6Su7aK7lXmJ/U79bYtBjLNaha4Fs1Rg9plHpcH+vvnE=
github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
github.com/hashicorp/go-sockaddr v1.0.7 h1:G+pTkSO01HpR5qCxg7lxfsFEZaG+C0VssTy/9dbT+Fw=
github.com/hashicorp/go-sockaddr v1.0.7/go.mod h1:FZQbEYa1pxkQ7WLpyXJ6cbjpT8q0YgQaK/JakXqGyWw=
github.com/hashicorp/hcl v1.0.1-vault-7 h1:ag5OxFVy3QYTFTJODRzTKVZ6xvdfLLCA1cy/Y6xGI0I=
github.com/hashicorp/hcl v1.0.1-vault-7/go.mod h1:XYhtn6ijBSAj6n4YqAaf7RBPS4I06AItNorpy+MoQNM=
github.com/hashicorp/vault/api v1.22.0 h1:+HYFquE35/B74fHoIeXlZIP2YADVboaPjaSicHEZiH0=
github.com/hashicorp/vault/api v1.22.0/go.mod h1:IUZA2cDvr4Ok3+NtK2Oq/r+lJeXkeCrHRmqdyWfpmGM=
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
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/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/ryanuber/go-glob v1.0.0 h1:iQh3xXAumdQ+4Ufa5b25cRpC5TYKlno6hsv6Cb3pkBk=
github.com/ryanuber/go-glob v1.0.0/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIHxIXzX/Yc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gitoa.ru/go-4devs/config v0.0.2 h1:bkTxW57kDDMf4cj/8W7fxPSN7JCPWEqlhCmL6LP3Vzg=
gitoa.ru/go-4devs/config v0.0.2/go.mod h1:xfEC2Al9xnMLJUuekYs3KhJ5BIzWAseNwkMwbN6/xss=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1 h1:NusfzzA6yGQ+ua51ck7E3omNUX/JuqbFSaRGqU8CcLI=
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gitoa.ru/go-4devs/config v0.0.5 h1:9GnQ1G6ebpkXe1tAeb3va/GIx7eBrMuJBYra47OXl+w=
gitoa.ru/go-4devs/config v0.0.5/go.mod h1:UINWnObZA0nLiJro+TtavUBBvN0cSt17aRHOk20pP74=
golang.org/x/crypto v0.40.0 h1:r4x+VvoG5Fm+eJcxMaY8CQM7Lb0l1lsmjGBQ6s8BfKM=
golang.org/x/crypto v0.40.0/go.mod h1:Qr1vMER5WyS2dfPHAlsOj01wgLbsyWtFn/aY+5+ZdxY=
golang.org/x/net v0.42.0 h1:jzkYrhi3YQWD6MLBJcsklgQsoAcw89EcZbJw8Z614hs=
golang.org/x/net v0.42.0/go.mod h1:FF1RA5d3u7nAYA4z2TkclSCKh68eSXtiFwcWQpPXdt8=
golang.org/x/sys v0.34.0 h1:H5Y5sJ2L2JRdyv7ROF1he/lPdvFsd0mJHFw2ThKHxLA=
golang.org/x/sys v0.34.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/text v0.27.0 h1:4fGWRpyh641NLlecmyl4LOe6yDdfaYNrGb2zdfo4JV4=
golang.org/x/text v0.27.0/go.mod h1:1D28KMCvyooCX9hBiosv5Tz/+YLxj0j7XhWjpSUF7CU=
golang.org/x/time v0.12.0 h1:ScB/8o8olJvc+CQPWrK3fPZNfh7qgwCrY0zJmoEQLSE=
golang.org/x/time v0.12.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@@ -33,7 +33,7 @@ func (p *provider) Value(context.Context, ...string) (config.Value, error) {
func TestWatcher(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer func() {
cancel()
@@ -41,7 +41,7 @@ func TestWatcher(t *testing.T) {
prov := &provider{}
w := watcher.New(time.Second, prov)
w := watcher.New(time.Second/3, prov)
wg := sync.WaitGroup{}
wg.Add(2)

View File

@@ -1,8 +1,8 @@
module gitoa.ru/go-4devs/config/provider/yaml
go 1.22
go 1.23
require (
gitoa.ru/go-4devs/config v0.0.1
gitoa.ru/go-4devs/config v0.0.5
gopkg.in/yaml.v3 v3.0.1
)

View File

@@ -1,5 +1,7 @@
gitoa.ru/go-4devs/config v0.0.1 h1:9KrOO09YbIMO8qL8aVn/G74DurGdOIW5y3O02bays4I=
gitoa.ru/go-4devs/config v0.0.1/go.mod h1:xfEC2Al9xnMLJUuekYs3KhJ5BIzWAseNwkMwbN6/xss=
gitoa.ru/go-4devs/config v0.0.5 h1:9GnQ1G6ebpkXe1tAeb3va/GIx7eBrMuJBYra47OXl+w=
gitoa.ru/go-4devs/config v0.0.5/go.mod h1:UINWnObZA0nLiJro+TtavUBBvN0cSt17aRHOk20pP74=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

View File

@@ -134,7 +134,7 @@ func NewErrorIs(exErr error, path ...string) Read {
Assert: func(*testing.T, config.Value) {},
Error: func(t *testing.T, err error) {
t.Helper()
require.ErrorIsf(t, exErr, err, "except err %v != %v", exErr, err)
require.ErrorIs(t, err, exErr)
},
}
}

View File

@@ -1,6 +1,8 @@
package value
import (
"errors"
"fmt"
"time"
"gitoa.ru/go-4devs/config"
@@ -45,7 +47,17 @@ func (s JBytes) ParseBool() (bool, error) {
}
func (s JBytes) ParseDuration() (time.Duration, error) {
return JParce[time.Duration](s.Bytes())
jdata, jerr := JParce[time.Duration](s.Bytes())
if jerr == nil {
return jdata, nil
}
ustr, serr := s.ParseString()
if serr != nil {
return 0, errors.Join(jerr, fmt.Errorf("parse duration:%w", serr))
}
return ParseDuration(ustr)
}
func (s JBytes) ParseTime() (time.Time, error) {

View File

@@ -3,6 +3,7 @@ package value_test
import (
"encoding/json"
"testing"
"time"
"gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/test/require"
@@ -40,3 +41,34 @@ func TestJBytes_Unmarshal(t *testing.T) {
require.ErrorIs(t, sta.Unmarshal(ac), config.ErrInvalidValue)
require.ErrorIs(t, sta.Unmarshal(&ac), config.ErrInvalidValue)
}
func TestJBytes_Duration(t *testing.T) {
t.Parallel()
ex := 42 * time.Minute
data, err := json.Marshal("42m")
require.NoError(t, err)
valueDuration := value.JBytes(data)
var exp time.Duration
require.ErrorIs(t, valueDuration.Unmarshal(&exp), config.ErrInvalidValue)
pdur, perr := valueDuration.ParseDuration()
require.NoError(t, perr)
require.Equal(t, pdur, ex)
jdata, jerr := json.Marshal(ex)
require.NoError(t, jerr)
jDuration := value.JBytes(jdata)
var jexp time.Duration
require.NoError(t, jDuration.Unmarshal(&jexp))
require.Equal(t, jexp, ex)
jpdur, jperr := jDuration.ParseDuration()
require.NoError(t, jperr)
require.Equal(t, jpdur, ex)
}