-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
204 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package yaconf | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"strings" | ||
) | ||
|
||
type validator interface { | ||
Validate() error | ||
} | ||
|
||
func addPrefix(prefix, name string) string { | ||
if prefix == "" { | ||
return name | ||
} | ||
|
||
return fmt.Sprintf("%s.%s", prefix, name) | ||
} | ||
|
||
func validate(config interface{}, prefix string) []string { | ||
t := reflect.TypeOf(config) | ||
v := reflect.ValueOf(config) | ||
if t.Kind() == reflect.Ptr { | ||
t = t.Elem() | ||
v = v.Elem() | ||
} | ||
|
||
errors := []string{} | ||
for i := 0; i < t.NumField(); i++ { | ||
f := t.Field(i) | ||
if !f.IsExported() { | ||
continue | ||
} | ||
|
||
name := f.Tag.Get("yaml") | ||
if name == "" { | ||
name = f.Name | ||
} | ||
|
||
if strings.Contains(f.Tag.Get("yaconf"), "required") && v.Field(i).IsZero() { | ||
errors = append(errors, fmt.Sprintf("%s is required", addPrefix(prefix, name))) | ||
continue | ||
} | ||
|
||
if f.Type.Kind() == reflect.Struct { | ||
errors = append(errors, validate(v.Field(i).Interface(), addPrefix(prefix, name))...) | ||
continue | ||
} | ||
|
||
if f.Type.Kind() == reflect.Ptr { | ||
if f.Type.Elem().Kind() != reflect.Struct { | ||
continue | ||
} | ||
if v.Field(i).IsNil() { | ||
errors = append(errors, validate(reflect.New(v.Field(i).Type().Elem()).Interface(), addPrefix(prefix, name))...) | ||
} else { | ||
errors = append(errors, validate(v.Field(i).Elem().Interface(), addPrefix(prefix, name))...) | ||
} | ||
} | ||
|
||
} | ||
return errors | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package yaconf | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestAddPrefix(t *testing.T) { | ||
s := addPrefix("", "bar") | ||
require.Equal(t, "bar", s) | ||
|
||
s = addPrefix("foo", "bar") | ||
require.Equal(t, "foo.bar", s) | ||
} | ||
|
||
func TestValidate(t *testing.T) { | ||
type db struct { | ||
Host string `yaconf:"required"` | ||
} | ||
pid := "/var/run/test.pid" | ||
config := struct { | ||
LogLevel string `yaml:"log_level" yaconf:"required,default=info"` | ||
LogFile string `yaml:"log_file" yaconf:"required"` | ||
PID *string `yaconf:"required"` | ||
DB1 db | ||
DB2 *db | ||
DB3 *db | ||
private string | ||
}{ | ||
PID: &pid, | ||
DB3: &db{ | ||
Host: "127.0.0.1", | ||
}, | ||
} | ||
|
||
errors := validate(&config, "") | ||
require.Equal(t, 4, len(errors), errors) | ||
require.Equal(t, "log_level is required", errors[0]) | ||
require.Equal(t, "log_file is required", errors[1]) | ||
require.Equal(t, "DB1.Host is required", errors[2]) | ||
require.Equal(t, "DB2.Host is required", errors[3]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package yaconf | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type testConfig3 struct { | ||
} | ||
|
||
func (c *testConfig3) Validate() error { | ||
return fmt.Errorf("invalid") | ||
} | ||
|
||
func TestRead(t *testing.T) { | ||
file, err := os.CreateTemp("", "") | ||
require.Nil(t, err) | ||
|
||
defer func() { | ||
os.Remove(file.Name()) | ||
}() | ||
|
||
_, err = file.Write([]byte(`log_level: info`)) | ||
require.Nil(t, err) | ||
|
||
err = file.Close() | ||
require.Nil(t, err) | ||
|
||
// Test success | ||
config := struct { | ||
LogLevel string `yaml:"log_level"` | ||
}{} | ||
|
||
err = Read(file.Name(), &config) | ||
require.Nil(t, err) | ||
|
||
// Test with errors | ||
config2 := struct { | ||
LogFile string `yaml:"log_file" yaconf:"required"` | ||
}{} | ||
|
||
err = Read(file.Name(), &config2) | ||
require.NotNil(t, err) | ||
|
||
// Test with custom validator | ||
err = Read(file.Name(), &testConfig3{}) | ||
require.NotNil(t, err) | ||
} |