Skip to content

Commit

Permalink
- fixes object generation for go snippets
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Biret <[email protected]>
  • Loading branch information
baywet committed Nov 4, 2021
1 parent 9cd1bec commit c700fb1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
8 changes: 4 additions & 4 deletions CodeSnippetsReflection.OpenAPI.Test/GoGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ public async Task WritesTheRequestPayload() {
var snippetModel = new SnippetModel(requestPayload, ServiceRootUrl, await GetV1TreeNode());
var result = _generator.GenerateCodeSnippet(snippetModel);
Assert.Contains("msgraphsdk.NewUser", result);
Assert.Contains("SetAccountEnabled(true)", result);
Assert.Contains("SetPasswordProfile = new PasswordProfile", result);
Assert.Contains("SetDisplayName(\"displayName-value\")", result);
Assert.Contains("SetAccountEnabled(&accountEnabled)", result);
Assert.Contains("passwordProfile := msgraphsdk.NewPasswordProfile()", result);
Assert.Contains("displayName := \"displayName-value\"", result);
}
[Fact]
public async Task WritesALongAndFindsAnAction() {
Expand Down Expand Up @@ -148,7 +148,7 @@ public async Task GeneratesADateTimeOffsetPayload() {
};
var snippetModel = new SnippetModel(requestPayload, ServiceRootUrl, await GetV1TreeNode());
var result = _generator.GenerateCodeSnippet(snippetModel);
Assert.Contains(",err := time.Parse(time.RFC3339,", result);
Assert.Contains(", err := time.Parse(time.RFC3339,", result);
}
[Fact]
public async Task GeneratesAnArrayPayloadInAdditionalData() {
Expand Down
35 changes: 22 additions & 13 deletions CodeSnippetsReflection.OpenAPI/LanguageGenerators/GoGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,46 +156,55 @@ private static void WriteJsonObjectValue(StringBuilder payloadSB, JsonElement va
foreach(var propertyAndSchema in propertiesAndSchema.Where(x => x.Item2 != null)) {
var propertyName = propertyAndSchema.Item1.Name.ToFirstCharacterUpperCase();
var propertyAssignment = includePropertyAssignment ? $"{variableName}Set{indentManager.GetIndent()}{propertyName}(" : string.Empty;
WriteProperty(payloadSB, propertyAndSchema.Item1.Value, propertyAndSchema.Item2, indentManager, propertyAssignment, ")");
WriteProperty(payloadSB, propertyAndSchema.Item1.Value, propertyAndSchema.Item2, indentManager, propertyAssignment, propertyName.ToFirstCharacterLowerCase(), ")");
}
var propertiesWithoutSchema = propertiesAndSchema.Where(x => x.Item2 == null).Select(x => x.Item1);
if(propertiesWithoutSchema.Any()) {
payloadSB.AppendLine($"{indentManager.GetIndent()}{variableName}SetAdditionalData(map[string]interface{{}}{{");
indentManager.Indent();
foreach(var property in propertiesWithoutSchema) {
var propertyAssignment = $"{indentManager.GetIndent()}\"{property.Name}\": ";
WriteProperty(payloadSB, property.Value, null, indentManager, propertyAssignment, ",");
WriteProperty(payloadSB, property.Value, null, indentManager, propertyAssignment, null, ",");
}
indentManager.Unindent();
payloadSB.AppendLine($"{indentManager.GetIndent()}}}");
}
}
private static void WriteProperty(StringBuilder payloadSB, JsonElement value, OpenApiSchema propSchema, IndentManager indentManager, string propertyAssignment, string propertySuffix = default) {
switch (value.ValueKind) {// TODO: this function needs to be splat between declaring the properties and assigning them with pointers
private static void WritePropertyFromTempVar(StringBuilder payloadSB, string propertyAssignment, string tempVarName, string valueAssignment, string valueDeclaration, bool addPointer, string propertySuffix) {
if(!string.IsNullOrEmpty(tempVarName)) {
payloadSB.AppendLine($"{tempVarName}{valueAssignment}{valueDeclaration}");
var pointer = addPointer ? "&" : string.Empty;
payloadSB.AppendLine($"{propertyAssignment}{pointer}{tempVarName}{propertySuffix}");
} else {
payloadSB.AppendLine($"{propertyAssignment}{valueDeclaration}{propertySuffix}");
}
}
private static void WriteProperty(StringBuilder payloadSB, JsonElement value, OpenApiSchema propSchema, IndentManager indentManager, string propertyAssignment, string tempVarName, string propertySuffix = default) {
switch (value.ValueKind) {
case JsonValueKind.String:
if(propSchema?.Format?.Equals("base64url", StringComparison.OrdinalIgnoreCase) ?? false)
payloadSB.AppendLine($"{propertyAssignment}[]byte(\"{value.GetString()}\"){propertySuffix}");
WritePropertyFromTempVar(payloadSB, propertyAssignment, tempVarName, " := ", $"[]byte(\"{value.GetString()}\")", true, propertySuffix);
else if (propSchema?.Format?.Equals("date-time", StringComparison.OrdinalIgnoreCase) ?? false)
payloadSB.AppendLine($"{propertyAssignment}time.Parse(time.RFC3339, \"{value.GetString()}\"){propertySuffix}");
WritePropertyFromTempVar(payloadSB, propertyAssignment, tempVarName, ", err := ", $"time.Parse(time.RFC3339, \"{value.GetString()}\")", true, propertySuffix);
else if (propSchema?.Format?.Equals("guid", StringComparison.OrdinalIgnoreCase) ?? false)
payloadSB.AppendLine($"{propertyAssignment}uuid.MustParse(\"{value.GetString()}\"){propertySuffix}");
WritePropertyFromTempVar(payloadSB, propertyAssignment, tempVarName, " := ", $"uuid.MustParse(\"{value.GetString()}\")", true, propertySuffix);
else
payloadSB.AppendLine($"{propertyAssignment}\"{value.GetString()}\"{propertySuffix}");
WritePropertyFromTempVar(payloadSB, propertyAssignment, tempVarName, " := ", $"\"{value.GetString()}\"", true, propertySuffix);
break;
case JsonValueKind.Number:
payloadSB.AppendLine($"{propertyAssignment}{GetNumberLiteral(propSchema, value)}{propertySuffix}");
WritePropertyFromTempVar(payloadSB, propertyAssignment, tempVarName, " := ", GetNumberLiteral(propSchema, value), true, propertySuffix);
break;
case JsonValueKind.False:
case JsonValueKind.True:
payloadSB.AppendLine($"{propertyAssignment}{value.GetBoolean().ToString().ToLowerInvariant()}{propertySuffix}");
WritePropertyFromTempVar(payloadSB, propertyAssignment, tempVarName, " := ", value.GetBoolean().ToString().ToLowerInvariant(), true, propertySuffix);
break;
case JsonValueKind.Null:
payloadSB.AppendLine($"{propertyAssignment}nil{propertySuffix}");
break;
case JsonValueKind.Object:
if(propSchema != null) {
payloadSB.AppendLine($"{propertyAssignment}msgraphsdk.New{propSchema.GetSchemaTitle().ToFirstCharacterUpperCase()}{propertySuffix}");
WriteJsonObjectValue(payloadSB, value, propSchema, indentManager);
WritePropertyFromTempVar(payloadSB, propertyAssignment, tempVarName, " := ", $"msgraphsdk.New{propSchema.GetSchemaTitle().ToFirstCharacterUpperCase()}()", false, propertySuffix);
WriteJsonObjectValue(payloadSB, value, propSchema, indentManager, variableName: tempVarName);
}
break;
case JsonValueKind.Array:
Expand All @@ -210,7 +219,7 @@ private static void WriteJsonArrayValue(StringBuilder payloadSB, JsonElement val
payloadSB.AppendLine($"{propertyAssignment} []{genericType} {{");
indentManager.Indent();
foreach(var item in value.EnumerateArray())
WriteProperty(payloadSB, item, schema, indentManager, indentManager.GetIndent(), ",");
WriteProperty(payloadSB, item, schema, indentManager, indentManager.GetIndent(), null, ",");
indentManager.Unindent();
payloadSB.AppendLine($"{indentManager.GetIndent()}}}");
}
Expand Down

0 comments on commit c700fb1

Please sign in to comment.