Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(176)

Side by Side Diff: lib/src/protobuf/field_type.dart

Issue 1277863003: cleanup: move fieldType constants and functions to a separate file (Closed) Base URL: git@github.com:dart-lang/dart-protobuf.git@master
Patch Set: _validateMessage should call _baseType, not callers Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/src/protobuf/field_info.dart ('k') | lib/src/protobuf/generated_message.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 part of protobuf;
6
7 bool _isRepeated(int fieldType) => (fieldType & FieldType._REPEATED_BIT) != 0;
8
9 bool _isRequired(int fieldType) => (fieldType & FieldType._REQUIRED_BIT) != 0;
10
11 bool _isEnum(int fieldType) =>
12 FieldType._baseType(fieldType) == FieldType._ENUM_BIT;
13
14 bool _isGroupOrMessage(int fieldType) =>
15 (fieldType & (FieldType._GROUP_BIT | FieldType._MESSAGE_BIT)) != 0;
16
17 /// Defines constants and functions for dealing with fieldType bits.
18 class FieldType {
19
20 /// Returns the base field type without any of the required, repeated
21 /// and packed bits.
22 static int _baseType(int fieldType) =>
23 fieldType & ~(_REQUIRED_BIT | _REPEATED_BIT | _PACKED_BIT);
24
25 static MakeDefaultFunc _defaultForType(int type) {
26 switch (type) {
27 case _OPTIONAL_BOOL:
28 case _REQUIRED_BOOL:
29 return _BOOL_FALSE;
30 case _OPTIONAL_BYTES:
31 case _REQUIRED_BYTES:
32 return _BYTES_EMPTY;
33 case _OPTIONAL_STRING:
34 case _REQUIRED_STRING:
35 return _STRING_EMPTY;
36 case _OPTIONAL_FLOAT:
37 case _REQUIRED_FLOAT:
38 case _OPTIONAL_DOUBLE:
39 case _REQUIRED_DOUBLE:
40 return _DOUBLE_ZERO;
41 case _OPTIONAL_INT32:
42 case _REQUIRED_INT32:
43 case _OPTIONAL_INT64:
44 case _REQUIRED_INT64:
45 case _OPTIONAL_SINT32:
46 case _REQUIRED_SINT32:
47 case _OPTIONAL_SINT64:
48 case _REQUIRED_SINT64:
49 case _OPTIONAL_UINT32:
50 case _REQUIRED_UINT32:
51 case _OPTIONAL_UINT64:
52 case _REQUIRED_UINT64:
53 case _OPTIONAL_FIXED32:
54 case _REQUIRED_FIXED32:
55 case _OPTIONAL_FIXED64:
56 case _REQUIRED_FIXED64:
57 case _OPTIONAL_SFIXED32:
58 case _REQUIRED_SFIXED32:
59 case _OPTIONAL_SFIXED64:
60 case _REQUIRED_SFIXED64:
61 return _INT_ZERO;
62 default:
63 return null;
64 }
65 }
66
67 // Closures commonly used by initializers.
68 static String _STRING_EMPTY() => '';
69 static List<int> _BYTES_EMPTY() => new PbList<int>();
70 static bool _BOOL_FALSE() => false;
71 static int _INT_ZERO() => 0;
72 static double _DOUBLE_ZERO() => 0.0;
73
74 static const int _REQUIRED_BIT = 0x1;
75 static const int _REPEATED_BIT = 0x2;
76 static const int _PACKED_BIT = 0x4;
77
78 static const int _BOOL_BIT = 0x10;
79 static const int _BYTES_BIT = 0x20;
80 static const int _STRING_BIT = 0x40;
81 static const int _DOUBLE_BIT = 0x80;
82 static const int _FLOAT_BIT = 0x100;
83 static const int _ENUM_BIT = 0x200;
84 static const int _GROUP_BIT = 0x400;
85 static const int _INT32_BIT = 0x800;
86 static const int _INT64_BIT = 0x1000;
87 static const int _SINT32_BIT = 0x2000;
88 static const int _SINT64_BIT = 0x4000;
89 static const int _UINT32_BIT = 0x8000;
90 static const int _UINT64_BIT = 0x10000;
91 static const int _FIXED32_BIT = 0x20000;
92 static const int _FIXED64_BIT = 0x40000;
93 static const int _SFIXED32_BIT = 0x80000;
94 static const int _SFIXED64_BIT = 0x100000;
95 static const int _MESSAGE_BIT = 0x200000;
96
97 static const int _OPTIONAL_BOOL = _BOOL_BIT;
98 static const int _OPTIONAL_BYTES = _BYTES_BIT;
99 static const int _OPTIONAL_STRING = _STRING_BIT;
100 static const int _OPTIONAL_FLOAT = _FLOAT_BIT;
101 static const int _OPTIONAL_DOUBLE = _DOUBLE_BIT;
102 static const int _OPTIONAL_ENUM = _ENUM_BIT;
103 static const int _OPTIONAL_GROUP = _GROUP_BIT;
104 static const int _OPTIONAL_INT32 = _INT32_BIT;
105 static const int _OPTIONAL_INT64 = _INT64_BIT;
106 static const int _OPTIONAL_SINT32 = _SINT32_BIT;
107 static const int _OPTIONAL_SINT64 = _SINT64_BIT;
108 static const int _OPTIONAL_UINT32 = _UINT32_BIT;
109 static const int _OPTIONAL_UINT64 = _UINT64_BIT;
110 static const int _OPTIONAL_FIXED32 = _FIXED32_BIT;
111 static const int _OPTIONAL_FIXED64 = _FIXED64_BIT;
112 static const int _OPTIONAL_SFIXED32 = _SFIXED32_BIT;
113 static const int _OPTIONAL_SFIXED64 = _SFIXED64_BIT;
114 static const int _OPTIONAL_MESSAGE = _MESSAGE_BIT;
115
116 static const int _REQUIRED_BOOL = _REQUIRED_BIT | _BOOL_BIT;
117 static const int _REQUIRED_BYTES = _REQUIRED_BIT | _BYTES_BIT;
118 static const int _REQUIRED_STRING = _REQUIRED_BIT | _STRING_BIT;
119 static const int _REQUIRED_FLOAT = _REQUIRED_BIT | _FLOAT_BIT;
120 static const int _REQUIRED_DOUBLE = _REQUIRED_BIT | _DOUBLE_BIT;
121 static const int _REQUIRED_ENUM = _REQUIRED_BIT | _ENUM_BIT;
122 static const int _REQUIRED_GROUP = _REQUIRED_BIT | _GROUP_BIT;
123 static const int _REQUIRED_INT32 = _REQUIRED_BIT | _INT32_BIT;
124 static const int _REQUIRED_INT64 = _REQUIRED_BIT | _INT64_BIT;
125 static const int _REQUIRED_SINT32 = _REQUIRED_BIT | _SINT32_BIT;
126 static const int _REQUIRED_SINT64 = _REQUIRED_BIT | _SINT64_BIT;
127 static const int _REQUIRED_UINT32 = _REQUIRED_BIT | _UINT32_BIT;
128 static const int _REQUIRED_UINT64 = _REQUIRED_BIT | _UINT64_BIT;
129 static const int _REQUIRED_FIXED32 = _REQUIRED_BIT | _FIXED32_BIT;
130 static const int _REQUIRED_FIXED64 = _REQUIRED_BIT | _FIXED64_BIT;
131 static const int _REQUIRED_SFIXED32 = _REQUIRED_BIT | _SFIXED32_BIT;
132 static const int _REQUIRED_SFIXED64 = _REQUIRED_BIT | _SFIXED64_BIT;
133 static const int _REQUIRED_MESSAGE = _REQUIRED_BIT | _MESSAGE_BIT;
134
135 static const int _REPEATED_BOOL = _REPEATED_BIT | _BOOL_BIT;
136 static const int _REPEATED_BYTES = _REPEATED_BIT | _BYTES_BIT;
137 static const int _REPEATED_STRING = _REPEATED_BIT | _STRING_BIT;
138 static const int _REPEATED_FLOAT = _REPEATED_BIT | _FLOAT_BIT;
139 static const int _REPEATED_DOUBLE = _REPEATED_BIT | _DOUBLE_BIT;
140 static const int _REPEATED_ENUM = _REPEATED_BIT | _ENUM_BIT;
141 static const int _REPEATED_GROUP = _REPEATED_BIT | _GROUP_BIT;
142 static const int _REPEATED_INT32 = _REPEATED_BIT | _INT32_BIT;
143 static const int _REPEATED_INT64 = _REPEATED_BIT | _INT64_BIT;
144 static const int _REPEATED_SINT32 = _REPEATED_BIT | _SINT32_BIT;
145 static const int _REPEATED_SINT64 = _REPEATED_BIT | _SINT64_BIT;
146 static const int _REPEATED_UINT32 = _REPEATED_BIT | _UINT32_BIT;
147 static const int _REPEATED_UINT64 = _REPEATED_BIT | _UINT64_BIT;
148 static const int _REPEATED_FIXED32 = _REPEATED_BIT | _FIXED32_BIT;
149 static const int _REPEATED_FIXED64 = _REPEATED_BIT | _FIXED64_BIT;
150 static const int _REPEATED_SFIXED32 = _REPEATED_BIT | _SFIXED32_BIT;
151 static const int _REPEATED_SFIXED64 = _REPEATED_BIT | _SFIXED64_BIT;
152 static const int _REPEATED_MESSAGE = _REPEATED_BIT | _MESSAGE_BIT;
153
154 static const int _PACKED_BOOL = _REPEATED_BIT | _PACKED_BIT | _BOOL_BIT;
155 static const int _PACKED_FLOAT = _REPEATED_BIT | _PACKED_BIT | _FLOAT_BIT;
156 static const int _PACKED_DOUBLE = _REPEATED_BIT | _PACKED_BIT | _DOUBLE_BIT;
157 static const int _PACKED_ENUM = _REPEATED_BIT | _PACKED_BIT | _ENUM_BIT;
158 static const int _PACKED_INT32 = _REPEATED_BIT | _PACKED_BIT | _INT32_BIT;
159 static const int _PACKED_INT64 = _REPEATED_BIT | _PACKED_BIT | _INT64_BIT;
160 static const int _PACKED_SINT32 = _REPEATED_BIT | _PACKED_BIT | _SINT32_BIT;
161 static const int _PACKED_SINT64 = _REPEATED_BIT | _PACKED_BIT | _SINT64_BIT;
162 static const int _PACKED_UINT32 = _REPEATED_BIT | _PACKED_BIT | _UINT32_BIT;
163 static const int _PACKED_UINT64 = _REPEATED_BIT | _PACKED_BIT | _UINT64_BIT;
164 static const int _PACKED_FIXED32 = _REPEATED_BIT | _PACKED_BIT | _FIXED32_BIT;
165 static const int _PACKED_FIXED64 = _REPEATED_BIT | _PACKED_BIT | _FIXED64_BIT;
166 static const int _PACKED_SFIXED32 =
167 _REPEATED_BIT | _PACKED_BIT | _SFIXED32_BIT;
168 static const int _PACKED_SFIXED64 =
169 _REPEATED_BIT | _PACKED_BIT | _SFIXED64_BIT;
170
171 // Short names for use in generated code.
172
173 // _O_ptional.
174 static const int OB = _OPTIONAL_BOOL;
175 static const int OY = _OPTIONAL_BYTES;
176 static const int OS = _OPTIONAL_STRING;
177 static const int OF = _OPTIONAL_FLOAT;
178 static const int OD = _OPTIONAL_DOUBLE;
179 static const int OE = _OPTIONAL_ENUM;
180 static const int OG = _OPTIONAL_GROUP;
181 static const int O3 = _OPTIONAL_INT32;
182 static const int O6 = _OPTIONAL_INT64;
183 static const int OS3 = _OPTIONAL_SINT32;
184 static const int OS6 = _OPTIONAL_SINT64;
185 static const int OU3 = _OPTIONAL_UINT32;
186 static const int OU6 = _OPTIONAL_UINT64;
187 static const int OF3 = _OPTIONAL_FIXED32;
188 static const int OF6 = _OPTIONAL_FIXED64;
189 static const int OSF3 = _OPTIONAL_SFIXED32;
190 static const int OSF6 = _OPTIONAL_SFIXED64;
191 static const int OM = _OPTIONAL_MESSAGE;
192
193 // re_Q_uired.
194 static const int QB = _REQUIRED_BOOL;
195 static const int QY = _REQUIRED_BYTES;
196 static const int QS = _REQUIRED_STRING;
197 static const int QF = _REQUIRED_FLOAT;
198 static const int QD = _REQUIRED_DOUBLE;
199 static const int QE = _REQUIRED_ENUM;
200 static const int QG = _REQUIRED_GROUP;
201 static const int Q3 = _REQUIRED_INT32;
202 static const int Q6 = _REQUIRED_INT64;
203 static const int QS3 = _REQUIRED_SINT32;
204 static const int QS6 = _REQUIRED_SINT64;
205 static const int QU3 = _REQUIRED_UINT32;
206 static const int QU6 = _REQUIRED_UINT64;
207 static const int QF3 = _REQUIRED_FIXED32;
208 static const int QF6 = _REQUIRED_FIXED64;
209 static const int QSF3 = _REQUIRED_SFIXED32;
210 static const int QSF6 = _REQUIRED_SFIXED64;
211 static const int QM = _REQUIRED_MESSAGE;
212
213 // re_P_eated.
214 static const int PB = _REPEATED_BOOL;
215 static const int PY = _REPEATED_BYTES;
216 static const int PS = _REPEATED_STRING;
217 static const int PF = _REPEATED_FLOAT;
218 static const int PD = _REPEATED_DOUBLE;
219 static const int PE = _REPEATED_ENUM;
220 static const int PG = _REPEATED_GROUP;
221 static const int P3 = _REPEATED_INT32;
222 static const int P6 = _REPEATED_INT64;
223 static const int PS3 = _REPEATED_SINT32;
224 static const int PS6 = _REPEATED_SINT64;
225 static const int PU3 = _REPEATED_UINT32;
226 static const int PU6 = _REPEATED_UINT64;
227 static const int PF3 = _REPEATED_FIXED32;
228 static const int PF6 = _REPEATED_FIXED64;
229 static const int PSF3 = _REPEATED_SFIXED32;
230 static const int PSF6 = _REPEATED_SFIXED64;
231 static const int PM = _REPEATED_MESSAGE;
232
233 // pac_K_ed.
234 static const int KB = _PACKED_BOOL;
235 static const int KE = _PACKED_ENUM;
236 static const int KF = _PACKED_FLOAT;
237 static const int KD = _PACKED_DOUBLE;
238 static const int K3 = _PACKED_INT32;
239 static const int K6 = _PACKED_INT64;
240 static const int KS3 = _PACKED_SINT32;
241 static const int KS6 = _PACKED_SINT64;
242 static const int KU3 = _PACKED_UINT32;
243 static const int KU6 = _PACKED_UINT64;
244 static const int KF3 = _PACKED_FIXED32;
245 static const int KF6 = _PACKED_FIXED64;
246 static const int KSF3 = _PACKED_SFIXED32;
247 static const int KSF6 = _PACKED_SFIXED64;
248 }
OLDNEW
« no previous file with comments | « lib/src/protobuf/field_info.dart ('k') | lib/src/protobuf/generated_message.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698