Skip to content

Commit

Permalink
[go] Improve examples generation (#7576)
Browse files Browse the repository at this point in the history
* fix go examples

* use constructor with required args
  • Loading branch information
zippolyte authored Oct 15, 2020
1 parent e58b1f6 commit 8afb067
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ private String constructExampleCode(CodegenParameter codegenParameter, HashMap<S
if (codegenParameter.isListContainer) { // array
return codegenParameter.dataType + "{" + constructExampleCode(codegenParameter.items, modelMaps, processedModelMap) + "}";
} else if (codegenParameter.isMap) {
return "map[string]string{ \"Key\" = \"Value\" }";
return codegenParameter.dataType + "{ \"key\": " + constructExampleCode(codegenParameter.items, modelMaps, processedModelMap) + "}";
} else if (codegenParameter.isPrimitiveType) { // primitive type
if (codegenParameter.isString) {
if (StringUtils.isEmpty(codegenParameter.example)) {
Expand All @@ -479,9 +479,9 @@ private String constructExampleCode(CodegenParameter codegenParameter, HashMap<S
return "false";
}
} else if (codegenParameter.isUri) { // URL
return "URL(string: \"https://example.com\")!";
return "\"https://example.com\"";
} else if (codegenParameter.isDateTime || codegenParameter.isDate) { // datetime or date
return "Get-Date";
return "time.Now()";
} else { // numeric
if (StringUtils.isEmpty(codegenParameter.example)) {
return codegenParameter.example;
Expand All @@ -502,9 +502,9 @@ private String constructExampleCode(CodegenParameter codegenParameter, HashMap<S

private String constructExampleCode(CodegenProperty codegenProperty, HashMap<String, CodegenModel> modelMaps, HashMap<String, Integer> processedModelMap) {
if (codegenProperty.isListContainer) { // array
return codegenProperty.dataType + "{" + constructExampleCode(codegenProperty.items, modelMaps, processedModelMap) + ")";
return codegenProperty.dataType + "{" + constructExampleCode(codegenProperty.items, modelMaps, processedModelMap) + "}";
} else if (codegenProperty.isMap) { // map
return "map[string]string{ \"Key\" = \"Value\" }";
return codegenProperty.dataType + "{ \"key\": " + constructExampleCode(codegenProperty.items, modelMaps, processedModelMap) + "}";
} else if (codegenProperty.isPrimitiveType) { // primitive type
if (codegenProperty.isString) {
if (StringUtils.isEmpty(codegenProperty.example)) {
Expand All @@ -519,7 +519,7 @@ private String constructExampleCode(CodegenProperty codegenProperty, HashMap<Str
return "false";
}
} else if (codegenProperty.isUri) { // URL
return "\"https://example.com\")!";
return "\"https://example.com\"";
} else if (codegenProperty.isDateTime || codegenProperty.isDate) { // datetime or date
return "time.Now()";
} else { // numeric
Expand Down Expand Up @@ -565,13 +565,10 @@ private String constructExampleCode(CodegenModel codegenModel, HashMap<String, C
processedModelMap.put(model, 1);
}

example = "" + goImportAlias + "." + codegenModel.name + "{";
List<String> propertyExamples = new ArrayList<>();
for (CodegenProperty codegenProperty : codegenModel.allVars) {
propertyExamples.add(codegenProperty.name + ": " + constructExampleCode(codegenProperty, modelMaps, processedModelMap));
for (CodegenProperty codegenProperty : codegenModel.requiredVars) {
propertyExamples.add(constructExampleCode(codegenProperty, modelMaps, processedModelMap));
}
example += StringUtils.join(propertyExamples, ", ");
example += "}";
return example;
return "*" + goImportAlias + ".New" + codegenModel.name + "(" + StringUtils.join(propertyExamples, ", ") + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
)

func main() {
body := openapiclient.Client{Client: "Client_example"} // Client | client model
body := *openapiclient.NewClient() // Client | client model

configuration := openapiclient.NewConfiguration()
api_client := openapiclient.NewAPIClient(configuration)
Expand Down
16 changes: 8 additions & 8 deletions samples/client/petstore/go/go-petstore/docs/FakeApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import (
)

func main() {
xmlItem := openapiclient.XmlItem{AttributeString: "AttributeString_example", AttributeNumber: 123, AttributeInteger: 123, AttributeBoolean: true, WrappedArray: []int32{123), NameString: "NameString_example", NameNumber: 123, NameInteger: 123, NameBoolean: true, NameArray: []int32{123), NameWrappedArray: []int32{123), PrefixString: "PrefixString_example", PrefixNumber: 123, PrefixInteger: 123, PrefixBoolean: true, PrefixArray: []int32{123), PrefixWrappedArray: []int32{123), NamespaceString: "NamespaceString_example", NamespaceNumber: 123, NamespaceInteger: 123, NamespaceBoolean: true, NamespaceArray: []int32{123), NamespaceWrappedArray: []int32{123), PrefixNsString: "PrefixNsString_example", PrefixNsNumber: 123, PrefixNsInteger: 123, PrefixNsBoolean: true, PrefixNsArray: []int32{123), PrefixNsWrappedArray: []int32{123)} // XmlItem | XmlItem Body
xmlItem := *openapiclient.NewXmlItem() // XmlItem | XmlItem Body

configuration := openapiclient.NewConfiguration()
api_client := openapiclient.NewAPIClient(configuration)
Expand Down Expand Up @@ -172,7 +172,7 @@ import (
)

func main() {
body := openapiclient.OuterComposite{MyNumber: 123, MyString: "MyString_example", MyBoolean: false} // OuterComposite | Input composite as post body (optional)
body := *openapiclient.NewOuterComposite() // OuterComposite | Input composite as post body (optional)

configuration := openapiclient.NewConfiguration()
api_client := openapiclient.NewAPIClient(configuration)
Expand Down Expand Up @@ -370,7 +370,7 @@ import (
)

func main() {
body := openapiclient.FileSchemaTestClass{File: openapiclient.File{SourceURI: "SourceURI_example"}, Files: []File{openapiclient.File{SourceURI: "SourceURI_example"})} // FileSchemaTestClass |
body := *openapiclient.NewFileSchemaTestClass() // FileSchemaTestClass |

configuration := openapiclient.NewConfiguration()
api_client := openapiclient.NewAPIClient(configuration)
Expand Down Expand Up @@ -433,7 +433,7 @@ import (

func main() {
query := "query_example" // string |
body := openapiclient.User{Id: int64(123), Username: "Username_example", FirstName: "FirstName_example", LastName: "LastName_example", Email: "Email_example", Password: "Password_example", Phone: "Phone_example", UserStatus: 123} // User |
body := *openapiclient.NewUser() // User |

configuration := openapiclient.NewConfiguration()
api_client := openapiclient.NewAPIClient(configuration)
Expand Down Expand Up @@ -498,7 +498,7 @@ import (
)

func main() {
body := openapiclient.Client{Client: "Client_example"} // Client | client model
body := *openapiclient.NewClient() // Client | client model

configuration := openapiclient.NewConfiguration()
api_client := openapiclient.NewAPIClient(configuration)
Expand Down Expand Up @@ -574,8 +574,8 @@ func main() {
float := 987 // float32 | None (optional)
string_ := "string__example" // string | None (optional)
binary := 987 // *os.File | None (optional)
date := Get-Date // string | None (optional)
dateTime := Get-Date // time.Time | None (optional)
date := time.Now() // string | None (optional)
dateTime := time.Now() // time.Time | None (optional)
password := "password_example" // string | None (optional)
callback := "callback_example" // string | None (optional)

Expand Down Expand Up @@ -804,7 +804,7 @@ import (
)

func main() {
param := map[string]string{ "Key" = "Value" } // map[string]string | request body
param := map[string]string{ "key": "Inner_example"} // map[string]string | request body

configuration := openapiclient.NewConfiguration()
api_client := openapiclient.NewAPIClient(configuration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
)

func main() {
body := openapiclient.Client{Client: "Client_example"} // Client | client model
body := *openapiclient.NewClient() // Client | client model

configuration := openapiclient.NewConfiguration()
api_client := openapiclient.NewAPIClient(configuration)
Expand Down
4 changes: 2 additions & 2 deletions samples/client/petstore/go/go-petstore/docs/PetApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
)

func main() {
body := openapiclient.Pet{Id: int64(123), Category: openapiclient.Category{Id: int64(123), Name: "Name_example"}, Name: "Name_example", PhotoUrls: []string{"PhotoUrls_example"), Tags: []Tag{openapiclient.Tag{Id: int64(123), Name: "Name_example"}), Status: "Status_example"} // Pet | Pet object that needs to be added to the store
body := *openapiclient.NewPet("Name_example", []string{"PhotoUrls_example"}) // Pet | Pet object that needs to be added to the store

configuration := openapiclient.NewConfiguration()
api_client := openapiclient.NewAPIClient(configuration)
Expand Down Expand Up @@ -367,7 +367,7 @@ import (
)

func main() {
body := openapiclient.Pet{Id: int64(123), Category: openapiclient.Category{Id: int64(123), Name: "Name_example"}, Name: "Name_example", PhotoUrls: []string{"PhotoUrls_example"), Tags: []Tag{openapiclient.Tag{Id: int64(123), Name: "Name_example"}), Status: "Status_example"} // Pet | Pet object that needs to be added to the store
body := *openapiclient.NewPet("Name_example", []string{"PhotoUrls_example"}) // Pet | Pet object that needs to be added to the store

configuration := openapiclient.NewConfiguration()
api_client := openapiclient.NewAPIClient(configuration)
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/go/go-petstore/docs/StoreApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ import (
)

func main() {
body := openapiclient.Order{Id: int64(123), PetId: int64(123), Quantity: 123, ShipDate: "TODO", Status: "Status_example", Complete: false} // Order | order placed for purchasing the pet
body := *openapiclient.NewOrder() // Order | order placed for purchasing the pet

configuration := openapiclient.NewConfiguration()
api_client := openapiclient.NewAPIClient(configuration)
Expand Down
4 changes: 2 additions & 2 deletions samples/client/petstore/go/go-petstore/docs/UserApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
)

func main() {
body := openapiclient.User{Id: int64(123), Username: "Username_example", FirstName: "FirstName_example", LastName: "LastName_example", Email: "Email_example", Password: "Password_example", Phone: "Phone_example", UserStatus: 123} // User | Created user object
body := *openapiclient.NewUser() // User | Created user object

configuration := openapiclient.NewConfiguration()
api_client := openapiclient.NewAPIClient(configuration)
Expand Down Expand Up @@ -98,7 +98,7 @@ import (
)

func main() {
body := []User{openapiclient.User{Id: int64(123), Username: "Username_example", FirstName: "FirstName_example", LastName: "LastName_example", Email: "Email_example", Password: "Password_example", Phone: "Phone_example", UserStatus: 123}} // []User | List of user object
body := []User{*openapiclient.NewUser()} // []User | List of user object

configuration := openapiclient.NewConfiguration()
api_client := openapiclient.NewAPIClient(configuration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
)

func main() {
client := openapiclient.Client{Client: "Client_example"} // Client | client model
client := *openapiclient.NewClient() // Client | client model

configuration := openapiclient.NewConfiguration()
api_client := openapiclient.NewAPIClient(configuration)
Expand Down
14 changes: 7 additions & 7 deletions samples/openapi3/client/petstore/go/go-petstore/docs/FakeApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ import (
)

func main() {
outerComposite := openapiclient.OuterComposite{MyNumber: 123, MyString: "MyString_example", MyBoolean: false} // OuterComposite | Input composite as post body (optional)
outerComposite := *openapiclient.NewOuterComposite() // OuterComposite | Input composite as post body (optional)

configuration := openapiclient.NewConfiguration()
api_client := openapiclient.NewAPIClient(configuration)
Expand Down Expand Up @@ -365,7 +365,7 @@ import (
)

func main() {
fileSchemaTestClass := openapiclient.FileSchemaTestClass{File: openapiclient.File{SourceURI: "SourceURI_example"}, Files: []File{openapiclient.File{SourceURI: "SourceURI_example"})} // FileSchemaTestClass |
fileSchemaTestClass := *openapiclient.NewFileSchemaTestClass() // FileSchemaTestClass |

configuration := openapiclient.NewConfiguration()
api_client := openapiclient.NewAPIClient(configuration)
Expand Down Expand Up @@ -428,7 +428,7 @@ import (

func main() {
query := "query_example" // string |
user := openapiclient.User{Id: int64(123), Username: "Username_example", FirstName: "FirstName_example", LastName: "LastName_example", Email: "Email_example", Password: "Password_example", Phone: "Phone_example", UserStatus: 123, ArbitraryObject: 123, ArbitraryNullableObject: 123, ArbitraryTypeValue: 123, ArbitraryNullableTypeValue: 123} // User |
user := *openapiclient.NewUser() // User |

configuration := openapiclient.NewConfiguration()
api_client := openapiclient.NewAPIClient(configuration)
Expand Down Expand Up @@ -493,7 +493,7 @@ import (
)

func main() {
client := openapiclient.Client{Client: "Client_example"} // Client | client model
client := *openapiclient.NewClient() // Client | client model

configuration := openapiclient.NewConfiguration()
api_client := openapiclient.NewAPIClient(configuration)
Expand Down Expand Up @@ -569,8 +569,8 @@ func main() {
float := 987 // float32 | None (optional)
string_ := "string__example" // string | None (optional)
binary := 987 // *os.File | None (optional)
date := Get-Date // string | None (optional)
dateTime := Get-Date // time.Time | None (optional)
date := time.Now() // string | None (optional)
dateTime := time.Now() // time.Time | None (optional)
password := "password_example" // string | None (optional)
callback := "callback_example" // string | None (optional)

Expand Down Expand Up @@ -799,7 +799,7 @@ import (
)

func main() {
requestBody := map[string]string{ "Key" = "Value" } // map[string]string | request body
requestBody := map[string]string{ "key": "Inner_example"} // map[string]string | request body

configuration := openapiclient.NewConfiguration()
api_client := openapiclient.NewAPIClient(configuration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
)

func main() {
client := openapiclient.Client{Client: "Client_example"} // Client | client model
client := *openapiclient.NewClient() // Client | client model

configuration := openapiclient.NewConfiguration()
api_client := openapiclient.NewAPIClient(configuration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
)

func main() {
pet := openapiclient.Pet{Id: int64(123), Category: openapiclient.Category{Id: int64(123), Name: "Name_example"}, Name: "Name_example", PhotoUrls: []string{"PhotoUrls_example"), Tags: []Tag{openapiclient.Tag{Id: int64(123), Name: "Name_example"}), Status: "Status_example"} // Pet | Pet object that needs to be added to the store
pet := *openapiclient.NewPet("Name_example", []string{"PhotoUrls_example"}) // Pet | Pet object that needs to be added to the store

configuration := openapiclient.NewConfiguration()
api_client := openapiclient.NewAPIClient(configuration)
Expand Down Expand Up @@ -367,7 +367,7 @@ import (
)

func main() {
pet := openapiclient.Pet{Id: int64(123), Category: openapiclient.Category{Id: int64(123), Name: "Name_example"}, Name: "Name_example", PhotoUrls: []string{"PhotoUrls_example"), Tags: []Tag{openapiclient.Tag{Id: int64(123), Name: "Name_example"}), Status: "Status_example"} // Pet | Pet object that needs to be added to the store
pet := *openapiclient.NewPet("Name_example", []string{"PhotoUrls_example"}) // Pet | Pet object that needs to be added to the store

configuration := openapiclient.NewConfiguration()
api_client := openapiclient.NewAPIClient(configuration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ import (
)

func main() {
order := openapiclient.Order{Id: int64(123), PetId: int64(123), Quantity: 123, ShipDate: "TODO", Status: "Status_example", Complete: false} // Order | order placed for purchasing the pet
order := *openapiclient.NewOrder() // Order | order placed for purchasing the pet

configuration := openapiclient.NewConfiguration()
api_client := openapiclient.NewAPIClient(configuration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
)

func main() {
user := openapiclient.User{Id: int64(123), Username: "Username_example", FirstName: "FirstName_example", LastName: "LastName_example", Email: "Email_example", Password: "Password_example", Phone: "Phone_example", UserStatus: 123, ArbitraryObject: 123, ArbitraryNullableObject: 123, ArbitraryTypeValue: 123, ArbitraryNullableTypeValue: 123} // User | Created user object
user := *openapiclient.NewUser() // User | Created user object

configuration := openapiclient.NewConfiguration()
api_client := openapiclient.NewAPIClient(configuration)
Expand Down Expand Up @@ -98,7 +98,7 @@ import (
)

func main() {
user := []User{openapiclient.User{Id: int64(123), Username: "Username_example", FirstName: "FirstName_example", LastName: "LastName_example", Email: "Email_example", Password: "Password_example", Phone: "Phone_example", UserStatus: 123, ArbitraryObject: 123, ArbitraryNullableObject: 123, ArbitraryTypeValue: 123, ArbitraryNullableTypeValue: 123}} // []User | List of user object
user := []User{*openapiclient.NewUser()} // []User | List of user object

configuration := openapiclient.NewConfiguration()
api_client := openapiclient.NewAPIClient(configuration)
Expand Down

0 comments on commit 8afb067

Please sign in to comment.