-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathmodelInnerEnum.mustache
More file actions
134 lines (118 loc) · 6.07 KB
/
Copy pathmodelInnerEnum.mustache
File metadata and controls
134 lines (118 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
{{^isContainer}}
/// <summary>
/// {{{description}}}{{^description}}Defines {{{name}}}.{{/description}}
/// </summary>
{{#description}}
/// <value>{{.}}</value>
{{/description}}
{{#isString}}
{{#useGenericHost}}
[JsonConverter(typeof({{datatypeWithEnum}}JsonConverter))]
{{/useGenericHost}}
{{/isString}}
{{>visibility}} class {{datatypeWithEnum}} : IEnum
{
/// <summary>
/// Returns the value of the {{datatypeWithEnum}}.
/// </summary>
public string? Value { get; set; }
{{#allowableValues}}
{{#enumVars}}
/// <summary>
/// {{datatypeWithEnum}}.{{name}} - {{value}}
/// </summary>
public static readonly {{datatypeWithEnum}} {{name}} = new("{{value}}");
{{^-last}}
{{/-last}}
{{/enumVars}}
{{/allowableValues}}
private {{datatypeWithEnum}}(string? value)
{
Value = value;
}
/// <summary>
/// Converts a string to a <see cref="{{datatypeWithEnum}}"/> implicitly.
/// </summary>
/// <param name="value">The string value to convert. Defaults to null.</param>
/// <returns>A new <see cref="{{datatypeWithEnum}}"/> instance initialized with the string value.</returns>
public static implicit operator {{datatypeWithEnum}}?(string? value) => value == null ? null : new {{datatypeWithEnum}}(value);
/// <summary>
/// Converts a <see cref="{{datatypeWithEnum}}"/> instance to a string implicitly.
/// </summary>
/// <param name="option">The <see cref="{{datatypeWithEnum}}"/> instance. Default to null.</param>
/// <returns>String value of the <see cref="{{datatypeWithEnum}}"/> instance.</returns>
public static implicit operator string?({{datatypeWithEnum}}? option) => option?.Value;
/// <summary>
/// Compares two <see cref="{{datatypeWithEnum}}"/> instances for equality.
/// </summary>
public static bool operator ==({{datatypeWithEnum}}? left, {{datatypeWithEnum}}? right) => string.Equals(left?.Value, right?.Value, StringComparison.OrdinalIgnoreCase);
/// <summary>
/// Compares two <see cref="{{datatypeWithEnum}}"/> instances for inequality.
/// </summary>
public static bool operator !=({{datatypeWithEnum}}? left, {{datatypeWithEnum}}? right) => !string.Equals(left?.Value, right?.Value, StringComparison.OrdinalIgnoreCase);
/// <summary>
/// Returns true if the given object is equal to this <see cref="{{datatypeWithEnum}}"/> instance.
/// </summary>
public override bool Equals(object? obj) => obj is {{datatypeWithEnum}} other && string.Equals(Value, other.Value, StringComparison.OrdinalIgnoreCase);
/// <summary>
/// Returns a hash code for this <see cref="{{datatypeWithEnum}}"/> instance.
/// </summary>
public override int GetHashCode() => Value?.GetHashCode() ?? 0;
/// <summary>
/// Returns the string value of the <see cref="{{datatypeWithEnum}}"/> instance.
/// </summary>
public override string ToString() => Value ?? string.Empty;
{{#useGenericHost}}
/// <summary>
/// Returns a <see cref="{{datatypeWithEnum}}?"/>.
/// </summary>
/// <param name="value"></param>
/// <returns><see cref="{{datatypeWithEnum}}"/> or null.</returns>
public static {{datatypeWithEnum}}? FromStringOrDefault(string? value)
{
return value switch {
{{#allowableValues}}{{#enumVars}}{{#isString}}"{{{value}}}"{{/isString}} => {{datatypeWithEnum}}.{{name}},
{{/enumVars}}{{/allowableValues}}_ => null,
};
}
/// <summary>
/// Converts the <see cref="{{datatypeWithEnum}}"/> to the json value.
/// </summary>
/// <param name="value"><see cref="{{datatypeWithEnum}}"/></param>
/// <returns>String value of the enum.</returns>
public static {{>EnumValueDataType}}?{{#lambda.first}}{{#nrt}}{{/nrt}}{{/lambda.first}} ToJsonValue({{datatypeWithEnum}}? value)
{
if (value == null)
return null;
{{#allowableValues}}
{{#enumVars}}
if (value == {{datatypeWithEnum}}.{{name}})
return {{#isString}}"{{{value}}}"{{/isString}};
{{/enumVars}}
{{/allowableValues}}
return value.Value;
}
/// <summary>
/// JsonConverter for writing {{datatypeWithEnum}}.
/// </summary>
public class {{datatypeWithEnum}}JsonConverter : JsonConverter<{{datatypeWithEnum}}>
{
/// <summary>
/// Deserializes a <see cref="{{datatypeWithEnum}}"/> from JSON.
/// </summary>
public override {{datatypeWithEnum}}? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions jsonOptions)
{
string? value = reader.GetString();
return value == null ? null : {{datatypeWithEnum}}.FromStringOrDefault(value) ?? new {{datatypeWithEnum}}(value);
}
/// <summary>
/// Serializes a <see cref="{{datatypeWithEnum}}"/> to JSON.
/// </summary>
public override void Write(Utf8JsonWriter writer, {{datatypeWithEnum}} value, JsonSerializerOptions jsonOptions)
{
writer.WriteStringValue({{datatypeWithEnum}}.ToJsonValue(value));
}
}
{{/useGenericHost}}
}
{{/isContainer}}