forked from RicoSuter/NJsonSchema
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeScriptGeneratorSettings.cs
More file actions
141 lines (109 loc) · 6.62 KB
/
Copy pathTypeScriptGeneratorSettings.cs
File metadata and controls
141 lines (109 loc) · 6.62 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
135
136
137
138
139
140
141
//-----------------------------------------------------------------------
// <copyright file="CSharpGeneratorSettings.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;
using System.Linq;
using System.Reflection;
namespace NJsonSchema.CodeGeneration.TypeScript
{
/// <summary>The generator settings.</summary>
public class TypeScriptGeneratorSettings : CodeGeneratorSettingsBase
{
/// <summary>Initializes a new instance of the <see cref="TypeScriptGeneratorSettings"/> class.</summary>
public TypeScriptGeneratorSettings()
{
ModuleName = "";
Namespace = "";
NullValue = TypeScriptNullValue.Undefined;
TypeStyle = TypeScriptTypeStyle.Class;
DateTimeType = TypeScriptDateTimeType.Date;
EnumStyle = TypeScriptEnumStyle.Enum;
UseLeafType = false;
ExtensionCode = string.Empty;
TypeScriptVersion = 2.7m;
GenerateConstructorInterface = true;
ConvertConstructorInterfaceData = false;
ExportTypes = true;
ValueGenerator = new TypeScriptValueGenerator(this);
PropertyNameGenerator = new TypeScriptPropertyNameGenerator();
TemplateFactory = new DefaultTemplateFactory(this, new Assembly[]
{
typeof(TypeScriptGeneratorSettings).GetTypeInfo().Assembly
});
ClassTypes = Array.Empty<string>();
ExtendedClasses = Array.Empty<string>();
InlineNamedDictionaries = false;
}
/// <summary>Gets or sets the target TypeScript version (default: 2.7).</summary>
public decimal TypeScriptVersion { get; set; }
/// <summary>Gets a value indicating whether the target TypeScript version supports strict null checks.</summary>
public bool SupportsStrictNullChecks => TypeScriptVersion >= 2.0m;
/// <summary>Gets a value indicating whether the target TypeScript version requires strict property initialization.</summary>
public bool RequiresStrictPropertyInitialization => TypeScriptVersion >= 2.7m;
/// <summary>Gets a value indicating whether the target TypeScript version supports override keyword.</summary>
public bool SupportsOverrideKeyword => TypeScriptVersion >= 4.3m;
/// <summary>Gets or sets a value indicating whether to mark optional properties with ? (default: false).</summary>
public bool MarkOptionalProperties { get; set; }
/// <summary>Gets or sets the type style (default: Class).</summary>
public TypeScriptTypeStyle TypeStyle { get; set; }
/// <summary>Gets or sets the date time type (default: 'Date').</summary>
public TypeScriptDateTimeType DateTimeType { get; set; }
/// <summary>
/// Whether to use UTC (default) or local time zone when deserializing dates 'yyyy-MM-dd' (default: 'false').
/// Only applicable if <see cref="DateTimeType"/> is <see cref="TypeScriptDateTimeType.Date"/>.
/// Other DateTimeTypes use local timezone by default.
/// </summary>
public bool ConvertDateToLocalTimezone { get; set; }
/// <summary>Gets or sets the enum style (default: Enum).</summary>
public TypeScriptEnumStyle EnumStyle { get; set; }
/// <summary>Generate leaf types for an object with discriminator (default: false).</summary>
public bool UseLeafType { get; set; }
/// <summary>Gets or sets the TypeScript module name (default: '', no module).</summary>
public string ModuleName { get; set; }
/// <summary>Gets or sets the TypeScript namespace (default: '', no namespace).</summary>
public string Namespace { get; set; }
/// <summary>Gets or sets the list of extended classes (the classes must be implemented in the <see cref="ExtensionCode"/>).</summary>
public string[] ExtendedClasses { get; set; }
/// <summary>Gets or sets the extension code to append to the generated code.</summary>
public string ExtensionCode { get; set; }
/// <summary>Gets or sets the type names which always generate plain TypeScript classes.</summary>
public string[] ClassTypes { get; set; }
/// <summary>Gets or sets the TypeScript null value.</summary>
public TypeScriptNullValue NullValue { get; set; }
/// <summary>Gets or sets a value indicating whether to handle JSON references (supports $ref, $id, $values, default: false).</summary>
public bool HandleReferences { get; set; }
/// <summary>Gets or sets a value indicating whether a clone() method should be generated in the DTO classes.</summary>
public bool GenerateCloneMethod { get; set; }
/// <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 { get; set; }
/// <summary>Gets or sets a value indicating whether POJO objects in the constructor data are converted to DTO instances (GenerateConstructorInterface must be enabled, default: false).</summary>
public bool ConvertConstructorInterfaceData { get; set; }
/// <summary>Gets or sets a value indicating whether the export keyword should be added to all classes and enums (default: true).</summary>
public bool ExportTypes { get; set; }
/// <summary>Gets or sets a value indicating whether named/referenced dictionaries should be inlined or generated as class with an indexer.</summary>
public bool InlineNamedDictionaries { get; set; }
internal ITemplate CreateTemplate(string typeName, object model)
{
if (ClassTypes != null && ClassTypes.Contains(typeName))
{
return TemplateFactory.CreateTemplate("TypeScript", "Class", model);
}
return TemplateFactory.CreateTemplate("TypeScript", TypeStyle.ToString(), model);
}
/// <summary>Gets the type style of the given type name.</summary>
/// <param name="typeName">The type name.</param>
/// <returns>The type style.</returns>
public TypeScriptTypeStyle GetTypeStyle(string typeName)
{
if (ClassTypes != null && ClassTypes.Contains(typeName))
{
return TypeScriptTypeStyle.Class;
}
return TypeStyle;
}
}
}