-
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
10 changed files
with
243 additions
and
43 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
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,70 @@ | ||
package rules | ||
|
||
import ( | ||
"errors" | ||
"reflect" | ||
"strconv" | ||
) | ||
|
||
type Default struct{} | ||
|
||
func (self Default) Select(schema map[string]string, parent reflect.Value, value reflect.Value) bool { | ||
return true | ||
} | ||
|
||
func (self Default) Validate(schema map[string]string, parent reflect.Value, value reflect.Value) (reflect.Value, []error) { | ||
errs := []error{} | ||
config, ok := schema["default"] | ||
|
||
if !ok || config == "" { | ||
errs = append(errs, errors.New("config is required")) | ||
return value, errs | ||
} | ||
|
||
if value.Kind() == reflect.Pointer && value.IsZero() { | ||
t := value.Type() | ||
ptr := reflect.New(t.Elem()) | ||
|
||
if t.Elem().ConvertibleTo(reflect.TypeFor[int]()) { | ||
v, err := strconv.ParseInt(config, 10, 64) | ||
|
||
if err != nil { | ||
errs = append(errs, errors.New("config must be an int")) | ||
} | ||
|
||
ptr.Elem().Set(reflect.ValueOf(v).Convert(t.Elem())) | ||
return ptr, errs | ||
} | ||
|
||
if t.Elem().ConvertibleTo(reflect.TypeFor[float64]()) { | ||
v, err := strconv.ParseFloat(config, 64) | ||
|
||
if err != nil { | ||
errs = append(errs, errors.New("config must be an float")) | ||
} | ||
|
||
ptr.Elem().Set(reflect.ValueOf(v).Convert(t.Elem())) | ||
return ptr, errs | ||
} | ||
|
||
if t.Elem().Kind() == reflect.Bool { | ||
v, err := strconv.ParseBool(config) | ||
|
||
if err != nil { | ||
errs = append(errs, errors.New("config must be a bool")) | ||
} | ||
|
||
ptr.Elem().SetBool(v) | ||
return ptr, errs | ||
} | ||
|
||
if t.Elem().Kind() == reflect.String { | ||
ptr.Elem().SetString(config) | ||
return ptr, errs | ||
} | ||
|
||
errs = append(errs, errors.New("default can only be used on primitive types")) | ||
} | ||
|
||
return value, errs | ||
} |
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,125 @@ | ||
package rules_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/aacebo/owl" | ||
) | ||
|
||
func Test_Default(t *testing.T) { | ||
t.Run("should error on no config", func(t *testing.T) { | ||
errs := owl.Validate(struct { | ||
A *string `json:"a" owl:"default"` | ||
}{}) | ||
|
||
if len(errs) == 0 { | ||
t.Error("should have error") | ||
} | ||
}) | ||
|
||
t.Run("should error on type mismatch", func(t *testing.T) { | ||
errs := owl.Validate(struct { | ||
A *int `json:"a" owl:"default=abc"` | ||
}{}) | ||
|
||
if len(errs) == 0 { | ||
t.Error("should have error") | ||
} | ||
}) | ||
|
||
t.Run("should succeed with int", func(t *testing.T) { | ||
v := struct { | ||
A *int `json:"a" owl:"default=123"` | ||
}{} | ||
|
||
errs := owl.Validate(&v) | ||
|
||
if len(errs) > 0 { | ||
t.Error(errs) | ||
} | ||
|
||
if v.A == nil || *v.A != 123 { | ||
t.Error("should set default value") | ||
} | ||
}) | ||
|
||
t.Run("should succeed with int64", func(t *testing.T) { | ||
v := struct { | ||
A *int64 `json:"a" owl:"default=123"` | ||
}{} | ||
|
||
errs := owl.Validate(&v) | ||
|
||
if len(errs) > 0 { | ||
t.Error(errs) | ||
} | ||
|
||
if v.A == nil || *v.A != 123 { | ||
t.Error("should set default value") | ||
} | ||
}) | ||
|
||
t.Run("should succeed with float32", func(t *testing.T) { | ||
v := struct { | ||
A *float32 `json:"a" owl:"default=123"` | ||
}{} | ||
|
||
errs := owl.Validate(&v) | ||
|
||
if len(errs) > 0 { | ||
t.Error(errs) | ||
} | ||
|
||
if v.A == nil || *v.A != 123 { | ||
t.Error("should set default value") | ||
} | ||
}) | ||
|
||
t.Run("should succeed with float64", func(t *testing.T) { | ||
v := struct { | ||
A *float64 `json:"a" owl:"default=123"` | ||
}{} | ||
|
||
errs := owl.Validate(&v) | ||
|
||
if len(errs) > 0 { | ||
t.Error(errs) | ||
} | ||
|
||
if v.A == nil || *v.A != 123 { | ||
t.Error("should set default value") | ||
} | ||
}) | ||
|
||
t.Run("should succeed with uint", func(t *testing.T) { | ||
v := struct { | ||
A *uint `json:"a" owl:"default=123"` | ||
}{} | ||
|
||
errs := owl.Validate(&v) | ||
|
||
if len(errs) > 0 { | ||
t.Error(errs) | ||
} | ||
|
||
if v.A == nil || *v.A != 123 { | ||
t.Error("should set default value") | ||
} | ||
}) | ||
|
||
t.Run("should succeed with string", func(t *testing.T) { | ||
v := struct { | ||
A *string `json:"a" owl:"default=123"` | ||
}{} | ||
|
||
errs := owl.Validate(&v) | ||
|
||
if len(errs) > 0 { | ||
t.Error(errs) | ||
} | ||
|
||
if v.A == nil || *v.A != "123" { | ||
t.Error("should set default value") | ||
} | ||
}) | ||
} |
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
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
Oops, something went wrong.