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

refactor: use type switch instead of if-else #595

Merged
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 10 additions & 9 deletions huma.go
Original file line number Diff line number Diff line change
Expand Up @@ -1334,17 +1334,18 @@ func Register[I, O any](api API, op Operation, handler func(context.Context, *I)
elem.Set(item)
item = ptr
}
if resolver, ok := item.Interface().(Resolver); ok {
if errs := resolver.Resolve(ctx); len(errs) > 0 {
res.Errors = append(res.Errors, errs...)
}
} else if resolver, ok := item.Interface().(ResolverWithPath); ok {
if errs := resolver.Resolve(ctx, pb); len(errs) > 0 {
res.Errors = append(res.Errors, errs...)
}
} else {
var errs []error
switch resolver := item.Interface().(type) {
case Resolver:
errs = resolver.Resolve(ctx)
case ResolverWithPath:
errs = resolver.Resolve(ctx, pb)
default:
alexandear marked this conversation as resolved.
Show resolved Hide resolved
panic("matched resolver cannot be run, please file a bug")
}
if len(errs) > 0 {
res.Errors = append(res.Errors, errs...)
}
})

if len(res.Errors) > 0 {
Expand Down
7 changes: 4 additions & 3 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,12 @@ func (s *Schema) PrecomputeMessages() {

func boolTag(f reflect.StructField, tag string, def bool) bool {
if v := f.Tag.Get(tag); v != "" {
if v == "true" {
switch v {
case "true":
return true
} else if v == "false" {
case "false":
return false
} else {
default:
panic(fmt.Errorf("invalid bool tag '%s' for field '%s': %v", tag, f.Name, v))
}
alexandear marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down
7 changes: 4 additions & 3 deletions validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,11 +520,12 @@ func Validate(r Registry, s *Schema, path *PathBuffer, mode ValidateMode, v any,
return
}
case TypeObject:
if vv, ok := v.(map[string]any); ok {
switch vv := v.(type) {
case map[string]any:
handleMapString(r, s, path, mode, vv, res)
} else if vv, ok := v.(map[any]any); ok {
case map[any]any:
handleMapAny(r, s, path, mode, vv, res)
} else {
default:
res.Add(path, v, validation.MsgExpectedObject)
return
}
Expand Down
5 changes: 3 additions & 2 deletions validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1476,10 +1476,11 @@ func BenchmarkValidate(b *testing.B) {

input := test.input
if s.Type == huma.TypeObject && s.Properties["value"] != nil {
if i, ok := input.(map[string]any); ok {
switch i := input.(type) {
case map[string]any:
input = i["value"]
s = s.Properties["value"]
} else if i, ok := input.(map[any]any); ok {
case map[any]any:
alexandear marked this conversation as resolved.
Show resolved Hide resolved
input = i["value"]
s = s.Properties["value"]
}
Expand Down