Skip to content

Commit

Permalink
[go-server] Update dependencies and use generics (#15087)
Browse files Browse the repository at this point in the history
* Update dependecies and go version

* Use generics for recurse

* Fix helpers.mustache

* Regenerate samples
  • Loading branch information
lwj5 committed Apr 12, 2023
1 parent 792c49a commit 0444118
Show file tree
Hide file tree
Showing 30 changed files with 53 additions and 293 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
module {{gitHost}}/{{gitUserId}}/{{gitRepoId}}

go 1.13
go 1.18

{{#routers}}
{{#mux}}
require github.com/gorilla/mux v1.7.3
require github.com/gorilla/mux v1.8.0
{{#featureCORS}}
require github.com/gorilla/handlers v1.5.1
{{/featureCORS}}
{{/mux}}
{{#chi}}
require github.com/go-chi/chi/v5 v5.0.3
require github.com/go-chi/chi/v5 v5.0.8
{{#featureCORS}}
require github.com/go-chi/cors v1.2.0
require github.com/go-chi/cors v1.2.1
{{/featureCORS}}
{{/chi}}
{{/routers}}
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,23 @@ func IsZeroValue(val interface{}) bool {

// AssertRecurseInterfaceRequired recursively checks each struct in a slice against the callback.
// This method traverse nested slices in a preorder fashion.
func AssertRecurseInterfaceRequired(obj interface{}, callback func(interface{}) error) error {
func AssertRecurseInterfaceRequired[T any](obj interface{}, callback func(T) error) error {
return AssertRecurseValueRequired(reflect.ValueOf(obj), callback)
}

// AssertRecurseValueRequired checks each struct in the nested slice against the callback.
// This method traverse nested slices in a preorder fashion.
func AssertRecurseValueRequired(value reflect.Value, callback func(interface{}) error) error {
// This method traverse nested slices in a preorder fashion. ErrTypeAssertionError is thrown if
// the underlying struct does not match type T.
func AssertRecurseValueRequired[T any](value reflect.Value, callback func(T) error) error {
switch value.Kind() {
// If it is a struct we check using callback
case reflect.Struct:
if err := callback(value.Interface()); err != nil {
obj, ok := value.Interface().(T)
if !ok {
return ErrTypeAssertionError
}

if err := callback(obj); err != nil {
return err
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func Assert{{classname}}Required(obj {{classname}}) error {
{{^items.isModel}}
{{#mostInnerItems.isModel}}
{{^mostInnerItems.isPrimitiveType}}
{{#isNullable}} {{/isNullable}} if err := AssertRecurse{{mostInnerItems.dataType}}Required({{#isNullable}}*{{/isNullable}}obj.{{name}}); err != nil {
{{#isNullable}} {{/isNullable}} if err := AssertRecurseInterfaceRequired({{#isNullable}}*{{/isNullable}}obj.{{name}}, Assert{{mostInnerItems.dataType}}Required); err != nil {
{{#isNullable}} {{/isNullable}} return err
{{#isNullable}} {{/isNullable}} }
{{/mostInnerItems.isPrimitiveType}}
Expand All @@ -119,16 +119,4 @@ func Assert{{classname}}Required(obj {{classname}}) error {
{{/isNullable}}
{{/Vars}}
return nil
}

// AssertRecurse{{classname}}Required recursively checks if required fields are not zero-ed in a nested slice.
// Accepts only nested slice of {{classname}} (e.g. [][]{{classname}}), otherwise ErrTypeAssertionError is thrown.
func AssertRecurse{{classname}}Required(objSlice interface{}) error {
return AssertRecurseInterfaceRequired(objSlice, func(obj interface{}) error {
a{{classname}}, ok := obj.({{classname}})
if !ok {
return ErrTypeAssertionError
}
return Assert{{classname}}Required(a{{classname}})
})
}{{/model}}{{/models}}
4 changes: 2 additions & 2 deletions samples/server/petstore/go-api-server/go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/GIT_USER_ID/GIT_REPO_ID

go 1.13
go 1.18

require github.com/gorilla/mux v1.7.3
require github.com/gorilla/mux v1.8.0
14 changes: 10 additions & 4 deletions samples/server/petstore/go-api-server/go/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,23 @@ func IsZeroValue(val interface{}) bool {

// AssertRecurseInterfaceRequired recursively checks each struct in a slice against the callback.
// This method traverse nested slices in a preorder fashion.
func AssertRecurseInterfaceRequired(obj interface{}, callback func(interface{}) error) error {
func AssertRecurseInterfaceRequired[T any](obj interface{}, callback func(T) error) error {
return AssertRecurseValueRequired(reflect.ValueOf(obj), callback)
}

// AssertRecurseValueRequired checks each struct in the nested slice against the callback.
// This method traverse nested slices in a preorder fashion.
func AssertRecurseValueRequired(value reflect.Value, callback func(interface{}) error) error {
// This method traverse nested slices in a preorder fashion. ErrTypeAssertionError is thrown if
// the underlying struct does not match type T.
func AssertRecurseValueRequired[T any](value reflect.Value, callback func(T) error) error {
switch value.Kind() {
// If it is a struct we check using callback
case reflect.Struct:
if err := callback(value.Interface()); err != nil {
obj, ok := value.Interface().(T)
if !ok {
return ErrTypeAssertionError
}

if err := callback(obj); err != nil {
return err
}

Expand Down
12 changes: 0 additions & 12 deletions samples/server/petstore/go-api-server/go/model_api_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,3 @@ type ApiResponse struct {
func AssertApiResponseRequired(obj ApiResponse) error {
return nil
}

// AssertRecurseApiResponseRequired recursively checks if required fields are not zero-ed in a nested slice.
// Accepts only nested slice of ApiResponse (e.g. [][]ApiResponse), otherwise ErrTypeAssertionError is thrown.
func AssertRecurseApiResponseRequired(objSlice interface{}) error {
return AssertRecurseInterfaceRequired(objSlice, func(obj interface{}) error {
aApiResponse, ok := obj.(ApiResponse)
if !ok {
return ErrTypeAssertionError
}
return AssertApiResponseRequired(aApiResponse)
})
}
12 changes: 0 additions & 12 deletions samples/server/petstore/go-api-server/go/model_category.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,3 @@ type Category struct {
func AssertCategoryRequired(obj Category) error {
return nil
}

// AssertRecurseCategoryRequired recursively checks if required fields are not zero-ed in a nested slice.
// Accepts only nested slice of Category (e.g. [][]Category), otherwise ErrTypeAssertionError is thrown.
func AssertRecurseCategoryRequired(objSlice interface{}) error {
return AssertRecurseInterfaceRequired(objSlice, func(obj interface{}) error {
aCategory, ok := obj.(Category)
if !ok {
return ErrTypeAssertionError
}
return AssertCategoryRequired(aCategory)
})
}
12 changes: 0 additions & 12 deletions samples/server/petstore/go-api-server/go/model_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,3 @@ type Order struct {
func AssertOrderRequired(obj Order) error {
return nil
}

// AssertRecurseOrderRequired recursively checks if required fields are not zero-ed in a nested slice.
// Accepts only nested slice of Order (e.g. [][]Order), otherwise ErrTypeAssertionError is thrown.
func AssertRecurseOrderRequired(objSlice interface{}) error {
return AssertRecurseInterfaceRequired(objSlice, func(obj interface{}) error {
aOrder, ok := obj.(Order)
if !ok {
return ErrTypeAssertionError
}
return AssertOrderRequired(aOrder)
})
}
12 changes: 0 additions & 12 deletions samples/server/petstore/go-api-server/go/model_pet.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,3 @@ func AssertPetRequired(obj Pet) error {
}
return nil
}

// AssertRecursePetRequired recursively checks if required fields are not zero-ed in a nested slice.
// Accepts only nested slice of Pet (e.g. [][]Pet), otherwise ErrTypeAssertionError is thrown.
func AssertRecursePetRequired(objSlice interface{}) error {
return AssertRecurseInterfaceRequired(objSlice, func(obj interface{}) error {
aPet, ok := obj.(Pet)
if !ok {
return ErrTypeAssertionError
}
return AssertPetRequired(aPet)
})
}
12 changes: 0 additions & 12 deletions samples/server/petstore/go-api-server/go/model_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,3 @@ type Tag struct {
func AssertTagRequired(obj Tag) error {
return nil
}

// AssertRecurseTagRequired recursively checks if required fields are not zero-ed in a nested slice.
// Accepts only nested slice of Tag (e.g. [][]Tag), otherwise ErrTypeAssertionError is thrown.
func AssertRecurseTagRequired(objSlice interface{}) error {
return AssertRecurseInterfaceRequired(objSlice, func(obj interface{}) error {
aTag, ok := obj.(Tag)
if !ok {
return ErrTypeAssertionError
}
return AssertTagRequired(aTag)
})
}
12 changes: 0 additions & 12 deletions samples/server/petstore/go-api-server/go/model_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,3 @@ type User struct {
func AssertUserRequired(obj User) error {
return nil
}

// AssertRecurseUserRequired recursively checks if required fields are not zero-ed in a nested slice.
// Accepts only nested slice of User (e.g. [][]User), otherwise ErrTypeAssertionError is thrown.
func AssertRecurseUserRequired(objSlice interface{}) error {
return AssertRecurseInterfaceRequired(objSlice, func(obj interface{}) error {
aUser, ok := obj.(User)
if !ok {
return ErrTypeAssertionError
}
return AssertUserRequired(aUser)
})
}
4 changes: 2 additions & 2 deletions samples/server/petstore/go-chi-server/go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/GIT_USER_ID/GIT_REPO_ID

go 1.13
go 1.18

require github.com/go-chi/chi/v5 v5.0.3
require github.com/go-chi/chi/v5 v5.0.8
14 changes: 10 additions & 4 deletions samples/server/petstore/go-chi-server/go/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,23 @@ func IsZeroValue(val interface{}) bool {

// AssertRecurseInterfaceRequired recursively checks each struct in a slice against the callback.
// This method traverse nested slices in a preorder fashion.
func AssertRecurseInterfaceRequired(obj interface{}, callback func(interface{}) error) error {
func AssertRecurseInterfaceRequired[T any](obj interface{}, callback func(T) error) error {
return AssertRecurseValueRequired(reflect.ValueOf(obj), callback)
}

// AssertRecurseValueRequired checks each struct in the nested slice against the callback.
// This method traverse nested slices in a preorder fashion.
func AssertRecurseValueRequired(value reflect.Value, callback func(interface{}) error) error {
// This method traverse nested slices in a preorder fashion. ErrTypeAssertionError is thrown if
// the underlying struct does not match type T.
func AssertRecurseValueRequired[T any](value reflect.Value, callback func(T) error) error {
switch value.Kind() {
// If it is a struct we check using callback
case reflect.Struct:
if err := callback(value.Interface()); err != nil {
obj, ok := value.Interface().(T)
if !ok {
return ErrTypeAssertionError
}

if err := callback(obj); err != nil {
return err
}

Expand Down
12 changes: 0 additions & 12 deletions samples/server/petstore/go-chi-server/go/model_api_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,3 @@ type ApiResponse struct {
func AssertApiResponseRequired(obj ApiResponse) error {
return nil
}

// AssertRecurseApiResponseRequired recursively checks if required fields are not zero-ed in a nested slice.
// Accepts only nested slice of ApiResponse (e.g. [][]ApiResponse), otherwise ErrTypeAssertionError is thrown.
func AssertRecurseApiResponseRequired(objSlice interface{}) error {
return AssertRecurseInterfaceRequired(objSlice, func(obj interface{}) error {
aApiResponse, ok := obj.(ApiResponse)
if !ok {
return ErrTypeAssertionError
}
return AssertApiResponseRequired(aApiResponse)
})
}
12 changes: 0 additions & 12 deletions samples/server/petstore/go-chi-server/go/model_category.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,3 @@ type Category struct {
func AssertCategoryRequired(obj Category) error {
return nil
}

// AssertRecurseCategoryRequired recursively checks if required fields are not zero-ed in a nested slice.
// Accepts only nested slice of Category (e.g. [][]Category), otherwise ErrTypeAssertionError is thrown.
func AssertRecurseCategoryRequired(objSlice interface{}) error {
return AssertRecurseInterfaceRequired(objSlice, func(obj interface{}) error {
aCategory, ok := obj.(Category)
if !ok {
return ErrTypeAssertionError
}
return AssertCategoryRequired(aCategory)
})
}
12 changes: 0 additions & 12 deletions samples/server/petstore/go-chi-server/go/model_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,3 @@ type Order struct {
func AssertOrderRequired(obj Order) error {
return nil
}

// AssertRecurseOrderRequired recursively checks if required fields are not zero-ed in a nested slice.
// Accepts only nested slice of Order (e.g. [][]Order), otherwise ErrTypeAssertionError is thrown.
func AssertRecurseOrderRequired(objSlice interface{}) error {
return AssertRecurseInterfaceRequired(objSlice, func(obj interface{}) error {
aOrder, ok := obj.(Order)
if !ok {
return ErrTypeAssertionError
}
return AssertOrderRequired(aOrder)
})
}
12 changes: 0 additions & 12 deletions samples/server/petstore/go-chi-server/go/model_pet.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,3 @@ func AssertPetRequired(obj Pet) error {
}
return nil
}

// AssertRecursePetRequired recursively checks if required fields are not zero-ed in a nested slice.
// Accepts only nested slice of Pet (e.g. [][]Pet), otherwise ErrTypeAssertionError is thrown.
func AssertRecursePetRequired(objSlice interface{}) error {
return AssertRecurseInterfaceRequired(objSlice, func(obj interface{}) error {
aPet, ok := obj.(Pet)
if !ok {
return ErrTypeAssertionError
}
return AssertPetRequired(aPet)
})
}
12 changes: 0 additions & 12 deletions samples/server/petstore/go-chi-server/go/model_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,3 @@ type Tag struct {
func AssertTagRequired(obj Tag) error {
return nil
}

// AssertRecurseTagRequired recursively checks if required fields are not zero-ed in a nested slice.
// Accepts only nested slice of Tag (e.g. [][]Tag), otherwise ErrTypeAssertionError is thrown.
func AssertRecurseTagRequired(objSlice interface{}) error {
return AssertRecurseInterfaceRequired(objSlice, func(obj interface{}) error {
aTag, ok := obj.(Tag)
if !ok {
return ErrTypeAssertionError
}
return AssertTagRequired(aTag)
})
}
12 changes: 0 additions & 12 deletions samples/server/petstore/go-chi-server/go/model_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,3 @@ type User struct {
func AssertUserRequired(obj User) error {
return nil
}

// AssertRecurseUserRequired recursively checks if required fields are not zero-ed in a nested slice.
// Accepts only nested slice of User (e.g. [][]User), otherwise ErrTypeAssertionError is thrown.
func AssertRecurseUserRequired(objSlice interface{}) error {
return AssertRecurseInterfaceRequired(objSlice, func(obj interface{}) error {
aUser, ok := obj.(User)
if !ok {
return ErrTypeAssertionError
}
return AssertUserRequired(aUser)
})
}
4 changes: 2 additions & 2 deletions samples/server/petstore/go-server-required/go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/GIT_USER_ID/GIT_REPO_ID

go 1.13
go 1.18

require github.com/go-chi/chi/v5 v5.0.3
require github.com/go-chi/chi/v5 v5.0.8
14 changes: 10 additions & 4 deletions samples/server/petstore/go-server-required/go/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,23 @@ func IsZeroValue(val interface{}) bool {

// AssertRecurseInterfaceRequired recursively checks each struct in a slice against the callback.
// This method traverse nested slices in a preorder fashion.
func AssertRecurseInterfaceRequired(obj interface{}, callback func(interface{}) error) error {
func AssertRecurseInterfaceRequired[T any](obj interface{}, callback func(T) error) error {
return AssertRecurseValueRequired(reflect.ValueOf(obj), callback)
}

// AssertRecurseValueRequired checks each struct in the nested slice against the callback.
// This method traverse nested slices in a preorder fashion.
func AssertRecurseValueRequired(value reflect.Value, callback func(interface{}) error) error {
// This method traverse nested slices in a preorder fashion. ErrTypeAssertionError is thrown if
// the underlying struct does not match type T.
func AssertRecurseValueRequired[T any](value reflect.Value, callback func(T) error) error {
switch value.Kind() {
// If it is a struct we check using callback
case reflect.Struct:
if err := callback(value.Interface()); err != nil {
obj, ok := value.Interface().(T)
if !ok {
return ErrTypeAssertionError
}

if err := callback(obj); err != nil {
return err
}

Expand Down
12 changes: 0 additions & 12 deletions samples/server/petstore/go-server-required/go/model_an_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,3 @@ func AssertAnObjectRequired(obj AnObject) error {
}
return nil
}

// AssertRecurseAnObjectRequired recursively checks if required fields are not zero-ed in a nested slice.
// Accepts only nested slice of AnObject (e.g. [][]AnObject), otherwise ErrTypeAssertionError is thrown.
func AssertRecurseAnObjectRequired(objSlice interface{}) error {
return AssertRecurseInterfaceRequired(objSlice, func(obj interface{}) error {
aAnObject, ok := obj.(AnObject)
if !ok {
return ErrTypeAssertionError
}
return AssertAnObjectRequired(aAnObject)
})
}
Loading

0 comments on commit 0444118

Please sign in to comment.