-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpecifier.cs
154 lines (129 loc) · 6.51 KB
/
Specifier.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
146
147
148
149
150
151
152
153
154
using System.Linq;
using System.Reflection;
namespace Documentation
{
public class Specifier<T> : ISpecifier
{
public string GetApiDescription()
{
return typeof(T).GetCustomAttributes().OfType<ApiDescriptionAttribute>().FirstOrDefault()?.Description;
}
public string[] GetApiMethodNames()
{
return typeof(T)
.GetMethods()
.Where(m => m.GetCustomAttributes().OfType<ApiMethodAttribute>().Any())
.Select(k => k.Name).ToArray();
}
public string GetApiMethodDescription(string methodName)
{
return typeof(T)
.GetMethods().FirstOrDefault(m => m.Name == methodName)
?.GetCustomAttributes().OfType<ApiDescriptionAttribute>().FirstOrDefault()
?.Description;
}
public string[] GetApiMethodParamNames(string methodName)
{
return typeof(T).GetMethods().FirstOrDefault(m => m.Name == methodName)
?.GetParameters().Select(p => p.Name).ToArray();
}
public string GetApiMethodParamDescription(string methodName, string paramName)
{
return typeof(T)
.GetMethods().FirstOrDefault(m => m.Name == methodName)
?.GetParameters().FirstOrDefault(p => p.Name == paramName)
?.GetCustomAttributes().OfType<ApiDescriptionAttribute>().FirstOrDefault()
?.Description;
}
public ApiParamDescription GetApiMethodParamFullDescription(string methodName, string paramName)
{
var methodInfo = typeof(T).GetMethods().FirstOrDefault(m => m.Name == methodName);
var parameterInfo = typeof(T)
.GetMethods().FirstOrDefault(m => m.Name == methodName)
?.GetParameters().FirstOrDefault(p => p.Name == paramName);
// если такого метода не существует
if (methodInfo == null)
return new ApiParamDescription {ParamDescription = new CommonDescription(paramName)};
// если такого параметра не существует
if (parameterInfo == null)
return new ApiParamDescription {ParamDescription = new CommonDescription(paramName)};
return GetParamDescriptions(new[] {parameterInfo})[0];
}
public ApiMethodDescription GetApiMethodFullDescription(string methodName)
{
var methodInfo = typeof(T).GetMethods().FirstOrDefault(m => m.Name == methodName);
var parameterInfos = methodInfo?.GetParameters();
// если метод не содержит атрибута [ApiMethod]
if (!(methodInfo?.GetCustomAttributes().OfType<ApiMethodAttribute>()).Any())
return null;
var methodDescription = new CommonDescription(methodName, GetApiMethodDescription(methodName));
var paramDescriptions = GetParamDescriptions(parameterInfos);
// если метод имееет тип void
if (methodInfo?.ReturnType.FullName == "System.Void")
return new ApiMethodDescription
{
MethodDescription = methodDescription,
ParamDescriptions = paramDescriptions
};
var returnDescription = GetReturnDescription(methodInfo);
return new ApiMethodDescription
{
MethodDescription = methodDescription,
ParamDescriptions = paramDescriptions,
ReturnDescription = returnDescription
};
}
private static ApiParamDescription[] GetParamDescriptions(ParameterInfo[] parameterInfos)
{
var paramDescriptions = new ApiParamDescription[parameterInfos.Length];
for (var i = 0; i < parameterInfos.Length; i++)
{
var isRequiredAttribute = parameterInfos[i].GetCustomAttributes().OfType<ApiRequiredAttribute>().Any();
var requiredAttribute = parameterInfos[i].GetCustomAttributes().OfType<ApiRequiredAttribute>()
.FirstOrDefault();
var apiDescription = parameterInfos[i].GetCustomAttributes().OfType<ApiDescriptionAttribute>()
.FirstOrDefault()?.Description;
var apiIntValidationAttribute = parameterInfos[i].GetCustomAttributes()
.OfType<ApiIntValidationAttribute>().FirstOrDefault();
if (apiIntValidationAttribute == null)
{
paramDescriptions[i] = new ApiParamDescription
{
ParamDescription = new CommonDescription(parameterInfos[i].Name, apiDescription),
Required = requiredAttribute?.Required ?? isRequiredAttribute
};
}
else
paramDescriptions[i] = new ApiParamDescription
{
ParamDescription = new CommonDescription(parameterInfos[i].Name, apiDescription),
Required = requiredAttribute?.Required ?? isRequiredAttribute,
MinValue = apiIntValidationAttribute.MinValue,
MaxValue = apiIntValidationAttribute.MaxValue
};
}
return paramDescriptions;
}
private static ApiParamDescription GetReturnDescription(MethodInfo methodInfo)
{
var apiRequiredAttribute = methodInfo
?.ReturnTypeCustomAttributes
.GetCustomAttributes(false).OfType<ApiRequiredAttribute>().FirstOrDefault();
var apiIntValidationAttribute = methodInfo
?.ReturnTypeCustomAttributes
.GetCustomAttributes(false).OfType<ApiIntValidationAttribute>().FirstOrDefault();
var returnDescription = new ApiParamDescription();
if (apiIntValidationAttribute != null && apiRequiredAttribute != null)
{
returnDescription = new ApiParamDescription
{
Required = apiRequiredAttribute.Required,
ParamDescription = new CommonDescription(),
MinValue = apiIntValidationAttribute.MinValue,
MaxValue = apiIntValidationAttribute.MaxValue
};
}
return returnDescription;
}
}
}