forked from Azure/azure-sdk-tools
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRunTimeMetadataModel.cs
More file actions
175 lines (150 loc) · 6.26 KB
/
RunTimeMetadataModel.cs
File metadata and controls
175 lines (150 loc) · 6.26 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
using Microsoft.AspNetCore.Mvc.RazorPages;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Xml;
using System.IO;
namespace Azure.Sdk.Tools.TestProxy.Models
{
public class RunTimeMetaDataModel : PageModel
{
public Assembly Assembly = Assembly.GetExecutingAssembly();
const string CTOR_FORMAT_STRING = "M:{0}.#ctor";
const string CLASS_FORMAT_STRING = "T:{0}";
public IEnumerable<ActionDescription> Descriptions;
public int Length { get { return Descriptions.Count(); } }
public IEnumerable<ActionDescription> Transforms
{
get { return Descriptions.Where(x => x.ActionType == MetaDataType.Transform); }
}
public IEnumerable<ActionDescription> Matchers
{
get { return Descriptions.Where(x => x.ActionType == MetaDataType.Matcher); }
}
public IEnumerable<ActionDescription> Sanitizers
{
get { return Descriptions.Where(x => x.ActionType == MetaDataType.Sanitizer); }
}
// generates a constructor description for a given type based off comment xml
public CtorDescription GetCtorDescription(Type type, XmlDocument docCommentXml)
{
var memberSearchString = String.Format(CTOR_FORMAT_STRING, type.FullName);
var description = String.Empty;
var arguments = new List<Tuple<string, string>>();
foreach (XmlElement xmlElement in docCommentXml["doc"]["members"])
{
if (xmlElement.Attributes["name"].Value.Contains(memberSearchString))
{
foreach(XmlNode child in xmlElement.ChildNodes)
{
if (child.Name == "summary")
{
description = child.InnerXml;
}
if (child.Name == "param")
{
arguments.Add(new Tuple<string, string>(child.Attributes["name"].Value, child.InnerText));
}
}
}
}
return new CtorDescription()
{
Description = description,
Arguments = arguments
};
}
// takes a already instantiated sanitizer, matcher, or transform and returns the instance details as a ctor description
// does not use the commentxml
public CtorDescription GetInstanceDetails(object target)
{
Type tType = target.GetType();
IList<FieldInfo> fields = new List<FieldInfo>(tType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance));
var arguments = new List<Tuple<string, string>>();
var filteredFields = fields.Where(x => x.FieldType.Name == "String" || x.FieldType.Name == "ApplyCondition");
// we only want to crawl the fields if it is an inherited type. customizations are not offered
// when looking at a base RecordMatcher, ResponseTransform, or RecordedTestSanitizer
// These 3 will have a basetype of Object
if (tType.BaseType != typeof(Object))
{
foreach (FieldInfo field in filteredFields)
{
var prop = field.GetValue(target);
string propValue;
if (prop == null)
{
propValue = "This argument is unset or null.";
}
else
{
if (field.FieldType.Name == "ApplyCondition")
{
propValue = prop.ToString();
if (propValue == null)
{
continue;
}
}
else
{
propValue = "\"" + prop.ToString() + "\"";
}
}
arguments.Add(new Tuple<string, string>(GetFriendlyFieldName(field.Name), propValue));
}
}
return new CtorDescription()
{
Description = String.Empty,
Arguments = arguments
};
}
public static Dictionary<string, string> FieldNameMapping = new Dictionary<string, string>()
{
{ "_jsonPath", "jsonPath" },
{ "_value", "value" },
{ "_regex", "regex" },
{ "_groupForReplace", "groupForReplace" },
{ "_condition", "condition" },
{ "_target", "target" },
{ "_key", "key" },
{ "_method", "method" },
{ "_newValue", "value" },
{ "_resetAfterFirst", "resetAfterFirst" },
{ "_regexValue", "regex" }
};
public string GetFriendlyFieldName(string fieldName)
{
if (!string.IsNullOrWhiteSpace(fieldName) && FieldNameMapping.ContainsKey(fieldName))
{
return FieldNameMapping[fieldName];
}
return fieldName;
}
public string GetClassDocComment(Type type, XmlDocument docCommentXml)
{
var memberSearchString = String.Format(CLASS_FORMAT_STRING, type.FullName);
foreach (XmlElement xmlElement in docCommentXml["doc"]["members"])
{
if (xmlElement.Attributes["name"].Value.Equals(memberSearchString))
{
return xmlElement.ChildNodes[0].InnerText;
}
}
return String.Empty;
}
// for this to work you need to have generatexmldoc activated and the generated comment xml MUST be alongside the assembly
public XmlDocument GetDocCommentXML()
{
var location = System.AppContext.BaseDirectory;
var name = Assembly.GetName().Name;
using (var xmlReader = new StreamReader(Path.Join(location, $"{name}.xml")))
{
XmlDocument result = new XmlDocument();
result.Load(xmlReader);
return result;
}
}
}
}