Skip to content

Commit

Permalink
chore: review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Fazt01 committed Sep 17, 2024
1 parent 3963ddc commit e4a1b2e
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions http/param/param.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ func (p Parser) Parse(r *http.Request, dest any) error {
return fmt.Errorf("can only parse into struct, but got %s", v.Type().Name())
}

var fieldIndexPaths []taggedFieldIndexPath
p.findTaggedIndexPaths(v.Type(), []int{}, &fieldIndexPaths)
fieldIndexPaths := p.findTaggedIndexPaths(v.Type(), []int{}, []taggedFieldIndexPath{})

for i := range fieldIndexPaths {
// Zero the value, even if it would not be set by following path or query parameter.
Expand All @@ -112,7 +111,7 @@ func (p Parser) Parse(r *http.Request, dest any) error {
type paramType int

const (
paramTypeQuery = iota
paramTypeQuery paramType = iota
paramTypePath
)

Expand All @@ -123,7 +122,7 @@ type taggedFieldIndexPath struct {
destValue reflect.Value
}

func (p Parser) findTaggedIndexPaths(typ reflect.Type, currentNestingIndexPath []int, resultPaths *[]taggedFieldIndexPath) {
func (p Parser) findTaggedIndexPaths(typ reflect.Type, currentNestingIndexPath []int, paths []taggedFieldIndexPath) []taggedFieldIndexPath {
for i := 0; i < typ.NumField(); i++ {
typeField := typ.Field(i)
if typeField.Anonymous {
Expand All @@ -132,7 +131,7 @@ func (p Parser) findTaggedIndexPaths(typ reflect.Type, currentNestingIndexPath [
t = t.Elem()
}
if t.Kind() == reflect.Struct {
p.findTaggedIndexPaths(t, append(currentNestingIndexPath, i), resultPaths)
paths = p.findTaggedIndexPaths(t, append(currentNestingIndexPath, i), paths)
}
}
if !typeField.IsExported() {
Expand All @@ -145,7 +144,7 @@ func (p Parser) findTaggedIndexPaths(typ reflect.Type, currentNestingIndexPath [
newPath := make([]int, 0, len(currentNestingIndexPath)+1)
newPath = append(newPath, currentNestingIndexPath...)
newPath = append(newPath, i)
*resultPaths = append(*resultPaths, taggedFieldIndexPath{
paths = append(paths, taggedFieldIndexPath{
paramType: paramTypePath,
paramName: pathParamName,
indexPath: newPath,
Expand All @@ -155,23 +154,23 @@ func (p Parser) findTaggedIndexPaths(typ reflect.Type, currentNestingIndexPath [
newPath := make([]int, 0, len(currentNestingIndexPath)+1)
newPath = append(newPath, currentNestingIndexPath...)
newPath = append(newPath, i)
*resultPaths = append(*resultPaths, taggedFieldIndexPath{
paths = append(paths, taggedFieldIndexPath{
paramType: paramTypeQuery,
paramName: queryParamName,
indexPath: newPath,
})
}
}
return paths
}

func zeroPath(v reflect.Value, path *taggedFieldIndexPath) error {
for n, i := range path.indexPath {
if v.Kind() == reflect.Pointer {
v = v.Elem()
}
if v.Kind() != reflect.Struct {
return fmt.Errorf("expected to nest into struct, but got %s", v.Type().Name())
}
// findTaggedIndexPaths prepared a path.indexPath in such a way, that respective field is always
// pointer to struct or struct -> should be always able to .Field() here
typeField := v.Type().Field(i)
v = v.Field(i)

Expand Down

0 comments on commit e4a1b2e

Please sign in to comment.