From d1e8d632ef7b42ad66a44f5721443dc0aad65a02 Mon Sep 17 00:00:00 2001 From: Slavek Kabrda Date: Mon, 18 May 2020 09:57:05 +0200 Subject: [PATCH] [go-experimental] Add convenience wrapper function for oneOf members (#6316) --- .../resources/go-experimental/model_oneof.mustache | 8 ++++++++ .../go-experimental/go-petstore/model_fruit.go | 11 +++++++++++ .../go-experimental/go-petstore/model_fruit_req.go | 11 +++++++++++ .../go-experimental/go-petstore/model_mammal.go | 11 +++++++++++ 4 files changed, 41 insertions(+) diff --git a/modules/openapi-generator/src/main/resources/go-experimental/model_oneof.mustache b/modules/openapi-generator/src/main/resources/go-experimental/model_oneof.mustache index b7f3de19ea4..4c37d1fdce1 100644 --- a/modules/openapi-generator/src/main/resources/go-experimental/model_oneof.mustache +++ b/modules/openapi-generator/src/main/resources/go-experimental/model_oneof.mustache @@ -5,6 +5,14 @@ type {{classname}} struct { {{/oneOf}} } +{{#oneOf}} +// {{{.}}}As{{classname}} is a convenience function that returns {{{.}}} wrapped in {{classname}} +func {{{.}}}As{{classname}}(v *{{{.}}}) {{classname}} { + return {{classname}}{ {{{.}}}: v} +} + +{{/oneOf}} + // Unmarshl JSON data into one of the pointers in the struct func (dst *{{classname}}) UnmarshalJSON(data []byte) error { var err error diff --git a/samples/openapi3/client/petstore/go-experimental/go-petstore/model_fruit.go b/samples/openapi3/client/petstore/go-experimental/go-petstore/model_fruit.go index 8d4b3ccf6f8..17d15b09e5a 100644 --- a/samples/openapi3/client/petstore/go-experimental/go-petstore/model_fruit.go +++ b/samples/openapi3/client/petstore/go-experimental/go-petstore/model_fruit.go @@ -20,6 +20,17 @@ type Fruit struct { Banana *Banana } +// AppleAsFruit is a convenience function that returns Apple wrapped in Fruit +func AppleAsFruit(v *Apple) Fruit { + return Fruit{ Apple: v} +} + +// BananaAsFruit is a convenience function that returns Banana wrapped in Fruit +func BananaAsFruit(v *Banana) Fruit { + return Fruit{ Banana: v} +} + + // Unmarshl JSON data into one of the pointers in the struct func (dst *Fruit) UnmarshalJSON(data []byte) error { var err error diff --git a/samples/openapi3/client/petstore/go-experimental/go-petstore/model_fruit_req.go b/samples/openapi3/client/petstore/go-experimental/go-petstore/model_fruit_req.go index c1b0500050a..cbbba809b1b 100644 --- a/samples/openapi3/client/petstore/go-experimental/go-petstore/model_fruit_req.go +++ b/samples/openapi3/client/petstore/go-experimental/go-petstore/model_fruit_req.go @@ -20,6 +20,17 @@ type FruitReq struct { BananaReq *BananaReq } +// AppleReqAsFruitReq is a convenience function that returns AppleReq wrapped in FruitReq +func AppleReqAsFruitReq(v *AppleReq) FruitReq { + return FruitReq{ AppleReq: v} +} + +// BananaReqAsFruitReq is a convenience function that returns BananaReq wrapped in FruitReq +func BananaReqAsFruitReq(v *BananaReq) FruitReq { + return FruitReq{ BananaReq: v} +} + + // Unmarshl JSON data into one of the pointers in the struct func (dst *FruitReq) UnmarshalJSON(data []byte) error { var err error diff --git a/samples/openapi3/client/petstore/go-experimental/go-petstore/model_mammal.go b/samples/openapi3/client/petstore/go-experimental/go-petstore/model_mammal.go index bd830b26689..5cd7d7dc5d1 100644 --- a/samples/openapi3/client/petstore/go-experimental/go-petstore/model_mammal.go +++ b/samples/openapi3/client/petstore/go-experimental/go-petstore/model_mammal.go @@ -20,6 +20,17 @@ type Mammal struct { Zebra *Zebra } +// WhaleAsMammal is a convenience function that returns Whale wrapped in Mammal +func WhaleAsMammal(v *Whale) Mammal { + return Mammal{ Whale: v} +} + +// ZebraAsMammal is a convenience function that returns Zebra wrapped in Mammal +func ZebraAsMammal(v *Zebra) Mammal { + return Mammal{ Zebra: v} +} + + // Unmarshl JSON data into one of the pointers in the struct func (dst *Mammal) UnmarshalJSON(data []byte) error { var err error