Skip to content

Commit

Permalink
Support serialization with json attributes
Browse files Browse the repository at this point in the history
Remove EnumLabel and use EnumMember attribute instead.
Update all test snapshots according to the new changes.
  • Loading branch information
dandro committed Feb 25, 2024
1 parent f599fef commit b679da8
Show file tree
Hide file tree
Showing 33 changed files with 220 additions and 799 deletions.
35 changes: 11 additions & 24 deletions core/data/tests/can_apply_prefix_correctly/output.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,22 @@
#nullable enable

using System;
using System.Reflection;
using System.Collections.Generic;

class EnumLabelAttribute : Attribute
{
public string Label { get; }

public EnumLabelAttribute(string label)
{
Label = label;
}
}

public static class EnumExtensions
{
public static string Label<T>(this T value)
where T : Enum
{
var fieldName = value.ToString();
var field = typeof(T).GetField(fieldName, BindingFlags.Public | BindingFlags.Static);
return field?.GetCustomAttribute<EnumLabelAttribute>()?.Label ?? fieldName;
}
}
using JsonSubTypes;
using Newtonsoft.Json;
using System.Runtime.Serialization;

public class ItemDetailsFieldValue {
public string Hello { get; set; }
}

public record AdvancedColors
[JsonConverter(typeof(JsonSubtypes), "t")]
[JsonSubtypes.KnownSubType(typeof(String), "String")]
[JsonSubtypes.KnownSubType(typeof(Number), "Number")]
[JsonSubtypes.KnownSubType(typeof(NumberArray), "NumberArray")]
[JsonSubtypes.KnownSubType(typeof(ReallyCoolType), "ReallyCoolType")]
[JsonSubtypes.KnownSubType(typeof(ArrayReallyCoolType), "ArrayReallyCoolType")]
[JsonSubtypes.KnownSubType(typeof(DictionaryReallyCoolType), "DictionaryReallyCoolType")]
public abstract record AdvancedColors
{
public record String(string C) : AdvancedColors();
public record Number(int C) : AdvancedColors();
Expand Down
41 changes: 16 additions & 25 deletions core/data/tests/can_generate_algebraic_enum/output.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,24 @@
#nullable enable

using System;
using System.Reflection;
using System.Collections.Generic;
using JsonSubTypes;
using Newtonsoft.Json;
using System.Runtime.Serialization;

namespace Company.Domain.Models;

class EnumLabelAttribute : Attribute
{
public string Label { get; }

public EnumLabelAttribute(string label)
{
Label = label;
}
}

public static class EnumExtensions
{
public static string Label<T>(this T value)
where T : Enum
{
var fieldName = value.ToString();
var field = typeof(T).GetField(fieldName, BindingFlags.Public | BindingFlags.Static);
return field?.GetCustomAttribute<EnumLabelAttribute>()?.Label ?? fieldName;
}
}

/** Struct comment */
public class ItemDetailsFieldValue {
}

/** Enum comment */
public record AdvancedColors
[JsonConverter(typeof(JsonSubtypes), "type")]
[JsonSubtypes.KnownSubType(typeof(String), "String")]
[JsonSubtypes.KnownSubType(typeof(Number), "Number")]
[JsonSubtypes.KnownSubType(typeof(UnsignedNumber), "UnsignedNumber")]
[JsonSubtypes.KnownSubType(typeof(NumberArray), "NumberArray")]
[JsonSubtypes.KnownSubType(typeof(ReallyCoolType), "ReallyCoolType")]
public abstract record AdvancedColors
{
/** This is a case comment */
public record String(string Content) : AdvancedColors();
Expand All @@ -44,7 +30,12 @@ public record ReallyCoolType(ItemDetailsFieldValue Content) : AdvancedColors();
}


public record AdvancedColors2
[JsonConverter(typeof(JsonSubtypes), "type")]
[JsonSubtypes.KnownSubType(typeof(String), "String")]
[JsonSubtypes.KnownSubType(typeof(Number), "Number")]
[JsonSubtypes.KnownSubType(typeof(NumberArray), "NumberArray")]
[JsonSubtypes.KnownSubType(typeof(ReallyCoolType), "ReallyCoolType")]
public abstract record AdvancedColors2
{
/** This is a case comment */
public record String(string Content) : AdvancedColors2();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,14 @@
#nullable enable

using System;
using System.Reflection;
using System.Collections.Generic;
using JsonSubTypes;
using Newtonsoft.Json;
using System.Runtime.Serialization;

class EnumLabelAttribute : Attribute
{
public string Label { get; }

public EnumLabelAttribute(string label)
{
Label = label;
}
}

public static class EnumExtensions
{
public static string Label<T>(this T value)
where T : Enum
{
var fieldName = value.ToString();
var field = typeof(T).GetField(fieldName, BindingFlags.Public | BindingFlags.Static);
return field?.GetCustomAttribute<EnumLabelAttribute>()?.Label ?? fieldName;
}
}

public record SomeEnum
[JsonConverter(typeof(JsonSubtypes), "type")]
[JsonSubtypes.KnownSubType(typeof(A), "A")]
[JsonSubtypes.KnownSubType(typeof(C), "C")]
public abstract record SomeEnum
{
public record A(): SomeEnum();
public record C(int Content) : SomeEnum();
Expand Down
29 changes: 3 additions & 26 deletions core/data/tests/can_generate_bare_string_enum/output.cs
Original file line number Diff line number Diff line change
@@ -1,40 +1,17 @@
#nullable enable

using System;
using System.Reflection;
using System.Collections.Generic;

class EnumLabelAttribute : Attribute
{
public string Label { get; }

public EnumLabelAttribute(string label)
{
Label = label;
}
}

public static class EnumExtensions
{
public static string Label<T>(this T value)
where T : Enum
{
var fieldName = value.ToString();
var field = typeof(T).GetField(fieldName, BindingFlags.Public | BindingFlags.Static);
return field?.GetCustomAttribute<EnumLabelAttribute>()?.Label ?? fieldName;
}
}
using JsonSubTypes;
using Newtonsoft.Json;
using System.Runtime.Serialization;

/** This is a comment. */
public enum Colors
{
[EnumLabel("Red")]
Red,

[EnumLabel("Blue")]
Blue,

[EnumLabel("Green")]
Green,

}
Expand Down
31 changes: 7 additions & 24 deletions core/data/tests/can_generate_empty_algebraic_enum/output.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,17 @@
#nullable enable

using System;
using System.Reflection;
using System.Collections.Generic;

class EnumLabelAttribute : Attribute
{
public string Label { get; }

public EnumLabelAttribute(string label)
{
Label = label;
}
}

public static class EnumExtensions
{
public static string Label<T>(this T value)
where T : Enum
{
var fieldName = value.ToString();
var field = typeof(T).GetField(fieldName, BindingFlags.Public | BindingFlags.Static);
return field?.GetCustomAttribute<EnumLabelAttribute>()?.Label ?? fieldName;
}
}
using JsonSubTypes;
using Newtonsoft.Json;
using System.Runtime.Serialization;

public class AddressDetails {
}

public record Address
[JsonConverter(typeof(JsonSubtypes), "type")]
[JsonSubtypes.KnownSubType(typeof(FixedAddress), "FixedAddress")]
[JsonSubtypes.KnownSubType(typeof(NoFixedAddress), "NoFixedAddress")]
public abstract record Address
{
public record FixedAddress(AddressDetails Content) : Address();
public record NoFixedAddress(): Address();
Expand Down
46 changes: 19 additions & 27 deletions core/data/tests/can_generate_generic_enum/output.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,14 @@
#nullable enable

using System;
using System.Reflection;
using System.Collections.Generic;

class EnumLabelAttribute : Attribute
{
public string Label { get; }

public EnumLabelAttribute(string label)
{
Label = label;
}
}

public static class EnumExtensions
{
public static string Label<T>(this T value)
where T : Enum
{
var fieldName = value.ToString();
var field = typeof(T).GetField(fieldName, BindingFlags.Public | BindingFlags.Static);
return field?.GetCustomAttribute<EnumLabelAttribute>()?.Label ?? fieldName;
}
}

public record GenericEnum<TA, TB>
using JsonSubTypes;
using Newtonsoft.Json;
using System.Runtime.Serialization;

[JsonConverter(typeof(JsonSubtypes), "type")]
[JsonSubtypes.KnownSubType(typeof(VariantA), "VariantA")]
[JsonSubtypes.KnownSubType(typeof(VariantB), "VariantB")]
public abstract record GenericEnum<TA, TB>
{
public record VariantA(TA Content) : GenericEnum<TA, TB>();
public record VariantB(TB Content) : GenericEnum<TA, TB>();
Expand All @@ -36,7 +19,11 @@ public class StructUsingGenericEnum {
public GenericEnum<string, short> EnumField { get; set; }
}

public record GenericEnumUsingGenericEnum<T>
[JsonConverter(typeof(JsonSubtypes), "type")]
[JsonSubtypes.KnownSubType(typeof(VariantC), "VariantC")]
[JsonSubtypes.KnownSubType(typeof(VariantD), "VariantD")]
[JsonSubtypes.KnownSubType(typeof(VariantE), "VariantE")]
public abstract record GenericEnumUsingGenericEnum<T>
{
public record VariantC(GenericEnum<T, T> Content) : GenericEnumUsingGenericEnum<T>();
public record VariantD(GenericEnum<string, IDictionary<string, T>> Content) : GenericEnumUsingGenericEnum<T>();
Expand Down Expand Up @@ -66,7 +53,12 @@ public class GenericEnumsUsingStructVariantsVariantIInner<T, TU> {
public MyType<T, TU> Action { get; set; }
}

public record GenericEnumsUsingStructVariants<T, TU>
[JsonConverter(typeof(JsonSubtypes), "type")]
[JsonSubtypes.KnownSubType(typeof(VariantF), "VariantF")]
[JsonSubtypes.KnownSubType(typeof(VariantG), "VariantG")]
[JsonSubtypes.KnownSubType(typeof(VariantH), "VariantH")]
[JsonSubtypes.KnownSubType(typeof(VariantI), "VariantI")]
public abstract record GenericEnumsUsingStructVariants<T, TU>
{
public record VariantF(GenericEnumsUsingStructVariantsVariantFInner<T> Content): GenericEnumsUsingStructVariants<T, TU>();
public record VariantG(GenericEnumsUsingStructVariantsVariantGInner<T, TU> Content): GenericEnumsUsingStructVariants<T, TU>();
Expand Down
26 changes: 3 additions & 23 deletions core/data/tests/can_generate_readonly_fields/output.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,9 @@
#nullable enable

using System;
using System.Reflection;
using System.Collections.Generic;

class EnumLabelAttribute : Attribute
{
public string Label { get; }

public EnumLabelAttribute(string label)
{
Label = label;
}
}

public static class EnumExtensions
{
public static string Label<T>(this T value)
where T : Enum
{
var fieldName = value.ToString();
var field = typeof(T).GetField(fieldName, BindingFlags.Public | BindingFlags.Static);
return field?.GetCustomAttribute<EnumLabelAttribute>()?.Label ?? fieldName;
}
}
using JsonSubTypes;
using Newtonsoft.Json;
using System.Runtime.Serialization;

public class SomeStruct {
public uint FieldA { get; set; }
Expand Down
29 changes: 3 additions & 26 deletions core/data/tests/can_generate_simple_enum/output.cs
Original file line number Diff line number Diff line change
@@ -1,44 +1,21 @@
#nullable enable

using System;
using System.Reflection;
using System.Collections.Generic;

class EnumLabelAttribute : Attribute
{
public string Label { get; }

public EnumLabelAttribute(string label)
{
Label = label;
}
}

public static class EnumExtensions
{
public static string Label<T>(this T value)
where T : Enum
{
var fieldName = value.ToString();
var field = typeof(T).GetField(fieldName, BindingFlags.Public | BindingFlags.Static);
return field?.GetCustomAttribute<EnumLabelAttribute>()?.Label ?? fieldName;
}
}
using JsonSubTypes;
using Newtonsoft.Json;
using System.Runtime.Serialization;

/**
* This is a comment.
* Continued lovingly here
*/
public enum Colors
{
[EnumLabel("Red")]
Red,

[EnumLabel("Blue")]
Blue,

/** Green is a cool color */
[EnumLabel("Green")]
Green,

}
Expand Down
Loading

0 comments on commit b679da8

Please sign in to comment.