Skip to content

Commit

Permalink
Support the writing of enum literals.
Browse files Browse the repository at this point in the history
  • Loading branch information
Corniel committed Oct 25, 2024
1 parent deb709e commit 22500af
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Qowaiv.CodeGeneration.Syntax;
using System.Text.Json.Serialization;

namespace Syntax.Attribute_decoration_specs;

Expand All @@ -19,6 +20,11 @@ public void parameter_sets()
=> new AttributeInfo(typeof(TestAttribute),
null, KeyValuePair.Create("Author", (object?)"Qowaiv"))
.Should().HaveContent("[NUnit.Framework.Test(Author = \"Qowaiv\")]\r\n");

[Test]
public void Enum_values()
=> AttributeInfo.System_Text_Json_Serialization_JsonIgnoreAttribute(JsonIgnoreCondition.WhenWritingDefault)
.Should().HaveContent("[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault)]");
}

public class Equaly
Expand Down
2 changes: 1 addition & 1 deletion src/Qowaiv.CodeGeneration/CSharpWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public CSharpWriter Write(Type type, bool attribute)

/// <summary>Writes a literal to the code file.</summary>
[FluentSyntax]
public CSharpWriter Literal(object? value) => Write(new Syntax.Literal(value));
public CSharpWriter Literal(object? value) => Write(new Literal(value));

/// <summary>Indents the current line of the code file.</summary>
[FluentSyntax]
Expand Down
10 changes: 9 additions & 1 deletion src/Qowaiv.CodeGeneration/Syntax/AttributeInfo.Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ public static AttributeInfo Qowaiv_Validation_DataAnnotations_MultipleOf(double
public static AttributeInfo System_Text_Json_Serialization_JsonDerivedTypeAttribute(Type type)
=> new(typeof(System.Text.Json.Serialization.JsonDerivedTypeAttribute), [type]);

/// <summary><see cref="System.Text.Json.Serialization.JsonDerivedTypeAttribute"/>.</summary>
[Pure]
public static AttributeInfo System_Text_Json_Serialization_JsonIgnoreAttribute(System.Text.Json.Serialization.JsonIgnoreCondition condition)
=> new(typeof(System.Text.Json.Serialization.JsonIgnoreAttribute), null, Kvp(nameof(System.Text.Json.Serialization.JsonIgnoreAttribute.Condition), condition));

/// <summary><see cref="System.Text.Json.Serialization.JsonPropertyNameAttribute"/>.</summary>
[Pure]
public static AttributeInfo System_Text_Json_Serialization_JsonPropertyName(string name)
Expand All @@ -81,5 +86,8 @@ public static AttributeInfo System_Text_Json_Serialization_JsonPropertyName(stri
/// <summary><see cref="System.Runtime.Serialization.EnumMemberAttribute"/>.</summary>
[Pure]
public static AttributeInfo System_Runtime_Serialization_EnumMember(string? value)
=> new(typeof(System.Runtime.Serialization.EnumMemberAttribute), null, KeyValuePair.Create("Value", (object?)value));
=> new(typeof(System.Runtime.Serialization.EnumMemberAttribute), null, Kvp("Value", (object?)value));

[Pure]
private static KeyValuePair<string, object?> Kvp(string key, object? value) => new(key, value);
}
4 changes: 2 additions & 2 deletions src/Qowaiv.CodeGeneration/Syntax/AttributeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ public void WriteTo(CSharpWriter writer)
if (CtorArguments.Any() || PropertyValues.Any())
{
writer.Write('(');
writer.Write(CtorArguments.Select(a => CtorArgument(a)), writer => writer.Write(", "));
writer.Write(CtorArguments.Select(CtorArgument), writer => writer.Write(", "));

if (CtorArguments.Any() && PropertyValues.Any()) writer.Write(", ");

writer.Write(PropertyValues.Select(a => PropertyValue(a)), writer => writer.Write(", "));
writer.Write(PropertyValues.Select(PropertyValue), writer => writer.Write(", "));
writer.Write(')');
}

Expand Down
25 changes: 14 additions & 11 deletions src/Qowaiv.CodeGeneration/Syntax/Literal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,25 @@ public sealed class Literal : Code
public void WriteTo(CSharpWriter writer)
{
Guard.NotNull(writer);

if (Value is null) writer.Write("null");
else if (Value is Type type) writer.Write("typeof(").Write(type).Write(')');
else if (Value is bool logical) writer.Write(logical ? "true" : "false");
else if (Value is int int32) writer.Write(int32.ToString(CultureInfo.InvariantCulture));
else if (Value is double dbl) writer.Write(Double(dbl));
else if (Value is decimal dec) writer.Write(dec.ToString(CultureInfo.InvariantCulture)).Write('m');
else if (Value is string str) writer.Write(String(str));
else throw new NotSupportedException($"Literals of type {Value.GetType()} are not supported");
_ = Value switch
{
null /*.........*/ => writer.Write("null"),
Type type /*....*/ => writer.Write("typeof(").Write(type).Write(')'),
bool boolean /*.*/ => writer.Write(boolean ? "true" : "false"),
int int32 /*....*/ => writer.Write(int32.ToString(CultureInfo.InvariantCulture)),
double dbl /*...*/ => writer.Write(Double(dbl)),
decimal dec /*..*/ => writer.Write(dec.ToString(CultureInfo.InvariantCulture)).Write('m'),
string str /*...*/ => writer.Write(String(str)),
Enum @enum /*...*/ => writer.Write(@enum.GetType()).Write('.').Write(@enum.ToString()),
_ => throw new NotSupportedException($"Literals of type {Value.GetType()} are not supported"),
};
}

[Pure]
private static string Double(double dbl)
{
if (dbl == double.MinValue) return "double.MinValue";
else if (dbl == double.MaxValue) return "double.MaxValue";
if (dbl <= double.MinValue) return "double.MinValue";
else if (dbl >= double.MaxValue) return "double.MaxValue";
else if (double.IsNaN(dbl)) return "double.NaN";
else if (double.IsPositiveInfinity(dbl)) return "double.PositiveInfinity";
else if (double.IsNegativeInfinity(dbl)) return "double.NegativeInfinity";
Expand Down

0 comments on commit 22500af

Please sign in to comment.