Skip to content

Commit 1d61bf3

Browse files
Dean KarnDean Karn
Dean Karn
authored and
Dean Karn
committed
Merge pull request #53 from bluesuncorp/v5-development
V5 development
2 parents 479d3f0 + 61e4429 commit 1d61bf3

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-4
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ _testmain.go
2222
*.exe
2323
*.test
2424
*.prof
25+
*.test
26+
*.out

validator.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,6 @@ func (v *Validate) Struct(s interface{}) *StructErrors {
166166
func (v *Validate) structRecursive(top interface{}, current interface{}, s interface{}) *StructErrors {
167167

168168
structValue := reflect.ValueOf(s)
169-
structType := reflect.TypeOf(s)
170-
structName := structType.Name()
171169

172170
if structValue.Kind() == reflect.Ptr && !structValue.IsNil() {
173171
return v.structRecursive(top, current, structValue.Elem().Interface())
@@ -177,6 +175,9 @@ func (v *Validate) structRecursive(top interface{}, current interface{}, s inter
177175
panic("interface passed for validation is not a struct")
178176
}
179177

178+
structType := reflect.TypeOf(s)
179+
structName := structType.Name()
180+
180181
validationErrors := &StructErrors{
181182
Struct: structName,
182183
Errors: map[string]*FieldError{},
@@ -339,7 +340,7 @@ func (v *Validate) fieldWithNameAndValue(val interface{}, current interface{}, f
339340
func (v *Validate) fieldWithNameAndSingleTag(val interface{}, current interface{}, f interface{}, name string, valTag string) (*FieldError, error) {
340341

341342
vals := strings.Split(valTag, tagKeySeparator)
342-
key := strings.Trim(vals[0], " ")
343+
key := strings.TrimSpace(vals[0])
343344

344345
if len(key) == 0 {
345346
panic(fmt.Sprintf("Invalid validation tag on field %s", name))
@@ -364,7 +365,7 @@ func (v *Validate) fieldWithNameAndSingleTag(val interface{}, current interface{
364365

365366
param := ""
366367
if len(vals) > 1 {
367-
param = strings.Trim(vals[1], " ")
368+
param = strings.TrimSpace(vals[1])
368369
}
369370

370371
if err := valFunc(val, current, f, param); !err {

validator_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ import (
1313
// - Run "go test" to run tests
1414
// - Run "gocov test | gocov report" to report on test converage by file
1515
// - Run "gocov test | gocov annotate -" to report on all code and functions, those ,marked with "MISS" were never called
16+
//
17+
//
18+
// go test -cpuprofile cpu.out
19+
// ./validator.test -test.bench=. -test.cpuprofile=cpu.prof
20+
// go tool pprof validator.test cpu.prof
21+
//
22+
//
23+
// go test -memprofile mem.out
1624

1725
type I interface {
1826
Foo() string
@@ -2310,3 +2318,54 @@ func TestInvalidValidatorFunction(t *testing.T) {
23102318

23112319
PanicMatches(t, func() { validate.Field(s.Test, "zzxxBadFunction") }, fmt.Sprintf("Undefined validation function on field %s", ""))
23122320
}
2321+
2322+
func BenchmarkValidateField(b *testing.B) {
2323+
for n := 0; n < b.N; n++ {
2324+
validate.Field("1", "len=1")
2325+
}
2326+
}
2327+
2328+
func BenchmarkValidateStruct(b *testing.B) {
2329+
2330+
// type Inner struct {
2331+
2332+
// }
2333+
2334+
// type Test struct {
2335+
// StringVal string `bson:"required,lt=10"`
2336+
// Int64Val int64 `bson:"gt=0,lt=10"`
2337+
// }
2338+
2339+
tFail := &TestString{
2340+
Required: "",
2341+
Len: "",
2342+
Min: "",
2343+
Max: "12345678901",
2344+
MinMax: "",
2345+
Lt: "0123456789",
2346+
Lte: "01234567890",
2347+
Gt: "1",
2348+
Gte: "1",
2349+
OmitEmpty: "12345678901",
2350+
Sub: &SubTest{
2351+
Test: "",
2352+
},
2353+
Anonymous: struct {
2354+
A string `validate:"required"`
2355+
}{
2356+
A: "",
2357+
},
2358+
Iface: &Impl{
2359+
F: "12",
2360+
},
2361+
}
2362+
2363+
// t := &Test{
2364+
// StringVal: "test",
2365+
// Int64Val: 5,
2366+
// }
2367+
2368+
for n := 0; n < b.N; n++ {
2369+
validate.Struct(tFail)
2370+
}
2371+
}

0 commit comments

Comments
 (0)