From 80ea93561e377a53cec6785fac23ca98cd4914fb Mon Sep 17 00:00:00 2001 From: Lee Ween Jiann <16207788+lwj5@users.noreply.github.com> Date: Fri, 31 Mar 2023 16:42:04 +0800 Subject: [PATCH 1/4] Update dependecies and go version --- .../src/main/resources/go-server/go.mod.mustache | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/go-server/go.mod.mustache b/modules/openapi-generator/src/main/resources/go-server/go.mod.mustache index ad22f71f6ba2..bb85cc224ae1 100644 --- a/modules/openapi-generator/src/main/resources/go-server/go.mod.mustache +++ b/modules/openapi-generator/src/main/resources/go-server/go.mod.mustache @@ -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}} From 57b89185e62253da06e0f44e4fa270e5fbfdf512 Mon Sep 17 00:00:00 2001 From: Lee Ween Jiann <16207788+lwj5@users.noreply.github.com> Date: Fri, 31 Mar 2023 16:42:16 +0800 Subject: [PATCH 2/4] Use generics for recurse --- .../src/main/resources/go-server/helpers.mustache | 14 ++++++++++---- .../src/main/resources/go-server/model.mustache | 14 +------------- 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/go-server/helpers.mustache b/modules/openapi-generator/src/main/resources/go-server/helpers.mustache index 6de33d11437d..4a27fdcde003 100644 --- a/modules/openapi-generator/src/main/resources/go-server/helpers.mustache +++ b/modules/openapi-generator/src/main/resources/go-server/helpers.mustache @@ -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[S [][]any, T any](obj S, 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 } diff --git a/modules/openapi-generator/src/main/resources/go-server/model.mustache b/modules/openapi-generator/src/main/resources/go-server/model.mustache index 8a4dd7a97904..73ee1c49fdb7 100644 --- a/modules/openapi-generator/src/main/resources/go-server/model.mustache +++ b/modules/openapi-generator/src/main/resources/go-server/model.mustache @@ -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}} @@ -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}} From 8b87450115d94dc818395a86218cd4b9f1c187ce Mon Sep 17 00:00:00 2001 From: Lee Ween Jiann <16207788+lwj5@users.noreply.github.com> Date: Fri, 31 Mar 2023 16:48:30 +0800 Subject: [PATCH 3/4] Fix helpers.mustache --- .../src/main/resources/go-server/helpers.mustache | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/openapi-generator/src/main/resources/go-server/helpers.mustache b/modules/openapi-generator/src/main/resources/go-server/helpers.mustache index 4a27fdcde003..ba5954ff46b8 100644 --- a/modules/openapi-generator/src/main/resources/go-server/helpers.mustache +++ b/modules/openapi-generator/src/main/resources/go-server/helpers.mustache @@ -34,7 +34,7 @@ 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[S [][]any, T any](obj S, callback func(T) error) error { +func AssertRecurseInterfaceRequired[T any](obj interface{}, callback func(T) error) error { return AssertRecurseValueRequired(reflect.ValueOf(obj), callback) } From 7235ea687f7cf10f68d71c3135c1b5ffb12f3206 Mon Sep 17 00:00:00 2001 From: Lee Ween Jiann <16207788+lwj5@users.noreply.github.com> Date: Fri, 31 Mar 2023 16:55:54 +0800 Subject: [PATCH 4/4] Regenerate samples --- samples/server/petstore/go-api-server/go.mod | 4 ++-- .../server/petstore/go-api-server/go/helpers.go | 14 ++++++++++---- .../go-api-server/go/model_api_response.go | 12 ------------ .../petstore/go-api-server/go/model_category.go | 12 ------------ .../petstore/go-api-server/go/model_order.go | 12 ------------ .../petstore/go-api-server/go/model_pet.go | 12 ------------ .../petstore/go-api-server/go/model_tag.go | 12 ------------ .../petstore/go-api-server/go/model_user.go | 12 ------------ samples/server/petstore/go-chi-server/go.mod | 4 ++-- .../server/petstore/go-chi-server/go/helpers.go | 14 ++++++++++---- .../go-chi-server/go/model_api_response.go | 12 ------------ .../petstore/go-chi-server/go/model_category.go | 12 ------------ .../petstore/go-chi-server/go/model_order.go | 12 ------------ .../petstore/go-chi-server/go/model_pet.go | 12 ------------ .../petstore/go-chi-server/go/model_tag.go | 12 ------------ .../petstore/go-chi-server/go/model_user.go | 12 ------------ .../server/petstore/go-server-required/go.mod | 4 ++-- .../petstore/go-server-required/go/helpers.go | 14 ++++++++++---- .../go-server-required/go/model_an_object.go | 12 ------------ .../go-server-required/go/model_api_response.go | 12 ------------ .../go-server-required/go/model_category.go | 12 ------------ .../go-server-required/go/model_order.go | 12 ------------ .../go-server-required/go/model_order_info.go | 12 ------------ .../petstore/go-server-required/go/model_pet.go | 12 ------------ .../go-server-required/go/model_special_info.go | 12 ------------ .../petstore/go-server-required/go/model_tag.go | 12 ------------ .../petstore/go-server-required/go/model_user.go | 16 ++-------------- 27 files changed, 38 insertions(+), 272 deletions(-) diff --git a/samples/server/petstore/go-api-server/go.mod b/samples/server/petstore/go-api-server/go.mod index 5b6a67c74e20..65afa7e70717 100644 --- a/samples/server/petstore/go-api-server/go.mod +++ b/samples/server/petstore/go-api-server/go.mod @@ -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 diff --git a/samples/server/petstore/go-api-server/go/helpers.go b/samples/server/petstore/go-api-server/go/helpers.go index f18a261018dd..078da22b8fab 100644 --- a/samples/server/petstore/go-api-server/go/helpers.go +++ b/samples/server/petstore/go-api-server/go/helpers.go @@ -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 } diff --git a/samples/server/petstore/go-api-server/go/model_api_response.go b/samples/server/petstore/go-api-server/go/model_api_response.go index 324cfbe08444..3cb1dc2d3e27 100644 --- a/samples/server/petstore/go-api-server/go/model_api_response.go +++ b/samples/server/petstore/go-api-server/go/model_api_response.go @@ -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) - }) -} diff --git a/samples/server/petstore/go-api-server/go/model_category.go b/samples/server/petstore/go-api-server/go/model_category.go index 3ae01892a3fc..9d73f38294ed 100644 --- a/samples/server/petstore/go-api-server/go/model_category.go +++ b/samples/server/petstore/go-api-server/go/model_category.go @@ -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) - }) -} diff --git a/samples/server/petstore/go-api-server/go/model_order.go b/samples/server/petstore/go-api-server/go/model_order.go index 80ecfea3dc7f..c40a82bab091 100644 --- a/samples/server/petstore/go-api-server/go/model_order.go +++ b/samples/server/petstore/go-api-server/go/model_order.go @@ -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) - }) -} diff --git a/samples/server/petstore/go-api-server/go/model_pet.go b/samples/server/petstore/go-api-server/go/model_pet.go index 8e3dc7ad66f1..5073463b3916 100644 --- a/samples/server/petstore/go-api-server/go/model_pet.go +++ b/samples/server/petstore/go-api-server/go/model_pet.go @@ -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) - }) -} diff --git a/samples/server/petstore/go-api-server/go/model_tag.go b/samples/server/petstore/go-api-server/go/model_tag.go index 0631ccc4866f..c441e14e9969 100644 --- a/samples/server/petstore/go-api-server/go/model_tag.go +++ b/samples/server/petstore/go-api-server/go/model_tag.go @@ -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) - }) -} diff --git a/samples/server/petstore/go-api-server/go/model_user.go b/samples/server/petstore/go-api-server/go/model_user.go index 83ac114e3c59..2fcaf883f4cb 100644 --- a/samples/server/petstore/go-api-server/go/model_user.go +++ b/samples/server/petstore/go-api-server/go/model_user.go @@ -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) - }) -} diff --git a/samples/server/petstore/go-chi-server/go.mod b/samples/server/petstore/go-chi-server/go.mod index fa1221d2bedc..5886c8aa7e79 100644 --- a/samples/server/petstore/go-chi-server/go.mod +++ b/samples/server/petstore/go-chi-server/go.mod @@ -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 diff --git a/samples/server/petstore/go-chi-server/go/helpers.go b/samples/server/petstore/go-chi-server/go/helpers.go index f18a261018dd..078da22b8fab 100644 --- a/samples/server/petstore/go-chi-server/go/helpers.go +++ b/samples/server/petstore/go-chi-server/go/helpers.go @@ -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 } diff --git a/samples/server/petstore/go-chi-server/go/model_api_response.go b/samples/server/petstore/go-chi-server/go/model_api_response.go index 324cfbe08444..3cb1dc2d3e27 100644 --- a/samples/server/petstore/go-chi-server/go/model_api_response.go +++ b/samples/server/petstore/go-chi-server/go/model_api_response.go @@ -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) - }) -} diff --git a/samples/server/petstore/go-chi-server/go/model_category.go b/samples/server/petstore/go-chi-server/go/model_category.go index 3ae01892a3fc..9d73f38294ed 100644 --- a/samples/server/petstore/go-chi-server/go/model_category.go +++ b/samples/server/petstore/go-chi-server/go/model_category.go @@ -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) - }) -} diff --git a/samples/server/petstore/go-chi-server/go/model_order.go b/samples/server/petstore/go-chi-server/go/model_order.go index 80ecfea3dc7f..c40a82bab091 100644 --- a/samples/server/petstore/go-chi-server/go/model_order.go +++ b/samples/server/petstore/go-chi-server/go/model_order.go @@ -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) - }) -} diff --git a/samples/server/petstore/go-chi-server/go/model_pet.go b/samples/server/petstore/go-chi-server/go/model_pet.go index 8e3dc7ad66f1..5073463b3916 100644 --- a/samples/server/petstore/go-chi-server/go/model_pet.go +++ b/samples/server/petstore/go-chi-server/go/model_pet.go @@ -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) - }) -} diff --git a/samples/server/petstore/go-chi-server/go/model_tag.go b/samples/server/petstore/go-chi-server/go/model_tag.go index 0631ccc4866f..c441e14e9969 100644 --- a/samples/server/petstore/go-chi-server/go/model_tag.go +++ b/samples/server/petstore/go-chi-server/go/model_tag.go @@ -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) - }) -} diff --git a/samples/server/petstore/go-chi-server/go/model_user.go b/samples/server/petstore/go-chi-server/go/model_user.go index 83ac114e3c59..2fcaf883f4cb 100644 --- a/samples/server/petstore/go-chi-server/go/model_user.go +++ b/samples/server/petstore/go-chi-server/go/model_user.go @@ -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) - }) -} diff --git a/samples/server/petstore/go-server-required/go.mod b/samples/server/petstore/go-server-required/go.mod index fa1221d2bedc..5886c8aa7e79 100644 --- a/samples/server/petstore/go-server-required/go.mod +++ b/samples/server/petstore/go-server-required/go.mod @@ -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 diff --git a/samples/server/petstore/go-server-required/go/helpers.go b/samples/server/petstore/go-server-required/go/helpers.go index f18a261018dd..078da22b8fab 100644 --- a/samples/server/petstore/go-server-required/go/helpers.go +++ b/samples/server/petstore/go-server-required/go/helpers.go @@ -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 } diff --git a/samples/server/petstore/go-server-required/go/model_an_object.go b/samples/server/petstore/go-server-required/go/model_an_object.go index eea13e9c72e2..d10c34be958b 100644 --- a/samples/server/petstore/go-server-required/go/model_an_object.go +++ b/samples/server/petstore/go-server-required/go/model_an_object.go @@ -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) - }) -} diff --git a/samples/server/petstore/go-server-required/go/model_api_response.go b/samples/server/petstore/go-server-required/go/model_api_response.go index 324cfbe08444..3cb1dc2d3e27 100644 --- a/samples/server/petstore/go-server-required/go/model_api_response.go +++ b/samples/server/petstore/go-server-required/go/model_api_response.go @@ -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) - }) -} diff --git a/samples/server/petstore/go-server-required/go/model_category.go b/samples/server/petstore/go-server-required/go/model_category.go index 3ae01892a3fc..9d73f38294ed 100644 --- a/samples/server/petstore/go-server-required/go/model_category.go +++ b/samples/server/petstore/go-server-required/go/model_category.go @@ -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) - }) -} diff --git a/samples/server/petstore/go-server-required/go/model_order.go b/samples/server/petstore/go-server-required/go/model_order.go index f3441fa8c7b5..3dd97db3f244 100644 --- a/samples/server/petstore/go-server-required/go/model_order.go +++ b/samples/server/petstore/go-server-required/go/model_order.go @@ -50,15 +50,3 @@ 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) - }) -} diff --git a/samples/server/petstore/go-server-required/go/model_order_info.go b/samples/server/petstore/go-server-required/go/model_order_info.go index 3c6bb2704c94..47720a31bf3c 100644 --- a/samples/server/petstore/go-server-required/go/model_order_info.go +++ b/samples/server/petstore/go-server-required/go/model_order_info.go @@ -27,15 +27,3 @@ type OrderInfo struct { func AssertOrderInfoRequired(obj OrderInfo) error { return nil } - -// AssertRecurseOrderInfoRequired recursively checks if required fields are not zero-ed in a nested slice. -// Accepts only nested slice of OrderInfo (e.g. [][]OrderInfo), otherwise ErrTypeAssertionError is thrown. -func AssertRecurseOrderInfoRequired(objSlice interface{}) error { - return AssertRecurseInterfaceRequired(objSlice, func(obj interface{}) error { - aOrderInfo, ok := obj.(OrderInfo) - if !ok { - return ErrTypeAssertionError - } - return AssertOrderInfoRequired(aOrderInfo) - }) -} diff --git a/samples/server/petstore/go-server-required/go/model_pet.go b/samples/server/petstore/go-server-required/go/model_pet.go index 5c71af290f61..52049f459db8 100644 --- a/samples/server/petstore/go-server-required/go/model_pet.go +++ b/samples/server/petstore/go-server-required/go/model_pet.go @@ -52,15 +52,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) - }) -} diff --git a/samples/server/petstore/go-server-required/go/model_special_info.go b/samples/server/petstore/go-server-required/go/model_special_info.go index 73bea15959c1..e193d5c53fe2 100644 --- a/samples/server/petstore/go-server-required/go/model_special_info.go +++ b/samples/server/petstore/go-server-required/go/model_special_info.go @@ -21,15 +21,3 @@ type SpecialInfo struct { func AssertSpecialInfoRequired(obj SpecialInfo) error { return nil } - -// AssertRecurseSpecialInfoRequired recursively checks if required fields are not zero-ed in a nested slice. -// Accepts only nested slice of SpecialInfo (e.g. [][]SpecialInfo), otherwise ErrTypeAssertionError is thrown. -func AssertRecurseSpecialInfoRequired(objSlice interface{}) error { - return AssertRecurseInterfaceRequired(objSlice, func(obj interface{}) error { - aSpecialInfo, ok := obj.(SpecialInfo) - if !ok { - return ErrTypeAssertionError - } - return AssertSpecialInfoRequired(aSpecialInfo) - }) -} diff --git a/samples/server/petstore/go-server-required/go/model_tag.go b/samples/server/petstore/go-server-required/go/model_tag.go index 0631ccc4866f..c441e14e9969 100644 --- a/samples/server/petstore/go-server-required/go/model_tag.go +++ b/samples/server/petstore/go-server-required/go/model_tag.go @@ -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) - }) -} diff --git a/samples/server/petstore/go-server-required/go/model_user.go b/samples/server/petstore/go-server-required/go/model_user.go index 8a45852e151f..80649d9f5141 100644 --- a/samples/server/petstore/go-server-required/go/model_user.go +++ b/samples/server/petstore/go-server-required/go/model_user.go @@ -48,24 +48,12 @@ func AssertUserRequired(obj User) error { } if obj.DeepSliceModel != nil { - if err := AssertRecurseTagRequired(*obj.DeepSliceModel); err != nil { + if err := AssertRecurseInterfaceRequired(*obj.DeepSliceModel, AssertTagRequired); err != nil { return err } } - if err := AssertRecurseAnObjectRequired(obj.DeepSliceMap); err != nil { + if err := AssertRecurseInterfaceRequired(obj.DeepSliceMap, AssertAnObjectRequired); err != nil { return err } 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) - }) -}