forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetadataTypes.cs
More file actions
500 lines (451 loc) · 19.9 KB
/
MetadataTypes.cs
File metadata and controls
500 lines (451 loc) · 19.9 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using ServiceStack.Script;
namespace ServiceStack
{
public class MetadataTypesConfig
{
public MetadataTypesConfig(
string baseUrl = null,
bool makePartial = true,
bool makeVirtual = true,
bool addReturnMarker = true,
bool convertDescriptionToComments = true,
bool addDataContractAttributes = false,
bool addIndexesToDataMembers = false,
bool addGeneratedCodeAttributes = false,
string addDefaultXmlNamespace = null,
string baseClass = null,
string package = null,
bool addResponseStatus = false,
bool addServiceStackTypes = true,
bool addModelExtensions = true,
bool addPropertyAccessors = true,
bool excludeGenericBaseTypes = false,
bool settersReturnThis = true,
bool makePropertiesOptional = true,
bool makeDataContractsExtensible = false,
bool initializeCollections = true,
int? addImplicitVersion = null)
{
BaseUrl = baseUrl;
MakePartial = makePartial;
MakeVirtual = makeVirtual;
AddReturnMarker = addReturnMarker;
AddDescriptionAsComments = convertDescriptionToComments;
AddDataContractAttributes = addDataContractAttributes;
AddDefaultXmlNamespace = addDefaultXmlNamespace;
BaseClass = baseClass;
Package = package;
MakeDataContractsExtensible = makeDataContractsExtensible;
AddIndexesToDataMembers = addIndexesToDataMembers;
AddGeneratedCodeAttributes = addGeneratedCodeAttributes;
InitializeCollections = initializeCollections;
AddResponseStatus = addResponseStatus;
AddServiceStackTypes = addServiceStackTypes;
AddModelExtensions = addModelExtensions;
AddPropertyAccessors = addPropertyAccessors;
ExcludeGenericBaseTypes = excludeGenericBaseTypes;
SettersReturnThis = settersReturnThis;
MakePropertiesOptional = makePropertiesOptional;
AddImplicitVersion = addImplicitVersion;
}
public string BaseUrl { get; set; }
public string UsePath { get; set; }
public bool MakePartial { get; set; }
public bool MakeVirtual { get; set; }
public bool MakeInternal { get; set; }
public string BaseClass { get; set; }
public string Package { get; set; }
public bool AddReturnMarker { get; set; }
public bool AddDescriptionAsComments { get; set; }
public bool AddDataContractAttributes { get; set; }
public bool AddIndexesToDataMembers { get; set; }
public bool AddGeneratedCodeAttributes { get; set; }
public int? AddImplicitVersion { get; set; }
public bool AddResponseStatus { get; set; }
public bool AddServiceStackTypes { get; set; }
public bool AddModelExtensions { get; set; }
public bool AddPropertyAccessors { get; set; }
public bool ExcludeGenericBaseTypes { get; set; }
public bool SettersReturnThis { get; set; }
public bool MakePropertiesOptional { get; set; }
public bool ExportAsTypes { get; set; }
public bool ExcludeImplementedInterfaces { get; set; }
public string AddDefaultXmlNamespace { get; set; }
public bool MakeDataContractsExtensible { get; set; }
public bool InitializeCollections { get; set; }
public List<string> AddNamespaces { get; set; }
public List<string> DefaultNamespaces { get; set; }
public List<string> DefaultImports { get; set; }
public List<string> IncludeTypes { get; set; }
public List<string> ExcludeTypes { get; set; }
public List<string> TreatTypesAsStrings { get; set; }
public bool ExportValueTypes { get; set; }
public string GlobalNamespace { get; set; }
public bool ExcludeNamespace { get; set; }
public HashSet<Type> IgnoreTypes { get; set; }
public HashSet<Type> ExportTypes { get; set; }
public HashSet<Type> ExportAttributes { get; set; }
public List<string> IgnoreTypesInNamespaces { get; set; }
}
public class MetadataTypes
{
public MetadataTypes()
{
Types = new List<MetadataType>();
Operations = new List<MetadataOperationType>();
Namespaces = new List<string>();
}
public MetadataTypesConfig Config { get; set; }
public List<string> Namespaces { get; set; }
public List<MetadataType> Types { get; set; }
public List<MetadataOperationType> Operations { get; set; }
}
public class AppMetadata : IMeta
{
public AppInfo App { get; set; }
public Dictionary<string, string> ContentTypeFormats { get; set; }
public PluginInfo Plugins { get; set; }
public Dictionary<string,CustomPlugin> CustomPlugins { get; set; }
public MetadataTypes Api { get; set; }
public Dictionary<string, string> Meta { get; set; }
}
public class PluginInfo : IMeta
{
public List<string> Loaded { get; set; }
public AuthInfo Auth { get; set; }
public AutoQueryInfo AutoQuery { get; set; }
public ValidationInfo Validation { get; set; }
public SharpPagesInfo SharpPages { get; set; }
public RequestLogsInfo RequestLogs { get; set; }
public AdminUsersInfo AdminUsers { get; set; }
public Dictionary<string, string> Meta { get; set; }
}
public class AuthInfo : IMeta
{
public bool? HasAuthSecret { get; set; }
public bool? HasAuthRepository { get; set; }
public bool? IncludesRoles { get; set; }
public bool? IncludesOAuthTokens { get; set; }
public string HtmlRedirect { get; set; }
public List<MetaAuthProvider> AuthProviders { get; set; }
public Dictionary<string,string[]> ServiceRoutes { get; set; }
public Dictionary<string, string> Meta { get; set; }
}
public class AutoQueryInfo : IMeta
{
public int? MaxLimit { get; set; }
public bool? UntypedQueries { get; set; }
public bool? RawSqlFilters { get; set; }
public bool? AutoQueryViewer { get; set; }
public bool? Async { get; set; }
public bool? OrderByPrimaryKey { get; set; }
public bool? CrudEvents { get; set; }
public bool? CrudEventsServices { get; set; }
public string AccessRole { get; set; }
public string NamedConnection { get; set; }
public List<AutoQueryConvention> ViewerConventions { get; set; }
public Dictionary<string, string> Meta { get; set; }
}
public class ValidationInfo : IMeta
{
public bool? HasValidationSource { get; set; }
public bool? HasValidationSourceAdmin { get; set; }
public Dictionary<string,string[]> ServiceRoutes { get; set; }
public List<ScriptMethodType> TypeValidators { get; set; }
public List<ScriptMethodType> PropertyValidators { get; set; }
public string AccessRole { get; set; }
public Dictionary<string, string> Meta { get; set; }
}
public class SharpPagesInfo : IMeta
{
public string ApiPath { get; set; }
public string ScriptAdminRole { get; set; }
public string MetadataDebugAdminRole { get; set; }
public bool? MetadataDebug { get; set; }
public bool? SpaFallback { get; set; }
public Dictionary<string, string> Meta { get; set; }
}
public class RequestLogsInfo : IMeta
{
public string[] RequiredRoles { get; set; }
public string RequestLogger { get; set; }
public Dictionary<string,string[]> ServiceRoutes { get; set; }
public Dictionary<string, string> Meta { get; set; }
}
public class AdminUsersInfo : IMeta
{
public string AccessRole { get; set; }
public List<string> Enabled { get; set; }
public MetadataType UserAuth { get; set; }
public MetadataType UserAuthDetails { get; set; }
public List<string> AllRoles { get; set; }
public List<string> AllPermissions { get; set; }
public List<string> QueryUserAuthProperties { get; set; }
public Dictionary<string, string> Meta { get; set; }
}
/// <summary>
/// Generic template for adding metadata info about custom plugins
/// </summary>
public class CustomPlugin : IMeta
{
/// <summary>
/// Which User Roles have access to this Plugins Services. See RoleNames for built-in Roles.
/// </summary>
public string AccessRole { get; set; }
/// <summary>
/// What Services Types (and their user-defined routes) are enabled in this plugin
/// </summary>
public Dictionary<string,string[]> ServiceRoutes { get; set; }
/// <summary>
/// List of enabled features in this plugin
/// </summary>
public List<string> Enabled { get; set; }
/// <summary>
/// Additional custom metadata about this plugin
/// </summary>
public Dictionary<string, string> Meta { get; set; }
}
public class ScriptMethodType
{
public string Name { get; set; }
public string[] ParamNames { get; set; }
public string[] ParamTypes { get; set; }
public string ReturnType { get; set; }
}
public class AutoQueryConvention
{
public string Name { get; set; }
public string Value { get; set; }
public string Types { get; set; }
public string ValueType { get; set; }
}
/// <summary>
/// App Info and
/// </summary>
public class AppInfo : IMeta
{
/// <summary>
/// The App's BaseUrl
/// </summary>
public string BaseUrl { get; set; }
/// <summary>
/// The ServiceStack Version
/// </summary>
public string ServiceStackVersion => Text.Env.VersionString;
/// <summary>
/// Name of the ServiceStack Instance
/// </summary>
public string ServiceName { get; set; }
/// <summary>
/// Textual description of the ServiceStack App (shown in Home Services list)
/// </summary>
public string ServiceDescription { get; set; }
/// <summary>
/// Icon for this ServiceStack App (shown in Home Services list)
/// </summary>
public string ServiceIconUrl { get; set; }
/// <summary>
/// Link to your website users can click to find out more about you
/// </summary>
public string BrandUrl { get; set; }
/// <summary>
/// A custom logo or image that users can click on to visit your site
/// </summary>
public string BrandImageUrl { get; set; }
/// <summary>
/// The default color of text
/// </summary>
public string TextColor { get; set; }
/// <summary>
/// The default color of links
/// </summary>
public string LinkColor { get; set; }
/// <summary>
/// The default background color of each screen
/// </summary>
public string BackgroundColor { get; set; }
/// <summary>
/// The default background image of each screen anchored to the bottom left
/// </summary>
public string BackgroundImageUrl { get; set; }
/// <summary>
/// The default icon for each of your App's Services
/// </summary>
public string IconUrl { get; set; }
/// <summary>
/// Custom User-Defined Attributes
/// </summary>
public Dictionary<string, string> Meta { get; set; }
}
public class MetaAuthProvider : IMeta
{
public string Name { get; set; }
public string Type { get; set; }
public NavItem NavItem { get; set; }
public Dictionary<string, string> Meta { get; set; }
}
public class MetadataOperationType
{
public MetadataType Request { get; set; }
public MetadataType Response { get; set; }
public List<string> Actions { get; set; }
public bool ReturnsVoid { get; set; }
public MetadataTypeName ReturnType { get; set; }
public List<MetadataRoute> Routes { get; set; }
public MetadataTypeName DataModel { get; set; }
public MetadataTypeName ViewModel { get; set; }
public bool RequiresAuth { get; set; }
public List<string> RequiredRoles { get; set; }
public List<string> RequiresAnyRole { get; set; }
public List<string> RequiredPermissions { get; set; }
public List<string> RequiresAnyPermission { get; set; }
public List<string> Tags { get; set; }
}
public class MetadataType : IMeta
{
[IgnoreDataMember]
public Type Type { get; set; }
[IgnoreDataMember]
public Dictionary<string, object> Items { get; set; }
[IgnoreDataMember]
public MetadataOperationType RequestType { get; set; }
// [IgnoreDataMember, Obsolete("Use MetadataOperationType.ReturnVoidMarker")]
// public bool ReturnVoidMarker => RequestType?.ReturnVoidMarker == true;
// [IgnoreDataMember, Obsolete("Use MetadataOperationType.ReturnMarkerTypeName")]
// public MetadataTypeName ReturnMarkerTypeName => RequestType?.ReturnMarkerTypeName;
public string Name { get; set; }
public string Namespace { get; set; }
public string[] GenericArgs { get; set; }
public MetadataTypeName Inherits { get; set; }
public MetadataTypeName[] Implements { get; set; }
public string DisplayType { get; set; }
public string Description { get; set; }
public bool? IsNested { get; set; }
public bool? IsEnum { get; set; }
public bool? IsEnumInt { get; set; }
public bool? IsInterface { get; set; }
public bool? IsAbstract { get; set; }
public MetadataDataContract DataContract { get; set; }
public List<MetadataPropertyType> Properties { get; set; }
public List<MetadataAttribute> Attributes { get; set; }
public List<MetadataTypeName> InnerTypes { get; set; }
public List<string> EnumNames { get; set; }
public List<string> EnumValues { get; set; }
public List<string> EnumMemberValues { get; set; }
public List<string> EnumDescriptions { get; set; }
public Dictionary<string, string> Meta { get; set; }
public string GetFullName() => Namespace + "." + Name;
}
public class MetadataTypeName
{
[IgnoreDataMember]
public Type Type { get; set; }
public string Name { get; set; }
public string Namespace { get; set; }
public string[] GenericArgs { get; set; }
}
public class MetadataRoute
{
[IgnoreDataMember]
public RouteAttribute RouteAttribute { get; set; }
public string Path { get; set; }
public string Verbs { get; set; }
public string Notes { get; set; }
public string Summary { get; set; }
}
public class MetadataDataContract
{
public string Name { get; set; }
public string Namespace { get; set; }
}
public class MetadataDataMember
{
public string Name { get; set; }
public int? Order { get; set; }
public bool? IsRequired { get; set; }
public bool? EmitDefaultValue { get; set; }
}
public class MetadataPropertyType
{
[IgnoreDataMember]
public PropertyInfo PropertyInfo { get; set; }
[IgnoreDataMember]
public Type PropertyType { get; set; }
[IgnoreDataMember]
public Dictionary<string, object> Items { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public bool? IsValueType { get; set; }
public bool? IsSystemType { get; set; }
public bool? IsEnum { get; set; }
public bool? IsPrimaryKey { get; set; }
public string TypeNamespace { get; set; }
public string[] GenericArgs { get; set; }
public string Value { get; set; }
public string Description { get; set; }
public MetadataDataMember DataMember { get; set; }
public bool? ReadOnly { get; set; }
public string ParamType { get; set; }
public string DisplayType { get; set; }
public bool? IsRequired { get; set; }
public string[] AllowableValues { get; set; }
public int? AllowableMin { get; set; }
public int? AllowableMax { get; set; }
public List<MetadataAttribute> Attributes { get; set; }
}
public class MetadataAttribute
{
[IgnoreDataMember]
public Attribute Attribute { get; set; }
public string Name { get; set; }
public List<MetadataPropertyType> ConstructorArgs { get; set; }
public List<MetadataPropertyType> Args { get; set; }
}
public static class MetadataTypeExtensions
{
public static bool InheritsAny(this MetadataType type, params string[] typeNames) =>
type.Inherits != null && typeNames.Contains(type.Inherits.Name);
public static bool ImplementsAny(this MetadataType type, params string[] typeNames) =>
type.Implements != null && type.Implements.Any(i => typeNames.Contains(i.Name));
public static bool ReferencesAny(this MetadataOperationType op, params string[] typeNames) =>
(op.Request.Inherits != null && (typeNames.Contains(op.Request.Inherits.Name) ||
op.Request.Inherits.GenericArgs?.Length > 0 &&
op.Request.Inherits.GenericArgs.Any(typeNames.Contains)))
||
(op.Response != null && (typeNames.Contains(op.Response.Name) ||
op.Response.GenericArgs?.Length > 0 &&
op.Response.GenericArgs.Any(typeNames.Contains)))
||
(op.Request.Implements != null && op.Request.Implements.Any(i =>
i.GenericArgs?.Length > 0 && i.GenericArgs.Any(typeNames.Contains)))
||
(op.Response?.Inherits != null && (typeNames.Contains(op.Response.Inherits.Name) ||
op.Response.Inherits.GenericArgs?.Length > 0 &&
op.Response.Inherits.GenericArgs.Any(typeNames.Contains)));
public static List<MetadataRoute> GetRoutes(this List<MetadataOperationType> operations, MetadataType type) =>
operations.FirstOrDefault(x => ReferenceEquals(x.Request, type))?.Routes ?? operations.GetRoutes(type.Name);
public static List<MetadataRoute> GetRoutes(this List<MetadataOperationType> operations, string typeName)
{
return operations.FirstOrDefault(x => x.Request.Name == typeName)?.Routes;
}
public static string ToScriptSignature(this ScriptMethodType method)
{
var paramCount = method.ParamNames?.Length ?? 0;
var firstParam = method.ParamNames?.Length > 0 ? method.ParamNames[0] : null;
var ret = method.ReturnType != null && method.ReturnType != nameof(StopExecution) ? " -> " + method.ReturnType : "";
var sig = paramCount == 0
? $"{method.Name}{ret}"
: paramCount == 1
? $"{firstParam} |> {method.Name}{ret}"
: $"{firstParam} |> {method.Name}(" + string.Join(", ", method.ParamNames?.Skip(1) ?? new string[0]) + $"){ret}";
return sig;
}
public static List<MetadataOperationType> GetOperationsByTags(this MetadataTypes types, string[] tags) =>
types.Operations.Where(x => x.Tags != null && x.Tags.Any(t => Array.IndexOf(tags, t) >= 0)).ToList();
}
}