Skip to content

Commit

Permalink
refactor: replace custom function getSubField by reflect.Value.FieldB…
Browse files Browse the repository at this point in the history
…yIndex
  • Loading branch information
b-kamphorst committed Jan 13, 2025
1 parent 7f9ea82 commit e09d4a2
Showing 1 changed file with 2 additions and 14 deletions.
16 changes: 2 additions & 14 deletions huma.go
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ func Register[I, O any](api API, op Operation, handler func(context.Context, *I)

// Store raw body
if len(rawBodyIndex) > 0 {
f := getSubField(v, rawBodyIndex)
f := v.FieldByIndex(rawBodyIndex)
f.SetBytes(body)
}

Expand Down Expand Up @@ -1622,7 +1622,7 @@ func parseBodyInto(v reflect.Value, bodyIndex []int, u intoUnmarshaler, body []b
// second time is faster than `mapstructure.Decode` or any of the other
// common reflection-based approaches when using real-world medium-sized
// JSON payloads with lots of strings.
f := getSubField(v, bodyIndex)
f := v.FieldByIndex(bodyIndex)
if err := u(body, f.Addr().Interface()); err != nil {
return &ErrorDetail{
Location: "body",
Expand Down Expand Up @@ -1671,18 +1671,6 @@ func readBody(buf io.Writer, ctx Context, maxBytes int64) *contextError {
return nil
}

// getSubField extracts a nested field from v. The field of interest is
// identified by its indices.
//
// For example, getSubField(v, [3, 1]) returns v.Field[3].Field[1]
func getSubField(v reflect.Value, indices []int) reflect.Value {
f := v
for _, i := range indices {
f = f.Field(i)
}
return f
}

// AutoRegister auto-detects operation registration methods and registers them
// with the given API. Any method named `Register...` will be called and
// passed the API as the only argument. Since registration happens at
Expand Down

0 comments on commit e09d4a2

Please sign in to comment.