|
5 | 5 |
|
6 | 6 | namespace Fonlow.Poco2Client
|
7 | 7 | {
|
8 |
| - /// <summary> |
9 |
| - /// Provide a dictionary to translate an attribute type to CodeAttributeDeclaration |
10 |
| - /// </summary> |
11 |
| - public sealed class AnnotationDeclarationGenerator |
12 |
| - { |
13 |
| - public static IDictionary<Type, Func<Attribute, CodeAttributeDeclaration>> Create() |
14 |
| - { |
15 |
| - return declaratinDic; |
16 |
| - } |
| 8 | + /// <summary> |
| 9 | + /// Provide a dictionary to translate an attribute type to CodeAttributeDeclaration |
| 10 | + /// </summary> |
| 11 | + public sealed class AnnotationDeclarationGenerator |
| 12 | + { |
| 13 | + public static IDictionary<Type, Func<Attribute, CodeAttributeDeclaration>> Create() |
| 14 | + { |
| 15 | + return declaratinDic; |
| 16 | + } |
17 | 17 |
|
18 |
| - static readonly IDictionary<Type, Func<Attribute, CodeAttributeDeclaration>> declaratinDic = new Dictionary<Type, Func<Attribute, CodeAttributeDeclaration>> |
19 |
| - { |
20 |
| - { typeof(RequiredAttribute), a => new CodeAttributeDeclaration("System.ComponentModel.DataAnnotations.Required") }, |
21 |
| - { typeof(RangeAttribute), a => |
22 |
| - { |
23 |
| - RangeAttribute obj = a as RangeAttribute; |
24 |
| - CodeSnippetExpression operandType = new CodeSnippetExpression($"typeof({obj.OperandType.FullName})"); |
25 |
| - CodeSnippetExpression min = new CodeSnippetExpression($"\"{obj.Minimum}\""); |
26 |
| - CodeSnippetExpression max = new CodeSnippetExpression($"\"{obj.Maximum}\""); |
| 18 | + static readonly IDictionary<Type, Func<Attribute, CodeAttributeDeclaration>> declaratinDic = new Dictionary<Type, Func<Attribute, CodeAttributeDeclaration>> |
| 19 | + { |
| 20 | + { typeof(RequiredAttribute), a => new CodeAttributeDeclaration("System.ComponentModel.DataAnnotations.Required") }, |
| 21 | + { typeof(RangeAttribute), a => |
| 22 | + { |
| 23 | + RangeAttribute obj = a as RangeAttribute; |
| 24 | + CodeSnippetExpression operandType = new CodeSnippetExpression($"typeof({obj.OperandType.FullName})"); |
| 25 | + CodeSnippetExpression min = new CodeSnippetExpression($"\"{obj.Minimum}\""); |
| 26 | + CodeSnippetExpression max = new CodeSnippetExpression($"\"{obj.Maximum}\""); |
27 | 27 | //var isNumber = obj.GetType()== typeof(int) || obj.GetType()==typeof(double);
|
28 | 28 | List<CodeAttributeArgument> attributeParams = new() { new CodeAttributeArgument(operandType),
|
29 |
| - new CodeAttributeArgument(min), |
30 |
| - new CodeAttributeArgument(max) }; |
31 |
| - if (!String.IsNullOrEmpty(obj.ErrorMessage)) |
32 |
| - { |
33 |
| - CodeSnippetExpression error= new CodeSnippetExpression($"\"{obj.ErrorMessage}\""); |
34 |
| - attributeParams.Add(new CodeAttributeArgument("ErrorMessage", error)); |
35 |
| - } |
| 29 | + new CodeAttributeArgument(min), |
| 30 | + new CodeAttributeArgument(max) }; |
| 31 | + if (!String.IsNullOrEmpty(obj.ErrorMessage)) |
| 32 | + { |
| 33 | + CodeSnippetExpression error= new CodeSnippetExpression($"\"{obj.ErrorMessage}\""); |
| 34 | + attributeParams.Add(new CodeAttributeArgument("ErrorMessage", error)); |
| 35 | + } |
36 | 36 |
|
37 |
| - return new CodeAttributeDeclaration("System.ComponentModel.DataAnnotations.Range", attributeParams.ToArray()); |
38 |
| - } |
39 |
| - }, |
40 |
| - { typeof(MaxLengthAttribute), a => |
41 |
| - { |
42 |
| - MaxLengthAttribute obj= a as MaxLengthAttribute; |
43 |
| - CodeSnippetExpression len = new CodeSnippetExpression(obj.Length.ToString()); |
44 |
| - List<CodeAttributeArgument> attributeParams = new() { new CodeAttributeArgument(len) }; |
45 |
| - if (!String.IsNullOrEmpty(obj.ErrorMessage)) |
46 |
| - { |
47 |
| - CodeSnippetExpression error= new CodeSnippetExpression($"\"{obj.ErrorMessage}\""); |
48 |
| - attributeParams.Add(new CodeAttributeArgument("ErrorMessage", error)); |
49 |
| - } |
| 37 | + return new CodeAttributeDeclaration("System.ComponentModel.DataAnnotations.Range", attributeParams.ToArray()); |
| 38 | + } |
| 39 | + }, |
| 40 | + { typeof(MaxLengthAttribute), a => |
| 41 | + { |
| 42 | + MaxLengthAttribute obj= a as MaxLengthAttribute; |
| 43 | + CodeSnippetExpression len = new CodeSnippetExpression(obj.Length.ToString()); |
| 44 | + List<CodeAttributeArgument> attributeParams = new() { new CodeAttributeArgument(len) }; |
| 45 | + if (!String.IsNullOrEmpty(obj.ErrorMessage)) |
| 46 | + { |
| 47 | + CodeSnippetExpression error= new CodeSnippetExpression($"\"{obj.ErrorMessage}\""); |
| 48 | + attributeParams.Add(new CodeAttributeArgument("ErrorMessage", error)); |
| 49 | + } |
50 | 50 |
|
51 |
| - return new CodeAttributeDeclaration("System.ComponentModel.DataAnnotations.MaxLength", attributeParams.ToArray()); |
52 |
| - } |
53 |
| - }, |
54 |
| - { typeof(MinLengthAttribute), a => |
55 |
| - { |
56 |
| - MinLengthAttribute obj= a as MinLengthAttribute; |
57 |
| - CodeSnippetExpression len = new CodeSnippetExpression(obj.Length.ToString()); |
58 |
| - List<CodeAttributeArgument> attributeParams = new() { new CodeAttributeArgument(len) }; |
59 |
| - if (!String.IsNullOrEmpty(obj.ErrorMessage)) |
60 |
| - { |
61 |
| - CodeSnippetExpression error= new CodeSnippetExpression($"\"{obj.ErrorMessage}\""); |
62 |
| - attributeParams.Add(new CodeAttributeArgument("ErrorMessage", error)); |
63 |
| - } |
| 51 | + return new CodeAttributeDeclaration("System.ComponentModel.DataAnnotations.MaxLength", attributeParams.ToArray()); |
| 52 | + } |
| 53 | + }, |
| 54 | + { typeof(MinLengthAttribute), a => |
| 55 | + { |
| 56 | + MinLengthAttribute obj= a as MinLengthAttribute; |
| 57 | + CodeSnippetExpression len = new CodeSnippetExpression(obj.Length.ToString()); |
| 58 | + List<CodeAttributeArgument> attributeParams = new() { new CodeAttributeArgument(len) }; |
| 59 | + if (!String.IsNullOrEmpty(obj.ErrorMessage)) |
| 60 | + { |
| 61 | + CodeSnippetExpression error= new CodeSnippetExpression($"\"{obj.ErrorMessage}\""); |
| 62 | + attributeParams.Add(new CodeAttributeArgument("ErrorMessage", error)); |
| 63 | + } |
64 | 64 |
|
65 |
| - return new CodeAttributeDeclaration("System.ComponentModel.DataAnnotations.MinLength", attributeParams.ToArray()); |
66 |
| - } |
67 |
| - }, |
68 |
| - { typeof(LengthAttribute), a => |
69 |
| - { |
70 |
| - LengthAttribute obj= a as LengthAttribute; |
71 |
| - CodeSnippetExpression min = new CodeSnippetExpression(obj.MinimumLength.ToString()); |
72 |
| - CodeSnippetExpression max = new CodeSnippetExpression(obj.MaximumLength.ToString()); |
73 |
| - List<CodeAttributeArgument> attributeParams = new() |
74 |
| - { |
75 |
| - new CodeAttributeArgument(min), |
76 |
| - new CodeAttributeArgument(max) |
77 |
| - }; |
| 65 | + return new CodeAttributeDeclaration("System.ComponentModel.DataAnnotations.MinLength", attributeParams.ToArray()); |
| 66 | + } |
| 67 | + }, |
| 68 | + { typeof(LengthAttribute), a => |
| 69 | + { |
| 70 | + LengthAttribute obj= a as LengthAttribute; |
| 71 | + CodeSnippetExpression min = new CodeSnippetExpression(obj.MinimumLength.ToString()); |
| 72 | + CodeSnippetExpression max = new CodeSnippetExpression(obj.MaximumLength.ToString()); |
| 73 | + List<CodeAttributeArgument> attributeParams = new() |
| 74 | + { |
| 75 | + new CodeAttributeArgument(min), |
| 76 | + new CodeAttributeArgument(max) |
| 77 | + }; |
78 | 78 |
|
79 |
| - if (!String.IsNullOrEmpty(obj.ErrorMessage)) |
80 |
| - { |
81 |
| - CodeSnippetExpression error= new CodeSnippetExpression($"\"{obj.ErrorMessage}\""); |
82 |
| - attributeParams.Add(new CodeAttributeArgument("ErrorMessage", error)); |
83 |
| - } |
| 79 | + if (!String.IsNullOrEmpty(obj.ErrorMessage)) |
| 80 | + { |
| 81 | + CodeSnippetExpression error= new CodeSnippetExpression($"\"{obj.ErrorMessage}\""); |
| 82 | + attributeParams.Add(new CodeAttributeArgument("ErrorMessage", error)); |
| 83 | + } |
84 | 84 |
|
85 |
| - return new CodeAttributeDeclaration("System.ComponentModel.DataAnnotations.Length", attributeParams.ToArray()); |
86 |
| - } |
87 |
| - }, |
88 |
| - { typeof(StringLengthAttribute), a => |
89 |
| - { |
90 |
| - StringLengthAttribute obj= a as StringLengthAttribute; |
91 |
| - CodeSnippetExpression max = new CodeSnippetExpression(obj.MaximumLength.ToString()); |
92 |
| - CodeSnippetExpression min = new CodeSnippetExpression(obj.MinimumLength.ToString()); |
93 |
| - List<CodeAttributeArgument> attributeParams = new() { new CodeAttributeArgument(max), |
94 |
| - new CodeAttributeArgument("MinimumLength", min) }; |
95 |
| - if (!String.IsNullOrEmpty(obj.ErrorMessage)) |
96 |
| - { |
97 |
| - CodeSnippetExpression error= new CodeSnippetExpression($"\"{obj.ErrorMessage}\""); |
98 |
| - attributeParams.Add(new CodeAttributeArgument("ErrorMessage", error)); |
99 |
| - } |
| 85 | + return new CodeAttributeDeclaration("System.ComponentModel.DataAnnotations.Length", attributeParams.ToArray()); |
| 86 | + } |
| 87 | + }, |
| 88 | + { typeof(StringLengthAttribute), a => |
| 89 | + { |
| 90 | + StringLengthAttribute obj= a as StringLengthAttribute; |
| 91 | + CodeSnippetExpression max = new CodeSnippetExpression(obj.MaximumLength.ToString()); |
| 92 | + CodeSnippetExpression min = new CodeSnippetExpression(obj.MinimumLength.ToString()); |
| 93 | + List<CodeAttributeArgument> attributeParams = new() { new CodeAttributeArgument(max), |
| 94 | + new CodeAttributeArgument("MinimumLength", min) }; |
| 95 | + if (!String.IsNullOrEmpty(obj.ErrorMessage)) |
| 96 | + { |
| 97 | + CodeSnippetExpression error= new CodeSnippetExpression($"\"{obj.ErrorMessage}\""); |
| 98 | + attributeParams.Add(new CodeAttributeArgument("ErrorMessage", error)); |
| 99 | + } |
100 | 100 |
|
101 |
| - return new CodeAttributeDeclaration("System.ComponentModel.DataAnnotations.StringLength", attributeParams.ToArray()); |
102 |
| - } |
103 |
| - }, |
104 |
| -{ typeof(DataTypeAttribute), a => |
105 |
| - { |
106 |
| - DataTypeAttribute obj= a as DataTypeAttribute; |
107 |
| - CodeSnippetExpression dataType = new CodeSnippetExpression("System.ComponentModel.DataAnnotations.DataType." + obj.DataType.ToString()); |
108 |
| - List<CodeAttributeArgument> attributeParams = new() { new CodeAttributeArgument(dataType) }; |
109 |
| - if (!String.IsNullOrEmpty(obj.ErrorMessage)) |
110 |
| - { |
111 |
| - CodeSnippetExpression error= new CodeSnippetExpression($"\"{obj.ErrorMessage}\""); |
112 |
| - attributeParams.Add(new CodeAttributeArgument("ErrorMessage", error)); |
113 |
| - } |
| 101 | + return new CodeAttributeDeclaration("System.ComponentModel.DataAnnotations.StringLength", attributeParams.ToArray()); |
| 102 | + } |
| 103 | + }, |
| 104 | + { typeof(DataTypeAttribute), a => |
| 105 | + { |
| 106 | + DataTypeAttribute obj= a as DataTypeAttribute; |
| 107 | + CodeSnippetExpression dataType = new CodeSnippetExpression("System.ComponentModel.DataAnnotations.DataType." + obj.DataType.ToString()); |
| 108 | + List<CodeAttributeArgument> attributeParams = new() { new CodeAttributeArgument(dataType) }; |
| 109 | + if (!String.IsNullOrEmpty(obj.ErrorMessage)) |
| 110 | + { |
| 111 | + CodeSnippetExpression error= new CodeSnippetExpression($"\"{obj.ErrorMessage}\""); |
| 112 | + attributeParams.Add(new CodeAttributeArgument("ErrorMessage", error)); |
| 113 | + } |
114 | 114 |
|
115 |
| - return new CodeAttributeDeclaration("System.ComponentModel.DataAnnotations.DataType", attributeParams.ToArray()); |
116 |
| - } |
117 |
| - }, |
118 |
| - { typeof(RegularExpressionAttribute), a => |
119 |
| - { |
120 |
| - RegularExpressionAttribute obj= a as RegularExpressionAttribute; |
121 |
| - string ps = $"@\"{obj.Pattern}\""; |
122 |
| - CodeSnippetExpression p = new CodeSnippetExpression(ps); |
123 |
| - List<CodeAttributeArgument> attributeParams = new() { new CodeAttributeArgument(p) }; |
124 |
| - if (!String.IsNullOrEmpty(obj.ErrorMessage)) |
125 |
| - { |
126 |
| - CodeSnippetExpression error= new CodeSnippetExpression($"\"{obj.ErrorMessage}\""); |
127 |
| - attributeParams.Add(new CodeAttributeArgument("ErrorMessage", error)); |
128 |
| - } |
| 115 | + return new CodeAttributeDeclaration("System.ComponentModel.DataAnnotations.DataType", attributeParams.ToArray()); |
| 116 | + } |
| 117 | + }, |
| 118 | + { typeof(RegularExpressionAttribute), a => |
| 119 | + { |
| 120 | + RegularExpressionAttribute obj= a as RegularExpressionAttribute; |
| 121 | + string ps = $"@\"{obj.Pattern}\""; |
| 122 | + CodeSnippetExpression p = new CodeSnippetExpression(ps); |
| 123 | + List<CodeAttributeArgument> attributeParams = new() { new CodeAttributeArgument(p) }; |
| 124 | + if (!String.IsNullOrEmpty(obj.ErrorMessage)) |
| 125 | + { |
| 126 | + CodeSnippetExpression error= new CodeSnippetExpression($"\"{obj.ErrorMessage}\""); |
| 127 | + attributeParams.Add(new CodeAttributeArgument("ErrorMessage", error)); |
| 128 | + } |
129 | 129 |
|
130 |
| - return new CodeAttributeDeclaration("System.ComponentModel.DataAnnotations.RegularExpressionAttribute", attributeParams.ToArray()); |
131 |
| - } |
132 |
| - }, |
133 |
| - }; |
| 130 | + return new CodeAttributeDeclaration("System.ComponentModel.DataAnnotations.RegularExpressionAttribute", attributeParams.ToArray()); |
| 131 | + } |
| 132 | + }, |
| 133 | + { typeof(ObsoleteAttribute), a => |
| 134 | + { |
| 135 | + ObsoleteAttribute obj= a as ObsoleteAttribute; |
| 136 | + CodeSnippetExpression messageExp = new CodeSnippetExpression($"\"{obj.Message}\""); |
| 137 | + CodeSnippetExpression errorExp= new CodeSnippetExpression(obj.IsError?"true":"false"); |
| 138 | + List<CodeAttributeArgument> attributeParams = obj.IsError ? new() { new CodeAttributeArgument(messageExp), new CodeAttributeArgument(errorExp) } |
| 139 | + : new() { new CodeAttributeArgument(messageExp)}; |
| 140 | + if (!String.IsNullOrEmpty(obj.DiagnosticId)) |
| 141 | + { |
| 142 | + CodeSnippetExpression diagnosticIdExp= new CodeSnippetExpression($"\"{obj.DiagnosticId}\""); |
| 143 | + attributeParams.Add(new CodeAttributeArgument("DiagnosticId", diagnosticIdExp)); |
| 144 | + } |
134 | 145 |
|
135 |
| - } |
| 146 | + if (!String.IsNullOrEmpty(obj.UrlFormat)) |
| 147 | + { |
| 148 | + CodeSnippetExpression urlFormatExp= new CodeSnippetExpression($"\"{obj.UrlFormat}\""); |
| 149 | + attributeParams.Add(new CodeAttributeArgument("UrlFormat", urlFormatExp)); |
| 150 | + } |
| 151 | + |
| 152 | + return new CodeAttributeDeclaration("System.ObsoleteAttribute", attributeParams.ToArray()); |
| 153 | + } |
| 154 | + }, |
| 155 | + |
| 156 | + }; |
| 157 | + |
| 158 | + } |
136 | 159 | }
|
0 commit comments