-
-
Notifications
You must be signed in to change notification settings - Fork 517
/
EnumTemplateModel.cs
176 lines (159 loc) · 6.98 KB
/
EnumTemplateModel.cs
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//-----------------------------------------------------------------------
// <copyright file="EnumTemplateModel.cs" company="NJsonSchema">
// Copyright (c) Rico Suter. All rights reserved.
// </copyright>
// <license>https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md</license>
// <author>Rico Suter, mail@rsuter.com</author>
//-----------------------------------------------------------------------
using NJsonSchema.Annotations;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using NJsonSchema.CodeGeneration.Models;
namespace NJsonSchema.CodeGeneration.CSharp.Models
{
// TODO: Add base class for CSharp.EnumTemplateModel and TypeScript.EnumTemplateModel
/// <summary>The CSharp enum template model.</summary>
public class EnumTemplateModel
{
private readonly JsonSchema _schema;
private readonly CSharpGeneratorSettings _settings;
/// <summary>Initializes a new instance of the <see cref="EnumTemplateModel" /> class.</summary>
/// <param name="typeName">Name of the type.</param>
/// <param name="schema">The schema.</param>
/// <param name="settings">The settings.</param>
public EnumTemplateModel(string typeName, JsonSchema schema, CSharpGeneratorSettings settings)
{
_schema = schema;
_settings = settings;
Name = typeName;
}
/// <summary>Gets the name.</summary>
public string Name { get; }
/// <summary>Gets a value indicating whether the enum has description.</summary>
public bool HasDescription => !(_schema is JsonSchemaProperty) && !string.IsNullOrEmpty(_schema.Description);
/// <summary>Gets the description.</summary>
public string? Description => _schema.Description;
/// <summary>Gets the property extension data.</summary>
public IDictionary<string, object?>? ExtensionData => _schema.ExtensionData;
/// <summary>Gets a value indicating whether the enum is of type string.</summary>
public bool IsStringEnum => _schema.Type != JsonObjectType.Integer;
/// <summary>Gets or sets the access modifier of generated classes and interfaces.</summary>
public string TypeAccessModifier => _settings.TypeAccessModifier;
/// <summary>Gets or sets if we output as Bit Flags.</summary>
public bool IsEnumAsBitFlags => _settings.EnforceFlagEnums || _schema.IsFlagEnumerable;
/// <summary>Gets a value indicating whether the enum needs an other base type to representing an extended value range.</summary>
public bool HasExtendedValueRange => _schema.Format == JsonFormatStrings.Long;
/// <summary>Gets the enum values.</summary>
public IEnumerable<EnumerationItemModel> Enums
{
get
{
var entries = new List<EnumerationItemModel>();
for (var i = 0; i < _schema.Enumeration.Count; i++)
{
var value = _schema.Enumeration.ElementAt(i);
if (value != null)
{
if (_schema.Type.IsInteger())
{
var name = _schema.EnumerationNames.Count > i ?
_schema.EnumerationNames.ElementAt(i) : "_" + value;
if (_schema.IsFlagEnumerable && TryGetInt64(value, out long valueInt64))
{
entries.Add(new EnumerationItemModel
{
Name = _settings.EnumNameGenerator.Generate(i, name, value, _schema),
Value = value.ToString(),
InternalValue = valueInt64.ToString(CultureInfo.InvariantCulture),
InternalFlagValue = valueInt64.ToString(CultureInfo.InvariantCulture)
});
}
else
{
entries.Add(new EnumerationItemModel
{
Name = _settings.EnumNameGenerator.Generate(i, name, value, _schema),
Value = value.ToString(),
InternalValue = value.ToString(),
InternalFlagValue = (1 << i).ToString(CultureInfo.InvariantCulture)
});
}
}
else
{
var name = _schema.EnumerationNames.Count > i ?
_schema.EnumerationNames.ElementAt(i) : value.ToString();
entries.Add(new EnumerationItemModel
{
Name = _settings.EnumNameGenerator.Generate(i, name, value, _schema),
Value = value.ToString(),
InternalValue = i.ToString(CultureInfo.InvariantCulture),
InternalFlagValue = (1 << i).ToString(CultureInfo.InvariantCulture)
});
}
}
}
return entries;
}
}
private static bool TryGetInt64(object value, out long valueInt64)
{
if (value is byte b)
{
valueInt64 = b;
return true;
}
else if (value is sbyte sb)
{
valueInt64 = sb;
return true;
}
else if (value is short int16)
{
valueInt64 = int16;
return true;
}
else if (value is ushort uint16)
{
valueInt64 = uint16;
return true;
}
else if (value is int int32)
{
valueInt64 = int32;
return true;
}
else if (value is uint uint32)
{
valueInt64 = uint32;
return true;
}
else if (value is long int64)
{
valueInt64 = int64;
return true;
}
else if (value is ulong uint64)
{
valueInt64 = (long)uint64;
return true;
}
else if (value is float ieee754 && System.Math.Floor(ieee754) == ieee754)
{
valueInt64 = (long)ieee754;
return true;
}
else if (value is double dieee754 && System.Math.Floor(dieee754) == dieee754)
{
valueInt64 = (long)dieee754;
return true;
}
else
{
valueInt64 = default(long);
return false;
}
}
}
}