9 Commits

Author SHA1 Message Date
954187a07a Merge pull request 'update values' (#16) from json into master
All checks were successful
Go Action / goaction (push) Successful in 27s
Reviewed-on: #16
2025-12-27 10:37:44 +03:00
915660eab1 update jstring test
All checks were successful
Go Action / goaction (push) Successful in 28s
Go Action / goaction (pull_request) Successful in 30s
2025-12-27 10:30:28 +03:00
6d42e1a5f0 update values
All checks were successful
Go Action / goaction (pull_request) Successful in 30s
Go Action / goaction (push) Successful in 48s
2025-12-26 20:59:11 +03:00
d2bc7dc1ec Merge pull request 'update json provider' (#15) from json into master
All checks were successful
Go Action / goaction (push) Successful in 28s
Reviewed-on: #15
2025-12-26 16:14:33 +03:00
6dafb6f7ce update json provider
All checks were successful
Go Action / goaction (push) Successful in 45s
Go Action / goaction (pull_request) Successful in 33s
2025-12-26 16:12:44 +03:00
7f144a5084 Merge pull request 'update ini provider' (#14) from ini into master
All checks were successful
Go Action / goaction (push) Successful in 27s
Reviewed-on: #14
2025-12-26 16:09:17 +03:00
fafe002796 update ini provider
All checks were successful
Go Action / goaction (push) Successful in 40s
Go Action / goaction (pull_request) Successful in 28s
2025-12-26 16:06:00 +03:00
c11f08ee07 update yaml provider (#13)
All checks were successful
Go Action / goaction (push) Successful in 38s
Reviewed-on: #13
2025-12-26 15:55:38 +03:00
f9a0411192 def (#12)
All checks were successful
Go Action / goaction (push) Successful in 29s
Reviewed-on: #12
2025-12-26 14:55:42 +03:00
19 changed files with 270 additions and 63 deletions

40
.gitea/workflows/etcd.yml Normal file
View File

@@ -0,0 +1,40 @@
name: Go Action
on: [push, pull_request]
jobs:
goaction:
runs-on: ubuntu-latest # Use a Gitea Actions runner label
services:
# The label "etcd-server" becomes the hostname for the service.
etcd-server:
# Use the official etcd Docker image
image: quay.io/coreos/etcd:v3.6.7
env:
ETCD_LISTEN_CLIENT_URLS: http://0.0.0.0:2379
ETCD_ADVERTISE_CLIENT_URLS: http://etcd-server:2379
env:
FDEVS_CONFIG_ETCD_HOST: 'etcd-server:2379'
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/etcd
- name: Run go test
run: go test ./...
working-directory: ./provider/etcd

28
.gitea/workflows/ini.yml Normal file
View File

@@ -0,0 +1,28 @@
name: Go Action
on: [push, pull_request]
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/ini
- name: Run go test
run: go test ./...
working-directory: ./provider/ini

28
.gitea/workflows/json.yml Normal file
View File

@@ -0,0 +1,28 @@
name: Go Action
on: [push, pull_request]
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/json
- name: Run go test
run: go test ./...
working-directory: ./provider/json

View File

@@ -1,10 +1,10 @@
module gitoa.ru/go-4devs/config/provider/etcd
go 1.22
go 1.23
require (
github.com/stretchr/testify v1.8.4
gitoa.ru/go-4devs/config v0.0.2
gitoa.ru/go-4devs/config v0.0.3
go.etcd.io/etcd/api/v3 v3.5.11
go.etcd.io/etcd/client/v3 v3.5.11
)

View File

@@ -31,6 +31,8 @@ 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.2 h1:bkTxW57kDDMf4cj/8W7fxPSN7JCPWEqlhCmL6LP3Vzg=
gitoa.ru/go-4devs/config v0.0.2/go.mod h1:xfEC2Al9xnMLJUuekYs3KhJ5BIzWAseNwkMwbN6/xss=
gitoa.ru/go-4devs/config v0.0.3 h1:+ecwDQj4fneJCh2uLNNAonm4cUJdGmlfxUsFhQRI9Ko=
gitoa.ru/go-4devs/config v0.0.3/go.mod h1:UINWnObZA0nLiJro+TtavUBBvN0cSt17aRHOk20pP74=
go.etcd.io/etcd/api/v3 v3.5.11 h1:B54KwXbWDHyD3XYAwprxNzTe7vlhR69LuBgZnMVvS7E=
go.etcd.io/etcd/api/v3 v3.5.11/go.mod h1:Ot+o0SWSyT6uHhA56al1oCED0JImsRiU9Dc26+C2a+4=
go.etcd.io/etcd/client/pkg/v3 v3.5.11 h1:bT2xVspdiCj2910T0V+/KHcVKjkUrCZVtk8J2JF2z1A=

View File

@@ -1,15 +1,10 @@
module gitoa.ru/go-4devs/config/provider/ini
go 1.22
go 1.23
require (
github.com/stretchr/testify v1.8.4
gitoa.ru/go-4devs/config v0.0.1
gitoa.ru/go-4devs/config v0.0.3
gopkg.in/ini.v1 v1.67.0
)
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
require github.com/stretchr/testify v1.11.1 // indirect

View File

@@ -2,12 +2,10 @@ 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/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/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.1 h1:9KrOO09YbIMO8qL8aVn/G74DurGdOIW5y3O02bays4I=
gitoa.ru/go-4devs/config v0.0.1/go.mod h1:xfEC2Al9xnMLJUuekYs3KhJ5BIzWAseNwkMwbN6/xss=
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=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
gitoa.ru/go-4devs/config v0.0.3 h1:+ecwDQj4fneJCh2uLNNAonm4cUJdGmlfxUsFhQRI9Ko=
gitoa.ru/go-4devs/config v0.0.3/go.mod h1:UINWnObZA0nLiJro+TtavUBBvN0cSt17aRHOk20pP74=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

View File

@@ -5,9 +5,9 @@ import (
"testing"
"time"
"github.com/stretchr/testify/require"
"gitoa.ru/go-4devs/config/provider/ini"
"gitoa.ru/go-4devs/config/test"
"gitoa.ru/go-4devs/config/test/require"
lib "gopkg.in/ini.v1"
)

View File

@@ -1,10 +1,10 @@
module gitoa.ru/go-4devs/config/provider/json
go 1.22
go 1.23
require (
github.com/tidwall/gjson v1.17.0
gitoa.ru/go-4devs/config v0.0.1
github.com/tidwall/gjson v1.18.0
gitoa.ru/go-4devs/config v0.0.3
)
require (

View File

@@ -1,10 +1,10 @@
github.com/tidwall/gjson v1.17.0 h1:/Jocvlh98kcTfpN2+JzGQWQcqrPQwDrVEMApx/M5ZwM=
github.com/tidwall/gjson v1.17.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY=
github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
gitoa.ru/go-4devs/config v0.0.0-20240125174937-085589a9383a h1:61iwpn1Ec4yIkBSvSz8knNENJuhj6v2rp6bfw1wkG0E=
gitoa.ru/go-4devs/config v0.0.0-20240125174937-085589a9383a/go.mod h1:3g2bwE2OTDyYwm33KN/Cqc8pEdGlWXnB8Ju3PsYNQr0=
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.3 h1:+ecwDQj4fneJCh2uLNNAonm4cUJdGmlfxUsFhQRI9Ko=
gitoa.ru/go-4devs/config v0.0.3/go.mod h1:UINWnObZA0nLiJro+TtavUBBvN0cSt17aRHOk20pP74=

View File

@@ -12,6 +12,7 @@ func Equal(t *testing.T, expected any, actual any, msgAndArgs ...any) bool {
return true
}
t.Errorf("not equal expect:%v actual:%v", expected, actual)
t.Error(msgAndArgs...)
return false
@@ -24,6 +25,7 @@ func Equalf(t *testing.T, expected any, actual any, msg string, args ...any) boo
return true
}
t.Errorf("not equal expect:%#v acctual: %#v", expected, actual)
t.Errorf(msg, args...)
return false

View File

@@ -24,7 +24,7 @@ func Run(t *testing.T, 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.Error(t, err)
read.Assert(t, val)
})
}
@@ -33,6 +33,7 @@ func Run(t *testing.T, provider config.Provider, read []Read) {
type Read struct {
Key []string
Assert func(t *testing.T, v config.Value)
Error func(t *testing.T, ex error)
}
type Config struct {
@@ -57,6 +58,14 @@ func NewReadUnmarshal(expected, target any, key ...string) Read {
require.NoErrorf(t, v.Unmarshal(target), "unmarshal")
require.Equal(t, expected, target, "unmarshal")
},
Error: exError(key...),
}
}
func exError(path ...string) func(t *testing.T, err error) {
return func(t *testing.T, err error) {
t.Helper()
require.NoError(t, err, path)
}
}
@@ -69,7 +78,8 @@ func Time(value string) time.Time {
// NewRead test data.
func NewRead(expected any, key ...string) Read {
return Read{
Key: key,
Key: key,
Error: exError(key...),
Assert: func(t *testing.T, v config.Value) {
t.Helper()
@@ -111,9 +121,20 @@ func NewRead(expected any, key ...string) Read {
require.Fail(t, "unexpected type:%+T", expected)
}
require.Equalf(t, val, short, "type:%T", expected)
require.NoErrorf(t, err, "type:%T", expected)
require.Equalf(t, expected, val, "type:%T", expected)
require.Equalf(t, val, short, "%q!=%q, type:%T", val, short, expected)
require.NoErrorf(t, err, "err:%v type:%T", err, expected)
require.Equalf(t, expected, val, "%q!=%q type:%T", expected, val, expected)
},
}
}
func NewErrorIs(exErr error, path ...string) Read {
return Read{
Key: path,
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)
},
}
}

View File

@@ -23,6 +23,5 @@ func Equalf(t *testing.T, expected any, actual any, msg string, args ...any) {
return
}
t.Errorf(msg, args...)
t.FailNow()
}

View File

@@ -9,6 +9,7 @@ func NoError(t *testing.T, err error, msgAndArgs ...any) {
t.Helper()
if err != nil {
t.Errorf("no error got:%v", err)
t.Error(msgAndArgs...)
t.FailNow()
}
@@ -26,8 +27,18 @@ func NoErrorf(t *testing.T, err error, msg string, args ...any) {
func ErrorIs(t *testing.T, err, ex error, msgAndArgs ...any) {
t.Helper()
if errors.Is(ex, err) {
if !errors.Is(err, ex) {
t.Errorf("expect:%#v got:%#v", ex, err)
t.Error(msgAndArgs...)
t.FailNow()
}
}
func ErrorIsf(t *testing.T, err, ex error, msg string, args ...any) {
t.Helper()
if !errors.Is(err, ex) {
t.Errorf(msg, args...)
t.FailNow()
}
}

View File

@@ -72,14 +72,6 @@ func ParseBool(s string) (bool, error) {
return b, nil
}
func JUnmarshal(b []byte, v any) error {
if err := json.Unmarshal(b, v); err != nil {
return fmt.Errorf("%w: %w", config.ErrInvalidValue, err)
}
return nil
}
func ParseUint64(s string) (uint64, error) {
i, err := strconv.ParseUint(s, 10, 64)
if err != nil {
@@ -88,3 +80,17 @@ func ParseUint64(s string) (uint64, error) {
return i, nil
}
func JUnmarshal(b []byte, v any) error {
if err := json.Unmarshal(b, v); err != nil {
return fmt.Errorf("%w: %w", config.ErrInvalidValue, err)
}
return nil
}
func JParce[T any](b []byte) (T, error) {
var data T
return data, JUnmarshal(b, &data)
}

View File

@@ -15,39 +15,41 @@ func (s JBytes) Unmarshal(v any) error {
}
func (s JBytes) ParseString() (string, error) {
return s.String(), nil
var data string
return data, s.Unmarshal(&data)
}
func (s JBytes) ParseInt() (int, error) {
return Atoi(s.String())
return JParce[int](s.Bytes())
}
func (s JBytes) ParseInt64() (int64, error) {
return ParseInt64(s.String())
return JParce[int64](s.Bytes())
}
func (s JBytes) ParseUint() (uint, error) {
return ParseUint(s.String())
return JParce[uint](s.Bytes())
}
func (s JBytes) ParseUint64() (uint64, error) {
return ParseUint64(s.String())
return JParce[uint64](s.Bytes())
}
func (s JBytes) ParseFloat64() (float64, error) {
return ParseFloat(s.String())
return JParce[float64](s.Bytes())
}
func (s JBytes) ParseBool() (bool, error) {
return ParseBool(s.String())
return JParce[bool](s.Bytes())
}
func (s JBytes) ParseDuration() (time.Duration, error) {
return ParseDuration(s.String())
return JParce[time.Duration](s.Bytes())
}
func (s JBytes) ParseTime() (time.Time, error) {
return ParseTime(s.String())
return JParce[time.Time](s.Bytes())
}
func (s JBytes) Bytes() []byte {
@@ -55,7 +57,9 @@ func (s JBytes) Bytes() []byte {
}
func (s JBytes) String() string {
return string(s)
data, _ := s.ParseString()
return data
}
func (s JBytes) Int() int {
@@ -107,7 +111,7 @@ func (s JBytes) Time() time.Time {
}
func (s JBytes) Any() any {
return s
return s.Bytes()
}
func (s JBytes) IsEquals(in config.Value) bool {

42
value/jbytes_test.go Normal file
View File

@@ -0,0 +1,42 @@
package value_test
import (
"encoding/json"
"testing"
"gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/test/require"
"gitoa.ru/go-4devs/config/value"
)
func TestJBytes_String(t *testing.T) {
t.Parallel()
data := value.JBytes([]byte("\"data\""))
res, err := data.ParseString()
require.NoError(t, err)
require.Equal(t, res, "data")
dataErr := value.JBytes([]byte("data"))
res2, err2 := dataErr.ParseString()
require.ErrorIs(t, err2, config.ErrInvalidValue)
require.Equal(t, "", res2)
}
func TestJBytes_Unmarshal(t *testing.T) {
t.Parallel()
data, err := json.Marshal([]string{"test1", "test2"})
require.NoError(t, err)
sta := value.JBytes(data)
ac := ""
aca := []string{}
require.NoError(t, sta.Unmarshal(&aca))
require.Equal(t, []string{"test1", "test2"}, aca)
require.ErrorIs(t, sta.Unmarshal(ac), config.ErrInvalidValue)
require.ErrorIs(t, sta.Unmarshal(&ac), config.ErrInvalidValue)
}

View File

@@ -1,7 +1,8 @@
package value_test
import (
"errors"
"math"
"strconv"
"testing"
"time"
@@ -10,7 +11,7 @@ import (
"gitoa.ru/go-4devs/config/value"
)
func TestStringDuration(t *testing.T) {
func TestJStringDuration(t *testing.T) {
t.Parallel()
tests := map[string]struct {
@@ -31,7 +32,47 @@ func TestStringDuration(t *testing.T) {
for name, data := range tests {
require.Equal(t, data.exp, data.raw.Duration(), name)
d, err := data.raw.ParseDuration()
require.Truef(t, errors.Is(err, data.err), "%[1]s: expect:%#[2]v, got:%#[3]v", name, data.err, err)
require.ErrorIsf(t, err, data.err, "%[1]s: expect:%#[2]v, got:%#[3]v", name, data.err, err)
require.Equal(t, data.exp, d, name)
}
}
func TestJStringInt(t *testing.T) {
t.Parallel()
tests := map[string]struct {
raw value.JString
exp int
err error
}{
"empty": {
raw: value.JString(strconv.Itoa(0)),
exp: 0,
},
"42": {
raw: value.JString("42"),
exp: 42,
},
"err": {
raw: value.JString("err"),
exp: 0,
err: config.ErrInvalidValue,
},
"float": {
raw: value.JString("0.23"),
exp: 0,
err: config.ErrInvalidValue,
},
"maxInt": {
raw: value.JString(strconv.Itoa(math.MaxInt)),
exp: math.MaxInt,
},
}
for name, data := range tests {
require.Equal(t, data.exp, data.raw.Int(), name)
res, err := data.raw.ParseInt()
require.ErrorIs(t, err, data.err)
require.Equal(t, data.exp, res, name)
}
}

View File

@@ -1,7 +1,6 @@
package value_test
import (
"encoding/json"
"testing"
"gitoa.ru/go-4devs/config"
@@ -14,21 +13,12 @@ func TestStringUnmarshal(t *testing.T) {
st := value.String("test")
data, err := json.Marshal([]string{"test1", "test2"})
require.NoError(t, err)
sta := value.JBytes(data)
ac := ""
require.NoError(t, st.Unmarshal(&ac))
require.Equal(t, "test", ac)
aca := []string{}
require.NoError(t, sta.Unmarshal(&aca))
require.Equal(t, []string{"test1", "test2"}, aca)
require.ErrorIs(t, sta.Unmarshal(ac), config.ErrWrongType)
require.ErrorIs(t, sta.Unmarshal(&ac), config.ErrWrongType)
require.ErrorIs(t, st.Unmarshal(aca), config.ErrWrongType)
require.ErrorIs(t, st.Unmarshal(&aca), config.ErrWrongType)
}