OLD | NEW |
---|---|
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of protoc; | 5 part of protoc; |
6 | 6 |
7 const String SP = ' '; | 7 const String SP = ' '; |
8 | 8 |
9 class MessageGenerator extends ProtobufContainer { | 9 class MessageGenerator extends ProtobufContainer { |
10 // List of Dart language reserved words in names which cannot be used in a | 10 // List of Dart language reserved words in names which cannot be used in a |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
98 ';', () { | 98 ';', () { |
99 for (ProtobufField field in _fieldList) { | 99 for (ProtobufField field in _fieldList) { |
100 String type = field.shortTypeName; | 100 String type = field.shortTypeName; |
101 String fieldType = field.baseTypeForPackage(package); | 101 String fieldType = field.baseTypeForPackage(package); |
102 | 102 |
103 String makeDefault = null; | 103 String makeDefault = null; |
104 if (field.hasInitialization) { | 104 if (field.hasInitialization) { |
105 makeDefault = field.initializationForPackage(package); | 105 makeDefault = field.initializationForPackage(package); |
106 } | 106 } |
107 String subBuilder = null; | 107 String subBuilder = null; |
108 if (field.message || field.group) { | 108 String subBuilderRepeated = null; |
109 if (field.message) { | |
110 subBuilder = '${fieldType}.create'; | |
111 subBuilderRepeated = '${fieldType}.createRepeated'; | |
112 } else if (field.group) { | |
Siggi Cherem (dart-lang)
2014/12/19 21:53:14
after chatting about it - seems we can do the same
sra1
2014/12/19 23:11:02
Done.
| |
109 subBuilder = '()${SP}=>${SP}new ${fieldType}()'; | 113 subBuilder = '()${SP}=>${SP}new ${fieldType}()'; |
114 subBuilderRepeated = '()${SP}=>${SP}new PbList<${fieldType}>()'; | |
Siggi Cherem (dart-lang)
2014/12/19 21:53:14
not for this CL, but I'll ask since I'm thinking a
Søren Gjesse
2015/01/05 16:00:07
I don't see any reason for using ${SP} when genera
| |
110 } | 115 } |
111 String valueOf = null; | 116 String valueOf = null; |
112 if (field.enm) { | 117 if (field.enm) { |
113 valueOf = '(var v)${SP}=>${SP}${fieldType}.valueOf(v)'; | 118 valueOf = '(var v)${SP}=>${SP}${fieldType}.valueOf(v)'; |
114 } | 119 } |
115 if ('PM' == type) { | 120 if ('PM' == type) { |
116 // Repeated message: default is an empty list | 121 // Repeated message: default is an empty list |
117 out.println('..m(${field.number},${SP}' | 122 out.println('..m(${field.number},${SP}' |
118 '\'${field.externalFieldName}\',${SP}$subBuilder,' | 123 '\'${field.externalFieldName}\',${SP}$subBuilder,' |
119 '${SP}()${SP}=>${SP}new PbList<${fieldType}>())'); | 124 '${SP}$subBuilderRepeated)'); |
120 } else if (type[0] == 'P' && type != 'PG' && type != 'PE') { | 125 } else if (type[0] == 'P' && type != 'PG' && type != 'PE') { |
121 // Repeated, not a message or enum: default is an empty list, | 126 // Repeated, not a message or enum: default is an empty list, |
122 // subBuilder is null, valueOf is null. | 127 // subBuilder is null, valueOf is null. |
123 out.println('..p(${field.number},${SP}' | 128 out.println('..p(${field.number},${SP}' |
124 '\'${field.externalFieldName}\',${SP}GeneratedMessage.$type)'); | 129 '\'${field.externalFieldName}\',${SP}GeneratedMessage.$type)'); |
125 } else if (type == 'OE' || type == 'QE') { | 130 } else if (type == 'OE' || type == 'QE') { |
126 out.println('..e(${field.number},${SP}' | 131 out.println('..e(${field.number},${SP}' |
127 '\'${field.externalFieldName}\',${SP}GeneratedMessage.$type,' | 132 '\'${field.externalFieldName}\',${SP}GeneratedMessage.$type,' |
128 '${SP}$makeDefault,${SP}$valueOf)'); | 133 '${SP}$makeDefault,${SP}$valueOf)'); |
129 } else { | 134 } else { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
165 '${SP}[ExtensionRegistry r = ExtensionRegistry.EMPTY])' | 170 '${SP}[ExtensionRegistry r = ExtensionRegistry.EMPTY])' |
166 '${SP}:${SP}super.fromBuffer(i,${SP}r);'); | 171 '${SP}:${SP}super.fromBuffer(i,${SP}r);'); |
167 out.println('${classname}.fromJson(String i,' | 172 out.println('${classname}.fromJson(String i,' |
168 '${SP}[ExtensionRegistry r = ExtensionRegistry.EMPTY])' | 173 '${SP}[ExtensionRegistry r = ExtensionRegistry.EMPTY])' |
169 '${SP}:${SP}super.fromJson(i,${SP}r);'); | 174 '${SP}:${SP}super.fromJson(i,${SP}r);'); |
170 out.println('${classname} clone()${SP}=>' | 175 out.println('${classname} clone()${SP}=>' |
171 '${SP}new ${classname}()..mergeFromMessage(this);'); | 176 '${SP}new ${classname}()..mergeFromMessage(this);'); |
172 | 177 |
173 out.println('BuilderInfo get info_${SP}=>${SP}_i;'); | 178 out.println('BuilderInfo get info_${SP}=>${SP}_i;'); |
174 | 179 |
180 // Factory functions which can be used as default value closures. | |
181 out.println('static ${classname}${SP}create()${SP}=>' | |
182 '${SP}new ${classname}();'); | |
183 out.println('static PbList<${classname}>${SP}createRepeated()${SP}=>' | |
184 '${SP}new PbList<${classname}>();'); | |
185 | |
175 generateFieldsAccessorsMutators(out); | 186 generateFieldsAccessorsMutators(out); |
176 }); | 187 }); |
177 out.println(); | 188 out.println(); |
178 } | 189 } |
179 | 190 |
180 // Returns true if the message type has any required fields. If it doesn't, | 191 // Returns true if the message type has any required fields. If it doesn't, |
181 // we can optimize out calls to its isInitialized()/_findInvalidFields() | 192 // we can optimize out calls to its isInitialized()/_findInvalidFields() |
182 // methods. | 193 // methods. |
183 // | 194 // |
184 // already_seen is used to avoid checking the same type multiple times | 195 // already_seen is used to avoid checking the same type multiple times |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
250 '(${fieldTypeString} v)${SP}' | 261 '(${fieldTypeString} v)${SP}' |
251 '{${SP}setField(${field.number},${SP}v);${SP}}'); | 262 '{${SP}setField(${field.number},${SP}v);${SP}}'); |
252 out.println('bool $hasIdentifier()${SP}=>' | 263 out.println('bool $hasIdentifier()${SP}=>' |
253 '${SP}hasField(${field.number});'); | 264 '${SP}hasField(${field.number});'); |
254 out.println('void $clearIdentifier()${SP}=>' | 265 out.println('void $clearIdentifier()${SP}=>' |
255 '${SP}clearField(${field.number});'); | 266 '${SP}clearField(${field.number});'); |
256 } | 267 } |
257 } | 268 } |
258 } | 269 } |
259 } | 270 } |
OLD | NEW |