-
Notifications
You must be signed in to change notification settings - Fork 64
/
SchemaExtensionMethods.cs
145 lines (120 loc) · 4.36 KB
/
SchemaExtensionMethods.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
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information
using System;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
namespace Generator.Schema
{
public static class SchemaExtensionMethods
{
public static string NamePCased(this YamlSchema value) =>
FileGenerator.PascalCase(value.Name);
public static string ExampleSanitized(this Field value) =>
value.Example.ToString().StartsWith("{") && value.Example.ToString().Contains("lat")
? value.Example.ToString()
: value.Example.ToString().StartsWith("[")
? "[" + string.Join(',', value.Example.ToString().Trim('[').Trim(']').Split(',').Select(s => s.Trim())) + "]"
: JsonConvert.SerializeObject(value.Example).Trim('"');
private static string Sanitized(this string value) =>
Regex.Replace(value.TrimEnd(), @"[\r\n]+", "<para/><para/>");
public static string DescriptionSanitized(this Field value) =>
value.Description.Sanitized();
public static string DescriptionSanitized(this YamlSchema value) =>
value.Description.Sanitized();
public static string DescriptionSanitized(this FieldAllowedValue value) =>
value.Description.Sanitized();
public static string GetEnumClrTypeName(this Field value) =>
FileGenerator.PascalCase(value.FlatName);
public static bool IsCustomEnum(this Field value) =>
value.AllowedValues != null && value.AllowedValues.Any();
public static bool IsArray(this Field value) => value.Normalize != null && value.Normalize.Contains("array");
public static string ClrType(this Field value)
{
var isArray = value.IsArray() ||
value.FlatName == "user.id" ||
value.FlatName == "client.ip" ||
value.FlatName == "destination.ip" ||
value.FlatName == "server.ip" ||
value.FlatName == "source.ip" ||
value.FlatName == "registry.data.strings";
// Special cases.
if (value.FlatName == "labels") return "IDictionary<string, object>";
// C# custom property
if (value.Name == "_metadata") return "IDictionary<string, object>";
var tipe = "";
switch (value.Type)
{
case FieldType.Keyword:
case FieldType.Text:
case FieldType.Ip:
tipe = "string";
break;
case FieldType.Long:
tipe = "long?";
break;
case FieldType.Integer:
tipe = "int?";
break;
case FieldType.Date:
tipe = "DateTimeOffset?";
break;
case FieldType.Object:
tipe = "object";
break;
case FieldType.Float:
tipe = "float?";
break;
case FieldType.GeoPoint:
tipe = "Location";
break;
case FieldType.Boolean:
tipe = "bool?";
break;
default:
throw new ArgumentOutOfRangeException();
}
return isArray ? $"{tipe}[]" : tipe;
}
public static string Extras(this Field value)
{
var builder = new StringBuilder();
if (value.IgnoreAbove.HasValue)
builder.AppendFormat(".IgnoreAbove({0})", value.IgnoreAbove.Value);
if (value.Norms.HasValue)
builder.AppendFormat(".Norms({0})", value.Norms.Value.ToString().ToLower());
if (value.Indexed.HasValue)
builder.AppendFormat(".Index({0})", value.Indexed.Value.ToString().ToLower());
if (value.DocValues.HasValue)
builder.AppendFormat(".DocValues({0})", value.DocValues.Value.ToString().ToLower());
if (value.Type == FieldType.Long
|| value.Type == FieldType.Integer
|| value.Type == FieldType.Float)
builder.AppendFormat(".Type(NumberType.{0:f})", value.Type);
return builder.ToString();
}
public static string JsonFieldName(this Field value) =>
string.IsNullOrEmpty(value.Schema.Prefix)
? value.FlatName
: value.FlatName.TrimStart(value.Schema.Prefix);
public static string MappingType(this Field value) =>
value.Type switch
{
FieldType.Keyword => "Keyword",
FieldType.Long => "Number",
FieldType.Integer => "Number",
FieldType.Date => "Date",
FieldType.Ip => "Ip",
FieldType.Object => $"Object<{value.ClrType()}>",
FieldType.Text => "Text",
FieldType.Float => "Number",
FieldType.GeoPoint => "GeoPoint",
FieldType.Boolean => "Boolean",
_ => throw new ArgumentOutOfRangeException()
};
public static string PropertyName(this Field value) =>
FileGenerator.PascalCase(value.JsonFieldName()).TrimStart('@');
}
}