added support for a few middlewares, added match_domain option, changed index reference prefix from $ to #, etc.

This commit is contained in:
yusing
2024-09-27 09:57:57 +08:00
parent 345a4417a6
commit f474ae4f75
47 changed files with 1523 additions and 446 deletions

View File

@@ -106,7 +106,7 @@ func Serialize(data any) (SerializedObject, E.NestedError) {
return result, nil
}
func Deserialize(src map[string]any, target any) E.NestedError {
func Deserialize(src SerializedObject, target any) E.NestedError {
// convert data fields to lower no-snake
// convert target fields to lower
// then check if the field of data is in the target
@@ -117,6 +117,10 @@ func Deserialize(src map[string]any, target any) E.NestedError {
snakeCaseField := strings.ToLower(field.Name)
mapping[snakeCaseField] = field.Name
}
tValue := reflect.ValueOf(target)
if tValue.IsZero() {
return E.Invalid("value", "nil")
}
for k, v := range src {
kCleaned := toLowerNoSnake(k)
if fieldName, ok := mapping[kCleaned]; ok {
@@ -150,13 +154,13 @@ func Deserialize(src map[string]any, target any) E.NestedError {
}
prop.Set(propNew)
default:
return E.Unsupported("field", k).Extraf("type=%s", propType)
return E.Invalid("conversion", k).Extraf("from %s to %s", vType, propType)
}
} else {
return E.Unsupported("field", k).Extraf("type=%s", propType)
return E.Unsupported("field", k).Extraf("type %s is not settable", propType)
}
} else {
return E.Failure("unknown field").With(k)
return E.Unexpected("field", k)
}
}

View File

@@ -38,6 +38,17 @@ func ExpectEqual[T comparable](t *testing.T, got T, want T) {
}
}
func ExpectEqualAny[T comparable](t *testing.T, got T, wants []T) {
t.Helper()
for _, want := range wants {
if got == want {
return
}
}
t.Errorf("expected any of:\n%v, got\n%v", wants, got)
t.FailNow()
}
func ExpectDeepEqual[T any](t *testing.T, got T, want T) {
t.Helper()
if !reflect.DeepEqual(got, want) {