From 5bc13a75fee7266fd662babcc675ad8e6eddfc8b Mon Sep 17 00:00:00 2001 From: Alex Bice Date: Mon, 11 Feb 2019 13:59:26 -0700 Subject: [PATCH] Code cleanup (#8) *Code cleanup --- .circleci/config.yml | 50 +++---- benchmark_failfast.md | 32 ++--- benchmark_nooptions.md | 32 ++--- benchmark_noprealloc.md | 32 ++--- benchmark_playground.md | 28 ++-- generator/assets.go | 44 +++---- generator/gencheck.go | 186 +++++++++++++++------------ glide.lock | 53 +++++--- glide.yaml | 4 +- internal/benchmark/benchmark_test.go | 10 +- internal/example/example_test.go | 20 +-- uuid_test.go | 8 +- 12 files changed, 258 insertions(+), 241 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 4fcaa3e..d653aa0 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -1,10 +1,8 @@ version: 2 -jobs: - golang_1.9: - working_directory: /go/src/github.com/abice/gencheck - docker: - - image: circleci/golang:1.9 - steps: + +jobBase: &jobBase + working_directory: /go/src/github.com/abice/gencheck + steps: - checkout - run: name: Install Go Deps @@ -25,35 +23,25 @@ jobs: make test make cover make coveralls - golang_1.10.1: - working_directory: /go/src/github.com/abice/gencheck + +jobs: + golang_1.9: + <<: *jobBase docker: - - image: circleci/golang:1.10.1 - steps: - - checkout - - run: - name: Install Go Deps - command: | - curl https://glide.sh/get | sh - go get -v github.com/kevinburke/go-bindata/... - go get -v golang.org/x/tools/cmd/cover - go get -v github.com/mattn/goveralls - go get -v github.com/modocache/gover - glide install - - run: - name: Build - command: | - make reinstall - - run: - name: Test - command: | - make test - make cover - make coveralls + - image: circleci/golang:1.9 + golang_1.10: + <<: *jobBase + docker: + - image: circleci/golang:1.10 + golang_1.11: + <<: *jobBase + docker: + - image: circleci/golang:1.11 workflows: version: 2 build_and_test: jobs: - golang_1.9 - - golang_1.10.1 \ No newline at end of file + - golang_1.10 + - golang_1.11 diff --git a/benchmark_failfast.md b/benchmark_failfast.md index 237e846..5d6bc92 100644 --- a/benchmark_failfast.md +++ b/benchmark_failfast.md @@ -3,21 +3,21 @@ goos: darwin goarch: amd64 pkg: github.com/abice/gencheck/internal/benchmark -BenchmarkCompareGencheck/UUID_Pass-8 2000000 845 ns/op 0 B/op 0 allocs/op -BenchmarkCompareGencheck/UUID_Fail-8 1000000 1153 ns/op 208 B/op 6 allocs/op -BenchmarkCompareGencheck/Hex_Pass-8 2000000 594 ns/op 0 B/op 0 allocs/op -BenchmarkCompareGencheck/Hex_Fail-8 2000000 945 ns/op 192 B/op 6 allocs/op -BenchmarkCompareGencheck/ContainsAny_Pass-8 30000000 47.8 ns/op 0 B/op 0 allocs/op -BenchmarkCompareGencheck/ContainsAny_Fail-8 10000000 224 ns/op 128 B/op 4 allocs/op -BenchmarkCompareGencheck/TestStrings_Pass-8 200000000 9.33 ns/op 0 B/op 0 allocs/op -BenchmarkCompareGencheck/TestStrings_Fail-8 10000000 173 ns/op 128 B/op 4 allocs/op -BenchmarkCompareGencheck/TestMap_Pass-8 200000000 8.77 ns/op 0 B/op 0 allocs/op -BenchmarkCompareGencheck/TestMap_Fail-8 10000000 167 ns/op 128 B/op 4 allocs/op -BenchmarkCompareGencheck/TestDive_Pass-8 100000000 23.2 ns/op 0 B/op 0 allocs/op -BenchmarkCompareGencheck/TestDive_Fail-8 1000000 1081 ns/op 800 B/op 15 allocs/op -BenchmarkCompareGencheck/TestDive_Nil-8 10000000 166 ns/op 128 B/op 4 allocs/op -BenchmarkCompareGencheck/TestAll_Pass-8 1000000 2371 ns/op 328 B/op 6 allocs/op -BenchmarkCompareGencheck/TestAll_Fail-8 10000000 175 ns/op 128 B/op 4 allocs/op +BenchmarkCompareGencheck/UUID_Pass-8 2000000 739 ns/op 0 B/op 0 allocs/op +BenchmarkCompareGencheck/UUID_Fail-8 1000000 1130 ns/op 208 B/op 6 allocs/op +BenchmarkCompareGencheck/Hex_Pass-8 3000000 538 ns/op 0 B/op 0 allocs/op +BenchmarkCompareGencheck/Hex_Fail-8 2000000 882 ns/op 192 B/op 6 allocs/op +BenchmarkCompareGencheck/ContainsAny_Pass-8 30000000 44.6 ns/op 0 B/op 0 allocs/op +BenchmarkCompareGencheck/ContainsAny_Fail-8 10000000 232 ns/op 128 B/op 4 allocs/op +BenchmarkCompareGencheck/TestStrings_Pass-8 200000000 7.11 ns/op 0 B/op 0 allocs/op +BenchmarkCompareGencheck/TestStrings_Fail-8 10000000 169 ns/op 128 B/op 4 allocs/op +BenchmarkCompareGencheck/TestMap_Pass-8 200000000 8.45 ns/op 0 B/op 0 allocs/op +BenchmarkCompareGencheck/TestMap_Fail-8 10000000 173 ns/op 128 B/op 4 allocs/op +BenchmarkCompareGencheck/TestDive_Pass-8 100000000 23.8 ns/op 0 B/op 0 allocs/op +BenchmarkCompareGencheck/TestDive_Fail-8 1000000 1271 ns/op 800 B/op 15 allocs/op +BenchmarkCompareGencheck/TestDive_Nil-8 10000000 193 ns/op 128 B/op 4 allocs/op +BenchmarkCompareGencheck/TestAll_Pass-8 500000 2682 ns/op 328 B/op 6 allocs/op +BenchmarkCompareGencheck/TestAll_Fail-8 10000000 201 ns/op 128 B/op 4 allocs/op PASS -ok github.com/abice/gencheck/internal/benchmark 31.191s +ok github.com/abice/gencheck/internal/benchmark 30.099s ``` diff --git a/benchmark_nooptions.md b/benchmark_nooptions.md index ebcef3c..c31cff2 100644 --- a/benchmark_nooptions.md +++ b/benchmark_nooptions.md @@ -3,21 +3,21 @@ goos: darwin goarch: amd64 pkg: github.com/abice/gencheck/internal/benchmark -BenchmarkCompareGencheck/UUID_Pass-8 2000000 826 ns/op 16 B/op 1 allocs/op -BenchmarkCompareGencheck/UUID_Fail-8 1000000 1096 ns/op 208 B/op 6 allocs/op -BenchmarkCompareGencheck/Hex_Pass-8 3000000 579 ns/op 16 B/op 1 allocs/op -BenchmarkCompareGencheck/Hex_Fail-8 2000000 881 ns/op 192 B/op 6 allocs/op -BenchmarkCompareGencheck/ContainsAny_Pass-8 20000000 74.4 ns/op 16 B/op 1 allocs/op -BenchmarkCompareGencheck/ContainsAny_Fail-8 10000000 199 ns/op 128 B/op 4 allocs/op -BenchmarkCompareGencheck/TestStrings_Pass-8 20000000 67.0 ns/op 64 B/op 1 allocs/op -BenchmarkCompareGencheck/TestStrings_Fail-8 3000000 408 ns/op 416 B/op 10 allocs/op -BenchmarkCompareGencheck/TestMap_Pass-8 50000000 37.8 ns/op 16 B/op 1 allocs/op -BenchmarkCompareGencheck/TestMap_Fail-8 10000000 151 ns/op 128 B/op 4 allocs/op -BenchmarkCompareGencheck/TestDive_Pass-8 20000000 80.0 ns/op 32 B/op 2 allocs/op -BenchmarkCompareGencheck/TestDive_Fail-8 1000000 1016 ns/op 800 B/op 15 allocs/op -BenchmarkCompareGencheck/TestDive_Nil-8 10000000 142 ns/op 128 B/op 4 allocs/op -BenchmarkCompareGencheck/TestAll_Pass-8 500000 2424 ns/op 632 B/op 8 allocs/op -BenchmarkCompareGencheck/TestAll_Fail-8 300000 4669 ns/op 2929 B/op 56 allocs/op +BenchmarkCompareGencheck/UUID_Pass-8 2000000 818 ns/op 16 B/op 1 allocs/op +BenchmarkCompareGencheck/UUID_Fail-8 1000000 1230 ns/op 208 B/op 6 allocs/op +BenchmarkCompareGencheck/Hex_Pass-8 2000000 537 ns/op 16 B/op 1 allocs/op +BenchmarkCompareGencheck/Hex_Fail-8 2000000 862 ns/op 192 B/op 6 allocs/op +BenchmarkCompareGencheck/ContainsAny_Pass-8 20000000 94.0 ns/op 16 B/op 1 allocs/op +BenchmarkCompareGencheck/ContainsAny_Fail-8 5000000 265 ns/op 128 B/op 4 allocs/op +BenchmarkCompareGencheck/TestStrings_Pass-8 20000000 88.3 ns/op 64 B/op 1 allocs/op +BenchmarkCompareGencheck/TestStrings_Fail-8 3000000 541 ns/op 416 B/op 10 allocs/op +BenchmarkCompareGencheck/TestMap_Pass-8 30000000 54.4 ns/op 16 B/op 1 allocs/op +BenchmarkCompareGencheck/TestMap_Fail-8 10000000 211 ns/op 128 B/op 4 allocs/op +BenchmarkCompareGencheck/TestDive_Pass-8 20000000 114 ns/op 32 B/op 2 allocs/op +BenchmarkCompareGencheck/TestDive_Fail-8 1000000 1284 ns/op 800 B/op 15 allocs/op +BenchmarkCompareGencheck/TestDive_Nil-8 10000000 204 ns/op 128 B/op 4 allocs/op +BenchmarkCompareGencheck/TestAll_Pass-8 500000 2927 ns/op 632 B/op 8 allocs/op +BenchmarkCompareGencheck/TestAll_Fail-8 300000 6046 ns/op 2929 B/op 56 allocs/op PASS -ok github.com/abice/gencheck/internal/benchmark 25.966s +ok github.com/abice/gencheck/internal/benchmark 28.927s ``` diff --git a/benchmark_noprealloc.md b/benchmark_noprealloc.md index b040b33..707ec34 100644 --- a/benchmark_noprealloc.md +++ b/benchmark_noprealloc.md @@ -3,21 +3,21 @@ goos: darwin goarch: amd64 pkg: github.com/abice/gencheck/internal/benchmark -BenchmarkCompareGencheck/UUID_Pass-8 2000000 791 ns/op 0 B/op 0 allocs/op -BenchmarkCompareGencheck/UUID_Fail-8 1000000 1110 ns/op 208 B/op 6 allocs/op -BenchmarkCompareGencheck/Hex_Pass-8 3000000 537 ns/op 0 B/op 0 allocs/op -BenchmarkCompareGencheck/Hex_Fail-8 2000000 991 ns/op 192 B/op 6 allocs/op -BenchmarkCompareGencheck/ContainsAny_Pass-8 30000000 46.2 ns/op 0 B/op 0 allocs/op -BenchmarkCompareGencheck/ContainsAny_Fail-8 10000000 218 ns/op 128 B/op 4 allocs/op -BenchmarkCompareGencheck/TestStrings_Pass-8 200000000 8.83 ns/op 0 B/op 0 allocs/op -BenchmarkCompareGencheck/TestStrings_Fail-8 3000000 529 ns/op 464 B/op 12 allocs/op -BenchmarkCompareGencheck/TestMap_Pass-8 200000000 8.54 ns/op 0 B/op 0 allocs/op -BenchmarkCompareGencheck/TestMap_Fail-8 10000000 171 ns/op 128 B/op 4 allocs/op -BenchmarkCompareGencheck/TestDive_Pass-8 100000000 23.7 ns/op 0 B/op 0 allocs/op -BenchmarkCompareGencheck/TestDive_Fail-8 1000000 1067 ns/op 800 B/op 15 allocs/op -BenchmarkCompareGencheck/TestDive_Nil-8 10000000 155 ns/op 128 B/op 4 allocs/op -BenchmarkCompareGencheck/TestAll_Pass-8 500000 2300 ns/op 328 B/op 6 allocs/op -BenchmarkCompareGencheck/TestAll_Fail-8 300000 5588 ns/op 3649 B/op 61 allocs/op +BenchmarkCompareGencheck/UUID_Pass-8 2000000 855 ns/op 0 B/op 0 allocs/op +BenchmarkCompareGencheck/UUID_Fail-8 1000000 1161 ns/op 208 B/op 6 allocs/op +BenchmarkCompareGencheck/Hex_Pass-8 3000000 507 ns/op 0 B/op 0 allocs/op +BenchmarkCompareGencheck/Hex_Fail-8 2000000 861 ns/op 192 B/op 6 allocs/op +BenchmarkCompareGencheck/ContainsAny_Pass-8 30000000 43.2 ns/op 0 B/op 0 allocs/op +BenchmarkCompareGencheck/ContainsAny_Fail-8 10000000 236 ns/op 128 B/op 4 allocs/op +BenchmarkCompareGencheck/TestStrings_Pass-8 200000000 7.11 ns/op 0 B/op 0 allocs/op +BenchmarkCompareGencheck/TestStrings_Fail-8 2000000 594 ns/op 464 B/op 12 allocs/op +BenchmarkCompareGencheck/TestMap_Pass-8 200000000 8.49 ns/op 0 B/op 0 allocs/op +BenchmarkCompareGencheck/TestMap_Fail-8 10000000 175 ns/op 128 B/op 4 allocs/op +BenchmarkCompareGencheck/TestDive_Pass-8 50000000 24.0 ns/op 0 B/op 0 allocs/op +BenchmarkCompareGencheck/TestDive_Fail-8 1000000 1159 ns/op 800 B/op 15 allocs/op +BenchmarkCompareGencheck/TestDive_Nil-8 10000000 167 ns/op 128 B/op 4 allocs/op +BenchmarkCompareGencheck/TestAll_Pass-8 1000000 2300 ns/op 328 B/op 6 allocs/op +BenchmarkCompareGencheck/TestAll_Fail-8 300000 5777 ns/op 3649 B/op 61 allocs/op PASS -ok github.com/abice/gencheck/internal/benchmark 29.810s +ok github.com/abice/gencheck/internal/benchmark 29.262s ``` diff --git a/benchmark_playground.md b/benchmark_playground.md index 31d4bf9..b3ccb49 100644 --- a/benchmark_playground.md +++ b/benchmark_playground.md @@ -3,21 +3,21 @@ goos: darwin goarch: amd64 pkg: github.com/abice/gencheck/internal/benchmark -BenchmarkComparePlayground/UUID_Pass-8 1000000 1081 ns/op 16 B/op 1 allocs/op -BenchmarkComparePlayground/UUID_Fail-8 1000000 1307 ns/op 224 B/op 5 allocs/op -BenchmarkComparePlayground/Hex_Pass-8 2000000 712 ns/op 0 B/op 0 allocs/op -BenchmarkComparePlayground/Hex_Fail-8 2000000 971 ns/op 208 B/op 4 allocs/op -BenchmarkComparePlayground/ContainsAny_Pass-8 10000000 206 ns/op 0 B/op 0 allocs/op -BenchmarkComparePlayground/ContainsAny_Fail-8 3000000 426 ns/op 224 B/op 4 allocs/op -BenchmarkComparePlayground/TestStrings_Pass-8 3000000 443 ns/op 16 B/op 1 allocs/op -BenchmarkComparePlayground/TestStrings_Fail-8 1000000 1209 ns/op 816 B/op 13 allocs/op +BenchmarkComparePlayground/UUID_Pass-8 2000000 880 ns/op 16 B/op 1 allocs/op +BenchmarkComparePlayground/UUID_Fail-8 1000000 1168 ns/op 224 B/op 5 allocs/op +BenchmarkComparePlayground/Hex_Pass-8 2000000 621 ns/op 0 B/op 0 allocs/op +BenchmarkComparePlayground/Hex_Fail-8 2000000 850 ns/op 208 B/op 4 allocs/op +BenchmarkComparePlayground/ContainsAny_Pass-8 10000000 170 ns/op 0 B/op 0 allocs/op +BenchmarkComparePlayground/ContainsAny_Fail-8 3000000 412 ns/op 224 B/op 4 allocs/op +BenchmarkComparePlayground/TestStrings_Pass-8 3000000 425 ns/op 16 B/op 1 allocs/op +BenchmarkComparePlayground/TestStrings_Fail-8 1000000 1240 ns/op 816 B/op 13 allocs/op --- SKIP: BenchmarkComparePlayground/TestMap_Pass --- SKIP: BenchmarkComparePlayground/TestMap_Fail -BenchmarkComparePlayground/TestDive_Pass-8 5000000 324 ns/op 32 B/op 2 allocs/op -BenchmarkComparePlayground/TestDive_Fail-8 3000000 568 ns/op 272 B/op 7 allocs/op -BenchmarkComparePlayground/TestDive_Nil-8 5000000 321 ns/op 208 B/op 4 allocs/op -BenchmarkComparePlayground/TestAll_Pass-8 500000 3886 ns/op 408 B/op 11 allocs/op -BenchmarkComparePlayground/TestAll_Fail-8 300000 5855 ns/op 4209 B/op 51 allocs/op +BenchmarkComparePlayground/TestDive_Pass-8 5000000 327 ns/op 32 B/op 2 allocs/op +BenchmarkComparePlayground/TestDive_Fail-8 3000000 591 ns/op 272 B/op 7 allocs/op +BenchmarkComparePlayground/TestDive_Nil-8 5000000 338 ns/op 208 B/op 4 allocs/op +BenchmarkComparePlayground/TestAll_Pass-8 500000 4019 ns/op 408 B/op 11 allocs/op +BenchmarkComparePlayground/TestAll_Fail-8 200000 6275 ns/op 4209 B/op 51 allocs/op PASS -ok github.com/abice/gencheck/internal/benchmark 24.440s +ok github.com/abice/gencheck/internal/benchmark 24.609s ``` diff --git a/generator/assets.go b/generator/assets.go index 8879df1..c4f97e3 100644 --- a/generator/assets.go +++ b/generator/assets.go @@ -1,25 +1,25 @@ // Code generated by go-bindata. DO NOT EDIT. // sources: -// template/bcp47.tmpl -// template/cidr.tmpl -// template/contains.tmpl -// template/containsany.tmpl -// template/dive.tmpl -// template/eq.tmpl -// template/gt.tmpl -// template/gte.tmpl -// template/hex.tmpl -// template/len.tmpl -// template/lt.tmpl -// template/lte.tmpl -// template/main.tmpl -// template/max.tmpl -// template/min.tmpl -// template/ne.tmpl -// template/notnil.tmpl -// template/required.tmpl -// template/url.tmpl -// template/uuid.tmpl +// template/bcp47.tmpl (748B) +// template/cidr.tmpl (2.197kB) +// template/contains.tmpl (1.666kB) +// template/containsany.tmpl (462B) +// template/dive.tmpl (600B) +// template/eq.tmpl (1.128kB) +// template/gt.tmpl (792B) +// template/gte.tmpl (795B) +// template/hex.tmpl (664B) +// template/len.tmpl (939B) +// template/lt.tmpl (793B) +// template/lte.tmpl (792B) +// template/main.tmpl (1.357kB) +// template/max.tmpl (1.286kB) +// template/min.tmpl (1.348kB) +// template/ne.tmpl (1.11kB) +// template/notnil.tmpl (270B) +// template/required.tmpl (1.067kB) +// template/url.tmpl (1.869kB) +// template/uuid.tmpl (909B) package generator @@ -39,7 +39,7 @@ import ( func bindataRead(data []byte, name string) ([]byte, error) { gz, err := gzip.NewReader(bytes.NewBuffer(data)) if err != nil { - return nil, fmt.Errorf("Read %q: %v", name, err) + return nil, fmt.Errorf("read %q: %v", name, err) } var buf bytes.Buffer @@ -47,7 +47,7 @@ func bindataRead(data []byte, name string) ([]byte, error) { clErr := gz.Close() if err != nil { - return nil, fmt.Errorf("Read %q: %v", name, err) + return nil, fmt.Errorf("read %q: %v", name, err) } if clErr != nil { return nil, err diff --git a/generator/gencheck.go b/generator/gencheck.go index 4a74476..2533484 100644 --- a/generator/gencheck.go +++ b/generator/gencheck.go @@ -11,9 +11,8 @@ import ( "strings" "text/template" - "golang.org/x/tools/imports" - "github.com/Masterminds/sprig" + "golang.org/x/tools/imports" ) const ( @@ -198,78 +197,40 @@ func (g *Generator) Generate(f *ast.File) ([]byte, error) { } g.getTypeString(&f) - if field.Tag != nil { - if strings.Contains(field.Tag.Value, validateTag) { - - // The AST keeps the rune marker on the string, so we trim them off - str := strings.Trim(field.Tag.Value, "`") - // Separate tag types are separated by spaces, so split on that - vals := strings.Split(str, " ") - for _, val := range vals { - // Only parse out the valid: tag - if strings.HasPrefix(val, validateTag) { - // Strip off the valid: prefix and the quotation marks - ruleStr := val[len(validateTag)+1 : len(val)-1] - // Split on commas for multiple validations - fieldRules := strings.Split(ruleStr, ",") - - // Store the validation as the duplication check so that *if* in the future we want to support certain rules as - // having duplicates, we can do that easier. - dupecheck := make(map[string]Validation) - for _, rule := range fieldRules { - // Check for fail fast and ignore that rule - if rule == failFastFlag { - f.FailFast = true - continue - } - // Rules are able to have parameters, - // but will have an = in them if that is the case. - - v := Validation{ - Name: rule, - FieldName: f.Name, - F: f.F, - FieldType: f.Type, - StructName: name, - Prealloc: g.noPrealloc, - } - - if strings.Contains(rule, `=`) { - // There is a parameter, so get the rule name, and the parameter - temp := strings.Split(rule, `=`) - v.Name = temp[0] - v.Param = temp[1] - } - - // Only keep the rule if it is a known template - if _, ok := g.knownTemplates[v.Name]; ok { - // Make sure it's not a duplicate rule - if _, isDupe := dupecheck[v.Name]; isDupe { - return nil, fmt.Errorf("Duplicate rules are not allowed: '%s' on field '%s'", v.Name, f.Name) - } - dupecheck[v.Name] = v - - f.Rules = append(f.Rules, v) - } else { - fmt.Printf("Skipping unknown validation template: '%s'\n", v.Name) - } - } - - if f.FailFast || g.failFast { - for index, val := range f.Rules { - val.FailFast = true - f.Rules[index] = val - } - } - } - } - - // If we have any rules for the field, add it to the map - if len(f.Rules) > 0 { - rules = append(rules, f) - } + // Skip untagged fields + if field.Tag == nil { + continue + } + + // Skip fields that aren't validated + if !strings.Contains(field.Tag.Value, validateTag) { + continue + } + + // The AST keeps the rune marker on the string, so we trim them off + str := strings.Trim(field.Tag.Value, "`") + // Separate tag types are separated by spaces, so split on that + vals := strings.Split(str, " ") + for _, val := range vals { + // Only parse out the valid: tag + if !strings.HasPrefix(val, validateTag) { + continue + } + // Strip off the valid: prefix and the quotation marks + ruleStr := val[len(validateTag)+1 : len(val)-1] + // Split on commas for multiple validations + fieldRules := strings.Split(ruleStr, ",") + + parseErr := g.parseFieldRules(&f, name, fieldRules) + if parseErr != nil { + return nil, parseErr } } + + // If we have any rules for the field, add it to the map + if len(f.Rules) > 0 { + rules = append(rules, f) + } } data := map[string]interface{}{ @@ -301,6 +262,59 @@ func (g *Generator) Generate(f *ast.File) ([]byte, error) { return formatted, err } +func (g *Generator) parseFieldRules(f *Field, structName string, fieldRules []string) error { + // Store the validation as the duplication check so that *if* in the future we want to support certain rules as + // having duplicates, we can do that easier. + dupecheck := make(map[string]Validation) + for _, rule := range fieldRules { + // Check for fail fast and ignore that rule + if rule == failFastFlag { + f.FailFast = true + continue + } + // Rules are able to have parameters, + // but will have an = in them if that is the case. + + v := Validation{ + Name: rule, + FieldName: f.Name, + F: f.F, + FieldType: f.Type, + StructName: structName, + Prealloc: g.noPrealloc, + } + + if strings.Contains(rule, `=`) { + // There is a parameter, so get the rule name, and the parameter + temp := strings.Split(rule, `=`) + v.Name = temp[0] + v.Param = temp[1] + } + + // Only keep the rule if it is a known template + if _, ok := g.knownTemplates[v.Name]; ok { + // Make sure it's not a duplicate rule + if _, isDupe := dupecheck[v.Name]; isDupe { + return fmt.Errorf("Duplicate rules are not allowed: '%s' on field '%s'", v.Name, f.Name) + } + dupecheck[v.Name] = v + + f.Rules = append(f.Rules, v) + } else { + fmt.Printf("Skipping unknown validation template: '%s'\n", v.Name) + } + } + + if f.FailFast || g.failFast { + for index, val := range f.Rules { + val.FailFast = true + f.Rules[index] = val + } + } + + return nil +} + func (g *Generator) getTypeString(f *Field) { tString := g.getStringForExpr(f.F.Type) f.Type = tString @@ -348,18 +362,22 @@ func (g *Generator) inspect(f *ast.File) map[string]*ast.StructType { ast.Inspect(f, func(n ast.Node) bool { switch x := n.(type) { case *ast.Ident: - if x.Obj != nil { - // Make sure it's a Type Identifier - if x.Obj.Kind == ast.Typ { - // Make sure it's a spec (Type Identifiers can be throughout the code) - if ts, ok := x.Obj.Decl.(*ast.TypeSpec); ok { - // Only store the struct types (we don't do anything for interfaces) - if sts, store := ts.Type.(*ast.StructType); store { - structs[x.Name] = sts - g.knownStructs[x.Name] = sts - } - } - } + if x.Obj == nil { + return true + } + // Make sure it's a Type Identifier + if x.Obj.Kind != ast.Typ { + return true + } + // Make sure it's a spec (Type Identifiers can be throughout the code) + ts, ok := x.Obj.Decl.(*ast.TypeSpec) + if !ok { + return true + } + // Only store the struct types (we don't do anything for interfaces) + if sts, store := ts.Type.(*ast.StructType); store { + structs[x.Name] = sts + g.knownStructs[x.Name] = sts } } // Return true to continue through the tree diff --git a/glide.lock b/glide.lock index f1cc9df..ece5d65 100644 --- a/glide.lock +++ b/glide.lock @@ -1,28 +1,30 @@ -hash: 5e255fd4a59e76f9cec260d2ee90e53144f85ce022db504cce655ad2204af1c5 -updated: 2018-02-28T10:24:34.094469617-07:00 +hash: 27aed37d7934fd5a9c0bd302aa1bbe60551ce38043572407a8bd93e85b732ab3 +updated: 2019-02-11T13:30:47.974438-07:00 imports: - name: github.com/aokoli/goutils - version: 3391d3790d23d03408670993e957e8f408993c34 + version: 41ac8693c5c10a92ea1ff5ac3a7f95646f6123b0 - name: github.com/Bowery/prompt - version: 94b504f42fdd503acc3b3c79ec2b517d90e0de8a + version: 8a1d5376df1cbec3468f2138fecc44dd8b48e342 +- name: github.com/google/uuid + version: 064e2069ce9c359c118179501254f67d7d37ba24 - name: github.com/huandu/xstrings - version: 2bf18b218c51864a87384c06996e40ff9dcff8e1 + version: f02667b379e2fb5916c3cda2cf31e0eb885d79f8 - name: github.com/imdario/mergo version: 7fe0c75c13abdee74b09fcacef5ea1c6bba6a874 - name: github.com/labstack/gommon - version: 058e16e77bbd66cc101dfeae2971b88add6ce266 + version: 82ef680aef5189b68682876cf70d09daa4ac0f51 subpackages: - color - name: github.com/Masterminds/semver version: 59c29afe1a994eacb71c833025ca7acf874bb1da - name: github.com/Masterminds/sprig - version: b217b9c388de2cacde4354c536e520c52c055563 + version: 544a9b1d90f323f6509491b389714fbbd126bee3 - name: github.com/mattn/go-colorable - version: 7dc3415be66d7cc68bf0182f35c8d31f8d2ad8a7 + version: efa589957cd060542a26d2dd7832fd6a6c6c3ade - name: github.com/mattn/go-isatty - version: 6ca4dbf54d38eea1a992b3c722a76a5d1c4cb25c + version: 3fb116b820352b7f0c281308a4d6250c22d94e27 - name: github.com/mkideal/cli - version: a9c1104566927924fdb041d198f05617492913f9 + version: a48c2cee5b5ee91096961d344681edc2083b9422 - name: github.com/mkideal/pkg version: 3e188c9e7ecc83d0fe7040a9161ce3c67885470d subpackages: @@ -30,47 +32,58 @@ imports: - expr - name: github.com/pkg/errors version: 645ef00459ed84a119197bfb8d8205042c6df63d -- name: github.com/satori/go.uuid - version: 0ca0c2cf9e6f8d79757fdc089a34c854ad557138 - name: golang.org/x/crypto - version: 3a6c3ce65c4f0844ac396b02aebda1496fd2ac9d + version: eb0de9b17e854e9b1ccd9963efafc79862359959 subpackages: - pbkdf2 - scrypt - name: golang.org/x/sys - version: 37707fdb30a5b38865cfb95e5aab41707daec7fd + version: 62eef0e2fa9b2c385f7b2778e763486da6880d37 subpackages: - unix - name: golang.org/x/text - version: 4e4a3210bb54bb31f6ab2cdca2edcc0b50c420c1 + version: e6919f6577db79269a6443b9dc46d18f2238fb5d subpackages: + - internal/language + - internal/language/compact - internal/tag - language - name: golang.org/x/tools - version: 73e16cff9e0d4a802937444bebb562458548241d + version: 657755b003d86beb7f1e59808a410123ce89b15b subpackages: - go/ast/astutil + - go/gcexportdata + - go/internal/cgo + - go/internal/gcimporter + - go/internal/packagesdriver + - go/packages - imports + - internal/fastwalk + - internal/gopathwalk + - internal/module + - internal/semver testImports: - name: github.com/davecgh/go-spew - version: 6d212800a42e8ab5c146b8ace3490ee17e5225f9 + version: 8991bc29aa16c548c550c7ff78260e27b9ab7c73 subpackages: - spew - name: github.com/go-playground/locales - version: e4cbcb5d0652150d40ad0646651076b6bd2be4f6 + version: f63010822830b6fe52288ee52d5a1151088ce039 subpackages: - currency - name: github.com/go-playground/universal-translator version: 71201497bace774495daed26a3874fd339e0b538 +- name: github.com/leodido/go-urn + version: a67a23e1c1af3c66528573bb86a87246477277c1 - name: github.com/pmezard/go-difflib version: d8ed2627bdf02c080bf22230dbb337003b7aba2d subpackages: - difflib - name: github.com/stretchr/testify - version: 12b6f73e6084dad08a7c6e575284b177ecafbc71 + version: ffdc059bfe9ce6a4e144ba849dbedead332c6053 subpackages: - assert - require - suite - name: gopkg.in/go-playground/validator.v9 - version: 150fe5b6a4ccc9cd6ffdbf5d184b67fbea75efcc + version: 774d09c3863ffb2e137ebe4c39ba27648d208f80 diff --git a/glide.yaml b/glide.yaml index 422b815..29cec1a 100644 --- a/glide.yaml +++ b/glide.yaml @@ -1,15 +1,13 @@ package: github.com/abice/gencheck import: - package: github.com/Masterminds/sprig - version: ^2.14.1 + version: ^2.17.1 - package: github.com/mkideal/cli - package: golang.org/x/tools subpackages: - imports - package: github.com/pkg/errors version: v0.8.0 -- package: github.com/satori/go.uuid - version: 0ca0c2cf9e6f8d79757fdc089a34c854ad557138 testImport: - package: github.com/stretchr/testify version: ^1.1.4 diff --git a/internal/benchmark/benchmark_test.go b/internal/benchmark/benchmark_test.go index 91999c7..40c2bfd 100644 --- a/internal/benchmark/benchmark_test.go +++ b/internal/benchmark/benchmark_test.go @@ -6,7 +6,7 @@ import ( "time" "github.com/abice/gencheck" - uuid "github.com/satori/go.uuid" + "github.com/google/uuid" "github.com/stretchr/testify/assert" "gopkg.in/go-playground/validator.v9" ) @@ -115,8 +115,8 @@ var benchmarks = []struct { hasError bool uut gencheck.Validateable }{ - {"UUID Pass", false, TestUUID{UUID: uuid.NewV4().String()}}, - {"UUID Fail", true, TestUUID{UUID: uuid.NewV4().String() + "notauuid"}}, + {"UUID Pass", false, TestUUID{UUID: uuid.New().String()}}, + {"UUID Fail", true, TestUUID{UUID: uuid.New().String() + "notauuid"}}, {"Hex Pass", false, TestHex{Value: hexPassingString}}, {"Hex Fail", true, TestHex{Value: hexFailingString}}, {"ContainsAny Pass", false, TestContainsAny{Any: containsAnyPassingString}}, @@ -150,7 +150,7 @@ var benchmarks = []struct { NotNil: &dummyString, Contains: "The quick brown fox jumped over the lazy dog", Hex: "1234567890AbCdEf", - UUID: uuid.NewV4().String(), + UUID: uuid.New().String(), MinInt: 12345, MaxInt: 12345, URL: "http://test.com/health?whatislife=something", @@ -169,7 +169,7 @@ var benchmarks = []struct { NotNil: nil, Contains: "The quick brown cat jumped over the lazy dog", Hex: "1234567890AbCdEfG", - UUID: strings.ToUpper(uuid.NewV4().String() + "adsf"), + UUID: strings.ToUpper(uuid.New().String() + "adsf"), MinInt: 12344, MaxInt: 12346, URL: "/health?whatislife=something", diff --git a/internal/example/example_test.go b/internal/example/example_test.go index 7d0edcd..7dac71f 100644 --- a/internal/example/example_test.go +++ b/internal/example/example_test.go @@ -8,8 +8,8 @@ import ( "github.com/abice/gencheck" "github.com/abice/gencheck/internal/benchmark" + "github.com/google/uuid" "github.com/pkg/errors" - uuid "github.com/satori/go.uuid" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" ) @@ -170,9 +170,9 @@ func (s *ExampleTestSuite) TestValidateTestStruct_Values() { MinNumber: 1113.000001, MinMultiple: []string{"", "", "", "", "", "", "", ""}, UUID: "7112EE37-3219-4A26-BA01-1D230BC9257B", - UUID3: uuid.NewV3(uuid.NewV4(), "test").String(), - UUID4: strings.ToUpper(uuid.NewV4().String()), - UUID5: uuid.NewV5(uuid.NewV4(), "test").String(), + UUID3: uuid.NewMD5(uuid.NameSpaceX500, []byte("test")).String(), + UUID4: strings.ToUpper(uuid.New().String()), + UUID5: uuid.NewSHA1(uuid.NameSpaceDNS, []byte("test")).String(), MinIntPtr: &i, GteString: "1234", GteNumber: 5.5600001, @@ -226,9 +226,9 @@ func (s *ExampleTestSuite) TestValidateTestStruct_MinPtrFailure() { MinNumber: 1113.000001, MinMultiple: []string{"", "", "", "", "", "", "", ""}, UUID: "7112EE37-3219-4A26-BA01-1D230BC9257B", - UUID3: uuid.NewV3(uuid.NewV4(), "test").String(), - UUID4: uuid.NewV4().String(), - UUID5: uuid.NewV5(uuid.NewV4(), "test").String(), + UUID3: uuid.NewMD5(uuid.New(), []byte("test")).String(), + UUID4: uuid.New().String(), + UUID5: uuid.NewSHA1(uuid.New(), []byte("test")).String(), GteString: "1234", GteNumber: 5.5600001, GteMultiple: []string{"", ""}, @@ -312,9 +312,9 @@ func (s *ExampleTestSuite) TestValidateTestStruct_LteTime() { MinNumber: 1113.000001, MinMultiple: []string{"", "", "", "", "", "", "", ""}, UUID: "7112EE37-3219-4A26-BA01-1D230BC9257B", - UUID3: uuid.NewV3(uuid.NewV4(), "test").String(), - UUID4: uuid.NewV4().String(), - UUID5: uuid.NewV5(uuid.NewV4(), "test").String(), + UUID3: uuid.NewMD5(uuid.New(), []byte("test")).String(), + UUID4: uuid.New().String(), + UUID5: uuid.NewSHA1(uuid.New(), []byte("test")).String(), GteString: "1234", GteNumber: 5.5600001, GteMultiple: []string{"", ""}, diff --git a/uuid_test.go b/uuid_test.go index 2cc3b35..9f0d5b8 100644 --- a/uuid_test.go +++ b/uuid_test.go @@ -4,7 +4,7 @@ import ( "errors" "testing" - uuid "github.com/satori/go.uuid" + "github.com/google/uuid" "github.com/stretchr/testify/suite" ) @@ -41,7 +41,7 @@ func (s *UUIDTestSuite) TestIsUUID_NotUUIDTooLong() { s.Require().Equal(errors.New("'AB603c9a2a-38db-4987-932a-2f57733a29fQ' is not a UUID"), IsUUID(&str)) } func (s *UUIDTestSuite) TestIsUUID_v3() { - str := uuid.NewV3(uuid.NewV1(), "test").String() + str := uuid.NewMD5(uuid.New(), []byte("test")).String() s.NoError(IsUUID(&str)) s.NoError(IsUUIDv3(&str)) s.Error(IsUUIDv4(&str)) @@ -49,7 +49,7 @@ func (s *UUIDTestSuite) TestIsUUID_v3() { } func (s *UUIDTestSuite) TestIsUUID_v4() { - str := uuid.NewV4().String() + str := uuid.New().String() s.NoError(IsUUID(&str)) s.NoError(IsUUIDv4(&str)) s.Error(IsUUIDv3(&str)) @@ -57,7 +57,7 @@ func (s *UUIDTestSuite) TestIsUUID_v4() { } func (s *UUIDTestSuite) TestIsUUID_v5() { - str := uuid.NewV5(uuid.NewV1(), "test").String() + str := uuid.NewSHA1(uuid.NameSpaceDNS, []byte("test")).String() s.NoError(IsUUID(&str)) s.NoError(IsUUIDv5(&str)) s.Error(IsUUIDv3(&str))