Merge pull request #2289 from neilotoole/issue-2285

Several minor fixes in the Go client code generator for Camelization of elements
This commit is contained in:
wing328 2016-03-02 13:58:17 +08:00
commit 336d80cbf3

View File

@ -44,7 +44,8 @@ public class GoClientCodegen extends DefaultCodegen implements CodegenConfig {
"case", "defer", "go", "map", "struct", "case", "defer", "go", "map", "struct",
"chan", "else", "goto", "package", "switch", "chan", "else", "goto", "package", "switch",
"const", "fallthrough", "if", "range", "type", "const", "fallthrough", "if", "range", "type",
"continue", "for", "import", "return", "var") "continue", "for", "import", "return", "var", "error")
// Added "error" as it's used so frequently that it may as well be a keyword
); );
defaultIncludes = new HashSet<String>( defaultIncludes = new HashSet<String>(
@ -131,8 +132,22 @@ public class GoClientCodegen extends DefaultCodegen implements CodegenConfig {
} }
@Override @Override
public String escapeReservedWord(String name) { public String escapeReservedWord(String name)
return "_" + name; {
// Can't start with an underscore, as our fields need to start with an
// UppercaseLetter so that Go treats them as public/visible.
// Options?
// - MyName
// - AName
// - TheName
// - XName
// - X_Name
// ... or maybe a suffix?
// - Name_ ... think this will work.
// FIXME: This should also really be a customizable option
return camelize(name) + '_';
} }
@Override @Override
@ -166,8 +181,13 @@ public class GoClientCodegen extends DefaultCodegen implements CodegenConfig {
@Override @Override
public String toParamName(String name) { public String toParamName(String name) {
// should be the same as variable name // params should be lowerCamelCase. E.g. "person Person", instead of
return toVarName(name); // "Person Person".
//
// REVISIT: Actually, for idiomatic go, the param name should
// really should just be a letter, e.g. "p Person"), but we'll get
// around to that some other time... Maybe.
return camelize(toVarName(name), true);
} }
@Override @Override
@ -200,7 +220,24 @@ public class GoClientCodegen extends DefaultCodegen implements CodegenConfig {
return getSwaggerType(p) + "[string]" + getTypeDeclaration(inner); return getSwaggerType(p) + "[string]" + getTypeDeclaration(inner);
} }
return super.getTypeDeclaration(p); //return super.getTypeDeclaration(p);
// Not using the supertype invocation, because we want to UpperCamelize
// the type.
String swaggerType = getSwaggerType(p);
if (typeMapping.containsKey(swaggerType)) {
return typeMapping.get(swaggerType);
}
if(typeMapping.containsValue(swaggerType)) {
return swaggerType;
}
if(languageSpecificPrimitives.contains(swaggerType)) {
return swaggerType;
}
return camelize(swaggerType, false);
} }
@Override @Override