-
-
Notifications
You must be signed in to change notification settings - Fork 535
/
EnumTemplateModel.cs
67 lines (59 loc) · 2.77 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
//-----------------------------------------------------------------------
// <copyright file="EnumTemplateModel.cs" company="NJsonSchema">
// Copyright (c) Rico Suter. All rights reserved.
// </copyright>
// <license>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</license>
// <author>Rico Suter, mail@rsuter.com</author>
//-----------------------------------------------------------------------
using System.Collections.Generic;
using System.Linq;
using NJsonSchema.CodeGeneration.Models;
namespace NJsonSchema.CodeGeneration.TypeScript.Models
{
/// <summary>The TypeScript enum template model.</summary>
public class EnumTemplateModel
{
private readonly JsonSchema4 _schema;
private readonly TypeScriptGeneratorSettings _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, JsonSchema4 schema, TypeScriptGeneratorSettings settings)
{
_schema = schema;
_settings = settings;
Name = typeName;
}
/// <summary>Gets the name of the enum.</summary>
public string Name { get; }
/// <summary>Gets a value indicating whether the enum has description.</summary>
public bool HasDescription => !(_schema is JsonProperty) && !string.IsNullOrEmpty(_schema.Description);
/// <summary>Gets the description.</summary>
public string Description => ConversionUtilities.RemoveLineBreaks(_schema.Description);
/// <summary>Gets the enum values.</summary>
public List<EnumerationItemModel> Enums
{
get
{
var entries = new List<EnumerationItemModel>();
for (int i = 0; i < _schema.Enumeration.Count; i++)
{
var value = _schema.Enumeration.ElementAt(i);
if (value != null)
{
var name = _schema.EnumerationNames.Count > i ?
_schema.EnumerationNames.ElementAt(i) :
_schema.Type.HasFlag(JsonObjectType.Integer) ? "_" + value : value.ToString();
entries.Add(new EnumerationItemModel
{
Name = _settings.EnumNameGenerator.Generate(i, name, value, _schema),
Value = _schema.Type.HasFlag(JsonObjectType.Integer) ? value.ToString() : "<any>\"" + value + "\"",
});
}
}
return entries;
}
}
}
}