Skip to content

Commit

Permalink
Merge pull request #2289 from neilotoole/issue-2285
Browse files Browse the repository at this point in the history
Several minor fixes in the Go client code generator for Camelization of elements
  • Loading branch information
wing328 committed Mar 2, 2016
2 parents 4bcd81a + 54b4da2 commit 336d80c
Showing 1 changed file with 43 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public GoClientCodegen() {
"case", "defer", "go", "map", "struct",
"chan", "else", "goto", "package", "switch",
"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>(
Expand Down Expand Up @@ -131,8 +132,22 @@ public void processOpts() {
}

@Override
public String escapeReservedWord(String name) {
return "_" + name;
public String escapeReservedWord(String 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
Expand Down Expand Up @@ -166,8 +181,13 @@ public String toVarName(String name) {

@Override
public String toParamName(String name) {
// should be the same as variable name
return toVarName(name);
// params should be lowerCamelCase. E.g. "person Person", instead of
// "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
Expand Down Expand Up @@ -200,7 +220,24 @@ else if (p instanceof MapProperty) {

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
Expand Down

0 comments on commit 336d80c

Please sign in to comment.