-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathClassGeneration.cs
More file actions
170 lines (132 loc) · 5.32 KB
/
ClassGeneration.cs
File metadata and controls
170 lines (132 loc) · 5.32 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
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
// <copyright file="ClassGeneration.cs" company="Drastic Actions">
// Copyright (c) Drastic Actions. All rights reserved.
// </copyright>
using FFSourceGen;
using FFSourceGen.Models;
public class ClassGeneration
{
public ClassGeneration(SchemaDocument document, string key, SchemaDefinition def, string path)
{
this.Path = path;
this.Document = document;
this.Definition = def;
this.Key = key;
this.Namespace = string.Join(".", document.Id.Split('.').Take(document.Id.Split('.').Length - 1).ToArray());
this.CSharpNamespace = string.Join(".", document.Id.Split('.').Take(document.Id.Split('.').Length - 1).Select(n => n.ToPascalCase()).ToArray());
this.ClassName = this.GenerateClassName();
this.ProcessProperties();
this.CBorProperty = $"this.{this.ClassName} = new {this.ClassName}(obj[\"{this.Key}\"]);";
}
private string GenerateClassName()
{
var name = string.Empty;
if (this.Key == "main")
{
var item = string.Join(string.Empty, this.Document.Id.Split('.').TakeLast(1));
var test = this.HasPropertyWithClassName(item);
var itemsToTake = 1;
if (this.HasPropertyWithClassName(item))
{
itemsToTake = 2;
}
name = string.Join(string.Empty, this.Document.Id.Split('.').TakeLast(itemsToTake).Select(n => n.ToPascalCase())).ToPascalCase();
}
else if (this.Key == "view" || this.Key == "viewExternal")
{
var item = this.Key.ToPascalCase();
var root = this.Document.Id.Split("#").First();
var newItem = $"{item}{string.Join(string.Empty, root.Split('.').TakeLast(1).Select(n => n.ToPascalCase())).ToPascalCase()}";
if (newItem == "ViewRecord")
{
return "ViewRecordDef";
}
name = newItem;
}
if (string.IsNullOrEmpty(name))
{
name = this.Key.ToPascalCase();
var testingBesting = $"{this.Namespace}{this.Key}";
var existingNamespace = AppCommands.AllClasses.Where(n => n.Namespace == this.Namespace);
var existingClass = existingNamespace.FirstOrDefault(n => n.ClassName == name);
if (existingClass != null)
{
name = $"{name}Def";
}
}
return name;
}
public string ClassName { get; }
public string FullClassName => $"{this.CSharpNamespace}.{this.ClassName}";
public bool IsOutput => this.Key.Contains("Output");
public bool IsArray => this.Definition.Type == "array";
public bool IsBaseType => this.Definition.Type == "string" || this.Definition.Type == "number" || this.Definition.Type == "boolean";
public bool IsArrayOfATObjects => this.Definition.Type == "array" && (this.Definition.Items.Type == "object" || this.Definition.Items.Type == "union");
public bool IsArrayOfStrings => this.Definition.Type == "string" && this.Definition.KnownValues?.Count() > 0;
public SchemaDocument Document { get; }
public SchemaDefinition Definition { get; }
public string FullNamespace { get; }
public string Namespace { get; }
public string CSharpNamespace { get; }
public string Path { get; }
public string Key { get; }
public string CBorProperty { get; }
public bool HasCursor => this.Properties.Any(n => n.PropertyName == "Cursor");
public bool HasLimit => this.Properties.Any(n => n.PropertyName == "Limit");
public bool HasOutputList => this.Properties.Any(n => n.IsArray);
public PropertyGeneration? OutputList => this.Properties.FirstOrDefault(n => n.IsArray);
public bool IsEndpoint => this.Definition.Type == "procedure";
public List<PropertyGeneration> Properties { get; } = new();
public bool IsDefinitionFile => this.Document.Id.EndsWith("defs");
public string Id
{
get
{
if (this.Key == "main")
{
return this.Document.Id;
}
else
{
return $"{this.Document.Id}#{this.Key}";
}
}
}
public override string ToString()
{
return $"Class: {this.Id}, {this.ClassName}";
}
private bool HasPropertyWithClassName(string key)
{
var test = this.Definition.Properties.TryGetValue(key, out var prop);
if (test)
{
return true;
}
return false;
}
private void ProcessProperties()
{
if (this.Definition.Properties != null)
{
foreach (var prop in this.Definition.Properties)
{
if (prop.Value.Description.Contains("DEPRECATED", StringComparison.OrdinalIgnoreCase))
{
continue;
}
this.Properties.Add(new PropertyGeneration(prop.Value, prop.Key, this));
}
}
if (this.Definition.Record is not null)
{
foreach (var prop in this.Definition.Record.Properties)
{
if (prop.Value.Description.Contains("DEPRECATED", StringComparison.OrdinalIgnoreCase))
{
continue;
}
this.Properties.Add(new PropertyGeneration(prop.Value, prop.Key, this));
}
}
}
}