Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISSUE-210 - Add Required property to indicate a config registration c… #211

Merged
merged 6 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -709,10 +709,10 @@ func TestConfig(t *testing.T) {
func TestEntryValidationRequired(t *testing.T) {
tests := []struct {
name string
entry Entry
key string
expectsError bool
errMsg string
entry Entry
expectsError bool
}{
{
name: "Required string empty",
Expand Down
69 changes: 39 additions & 30 deletions config/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,36 +62,8 @@ func (e *Entry) validate(key string) error {
return errors.Errorf(message, key, e.Type)
}

v := reflect.ValueOf(e.Value)
if e.Required {
switch kind {
case reflect.String:
if v.Len() == 0 {
return errors.Errorf("%q is required and cannot be an empty string", key)
}
case reflect.Map:
if len(v.MapKeys()) == 0 {
return errors.Errorf("%q is required and cannot be an empty map", key)
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if v.Int() == 0 {
return errors.Errorf("%q is required and cannot be zero", key)
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if v.Uint() == 0 {
return errors.Errorf("%q is required and cannot be zero", key)
}
case reflect.Float32, reflect.Float64:
if v.Float() == 0 {
return errors.Errorf("%q is required and cannot be zero", key)
}
case reflect.Ptr, reflect.Interface:
if v.IsNil() {
return errors.Errorf("%q is required and cannot be nil", key)
}
default:
// Nothing to reflect on. Should pass.
}
if err := e.validateRequired(kind, key); err != nil {
return err
}

if len(e.AuthorizedValues) > 0 {
Expand Down Expand Up @@ -230,3 +202,40 @@ func (e *Entry) convertEnvVar(str, key string) (any, error) {

return nil, nil
}

func (e *Entry) validateRequired(kind reflect.Kind, key string) error {
System-Glitch marked this conversation as resolved.
Show resolved Hide resolved
v := reflect.ValueOf(e.Value)

if e.Required {
switch kind {
case reflect.String:
if v.Len() == 0 {
return errors.Errorf("%q is required and cannot be an empty string", key)
}
case reflect.Map:
if len(v.MapKeys()) == 0 {
return errors.Errorf("%q is required and cannot be an empty map", key)
}
BowlOfSoup marked this conversation as resolved.
Show resolved Hide resolved
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if v.Int() == 0 {
return errors.Errorf("%q is required and cannot be zero", key)
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if v.Uint() == 0 {
return errors.Errorf("%q is required and cannot be zero", key)
}
case reflect.Float32, reflect.Float64:
if v.Float() == 0 {
return errors.Errorf("%q is required and cannot be zero", key)
}
case reflect.Ptr, reflect.Interface:
if v.IsNil() {
return errors.Errorf("%q is required and cannot be nil", key)
}
default:
return nil
}
}

return nil
}