Skip to content

Commit

Permalink
Adds mask for go enums
Browse files Browse the repository at this point in the history
  • Loading branch information
rkodev committed Aug 28, 2023
1 parent bf0f64e commit 8d6f606
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added support for external documentation links within descriptions in Python. [#2041](https://github.com/microsoft/kiota/issues/2041)
- Added support for API manifests. [#3104](https://github.com/microsoft/kiota/issues/3104)
- Added support for reserved path parameters. [#2320](https://github.com/microsoft/kiota/issues/2320)
- Added support for csv values in enums using a mask.

### Changed

Expand Down
10 changes: 7 additions & 3 deletions src/Kiota.Builder/Refiners/GoRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance
cancellationToken.ThrowIfCancellationRequested();
MakeModelPropertiesNullable(
generatedCode);
AddErrorImportForEnums(
AddErrorAndStringsImportForEnums(
generatedCode);
var defaultConfiguration = new GenerationConfiguration();
ReplaceDefaultSerializationModules(
Expand Down Expand Up @@ -472,16 +472,20 @@ private static void ReplaceRequestBuilderPropertiesByMethods(CodeElement current
}
CrawlTree(currentElement, ReplaceRequestBuilderPropertiesByMethods);
}
private static void AddErrorImportForEnums(CodeElement currentElement)
private static void AddErrorAndStringsImportForEnums(CodeElement currentElement)
{
if (currentElement is CodeEnum currentEnum)
{
currentEnum.AddUsing(new CodeUsing
{
Name = "errors",
});
currentEnum.AddUsing(new CodeUsing
{
Name = "strings",
});
}
CrawlTree(currentElement, AddErrorImportForEnums);
CrawlTree(currentElement, AddErrorAndStringsImportForEnums);
}
private static readonly GoConventionService conventions = new();
private static readonly HashSet<string> typeToSkipStrConv = new(StringComparer.OrdinalIgnoreCase) {
Expand Down
22 changes: 18 additions & 4 deletions src/Kiota.Builder/Writers/Go/CodeEnumWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,43 @@ public override void WriteCodeElement(CodeEnum codeElement, LanguageWriter write
string.Empty,
$"func (i {typeName}) String() string {{");
writer.IncreaseIndent();
writer.WriteLine($"var values []string");
var literalOptions = enumOptions
.Select(x => $"\"{x.WireName}\"")
.Aggregate((x, y) => x + ", " + y);
writer.WriteLine($"return []string{{{literalOptions}}}[i]");
writer.WriteLine($"for p := {typeName}(1); p <= {enumOptions.Last()}; p <<= 1 {{");
writer.IncreaseIndent();
writer.WriteLine($"if i&p == p {{");
writer.IncreaseIndent();
writer.WriteLine($"values = append(values, []string{{{literalOptions}}}[p])");
writer.DecreaseIndent();
writer.WriteLine($"}}");
writer.DecreaseIndent();
writer.WriteLine($"}}");
writer.WriteLine($"return strings.Join(values, \",\")");
writer.DecreaseIndent();
writer.WriteLines("}",
$"func Parse{typeName}(v string) (any, error) {{");
writer.IncreaseIndent();
writer.WriteLine($"result := {enumOptions.First().Name.ToUpperInvariant()}_{typeName.ToUpperInvariant()}");
writer.WriteLine("switch v {");
writer.WriteLine($"var result {typeName}");
writer.WriteLine("values := strings.Split(v, \",\")");
writer.WriteLine("for _, str := range values {");
writer.IncreaseIndent();
writer.WriteLine("switch str {");
writer.IncreaseIndent();
foreach (var item in enumOptions)
{
writer.WriteLine($"case \"{item.WireName}\":");
writer.IncreaseIndent();
writer.WriteLine($"result = {item.Name.ToUpperInvariant()}_{typeName.ToUpperInvariant()}");
writer.WriteLine($"result |= {item.Name.ToUpperInvariant()}_{typeName.ToUpperInvariant()}");
writer.DecreaseIndent();
}
writer.WriteLine("default:");
writer.IncreaseIndent();
writer.WriteLine($"return 0, errors.New(\"Unknown {typeName} value: \" + v)");
writer.DecreaseIndent();
writer.CloseBlock();
writer.CloseBlock();
writer.WriteLine("return &result, nil");
writer.CloseBlock();
writer.WriteLine($"func Serialize{typeName}(values []{typeName}) []string {{");
Expand Down
4 changes: 3 additions & 1 deletion tests/Kiota.Builder.Tests/Writers/Go/CodeEnumWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ public void WritesEnum()
Assert.Contains($"{EnumName.ToFirstCharacterUpperCase()} = iota", result);
Assert.Contains("func (i", result);
Assert.Contains("String() string {", result);
Assert.Contains("return []string{", result);
Assert.Contains("values := strings.Split(v, \",\"))", result);
Assert.Contains("strings.Join(values", result);
Assert.Contains("result |= OPTION1_SOMEENUM", result);
Assert.Contains("[i]", result);
Assert.Contains("func Parse", result);
Assert.Contains("(v string) (any, error)", result);
Expand Down

0 comments on commit 8d6f606

Please sign in to comment.