1
+ @using System
1
2
@using System .Collections .Generic
2
3
@using System .Linq
3
4
@using ApiGenerator .Domain
4
5
@using ApiGenerator
5
-
6
6
@functions {
7
7
private const string RawSize = " Raw" ;
8
8
private const string SizeEnum = " Size" ;
14
14
}
15
15
private string CreateCase (string e , string o )
16
16
{
17
- var enumValue = ( e == SizeEnum && o == string . Empty ) ? RawSize : o . ToPascalCase ( true );
17
+ var enumValue = GetEnumValue ( e , o );
18
18
return string .Format (" case {0}.{1}: return \" {2}\" ;" , e , enumValue , o );
19
19
}
20
20
private bool IsFlag (string name )
21
21
{
22
- return (name .EndsWith (" Metric" )) || name .EndsWith (" Feature" );
22
+ return name .EndsWith (" Metric" ) || name .EndsWith (" Feature" );
23
+ }
24
+
25
+ private string CreateEnumKeyValue (string enumName , string value , int index )
26
+ {
27
+ var enumValue = GetEnumValue (enumName , value );
28
+ return string .Format (" {3}{{ {0}.{1}, \" {2}\" }}," , enumName , enumValue , value , index == 0 ? " \t\t\t\t " : string .Empty );
29
+ }
30
+
31
+ private string GetEnumValue (string enumName , string value )
32
+ {
33
+ return enumName == SizeEnum && value == string .Empty
34
+ ? RawSize
35
+ : value .ToPascalCase (true );
23
36
}
24
37
}
25
38
using System;
26
39
using System.Collections.Generic;
40
+ using System.Collections.Concurrent;
27
41
using System.Linq;
28
42
using System.Text;
43
+ using System.Reflection;
29
44
using System.Runtime.Serialization;
30
45
31
46
///This file contains all the typed enums that the client rest api spec exposes.
32
47
///This file is automatically generated from https://github.com/elastic/elasticsearch/tree/@Model.Commit /rest-api-spec
33
48
///Generated of commit @Model.Commit
34
49
namespace Elasticsearch.Net
35
50
{
36
- @foreach ( EnumDescription e in Model .EnumsInTheSpec )
37
- {
38
- var isFlag = IsFlag (e .Name );
51
+ @foreach ( EnumDescription e in Model .EnumsInTheSpec )
52
+ {
53
+ var isFlag = IsFlag (e .Name );
39
54
<text >
40
- @( isFlag ? " [Flags]" : string .Empty ) public enum @e .Name
55
+ @( isFlag ? " [Flags]" : string .Empty ) public enum @e .Name
41
56
{
42
- @Raw (string.Join(",\r \n\t\t", e .Options.OrderBy(s=>s == "_all" ? 1 : 0).Select((s, i ) => CreateEnum(e.Name, s , isFlag ? (int?)i : null ))))
43
- }
44
- </text >
45
- }
57
+ @Raw (string.Join(",\r \n\t\t", e .Options.OrderBy(s => s == "_all" ? 1 : 0).Select((s, i ) => CreateEnum(e.Name, s , isFlag ? (int?)i : null))))
58
+ }</text >
59
+ }
46
60
47
61
public static class KnownEnums
62
+ {
63
+ private class EnumDictionary : @(Raw ("Dictionary <Enum , string >"))
64
+ {
65
+ public EnumDictionary (int capacity ) : base (capacity ) {}
66
+ public @(Raw ("Func <Enum , string >")) Resolver { get ; set ; }
67
+ }
68
+
69
+ @foreach (EnumDescription e in Model .EnumsInTheSpec)
48
70
{
49
- public static string UnknownEnum { get ; } = " _UNKNOWN_ENUM_" ;
50
- public static string Resolve (Enum e )
71
+ var isFlag = IsFlag (e .Name );
72
+ < text >
73
+ public static string GetStringValue (this @(e .Name ) enumValue )
74
+ {
75
+ < / text >
76
+ if (isFlag )
77
+ {
78
+ var allOption = e .Options .FirstOrDefault (o => o == " _all" );
79
+ if (allOption != null )
80
+ {
81
+ < text > if ((enumValue & @(e .Name ).All ) != 0 ) return " _all" ;< / text >
82
+ }
83
+ < text > var list = new @(Raw (" List<string>()" ));< / text >
84
+ foreach (var option in e .Options .Where (o => o != " _all" ))
85
+ {
86
+ < text > if ((enumValue & @(e .Name ).@(GetEnumValue (e .Name , option ))) != 0 ) list .Add (" @(option)" );< / text >
87
+ }
88
+ < text > return string .Join (" ," , list );
89
+ }< / text >
90
+ }
91
+ else
92
+ {
93
+ < text > switch (enumValue )
94
+ {
95
+ @Raw (string .Join (" \r\n\t\t\t\t " , e .Options .Select (o => CreateCase (e .Name , o ))))
96
+ }
97
+ throw new ArgumentException ($" '{enumValue .ToString ()}' is not a valid value for enum '@(e.Name)'" );
98
+ }< / text >
99
+ }
100
+ }
101
+
102
+ private static readonly @( Raw (" ConcurrentDictionary<Type, Func<Enum, string>>" )) EnumStringResolvers =
103
+ new @(Raw (" ConcurrentDictionary<Type, Func<Enum, string>>" ))();
104
+
105
+ static KnownEnums ()
106
+ {
107
+ @foreach ( EnumDescription e in Model .EnumsInTheSpec )
108
+ {
109
+ <text >EnumStringResolvers .TryAdd (typeof (@(e .Name )), (e ) => GetStringValue ((@(e .Name ))e ));</text >
110
+ }
111
+ }
112
+
113
+ public static string GetStringValue (this Enum e )
114
+ {
115
+ var type = e .GetType ();
116
+ var resolver = EnumStringResolvers .GetOrAdd (type , GetEnumStringResolver );
117
+ return resolver (e );
118
+ }
119
+
120
+ private static @Raw( " Func<Enum, string>" ) GetEnumStringResolver (Type type )
51
121
{
52
- @foreach (EnumDescription e in Model .EnumsInTheSpec )
122
+ var values = Enum .GetValues (type );
123
+ var dictionary = new EnumDictionary (values .Length );
124
+
125
+ for (int index = 0 ; index < values .Length ; index ++ )
53
126
{
54
- var isFlag = IsFlag (e .Name );
55
- < text > if (e is @e .Name )
56
- { < / text >
57
- if (isFlag )
127
+ var value = values .GetValue (index );
128
+ #if DOTNETCORE
129
+ var info = type .GetTypeInfo ().GetDeclaredField (value .ToString ());
130
+ #else
131
+ var info = type .GetField (value .ToString ());
132
+ #endif
133
+ var da = (EnumMemberAttribute [])info .GetCustomAttributes (typeof (EnumMemberAttribute ), false );
134
+ var stringValue = da .Length > 0 ? da [0 ].Value : Enum .GetName (type , value );
135
+ dictionary .Add ((Enum )value , stringValue );
136
+ }
137
+
138
+ #if DOTNETCORE
139
+ var isFlag = type .GetTypeInfo ().GetCustomAttributes (typeof (FlagsAttribute ), false ).Any ();
140
+ #else
141
+ var isFlag = type .GetCustomAttributes (typeof (FlagsAttribute ), false ).Length > 0 ;
142
+ #endif
143
+
144
+ return (e ) =>
58
145
{
59
- < text > var list = new @(Raw (" List<string>()" ));< / text >
60
- foreach (var option in e .Options .OrderBy (s => s == " _all" ? 1 : 0 ))
146
+ if (isFlag )
61
147
{
62
- if (option != " _all" )
148
+ var list = new @(Raw (" List<string>()" ));
149
+ foreach (var kv in dictionary )
63
150
{
64
- < text > if (e .HasFlag (@(e .Name ).@(option .ToPascalCase (true )))) list .Add (" @(option)" );< / text >
65
- }
66
- else
67
- {
68
- < text > if (e .HasFlag (@(e .Name ).@(option .ToPascalCase (true )))) return " @(option)" ;< / text >
151
+ if (e .HasFlag (kv .Key )) list .Add (kv .Value );
69
152
}
153
+ return string .Join (" ," , list );
70
154
}
71
- < text > return string .Join (" ," , list );< / text >
72
- }
73
- else
74
- {
75
- < text > switch ((@e .Name )e )
155
+ else
76
156
{
77
- @Raw (string .Join (" \r\n\t\t\t\t\t " , e .Options .Select (o => CreateCase (e .Name ,o ))))
78
- }< / text >
79
- }
80
- < text >
81
- }
82
- < / text >
83
- }
84
- return UnknownEnum ;
157
+ return dictionary [e ];
158
+ }
159
+ };
85
160
}
86
161
}
87
- }
88
-
162
+ }
0 commit comments