Merge pull request #3543 from guohuang/compile_issue2

[GO] Fixing compilation issue when pathParams  is not presented
This commit is contained in:
wing328 2016-08-06 14:07:00 +08:00 committed by GitHub
commit e8095c6a04
5 changed files with 29 additions and 20 deletions

View File

@ -382,13 +382,21 @@ public class GoClientCodegen extends DefaultCodegen implements CodegenConfig {
// if the return type is not primitive, import encoding/json
for (CodegenOperation operation : operations) {
if(operation.returnBaseType != null && needToImport(operation.returnBaseType)) {
Map<String, String> customImport = new HashMap<String, String>();
customImport.put("import", "encoding/json");
imports.add(customImport);
imports.add(createMapping("import", "encoding/json"));
break; //just need to import once
}
}
// this will only import "strings" "fmt" if there are items in pathParams
for (CodegenOperation operation : operations) {
if(operation.pathParams != null && operation.pathParams.size() > 0) {
imports.add(createMapping("import", "fmt"));
imports.add(createMapping("import", "strings"));
break; //just need to import once
}
}
// recursivly add import for mapping one type to multipe imports
List<Map<String, String>> recursiveImports = (List<Map<String, String>>) objs.get("imports");
if (recursiveImports == null)
@ -400,9 +408,7 @@ public class GoClientCodegen extends DefaultCodegen implements CodegenConfig {
// if the import package happens to be found in the importMapping (key)
// add the corresponding import package to the list
if (importMapping.containsKey(_import)) {
Map<String, String> newImportMap= new HashMap<String, String>();
newImportMap.put("import", importMapping.get(_import));
listIterator.add(newImportMap);
listIterator.add(createMapping("import", importMapping.get(_import)));
}
}
@ -432,9 +438,7 @@ public class GoClientCodegen extends DefaultCodegen implements CodegenConfig {
// if the import package happens to be found in the importMapping (key)
// add the corresponding import package to the list
if (importMapping.containsKey(_import)) {
Map<String, String> newImportMap= new HashMap<String, String>();
newImportMap.put("import", importMapping.get(_import));
listIterator.add(newImportMap);
listIterator.add(createMapping("import", importMapping.get(_import)));
}
}
@ -465,4 +469,11 @@ public class GoClientCodegen extends DefaultCodegen implements CodegenConfig {
public String escapeUnsafeCharacters(String input) {
return input.replace("*/", "*_/").replace("/*", "/_*");
}
public Map<String, String> createMapping(String key, String value){
Map<String, String> customImport = new HashMap<String, String>();
customImport.put(key, value);
return customImport;
}
}

View File

@ -3,8 +3,6 @@ package {{packageName}}
{{#operations}}
import (
"strings"
"fmt"
"net/url"
{{#imports}} "{{import}}"
{{/imports}}

View File

@ -23,12 +23,12 @@
package petstore
import (
"strings"
"fmt"
"net/url"
"os"
"io/ioutil"
"encoding/json"
"fmt"
"strings"
)
type PetApi struct {

View File

@ -23,10 +23,10 @@
package petstore
import (
"strings"
"fmt"
"net/url"
"encoding/json"
"fmt"
"strings"
)
type StoreApi struct {

View File

@ -23,10 +23,10 @@
package petstore
import (
"strings"
"fmt"
"net/url"
"encoding/json"
"fmt"
"strings"
)
type UserApi struct {