Skip to content

Commit

Permalink
feat: auto validate config
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan committed Jan 27, 2025
1 parent d415ba3 commit 95892bf
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 2 deletions.
13 changes: 11 additions & 2 deletions core/conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ func Load(file string, v any, opts ...Option) error {
return loader([]byte(os.ExpandEnv(string(content))), v)
}

return loader(content, v)
if err = loader(content, v); err != nil {
return err
}

Check warning on line 67 in core/conf/config.go

View check run for this annotation

Codecov / codecov/patch

core/conf/config.go#L66-L67

Added lines #L66 - L67 were not covered by tests

return validate(v)
}

// LoadConfig loads config into v from file, .json, .yaml and .yml are acceptable.
Expand All @@ -85,7 +89,12 @@ func LoadFromJsonBytes(content []byte, v any) error {

lowerCaseKeyMap := toLowerCaseKeyMap(m, info)

return mapping.UnmarshalJsonMap(lowerCaseKeyMap, v, mapping.WithCanonicalKeyFunc(toLowerCase))
if err = mapping.UnmarshalJsonMap(lowerCaseKeyMap, v,
mapping.WithCanonicalKeyFunc(toLowerCase)); err != nil {
return err
}

return validate(v)
}

// LoadConfigFromJsonBytes loads config into v from content json bytes.
Expand Down
38 changes: 38 additions & 0 deletions core/conf/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package conf

import (
"errors"
"os"
"reflect"
"testing"
Expand Down Expand Up @@ -1222,6 +1223,26 @@ func Test_getFullName(t *testing.T) {
assert.Equal(t, "a", getFullName("", "a"))
}

func TestValidate(t *testing.T) {
t.Run("normal config", func(t *testing.T) {
var c mockConfig
err := LoadFromJsonBytes([]byte(`{"val": "hello", "number": 8}`), &c)
assert.NoError(t, err)
})

t.Run("error no int", func(t *testing.T) {
var c mockConfig
err := LoadFromJsonBytes([]byte(`{"val": "hello"}`), &c)
assert.Error(t, err)
})

t.Run("error no string", func(t *testing.T) {
var c mockConfig
err := LoadFromJsonBytes([]byte(`{"number": 8}`), &c)
assert.Error(t, err)
})
}

func Test_buildFieldsInfo(t *testing.T) {
type ParentSt struct {
Name string
Expand Down Expand Up @@ -1328,3 +1349,20 @@ func createTempFile(ext, text string) (string, error) {

return filename, nil
}

type mockConfig struct {
Val string
Number int
}

func (m mockConfig) Validate() error {
if len(m.Val) == 0 {
return errors.New("val is empty")
}

if m.Number == 0 {
return errors.New("number is zero")
}

return nil
}
11 changes: 11 additions & 0 deletions core/conf/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package conf

import "github.com/zeromicro/go-zero/core/validation"

func validate(v any) error {
if val, ok := v.(validation.Validator); ok {
return val.Validate()
}

return nil
}
50 changes: 50 additions & 0 deletions core/conf/validate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package conf

import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
)

type mockType int

func (m mockType) Validate() error {
if m < 10 {
return errors.New("invalid value")
}

return nil
}

type anotherMockType int

func Test_validate(t *testing.T) {
tests := []struct {
name string
v any
wantErr bool
}{
{
name: "invalid",
v: mockType(5),
wantErr: true,
},
{
name: "valid",
v: mockType(10),
wantErr: false,
},
{
name: "not validator",
v: anotherMockType(5),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validate(tt.v)
assert.Equal(t, tt.wantErr, err != nil)
})
}
}

0 comments on commit 95892bf

Please sign in to comment.