Skip to content

Commit

Permalink
feat(csharp-sdk): Fix unnecessary Equals and GetHashCode, Add Stringify
Browse files Browse the repository at this point in the history
  • Loading branch information
Swimburger committed Oct 5, 2024
1 parent d20b753 commit b9ff0c6
Show file tree
Hide file tree
Showing 16 changed files with 29 additions and 138 deletions.
7 changes: 4 additions & 3 deletions generators/csharp/codegen/src/AsIs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ export enum AsIsFiles {
RawClient = "RawClient.Template.cs",
RawClientTests = "RawClientTests.Template.cs",
RawGrpcClient = "RawGrpcClient.Template.cs",
StringEnum = "StringEnum.Template.cs",
EnumSerializer = "EnumSerializer.cs",
EnumSerializer = "EnumSerializer.Template.cs",
EnumSerializerTests = "EnumSerializerTests.Template.cs",
StringEnumSerializer = "StringEnumSerializer.cs",
StringEnum = "StringEnum.Template.cs",
StringEnumExtensions = "StringEnumExtensions.Template.cs",
StringEnumSerializer = "StringEnumSerializer.Template.cs",
StringEnumSerializerTests = "StringEnumSerializerTests.Template.cs",
TemplateCsProj = "Template.csproj",
TemplateTestCsProj = "Template.Test.csproj",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace <%= namespace%>;

internal static class StringEnumExtensions
{
public static string Stringify(this IStringEnum stringEnum) => stringEnum.Value;
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public class DummyObject
}

[JsonConverter(typeof(StringEnumSerializer<DummyEnum>))]
public readonly struct DummyEnum : IStringEnum, IEquatable<DummyEnum>
public readonly record struct DummyEnum : IStringEnum
{
public DummyEnum(string value)
{
Expand Down Expand Up @@ -120,36 +120,16 @@ public override string ToString()
return Value;
}

public bool Equals(DummyEnum other)
{
return Value == other.Value;
}

public bool Equals(string? other)
{
return Value.Equals(other);
}

public override bool Equals(object? obj)
{
if (obj is null)
return false;
if (obj is string stringObj)
return Value.Equals(stringObj);
if (obj.GetType() != GetType())
return false;
return Equals((DummyEnum)obj);
}

public override int GetHashCode()
{
return Value.GetHashCode();
}

public static bool operator ==(DummyEnum value1, DummyEnum value2) => value1.Equals(value2);

public static bool operator !=(DummyEnum value1, DummyEnum value2) => !(value1 == value2);

public static bool operator ==(DummyEnum value1, string value2) => value1.Value.Equals(value2);

public static bool operator !=(DummyEnum value1, string value2) => !value1.Value.Equals(value2);
Expand Down
1 change: 1 addition & 0 deletions generators/csharp/model/src/ModelGeneratorContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export class ModelGeneratorContext extends AbstractCsharpGeneratorContext<ModelC
];
if (this.customConfig["enable-forward-compatible-enums"] ?? false) {
files.push(AsIsFiles.StringEnum);
files.push(AsIsFiles.StringEnumExtensions);
files.push(AsIsFiles.StringEnumSerializer);
} else {
files.push(AsIsFiles.EnumSerializer);
Expand Down
34 changes: 2 additions & 32 deletions generators/csharp/model/src/enum/StringEnumGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,24 +152,7 @@ export class StringEnumGenerator extends FileGenerator<CSharpFile, ModelCustomCo
summary: "Returns the string value of the enum.",
type: csharp.MethodType.INSTANCE,
body: csharp.codeblock((writer) => {
writer.write("return Value;");
})
})
);

stringEnum.addMethod(
csharp.method({
access: "public",
name: "Equals",
parameters: [
csharp.parameter({
name: "other",
type: csharp.Type.reference(this.classReference)
})
],
return_: csharp.Type.boolean(),
body: csharp.codeblock((writer) => {
writer.write("return Value == other.Value;");
writer.writeTextStatement("return Value");
})
})
);
Expand All @@ -186,20 +169,7 @@ export class StringEnumGenerator extends FileGenerator<CSharpFile, ModelCustomCo
],
return_: csharp.Type.boolean(),
body: csharp.codeblock((writer) => {
writer.write("return Value.Equals(other);");
})
})
);

stringEnum.addMethod(
csharp.method({
access: "public",
name: "GetHashCode",
override: true,
return_: csharp.Type.integer(),
parameters: [],
body: csharp.codeblock((writer) => {
writer.write("return Value.GetHashCode();");
writer.writeTextStatement("return Value.Equals(other)");
})
})
);
Expand Down
1 change: 1 addition & 0 deletions generators/csharp/sdk/src/SdkGeneratorContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export class SdkGeneratorContext extends AbstractCsharpGeneratorContext<SdkCusto
}
if (this.customConfig["enable-forward-compatible-enums"] ?? false) {
files.push(AsIsFiles.StringEnum);
files.push(AsIsFiles.StringEnumExtensions);
files.push(AsIsFiles.StringEnumSerializer);
} else {
files.push(AsIsFiles.EnumSerializer);
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b9ff0c6

Please sign in to comment.