[go] Improve examples generation (#7576)

* fix go examples

* use constructor with required args
This commit is contained in:
Hippolyte HENRY
2020-10-15 18:04:30 +02:00
committed by GitHub
parent e58b1f6abb
commit 8afb067ec9
13 changed files with 38 additions and 41 deletions

View File

@@ -464,7 +464,7 @@ public class GoClientCodegen extends AbstractGoCodegen {
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)) {
@@ -479,9 +479,9 @@ public class GoClientCodegen extends AbstractGoCodegen {
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;
@@ -502,9 +502,9 @@ public class GoClientCodegen extends AbstractGoCodegen {
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)) {
@@ -519,7 +519,7 @@ public class GoClientCodegen extends AbstractGoCodegen {
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
@@ -565,13 +565,10 @@ public class GoClientCodegen extends AbstractGoCodegen {
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, ", ") + ")";
}
}

View File

@@ -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)

View File

@@ -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)
@@ -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)
@@ -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)
@@ -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)
@@ -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)
@@ -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)
@@ -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)

View File

@@ -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)

View File

@@ -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)
@@ -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)

View File

@@ -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)

View File

@@ -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)
@@ -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)

View File

@@ -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)

View File

@@ -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)
@@ -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)
@@ -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)
@@ -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)
@@ -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)
@@ -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)

View File

@@ -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)

View File

@@ -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)
@@ -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)

View File

@@ -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)

View File

@@ -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)
@@ -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)