fixed multiple import mapping issue

This commit is contained in:
Guo Huang 2016-04-23 09:52:17 -07:00
parent e555b3ad34
commit e7df5f9551
3 changed files with 38 additions and 12 deletions

View File

@ -104,7 +104,7 @@ public class GoClientCodegen extends DefaultCodegen implements CodegenConfig {
importMapping = new HashMap<String, String>();
importMapping.put("time.Time", "time");
importMapping.put("*os.File", "os");
importMapping.put("ioutil", "io/ioutil");
importMapping.put("os", "io/ioutil");
cliOptions.clear();
cliOptions.add(new CliOption(CodegenConstants.PACKAGE_NAME, "Go package name (convention: lowercase).")
@ -375,6 +375,23 @@ public class GoClientCodegen extends DefaultCodegen implements CodegenConfig {
iterator.remove();
}
// 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)
return objs;
ListIterator<Map<String, String>> listIterator = imports.listIterator();
while (listIterator.hasNext()) {
String _import = listIterator.next().get("import");
// 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);
}
}
return objs;
}
@ -389,6 +406,24 @@ public class GoClientCodegen extends DefaultCodegen implements CodegenConfig {
if (_import.startsWith(prefix))
iterator.remove();
}
// 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)
return objs;
ListIterator<Map<String, String>> listIterator = imports.listIterator();
while (listIterator.hasNext()) {
String _import = listIterator.next().get("import");
// 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);
}
}
return objs;
}
@ -405,4 +440,4 @@ public class GoClientCodegen extends DefaultCodegen implements CodegenConfig {
public void setPackageVersion(String packageVersion) {
this.packageVersion = packageVersion;
}
}
}

View File

@ -6,15 +6,6 @@ import (
"fmt"
"encoding/json"
"errors"
{{#operation}}
{{#hasFormParams}}
{{#formParams}}
{{#isFile}}
"io/ioutil"
{{/isFile}}
{{/formParams}}
{{/hasFormParams}}
{{/operation}}
{{#imports}} "{{import}}"
{{/imports}}
)

View File

@ -5,8 +5,8 @@ import (
"fmt"
"encoding/json"
"errors"
"io/ioutil"
"os"
"io/ioutil"
)
type PetApi struct {