|
1 | | -using System.Linq; |
2 | | - |
3 | | -namespace Typewriter |
| 1 | +namespace Typewriter |
4 | 2 | { |
5 | | - using ApiDoctor.Validation; |
6 | | - using ApiDoctor.Validation.Error; |
7 | 3 | using ApiDoctor.Validation.OData; |
8 | 4 | using ApiDoctor.Validation.Utility; |
9 | | - using System; |
10 | | - using System.Collections.Generic; |
11 | 5 | using System.Linq; |
12 | | - using System.Text; |
13 | 6 |
|
| 7 | + /// <summary> |
| 8 | + /// From https://github.com/OneDrive/apidoctor/blob/master/ApiDoctor.Publishing/CSDL/csdlextensionmethods.cs |
| 9 | + /// </summary> |
14 | 10 | internal static class CsdlExtensionMethods |
15 | 11 | { |
16 | | - |
17 | | - public static string RequestUriPathOnly(this MethodDefinition method, string[] baseUrlsToRemove, IssueLogger issues) |
18 | | - { |
19 | | - var path = method.Request.FirstLineOnly().TextBetweenCharacters(' ', '?').TrimEnd('/'); |
20 | | - |
21 | | - if (baseUrlsToRemove != null) |
22 | | - { |
23 | | - foreach (var baseUrl in baseUrlsToRemove) |
24 | | - { |
25 | | - if (!string.IsNullOrEmpty(baseUrl) && path.StartsWith(baseUrl, StringComparison.OrdinalIgnoreCase)) |
26 | | - { |
27 | | - path = path.Substring(baseUrl.Length); |
28 | | - } |
29 | | - } |
30 | | - } |
31 | | - |
32 | | - // just in case there's a stray example that doesn't match, at least chop the domain part off. |
33 | | - foreach (var scheme in new[] { "https://", "http://" }) |
34 | | - { |
35 | | - if (path.StartsWith(scheme, StringComparison.OrdinalIgnoreCase)) |
36 | | - { |
37 | | - int pathStartIndex = path.IndexOf('/', scheme.Length); |
38 | | - if (pathStartIndex != -1) |
39 | | - { |
40 | | - path = path.Substring(pathStartIndex); |
41 | | - break; |
42 | | - } |
43 | | - } |
44 | | - } |
45 | | - |
46 | | - // Normalize variables in the request path |
47 | | - path = path.ReplaceTextBetweenCharacters('{', '}', "var"); |
48 | | - |
49 | | - if (method.RequestMetadata.SampleKeys != null) |
50 | | - { |
51 | | - foreach (var key in method.RequestMetadata.SampleKeys) |
52 | | - { |
53 | | - path = path.Replace("/" + key, "/{var}"); |
54 | | - } |
55 | | - } |
56 | | - |
57 | | - // Normalize function params |
58 | | - var substitutions = new Dictionary<string, string>(); |
59 | | - var parenIndex = path.IndexOf('('); |
60 | | - for (int i = 0; i < path.Length; i++) |
61 | | - { |
62 | | - if (path[i] == '(') |
63 | | - { |
64 | | - // this is the start of a function. let's find the closing paren. |
65 | | - var close = path.IndexOf(')', i); |
66 | | - if (close > -1) |
67 | | - { |
68 | | - var inner = path.Substring(i + 1, close - i - 1); |
69 | | - substitutions[inner] = NormalizeFunctionParameters(inner, issues); |
70 | | - i = close; |
71 | | - } |
72 | | - } |
73 | | - } |
74 | | - |
75 | | - foreach (var sub in substitutions) |
76 | | - { |
77 | | - path = path.Replace(sub.Key, sub.Value); |
78 | | - } |
79 | | - |
80 | | - // Rewrite path syntax into what it logically means |
81 | | - path = path.ReplaceTextBetweenCharacters(':', ':', "/children/{var}", requireSecondChar: false, removeTargetChars: true); |
82 | | - |
83 | | - return path; |
84 | | - } |
85 | | - |
86 | | - private static string NormalizeFunctionParameters(string funcParams, IssueLogger issues) |
87 | | - { |
88 | | - // foo=bar, baz ='qux',x= 9 |
89 | | - var normalized = new StringBuilder(); |
90 | | - var allParams = funcParams.Split(','); |
91 | | - for (int i = 0; i < allParams.Length; i++) |
92 | | - { |
93 | | - var param = allParams[i].Trim(); |
94 | | - var kvp = param.Split('='); |
95 | | - if (kvp.Length != 2) |
96 | | - { |
97 | | - issues.Error(ValidationErrorCode.ParameterParserError, $"Malformed function params {funcParams}"); |
98 | | - return funcParams; |
99 | | - } |
100 | | - |
101 | | - allParams[i] = kvp[0].Trim() + "={var}"; |
102 | | - } |
103 | | - |
104 | | - return string.Join(",", allParams.OrderBy(p => p)); |
105 | | - } |
106 | | - |
107 | | - //public static string HttpMethodVerb(this MethodDefinition method) |
108 | | - //{ |
109 | | - // HttpParser parser = new HttpParser(); |
110 | | - // var request = parser.ParseHttpRequest(method.Request); |
111 | | - // return request.Method; |
112 | | - |
113 | | - //} |
114 | | - |
115 | | - internal static void AppendWithCondition(this System.Text.StringBuilder sb, bool condition, string text, string prefixIfExistingContent = null) |
116 | | - { |
117 | | - if (condition) |
118 | | - { |
119 | | - if (sb.Length > 0 && prefixIfExistingContent != null) |
120 | | - sb.Append(prefixIfExistingContent); |
121 | | - sb.Append(text); |
122 | | - } |
123 | | - } |
124 | | - |
125 | 12 | /// <summary> |
126 | 13 | /// Merge two EntityFramework instances together into the first framework |
127 | 14 | /// </summary> |
@@ -154,7 +41,6 @@ internal static EntityFramework MergeWith(this EntityFramework framework1, Entit |
154 | 41 | } |
155 | 42 |
|
156 | 43 | return edmx; |
157 | | - |
158 | 44 | } |
159 | 45 | } |
160 | 46 | } |
0 commit comments