Skip to content

Commit 43d7f25

Browse files
joeybloggsjoeybloggs
authored andcommitted
update nefield and necsfield to hav own logic instead of calling !eqfield...
1 parent 55f9e44 commit 43d7f25

File tree

3 files changed

+71
-4
lines changed

3 files changed

+71
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ _testmain.go
2424
*.prof
2525
*.test
2626
*.out
27+
*.txt
2728
cover.html
2829
README.html

baked_in.go

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,13 +252,45 @@ func contains(v *Validate, topStruct reflect.Value, currentStruct reflect.Value,
252252

253253
func isNeField(v *Validate, topStruct reflect.Value, currentStruct reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
254254

255-
_, currentKind, ok := v.getStructFieldOK(currentStruct, param)
255+
currentField, currentKind, ok := v.getStructFieldOK(currentStruct, param)
256256

257257
if !ok || currentKind != fieldKind {
258258
return true
259259
}
260260

261-
return !isEqField(v, topStruct, currentStruct, field, fieldType, fieldKind, param)
261+
switch fieldKind {
262+
263+
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
264+
return field.Int() != currentField.Int()
265+
266+
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
267+
return field.Uint() != currentField.Uint()
268+
269+
case reflect.Float32, reflect.Float64:
270+
return field.Float() != currentField.Float()
271+
272+
case reflect.Slice, reflect.Map, reflect.Array:
273+
return int64(field.Len()) != int64(currentField.Len())
274+
275+
case reflect.Struct:
276+
277+
// Not Same underlying type i.e. struct and time
278+
if fieldType != currentField.Type() {
279+
return true
280+
}
281+
282+
if fieldType == timeType {
283+
284+
t := currentField.Interface().(time.Time)
285+
fieldTime := field.Interface().(time.Time)
286+
287+
return !fieldTime.Equal(t)
288+
}
289+
290+
}
291+
292+
// default reflect.String:
293+
return field.String() != currentField.String()
262294
}
263295

264296
func isNe(v *Validate, topStruct reflect.Value, currentStruct reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
@@ -431,12 +463,43 @@ func isGtCrossStructField(v *Validate, topStruct reflect.Value, current reflect.
431463

432464
func isNeCrossStructField(v *Validate, topStruct reflect.Value, current reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
433465

434-
_, currentKind, ok := v.getStructFieldOK(topStruct, param)
466+
topField, currentKind, ok := v.getStructFieldOK(topStruct, param)
435467
if !ok || currentKind != fieldKind {
436468
return true
437469
}
438470

439-
return !isEqCrossStructField(v, topStruct, current, field, fieldType, fieldKind, param)
471+
switch fieldKind {
472+
473+
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
474+
return topField.Int() != field.Int()
475+
476+
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
477+
return topField.Uint() != field.Uint()
478+
479+
case reflect.Float32, reflect.Float64:
480+
return topField.Float() != field.Float()
481+
482+
case reflect.Slice, reflect.Map, reflect.Array:
483+
return int64(topField.Len()) != int64(field.Len())
484+
485+
case reflect.Struct:
486+
487+
// Not Same underlying type i.e. struct and time
488+
if fieldType != topField.Type() {
489+
return true
490+
}
491+
492+
if fieldType == timeType {
493+
494+
t := field.Interface().(time.Time)
495+
fieldTime := topField.Interface().(time.Time)
496+
497+
return !fieldTime.Equal(t)
498+
}
499+
}
500+
501+
// default reflect.String:
502+
return topField.String() != field.String()
440503
}
441504

442505
func isEqCrossStructField(v *Validate, topStruct reflect.Value, current reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {

validator_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2914,6 +2914,9 @@ func TestIsNeFieldValidation(t *testing.T) {
29142914
errs = validate.FieldWithValue(nil, 1, "nefield")
29152915
Equal(t, errs, nil)
29162916

2917+
errs = validate.FieldWithValue(sv, now, "nefield")
2918+
Equal(t, errs, nil)
2919+
29172920
type Test2 struct {
29182921
Start *time.Time `validate:"nefield=NonExistantField"`
29192922
End *time.Time

0 commit comments

Comments
 (0)