-
Notifications
You must be signed in to change notification settings - Fork 8.1k
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
Support binding for slice/array obj [Rewrite] #2302
Merged
appleboy
merged 11 commits into
gin-gonic:master
from
wuhuizuo:feature/array_support_for_json_bind
Jan 3, 2021
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
bc42058
[ADD] Support binding for slice/array obj
wuhuizuo 03ce1a6
rename unit testing func
wuhuizuo 023a72b
opt switch branchs in ValidateStruct()
wuhuizuo d33cb62
Merge branch 'master' into feature/array_support_for_json_bind
wuhuizuo c078ab8
Merge branch 'master' into feature/array_support_for_json_bind
thinkerou 8ecfac8
Merge branch 'master' into feature/array_support_for_json_bind
wuhuizuo 0475d39
[FIX] SYNTAX error from conflicted merging.
wuhuizuo 1504360
format code from conflict merging
wuhuizuo c275770
Merge branch 'master' into feature/array_support_for_json_bind
appleboy 100b31c
Merge branch 'master' into feature/array_support_for_json_bind
appleboy d2db51d
Merge branch 'master' into feature/array_support_for_json_bind
appleboy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright 2020 Gin Core Team. All rights reserved. | ||
// Use of this source code is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
|
||
package binding | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
) | ||
|
||
func TestSliceValidateError(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
err sliceValidateError | ||
want string | ||
}{ | ||
{"has nil elements", sliceValidateError{errors.New("test error"), nil}, "[0]: test error"}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := tt.err.Error(); got != tt.want { | ||
t.Errorf("sliceValidateError.Error() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestDefaultValidator(t *testing.T) { | ||
type exampleStruct struct { | ||
A string `binding:"max=8"` | ||
B int `binding:"gt=0"` | ||
} | ||
tests := []struct { | ||
name string | ||
v *defaultValidator | ||
obj interface{} | ||
wantErr bool | ||
}{ | ||
{"validate nil obj", &defaultValidator{}, nil, false}, | ||
{"validate int obj", &defaultValidator{}, 3, false}, | ||
{"validate struct failed-1", &defaultValidator{}, exampleStruct{A: "123456789", B: 1}, true}, | ||
{"validate struct failed-2", &defaultValidator{}, exampleStruct{A: "12345678", B: 0}, true}, | ||
{"validate struct passed", &defaultValidator{}, exampleStruct{A: "12345678", B: 1}, false}, | ||
{"validate *struct failed-1", &defaultValidator{}, &exampleStruct{A: "123456789", B: 1}, true}, | ||
{"validate *struct failed-2", &defaultValidator{}, &exampleStruct{A: "12345678", B: 0}, true}, | ||
{"validate *struct passed", &defaultValidator{}, &exampleStruct{A: "12345678", B: 1}, false}, | ||
{"validate []struct failed-1", &defaultValidator{}, []exampleStruct{{A: "123456789", B: 1}}, true}, | ||
{"validate []struct failed-2", &defaultValidator{}, []exampleStruct{{A: "12345678", B: 0}}, true}, | ||
{"validate []struct passed", &defaultValidator{}, []exampleStruct{{A: "12345678", B: 1}}, false}, | ||
{"validate []*struct failed-1", &defaultValidator{}, []*exampleStruct{{A: "123456789", B: 1}}, true}, | ||
{"validate []*struct failed-2", &defaultValidator{}, []*exampleStruct{{A: "12345678", B: 0}}, true}, | ||
{"validate []*struct passed", &defaultValidator{}, []*exampleStruct{{A: "12345678", B: 1}}, false}, | ||
{"validate *[]struct failed-1", &defaultValidator{}, &[]exampleStruct{{A: "123456789", B: 1}}, true}, | ||
{"validate *[]struct failed-2", &defaultValidator{}, &[]exampleStruct{{A: "12345678", B: 0}}, true}, | ||
{"validate *[]struct passed", &defaultValidator{}, &[]exampleStruct{{A: "12345678", B: 1}}, false}, | ||
{"validate *[]*struct failed-1", &defaultValidator{}, &[]*exampleStruct{{A: "123456789", B: 1}}, true}, | ||
{"validate *[]*struct failed-2", &defaultValidator{}, &[]*exampleStruct{{A: "12345678", B: 0}}, true}, | ||
{"validate *[]*struct passed", &defaultValidator{}, &[]*exampleStruct{{A: "12345678", B: 1}}, false}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if err := tt.v.ValidateStruct(tt.obj); (err != nil) != tt.wantErr { | ||
t.Errorf("defaultValidator.Validate() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SYNTAX ERROR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry! it's fixed.