Skip to content

Commit

Permalink
Merge pull request #4388 from lampajr/gh4380_go_scalar_array
Browse files Browse the repository at this point in the history
Fix go scalar collection cast
  • Loading branch information
baywet authored Mar 25, 2024
2 parents a1164f1 + b0337cf commit 95091be
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Fixed Go generation bug when dealing with scalar collection body. [#4380](https://github.com/microsoft/kiota/issues/4380)
- Fixed a bug where multiple Visual Studio Code instances would make the extension install/update fail. [#3686](https://github.com/microsoft/kiota/issues/3686)
- Fixed a bug where models properties named "additionalData" or "backingstore" would be ignored. [#4224](https://github.com/microsoft/kiota/issues/4224)
- PREVIEW: Renamed the config commands to workspace. [#4310](https://github.com/microsoft/kiota/issues/4310)
Expand Down
26 changes: 20 additions & 6 deletions src/Kiota.Builder/Writers/Go/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -723,16 +723,23 @@ private void WriteFieldDeserializer(CodeProperty property, LanguageWriter writer
private static string GetTypeAssertion(string originalReference, string typeImportName, string? assignVarName = default, string? statusVarName = default) =>
$"{assignVarName}{(!string.IsNullOrEmpty(statusVarName) && !string.IsNullOrEmpty(assignVarName) ? ", " : string.Empty)}{statusVarName}{(string.IsNullOrEmpty(statusVarName) && string.IsNullOrEmpty(assignVarName) ? string.Empty : " := ")}{originalReference}.({typeImportName})";

private static void WriteCollectionCast(string propertyTypeImportName, string sourceVarName, string targetVarName, LanguageWriter writer, string pointerSymbol = "*", bool dereference = true)
private static void WriteCollectionCast(string propertyTypeImportName, string sourceVarName, string targetVarName, LanguageWriter writer, string pointerSymbol = "*", bool dereference = true, bool scalar = false)
{
writer.WriteLines($"{targetVarName} := make([]{propertyTypeImportName}, len({sourceVarName}))",
$"for i, v := range {sourceVarName} {{");
writer.IncreaseIndent();
writer.StartBlock("if v != nil {");
var derefPrefix = dereference ? "*(" : string.Empty;
var derefSuffix = dereference ? ")" : string.Empty;
writer.WriteLine($"{targetVarName}[i] = {GetTypeAssertion(derefPrefix + "v", pointerSymbol + propertyTypeImportName)}{derefSuffix}");
writer.CloseBlock();
if (!scalar)
{
writer.StartBlock("if v != nil {");
var derefPrefix = dereference ? "*(" : string.Empty;
var derefSuffix = dereference ? ")" : string.Empty;
writer.WriteLine($"{targetVarName}[i] = {GetTypeAssertion(derefPrefix + "v", pointerSymbol + propertyTypeImportName)}{derefSuffix}");
writer.CloseBlock();
}
else
{
writer.WriteLine($"{targetVarName}[i] = v");
}
writer.CloseBlock();
}

Expand Down Expand Up @@ -909,7 +916,14 @@ private void WriteRequestGeneratorBody(CodeMethod codeElement, RequestParams req
writer.WriteBlock("if err != nil {", "}", "return nil, err");
}
else
{
if (requestParams.requestBody.Type.IsCollection)
{
WriteCollectionCast("interface{}", bodyParamReference, "cast", writer, string.Empty, false, true);
bodyParamReference = "cast";
}
writer.WriteLine($"{RequestInfoVarName}.SetContentFromScalar{collectionSuffix}({contextParameterName}, m.{requestAdapterPropertyName}, \"{sanitizedRequestBodyContentType}\", {bodyParamReference})");
}
}

writer.WriteLine($"return {RequestInfoVarName}, nil");
Expand Down
4 changes: 4 additions & 0 deletions tests/Kiota.Builder.Tests/Writers/Go/CodeMethodWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,7 @@ public async Task WritesRequestGeneratorBodyForScalar()
Assert.Contains("if c.Q != nil", result);
Assert.Contains("requestInfo.AddQueryParameters(", result);
Assert.Contains("requestInfo.AddRequestOptions(", result);
Assert.DoesNotContain("cast[i] = v", result);
Assert.Contains("requestInfo.SetContentFromScalar(ctx, m.BaseRequestBuilder.RequestAdapter", result);
Assert.Contains("return requestInfo, nil", result);
AssertExtensions.CurlyBracesAreClosed(result);
Expand Down Expand Up @@ -1205,6 +1206,8 @@ public async Task WritesRequestGeneratorBodyForScalarCollection()
Assert.Contains("if c.Q != nil", result);
Assert.Contains("requestInfo.AddQueryParameters(", result);
Assert.Contains("requestInfo.AddRequestOptions(", result);
Assert.Contains("cast := make([]interface{}, ", result);
Assert.Contains("cast[i] = v", result);
Assert.Contains("requestInfo.SetContentFromScalarCollection(ctx, m.BaseRequestBuilder.RequestAdapter", result);
Assert.Contains("return requestInfo, nil", result);
AssertExtensions.CurlyBracesAreClosed(result);
Expand Down Expand Up @@ -1305,6 +1308,7 @@ public async Task WritesRequestGeneratorBodyForParsableCollection()
Assert.Contains("if c.Q != nil", result);
Assert.Contains("requestInfo.AddQueryParameters(", result);
Assert.Contains("requestInfo.AddRequestOptions(", result);
Assert.Contains("cast[i] =", result);
Assert.Contains("requestInfo.SetContentFromParsableCollection(ctx, m.BaseRequestBuilder.RequestAdapter", result);
Assert.Contains("return requestInfo, nil", result);
AssertExtensions.CurlyBracesAreClosed(result);
Expand Down

0 comments on commit 95091be

Please sign in to comment.