Skip to content

Commit

Permalink
Merge pull request #20 from Barberrrry/fix-not-used-import-and-deep-n…
Browse files Browse the repository at this point in the history
…ested-slices

Fix unused import of "fmt" package. Fix deep nested slices.
  • Loading branch information
vpbarb authored Dec 29, 2017
2 parents 8ea5e7f + b62ae7b commit 9a95f1e
Show file tree
Hide file tree
Showing 24 changed files with 251 additions and 123 deletions.
7 changes: 6 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ language: go

go:
- 1.7
- tip
- tip

install:
- go get github.com/pkg/errors
- go get github.com/stretchr/testify/assert
- go get github.com/stretchr/testify/require
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
all:
go build
go generate
go test ./examples/...
18 changes: 8 additions & 10 deletions examples/aliases/validators.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//This file was automatically generated by the genval generator v1.4
//This file was automatically generated by the genval generator v1.5
//Please don't modify it manually. Edit your entity tags and then
//run go generate

Expand Down Expand Up @@ -72,17 +72,15 @@ func (r User) Validate() error {
if len(r.SomeMap) < 2 {
errs.AddFieldf("SomeMap", "less items than 2")
}
for SomeMapKey, SomeMapValue := range r.SomeMap {
_ = SomeMapKey
_ = SomeMapValue
if utf8.RuneCountInString(string(SomeMapKey)) > 64 {
errs.AddFieldf(fmt.Sprintf("SomeMap"+".%v", SomeMapKey), "longer than 64 chars")
for kSomeMap, vSomeMap := range r.SomeMap {
if utf8.RuneCountInString(string(kSomeMap)) > 64 {
errs.AddFieldf(fmt.Sprintf("SomeMap"+".key[%v]", kSomeMap), "longer than 64 chars")
}
if SomeMapValue < -35 {
errs.AddFieldf(fmt.Sprintf("SomeMap"+".%v", SomeMapKey), "less than -35")
if vSomeMap < -35 {
errs.AddFieldf(fmt.Sprintf("SomeMap"+".%v", kSomeMap), "less than -35")
}
if SomeMapValue > 34 {
errs.AddFieldf(fmt.Sprintf("SomeMap"+".%v", SomeMapKey), "more than 34")
if vSomeMap > 34 {
errs.AddFieldf(fmt.Sprintf("SomeMap"+".%v", kSomeMap), "more than 34")
}
}
if r.SomePointer == nil {
Expand Down
2 changes: 1 addition & 1 deletion examples/complicated/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ func (s State) Validate() error { //overriding
if s > StateOk && s < StateError {
return nil
}
return fmt.Errorf("unrecognized state: %s", s)
return fmt.Errorf("unrecognized state: %v", s)
}
7 changes: 6 additions & 1 deletion examples/complicated/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ type User struct {
Alias DogsMapAlias
AliasOnAlias AliasOnDogsMapAlias
AliasOnAliasWithCustomValidate AliasOnDogsMapAlias `validate:"func=.ValidateAlias"`
MapOfMap map[string]map[int]string `validate:"value=[min_items=1,value=[min_len=3]]"`
MapOfMap map[string]map[int]string `validate:"key=[min_len=3],value=[min_items=1,value=[min_len=3]]"`
MapOfSlice map[string][]string `validate:"value=[min_items=1,item=[max_len=256]]"`
SliceOfMap []map[string]int `validate:"item=[min_items=1,key=[min_len=3],value=[min=1]]"`
SliceOfSliceOfSlice [][][]string `validate:"min_items=1,item=[min_items=1,item=[min_items=1,item=[max_len=256]]]"`
MapWithoutValidation map[string]interface{}
SliceWithoutValidation []interface{}
FuncField func(int) string
ChanField <-chan int
ByteField byte
Expand Down
66 changes: 66 additions & 0 deletions examples/complicated/entities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,28 @@ func Test_Request1_Validate(t *testing.T) {
"test": 1,
"test_2": 2,
},
MapOfMap: map[string]map[int]string{
"key": {
1: "value",
},
},
MapOfSlice: map[string][]string{
"key": {
"value",
},
},
SliceOfMap: []map[string]int{
{
"key": 1,
},
},
SliceOfSliceOfSlice: [][][]string{
{
{
"value",
},
},
},
}

t.Run("valid", func(t *testing.T) {
Expand Down Expand Up @@ -129,5 +151,49 @@ func Test_Request1_Validate(t *testing.T) {
err := r.Validate()
require.NoError(t, err)
})

t.Run("MapOfMap: invalid value", func(t *testing.T) {
r := validUser
r.MapOfMap = map[string]map[int]string{
"key": {
1: "v",
},
}

err := r.Validate()
require.NotNil(t, err)
assert.Equal(t, `[MapOfMap.key.1: shorter than 3 chars]`, err.Error())
})

t.Run("SliceOfMap: invalid key", func(t *testing.T) {
r := validUser
r.SliceOfMap = []map[string]int{
{
"k": 1,
},
}

err := r.Validate()
require.NotNil(t, err)
assert.Equal(t, `[SliceOfMap.0.key[k]: shorter than 3 chars]`, err.Error())
})

t.Run("SliceOfSliceOfSlice: invalid length", func(t *testing.T) {
r := validUser
r.SliceOfSliceOfSlice = [][][]string{
{
{
"value",
},
},
{
{},
},
}

err := r.Validate()
require.NotNil(t, err)
assert.Equal(t, `[SliceOfSliceOfSlice.1.0: less items than 1]`, err.Error())
})
})
}
134 changes: 80 additions & 54 deletions examples/complicated/validators.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//This file was automatically generated by the genval generator v1.4
//This file was automatically generated by the genval generator v1.5
//Please don't modify it manually. Edit your entity tags and then
//run go generate

Expand Down Expand Up @@ -46,11 +46,9 @@ func (r Dog) Validate() error {
// Validate validates DogsMapAlias
func (r DogsMapAlias) Validate() error {
var errs errlist.List
for rKey, rValue := range r {
_ = rKey
_ = rValue
if err := rValue.Validate(); err != nil {
errs.AddField(fmt.Sprintf("r"+".%v", rKey), err)
for kr, vr := range r {
if err := vr.Validate(); err != nil {
errs.AddField(fmt.Sprintf("r"+".%v", kr), err)
}
}
return errs.ErrorOrNil()
Expand Down Expand Up @@ -122,34 +120,28 @@ func (r User) Validate() error {
if len(r.Urls) < 1 {
errs.AddFieldf("Urls", "less items than 1")
}
for i, x := range r.Urls {
_ = i
_ = x
if utf8.RuneCountInString(string(x)) > 256 {
errs.AddFieldf(fmt.Sprintf("Urls.%v", i), "longer than 256 chars")
for kUrls, vUrls := range r.Urls {
if utf8.RuneCountInString(string(vUrls)) > 256 {
errs.AddFieldf(fmt.Sprintf("Urls"+".%v", kUrls), "longer than 256 chars")
}
}
if len(r.Dogs) < 1 {
errs.AddFieldf("Dogs", "less items than 1")
}
for i, x := range r.Dogs {
_ = i
_ = x
if x != nil {
if err := x.Validate(); err != nil {
errs.AddField(fmt.Sprintf("Dogs.%v", i), err)
for kDogs, vDogs := range r.Dogs {
if vDogs != nil {
if err := vDogs.Validate(); err != nil {
errs.AddField(fmt.Sprintf("Dogs"+".%v", kDogs), err)
}
}
}
if r.Test != nil {
if len(*r.Test) < 1 {
errs.AddFieldf("Test", "less items than 1")
}
for i, x := range *r.Test {
_ = i
_ = x
if x < 4 {
errs.AddFieldf(fmt.Sprintf("Test.%v", i), "less than 4")
for kTest, vTest := range *r.Test {
if vTest < 4 {
errs.AddFieldf(fmt.Sprintf("Test"+".%v", kTest), "less than 4")
}
}
}
Expand All @@ -159,37 +151,31 @@ func (r User) Validate() error {
if len(r.SomeArray) < 1 {
errs.AddFieldf("SomeArray", "less items than 1")
}
for i, x := range r.SomeArray {
_ = i
_ = x
if err := validateSome(x); err != nil {
errs.AddField(fmt.Sprintf("SomeArray.%v", i), err)
for kSomeArray, vSomeArray := range r.SomeArray {
if err := validateSome(vSomeArray); err != nil {
errs.AddField(fmt.Sprintf("SomeArray"+".%v", kSomeArray), err)
}
}
if len(r.Dict) < 2 {
errs.AddFieldf("Dict", "less items than 2")
}
for DictKey, DictValue := range r.Dict {
_ = DictKey
_ = DictValue
if utf8.RuneCountInString(string(DictKey)) > 64 {
errs.AddFieldf(fmt.Sprintf("Dict"+".%v", DictKey), "longer than 64 chars")
for kDict, vDict := range r.Dict {
if utf8.RuneCountInString(string(kDict)) > 64 {
errs.AddFieldf(fmt.Sprintf("Dict"+".key[%v]", kDict), "longer than 64 chars")
}
if DictValue < -35 {
errs.AddFieldf(fmt.Sprintf("Dict"+".%v", DictKey), "less than -35")
if vDict < -35 {
errs.AddFieldf(fmt.Sprintf("Dict"+".%v", kDict), "less than -35")
}
if DictValue > 34 {
errs.AddFieldf(fmt.Sprintf("Dict"+".%v", DictKey), "more than 34")
if vDict > 34 {
errs.AddFieldf(fmt.Sprintf("Dict"+".%v", kDict), "more than 34")
}
}
for DictDogsKey, DictDogsValue := range r.DictDogs {
_ = DictDogsKey
_ = DictDogsValue
if err := DictDogsValue.ValidateOptional(); err != nil {
errs.AddField(fmt.Sprintf("DictDogs"+".%v", DictDogsKey), err)
for kDictDogs, vDictDogs := range r.DictDogs {
if err := vDictDogs.ValidateOptional(); err != nil {
errs.AddField(fmt.Sprintf("DictDogs"+".%v", kDictDogs), err)
}
if err := validateMaxDogName(DictDogsValue); err != nil {
errs.AddField(fmt.Sprintf("DictDogs"+".%v", DictDogsKey), err)
if err := validateMaxDogName(vDictDogs); err != nil {
errs.AddField(fmt.Sprintf("DictDogs"+".%v", kDictDogs), err)
}
}
if err := r.Alias.Validate(); err != nil {
Expand All @@ -201,17 +187,57 @@ func (r User) Validate() error {
if err := r.AliasOnAliasWithCustomValidate.ValidateAlias(); err != nil {
errs.AddField("AliasOnAliasWithCustomValidate", err)
}
for MapOfMapKey, MapOfMapValue := range r.MapOfMap {
_ = MapOfMapKey
_ = MapOfMapValue
if len(MapOfMapValue) < 1 {
errs.AddFieldf(fmt.Sprintf("MapOfMap"+".%v", MapOfMapKey), "less items than 1")
}
for MapOfMapValueKey, MapOfMapValueValue := range MapOfMapValue {
_ = MapOfMapValueKey
_ = MapOfMapValueValue
if utf8.RuneCountInString(string(MapOfMapValueValue)) < 3 {
errs.AddFieldf(fmt.Sprintf(fmt.Sprintf("MapOfMap"+".%v", MapOfMapKey)+".%v", MapOfMapValueKey), "shorter than 3 chars")
for kMapOfMap, vMapOfMap := range r.MapOfMap {
if utf8.RuneCountInString(string(kMapOfMap)) < 3 {
errs.AddFieldf(fmt.Sprintf("MapOfMap"+".key[%v]", kMapOfMap), "shorter than 3 chars")
}
if len(vMapOfMap) < 1 {
errs.AddFieldf(fmt.Sprintf("MapOfMap"+".%v", kMapOfMap), "less items than 1")
}
for kvMapOfMap, vvMapOfMap := range vMapOfMap {
if utf8.RuneCountInString(string(vvMapOfMap)) < 3 {
errs.AddFieldf(fmt.Sprintf(fmt.Sprintf("MapOfMap"+".%v", kMapOfMap)+".%v", kvMapOfMap), "shorter than 3 chars")
}
}
}
for kMapOfSlice, vMapOfSlice := range r.MapOfSlice {
if len(vMapOfSlice) < 1 {
errs.AddFieldf(fmt.Sprintf("MapOfSlice"+".%v", kMapOfSlice), "less items than 1")
}
for kvMapOfSlice, vvMapOfSlice := range vMapOfSlice {
if utf8.RuneCountInString(string(vvMapOfSlice)) > 256 {
errs.AddFieldf(fmt.Sprintf(fmt.Sprintf("MapOfSlice"+".%v", kMapOfSlice)+".%v", kvMapOfSlice), "longer than 256 chars")
}
}
}
for kSliceOfMap, vSliceOfMap := range r.SliceOfMap {
if len(vSliceOfMap) < 1 {
errs.AddFieldf(fmt.Sprintf("SliceOfMap"+".%v", kSliceOfMap), "less items than 1")
}
for kvSliceOfMap, vvSliceOfMap := range vSliceOfMap {
if utf8.RuneCountInString(string(kvSliceOfMap)) < 3 {
errs.AddFieldf(fmt.Sprintf(fmt.Sprintf("SliceOfMap"+".%v", kSliceOfMap)+".key[%v]", kvSliceOfMap), "shorter than 3 chars")
}
if vvSliceOfMap < 1 {
errs.AddFieldf(fmt.Sprintf(fmt.Sprintf("SliceOfMap"+".%v", kSliceOfMap)+".%v", kvSliceOfMap), "less than 1")
}
}
}
if len(r.SliceOfSliceOfSlice) < 1 {
errs.AddFieldf("SliceOfSliceOfSlice", "less items than 1")
}
for kSliceOfSliceOfSlice, vSliceOfSliceOfSlice := range r.SliceOfSliceOfSlice {
if len(vSliceOfSliceOfSlice) < 1 {
errs.AddFieldf(fmt.Sprintf("SliceOfSliceOfSlice"+".%v", kSliceOfSliceOfSlice), "less items than 1")
}
for kvSliceOfSliceOfSlice, vvSliceOfSliceOfSlice := range vSliceOfSliceOfSlice {
if len(vvSliceOfSliceOfSlice) < 1 {
errs.AddFieldf(fmt.Sprintf(fmt.Sprintf("SliceOfSliceOfSlice"+".%v", kSliceOfSliceOfSlice)+".%v", kvSliceOfSliceOfSlice), "less items than 1")
}
for kvvSliceOfSliceOfSlice, vvvSliceOfSliceOfSlice := range vvSliceOfSliceOfSlice {
if utf8.RuneCountInString(string(vvvSliceOfSliceOfSlice)) > 256 {
errs.AddFieldf(fmt.Sprintf(fmt.Sprintf(fmt.Sprintf("SliceOfSliceOfSlice"+".%v", kSliceOfSliceOfSlice)+".%v", kvSliceOfSliceOfSlice)+".%v", kvvSliceOfSliceOfSlice), "longer than 256 chars")
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/empty/validators.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//This file was automatically generated by the genval generator v1.4
//This file was automatically generated by the genval generator v1.5
//Please don't modify it manually. Edit your entity tags and then
//run go generate

Expand Down
2 changes: 1 addition & 1 deletion examples/overriding/validators.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//This file was automatically generated by the genval generator v1.4
//This file was automatically generated by the genval generator v1.5
//Please don't modify it manually. Edit your entity tags and then
//run go generate

Expand Down
2 changes: 1 addition & 1 deletion examples/simple/entities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func Test_User_Validate(t *testing.T) {

err := user.Validate()
require.NotNil(t, err)
assert.Equal(t, "[Emails.1111: more than 3, Emails.1111: shorter than 5 chars]", err.Error())
assert.Equal(t, "[Emails.key[1111]: more than 3, Emails.1111: shorter than 5 chars]", err.Error())
})

t.Run("too young", func(t *testing.T) {
Expand Down
14 changes: 6 additions & 8 deletions examples/simple/validators.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//This file was automatically generated by the genval generator v1.4
//This file was automatically generated by the genval generator v1.5
//Please don't modify it manually. Edit your entity tags and then
//run go generate

Expand Down Expand Up @@ -69,14 +69,12 @@ func (r User) Validate() error {
if len(r.Emails) < 1 {
errs.AddFieldf("Emails", "less items than 1")
}
for EmailsKey, EmailsValue := range r.Emails {
_ = EmailsKey
_ = EmailsValue
if EmailsKey > 3 {
errs.AddFieldf(fmt.Sprintf("Emails"+".%v", EmailsKey), "more than 3")
for kEmails, vEmails := range r.Emails {
if kEmails > 3 {
errs.AddFieldf(fmt.Sprintf("Emails"+".key[%v]", kEmails), "more than 3")
}
if utf8.RuneCountInString(string(EmailsValue)) < 5 {
errs.AddFieldf(fmt.Sprintf("Emails"+".%v", EmailsKey), "shorter than 5 chars")
if utf8.RuneCountInString(string(vEmails)) < 5 {
errs.AddFieldf(fmt.Sprintf("Emails"+".%v", kEmails), "shorter than 5 chars")
}
}
if r.Title == nil {
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

const (
version = "1.4"
version = "1.5"
)

var (
Expand Down
Loading

0 comments on commit 9a95f1e

Please sign in to comment.