Skip to content

Commit 325112e

Browse files
committed
fix:improve code formatting, fix lint errors, and increase test coverage
1 parent 69d9718 commit 325112e

File tree

2 files changed

+353
-364
lines changed

2 files changed

+353
-364
lines changed

formbind/bind.go

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func Bind(dst interface{}, data url.Values) error {
7979
func parseFieldPathWithGroups(key string, groups map[string]*groupInfo) []interface{} {
8080
var parts []interface{}
8181
start := 0
82-
82+
8383
for i := 0; i < len(key); i++ {
8484
switch key[i] {
8585
case '.':
@@ -91,15 +91,15 @@ func parseFieldPathWithGroups(key string, groups map[string]*groupInfo) []interf
9191
if i > start {
9292
fieldName := key[start:i]
9393
parts = append(parts, fieldName)
94-
94+
9595
// Find the closing bracket
9696
j := i + 1
9797
for j < len(key) && key[j] != ']' {
9898
j++
9999
}
100100
if j < len(key) && j > i+1 {
101101
groupKey := key[i+1 : j]
102-
102+
103103
// Look up the group info for this field
104104
arrayFieldPath := strings.Join(getStringParts(parts), ".")
105105
if group, exists := groups[arrayFieldPath]; exists {
@@ -117,7 +117,7 @@ func parseFieldPathWithGroups(key string, groups map[string]*groupInfo) []interf
117117
return []interface{}{}
118118
}
119119
}
120-
120+
121121
i = j
122122
start = j + 1
123123
} else {
@@ -126,11 +126,11 @@ func parseFieldPathWithGroups(key string, groups map[string]*groupInfo) []interf
126126
}
127127
}
128128
}
129-
129+
130130
if start < len(key) {
131131
parts = append(parts, key[start:])
132132
}
133-
133+
134134
return parts
135135
}
136136

@@ -145,21 +145,9 @@ func getStringParts(parts []interface{}) []string {
145145
return stringParts
146146
}
147147

148-
// parseFieldPath parses a field path like "group.items[0].name" into parts.
149-
// Returns empty slice if path contains invalid indices (negative or too large).
150-
func parseFieldPath(key string) []interface{} {
151-
return parseFieldPathWithGroups(key, nil) // Use legacy behavior when no groups provided
152-
}
153148

154-
// bindNestedFormField binds a nested form field to the struct.
155-
func bindNestedFormField(val reflect.Value, typ reflect.Type, key string, values []string) error {
156-
parts := parseFieldPath(key)
157-
if len(parts) == 0 {
158-
// Invalid path, ignore silently
159-
return nil
160-
}
161-
return setValueByParts(val, typ, parts, values[0])
162-
}
149+
150+
163151

164152
// bindNestedFormFieldWithGroups binds a nested form field using group-aware parsing
165153
func bindNestedFormFieldWithGroups(val reflect.Value, typ reflect.Type, key string, values []string, groups map[string]*groupInfo) error {
@@ -173,19 +161,19 @@ func bindNestedFormFieldWithGroups(val reflect.Value, typ reflect.Type, key stri
173161

174162
// groupInfo holds information about array group keys
175163
type groupInfo struct {
176-
keys []string // distinct keys found (e.g., ["0", "5", "10"])
164+
keys []string // distinct keys found (e.g., ["0", "5", "10"])
177165
keyToIdx map[string]int // mapping from key to array index
178166
}
179167

180168
// collectArrayGroups analyzes form data to collect array grouping information
181169
func collectArrayGroups(data map[string][]string) map[string]*groupInfo {
182170
groups := make(map[string]*groupInfo)
183-
171+
184172
for key := range data {
185173
if !strings.Contains(key, "[") {
186174
continue
187175
}
188-
176+
189177
// Extract array field path and grouping key
190178
if arrayField, groupKey := extractArrayGroup(key); arrayField != "" && groupKey != "" {
191179
if groups[arrayField] == nil {
@@ -194,15 +182,15 @@ func collectArrayGroups(data map[string][]string) map[string]*groupInfo {
194182
keyToIdx: make(map[string]int),
195183
}
196184
}
197-
185+
198186
group := groups[arrayField]
199187
if _, exists := group.keyToIdx[groupKey]; !exists {
200188
group.keyToIdx[groupKey] = len(group.keys)
201189
group.keys = append(group.keys, groupKey)
202190
}
203191
}
204192
}
205-
193+
206194
return groups
207195
}
208196

@@ -214,21 +202,21 @@ func extractArrayGroup(key string) (arrayField, groupKey string) {
214202
if start == -1 {
215203
return "", ""
216204
}
217-
205+
218206
end := strings.Index(key[start:], "]")
219207
if end == -1 {
220208
return "", ""
221209
}
222210
end += start
223-
211+
224212
arrayField = key[:start]
225213
groupKey = key[start+1 : end]
226-
214+
227215
// Validate grouping key (should be reasonable)
228216
if len(groupKey) == 0 || len(groupKey) > 20 {
229217
return "", ""
230218
}
231-
219+
232220
return arrayField, groupKey
233221
}
234222

@@ -455,13 +443,13 @@ func setTimeField(value string, field reflect.Value) error {
455443
func setSliceField(value string, field reflect.Value) error {
456444
slice := reflect.MakeSlice(field.Type(), 0, 1)
457445
elemType := field.Type().Elem()
458-
446+
459447
elem := reflect.New(elemType).Elem()
460448
if err := setWithProperType(elemType.Kind(), value, elem); err != nil {
461449
return err
462450
}
463-
451+
464452
slice = reflect.Append(slice, elem)
465453
field.Set(slice)
466454
return nil
467-
}
455+
}

0 commit comments

Comments
 (0)