From a20db7fc16738ea08271a80d9dba57231bee7ec1 Mon Sep 17 00:00:00 2001 From: Andrey Ivanov Date: Thu, 20 Aug 2020 10:41:44 +0300 Subject: [PATCH] HW09 is completed --- .../go-validate/main.go | 2 +- .../go-validate/main_test.go | 4 +- hw09_generator_of_validators/models/models.go | 16 +++--- .../models/models_test.go | 54 ++++++++++++------- .../models/models_validation_generated.go | 1 + 5 files changed, 49 insertions(+), 28 deletions(-) diff --git a/hw09_generator_of_validators/go-validate/main.go b/hw09_generator_of_validators/go-validate/main.go index d47af70..3d44e20 100644 --- a/hw09_generator_of_validators/go-validate/main.go +++ b/hw09_generator_of_validators/go-validate/main.go @@ -91,7 +91,7 @@ func iterStructFields(name string, s *ast.StructType, buf io.Writer) (bool, erro // Рекурсивный вызов, в случае если поле является структурой switch field.Type.(type) { case *ast.StructType: - if _, err := iterStructFields("."+field.Names[0].Name, field.Type.(*ast.StructType), buf); err != nil { + if _, err := iterStructFields(field.Names[0].Name+".", field.Type.(*ast.StructType), buf); err != nil { return false, err } } diff --git a/hw09_generator_of_validators/go-validate/main_test.go b/hw09_generator_of_validators/go-validate/main_test.go index e68c2e8..c9db0d2 100644 --- a/hw09_generator_of_validators/go-validate/main_test.go +++ b/hw09_generator_of_validators/go-validate/main_test.go @@ -164,7 +164,7 @@ func TestIterStructFields(T *testing.T) { } T.Run("Fine structure", func(t *testing.T) { buf := bytes.NewBufferString("") - err := iterStructFields(s, buf) + _, err := iterStructFields("", s, buf) require.Equal(t, FineStructureExpcted, buf.String()) require.NoError(t, err) }) @@ -189,7 +189,7 @@ func TestIterStructFields(T *testing.T) { } T.Run("Wrong structure", func(t *testing.T) { buf1 := bytes.NewBufferString("") - err := iterStructFields(s1, buf1) + _, err := iterStructFields("", s1, buf1) require.Equal(t, WrongStructureExpcted, buf1.String()) require.NoError(t, err) }) diff --git a/hw09_generator_of_validators/models/models.go b/hw09_generator_of_validators/models/models.go index 2700eb6..3f603f1 100644 --- a/hw09_generator_of_validators/models/models.go +++ b/hw09_generator_of_validators/models/models.go @@ -6,12 +6,16 @@ type UserRole string // NOTE: Several struct specs in one type declaration are allowed. type ( User struct { - ID string `json:"id" validate:"len:36"` - Name string - Age int `validate:"min:18|max:50"` - Email string `validate:"regexp:^\\w+@\\w+\\.\\w+$"` - Role UserRole `validate:"in:admin,stuff"` - Phones []string `validate:"len:11"` + ID string `json:"id" validate:"len:36"` + Name string + Age int `validate:"min:18|max:50"` + Email string `validate:"regexp:^\\w+@\\w+\\.\\w+$"` + Role UserRole `validate:"in:admin,stuff"` + Phones []string `validate:"len:11"` + Response struct { + Code int `validate:"in:200,404,500"` + Body string `json:"omitempty"` + } } App struct { diff --git a/hw09_generator_of_validators/models/models_test.go b/hw09_generator_of_validators/models/models_test.go index 9522ac3..99ba068 100644 --- a/hw09_generator_of_validators/models/models_test.go +++ b/hw09_generator_of_validators/models/models_test.go @@ -17,11 +17,12 @@ func TestUserValidation(t *testing.T) { requireValidation(t, User{}) goodUser := User{ - ID: "0a44d582-9749-11ea-a056-9ff7f30f0608", - Name: "John", - Age: 24, - Email: "john@abrams.com", - Role: "admin", + ID: "0a44d582-9749-11ea-a056-9ff7f30f0608", + Name: "John", + Age: 24, + Email: "john@abrams.com", + Role: "admin", + Response: Response{Code: 200}, } requireNoValidationErrors(t, goodUser) @@ -61,12 +62,13 @@ func TestUserValidation(t *testing.T) { t.Run("fail phones slice", func(t *testing.T) { badUser := User{ - ID: "0a44d582-9749-11ea-a056-9ff7f30f0608", - Name: "John", - Age: 24, - Email: "john@abrams.com", - Role: "admin", - Phones: []string{"+12dfwdf343242343", "898298741293", "fdsf"}, + ID: "0a44d582-9749-11ea-a056-9ff7f30f0608", + Name: "John", + Age: 24, + Email: "john@abrams.com", + Role: "admin", + Phones: []string{"+12dfwdf343242343", "898298741293", "fdsf"}, + Response: Response{Code: 404}, } errs, err := badUser.Validate() @@ -75,16 +77,30 @@ func TestUserValidation(t *testing.T) { }) t.Run("pass phones slice", func(t *testing.T) { - badUser := User{ - ID: "0a44d582-9749-11ea-a056-9ff7f30f0608", - Name: "John", - Age: 24, - Email: "john@abrams.com", - Role: "admin", - Phones: []string{"12345678901", "qazxswedcvf", "............"}, + goodUser := User{ + ID: "0a44d582-9749-11ea-a056-9ff7f30f0608", + Name: "John", + Age: 24, + Email: "john@abrams.com", + Role: "admin", + Phones: []string{"12345678901", "qazxswedcvf", "..........."}, + Response: Response{Code: 500}, + } + requireNoValidationErrors(t, goodUser) + }) + + t.Run("embeded structure", func(t *testing.T) { + goodUser := User{ + ID: "0a44d582-9749-11ea-a056-9ff7f30f0608", + Name: "John", + Age: 24, + Email: "john@abrams.com", + Role: "admin", + Phones: []string{"12345678901", "qazxswedcvf", "............"}, + Response: Response{Code: 500}, } - errs, err := badUser.Validate() + errs, err := goodUser.Validate() require.Nil(t, err) requireOneFieldErr(t, errs, "Phones") }) diff --git a/hw09_generator_of_validators/models/models_validation_generated.go b/hw09_generator_of_validators/models/models_validation_generated.go index f1589ec..acec527 100644 --- a/hw09_generator_of_validators/models/models_validation_generated.go +++ b/hw09_generator_of_validators/models/models_validation_generated.go @@ -24,6 +24,7 @@ func (u User) Validate() ([]ValidationError, error) { if ok, err = valRegexp([]string{string(u.Email)},`^\w+@\w+\.\w+$`); !ok { res=append(res,ValidationError{"Email",fmt.Errorf(`regexp:^\w+@\w+\.\w+$`)})} if ok, err = valInString([]string{string(u.Role)},`admin,stuff`); !ok { res=append(res,ValidationError{"Role",fmt.Errorf(`in:admin,stuff`)})} if ok, err = valLen(u.Phones,11); !ok { res=append(res,ValidationError{"Phones",fmt.Errorf("len:11")})} + if ok, err = valInInt([]int{int(u.Response.Code)},`200,404,500`); !ok { res=append(res,ValidationError{"Response.Code",fmt.Errorf("in:200,404,500")})} log.Println(ok) return res, err }