-
-
Notifications
You must be signed in to change notification settings - Fork 517
/
ClassTemplateModel.cs
160 lines (128 loc) · 8.31 KB
/
ClassTemplateModel.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
//-----------------------------------------------------------------------
// <copyright file="ClassTemplateModel.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 System.Collections.Generic;
using System.Linq;
using NJsonSchema.CodeGeneration.Models;
namespace NJsonSchema.CodeGeneration.TypeScript.Models
{
/// <summary>The TypeScript class template model.</summary>
public class ClassTemplateModel : ClassTemplateModelBase
{
private readonly TypeScriptGeneratorSettings _settings;
private readonly JsonSchema _schema;
private readonly TypeScriptTypeResolver _resolver;
private readonly string _discriminatorName;
/// <summary>Initializes a new instance of the <see cref="ClassTemplateModel" /> class.</summary>
/// <param name="typeName">The type name.</param>
/// <param name="discriminatorName">The name to compare the discriminator against.</param>
/// <param name="settings">The settings.</param>
/// <param name="resolver">The resolver.</param>
/// <param name="schema">The schema.</param>
/// <param name="rootObject">The root object.</param>
public ClassTemplateModel(string typeName, string discriminatorName,
TypeScriptGeneratorSettings settings, TypeScriptTypeResolver resolver,
JsonSchema schema, object rootObject)
: base(resolver, schema, rootObject)
{
_settings = settings;
_schema = schema;
_resolver = resolver;
_discriminatorName = discriminatorName;
ClassName = typeName;
Properties = _schema.ActualProperties.Values
.Where(v => settings.TypeStyle == TypeScriptTypeStyle.Interface ||
v.IsInheritanceDiscriminator == false)
.Select(property => new PropertyModel(this, property, ClassName, _resolver, _settings))
.ToList();
}
/// <summary>Gets the class name.</summary>
public override string ClassName { get; }
/// <summary>Gets the name for the discriminator check.</summary>
public string DiscriminatorName => HasBaseDiscriminator ?
(_schema.ResponsibleDiscriminatorObject!.Mapping.FirstOrDefault(m => m.Value.ActualTypeSchema == _schema.ActualTypeSchema).Key ?? _discriminatorName) :
_discriminatorName;
/// <summary>Gets a value indicating whether the class has a discriminator property.</summary>
public bool HasDiscriminator => !string.IsNullOrEmpty(_schema.ActualDiscriminator);
/// <summary>Gets a value indicating whether the class or an inherited class has a discriminator property.</summary>
public bool HasBaseDiscriminator => _schema.ResponsibleDiscriminatorObject != null;
/// <summary>Gets the class discriminator property name (may be defined in a inherited class).</summary>
public string? BaseDiscriminator => _schema.ResponsibleDiscriminatorObject?.PropertyName;
/// <summary>Gets a value indicating whether the class has description.</summary>
public bool HasDescription => !(_schema is JsonSchemaProperty) &&
(!string.IsNullOrEmpty(_schema.Description) ||
!string.IsNullOrEmpty(_schema.ActualTypeSchema.Description));
/// <summary>Gets the description.</summary>
public string Description => ConversionUtilities.RemoveLineBreaks(
!string.IsNullOrEmpty(_schema.Description) ?
_schema.Description : _schema.ActualTypeSchema.Description);
/// <summary>Gets a value indicating whether this class has a parent class.</summary>
public bool HasInheritance => InheritedSchema != null && !InheritedSchema.IsDictionary;
/// <summary>Gets the inheritance code.</summary>
public string Inheritance
{
get
{
if (HasInheritance)
{
return " extends " + BaseClass + (GenerateConstructorInterface && _settings.TypeStyle == TypeScriptTypeStyle.Class ?
" implements I" + ClassName : string.Empty);
}
return GenerateConstructorInterface && _settings.TypeStyle == TypeScriptTypeStyle.Class ?
" implements I" + ClassName : string.Empty;
}
}
/// <summary>Gets the constructor interface inheritance code.</summary>
public string InterfaceInheritance => HasInheritance ? " extends I" + BaseClass : string.Empty;
/// <summary>Gets the base class name.</summary>
public string? BaseClass => HasInheritance ? _resolver.Resolve(InheritedSchema!, true, string.Empty) : null;
/// <summary>Gets or sets a value indicating whether a clone() method should be generated in the DTO classes.</summary>
public bool GenerateCloneMethod => _settings.GenerateCloneMethod;
/// <summary>Gets or sets a value indicating whether to generate an class interface which is used in the constructor to initialize the class (default: true).</summary>
public bool GenerateConstructorInterface => _settings.GenerateConstructorInterface;
/// <summary>Gets or sets a value indicating whether POJO objects in the constructor data are converted to DTO instances (default: true).</summary>
public bool ConvertConstructorInterfaceData => _settings.ConvertConstructorInterfaceData && Properties.Exists(p => p.SupportsConstructorConversion);
/// <summary>Gets the null value.</summary>
public string NullValue => _settings.NullValue.ToString().ToLowerInvariant();
/// <summary>Gets a value indicating whether the class inherits from dictionary.</summary>
public bool HasIndexerProperty => _schema.IsDictionary ||
_schema.InheritedSchema?.IsDictionary == true ||
_schema.ActualTypeSchema.AllowAdditionalProperties ||
_schema.ActualTypeSchema.AdditionalPropertiesSchema != null;
/// <summary>Gets the type of the indexer property value.</summary>
public string IndexerPropertyValueType
{
get
{
var valueType =
_schema?.AdditionalPropertiesSchema != null ? _resolver.Resolve(_schema.AdditionalPropertiesSchema, true, string.Empty) :
InheritedSchema?.AdditionalPropertiesSchema != null ? _resolver.Resolve(InheritedSchema.AdditionalPropertiesSchema, true, string.Empty) :
"any";
// TODO: Find solution to avoid using union with any
return valueType != "any" ? valueType + " | any" : valueType;
}
}
/// <summary>Gets a value indicating whether to handle JSON references.</summary>
public bool HandleReferences => _settings.HandleReferences;
/// <summary>Gets a value indicating whether the type has properties.</summary>
public bool HasProperties => Properties.Count > 0;
/// <summary>Gets the property models.</summary>
public List<PropertyModel> Properties { get; }
/// <summary>Gets a value indicating whether any property has a default value.</summary>
public bool HasDefaultValues => Properties.Exists(p => p.HasDefaultValue);
/// <summary>Gets a value indicating whether </summary>
public bool RequiresStrictPropertyInitialization => _settings.RequiresStrictPropertyInitialization;
/// <summary>Gets a value indicating whether </summary>
public bool SupportsOverrideKeyword => _settings.SupportsOverrideKeyword;
/// <summary>Gets a value indicating whether the export keyword should be added to all classes.</summary>
public bool ExportTypes => _settings.ExportTypes;
/// <summary>Gets a value indicating whether to generate type check functions.</summary>
public bool GenerateTypeCheckFunctions => _settings.GenerateTypeCheckFunctions;
/// <summary>Gets the inherited schema.</summary>
private JsonSchema? InheritedSchema => _schema.InheritedSchema?.ActualSchema;
}
}