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 String subBuilderRepeated = null; |
108 if (field.message || field.group) { | 109 if (field.message || field.group) { |
109 subBuilder = '()${SP}=>${SP}new ${fieldType}()'; | 110 subBuilder = '${fieldType}.create'; |
| 111 subBuilderRepeated = '${fieldType}.createRepeated'; |
110 } | 112 } |
111 String valueOf = null; | 113 String valueOf = null; |
112 if (field.enm) { | 114 if (field.enm) { |
113 valueOf = '(var v)${SP}=>${SP}${fieldType}.valueOf(v)'; | 115 valueOf = '(var v)${SP}=>${SP}${fieldType}.valueOf(v)'; |
114 } | 116 } |
115 if ('PM' == type) { | 117 if ('PM' == type) { |
116 // Repeated message: default is an empty list | 118 // Repeated message: default is an empty list |
117 out.println('..m(${field.number},${SP}' | 119 out.println('..m(${field.number},${SP}' |
118 '\'${field.externalFieldName}\',${SP}$subBuilder,' | 120 '\'${field.externalFieldName}\',${SP}$subBuilder,' |
119 '${SP}()${SP}=>${SP}new PbList<${fieldType}>())'); | 121 '${SP}$subBuilderRepeated)'); |
120 } else if (type[0] == 'P' && type != 'PG' && type != 'PE') { | 122 } else if (type[0] == 'P' && type != 'PG' && type != 'PE') { |
121 // Repeated, not a message or enum: default is an empty list, | 123 // Repeated, not a message or enum: default is an empty list, |
122 // subBuilder is null, valueOf is null. | 124 // subBuilder is null, valueOf is null. |
123 out.println('..p(${field.number},${SP}' | 125 out.println('..p(${field.number},${SP}' |
124 '\'${field.externalFieldName}\',${SP}GeneratedMessage.$type)'); | 126 '\'${field.externalFieldName}\',${SP}GeneratedMessage.$type)'); |
125 } else if (type == 'OE' || type == 'QE') { | 127 } else if (type == 'OE' || type == 'QE') { |
126 out.println('..e(${field.number},${SP}' | 128 out.println('..e(${field.number},${SP}' |
127 '\'${field.externalFieldName}\',${SP}GeneratedMessage.$type,' | 129 '\'${field.externalFieldName}\',${SP}GeneratedMessage.$type,' |
128 '${SP}$makeDefault,${SP}$valueOf)'); | 130 '${SP}$makeDefault,${SP}$valueOf)'); |
129 } else { | 131 } else { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 '${SP}[ExtensionRegistry r = ExtensionRegistry.EMPTY])' | 167 '${SP}[ExtensionRegistry r = ExtensionRegistry.EMPTY])' |
166 '${SP}:${SP}super.fromBuffer(i,${SP}r);'); | 168 '${SP}:${SP}super.fromBuffer(i,${SP}r);'); |
167 out.println('${classname}.fromJson(String i,' | 169 out.println('${classname}.fromJson(String i,' |
168 '${SP}[ExtensionRegistry r = ExtensionRegistry.EMPTY])' | 170 '${SP}[ExtensionRegistry r = ExtensionRegistry.EMPTY])' |
169 '${SP}:${SP}super.fromJson(i,${SP}r);'); | 171 '${SP}:${SP}super.fromJson(i,${SP}r);'); |
170 out.println('${classname} clone()${SP}=>' | 172 out.println('${classname} clone()${SP}=>' |
171 '${SP}new ${classname}()..mergeFromMessage(this);'); | 173 '${SP}new ${classname}()..mergeFromMessage(this);'); |
172 | 174 |
173 out.println('BuilderInfo get info_${SP}=>${SP}_i;'); | 175 out.println('BuilderInfo get info_${SP}=>${SP}_i;'); |
174 | 176 |
| 177 // Factory functions which can be used as default value closures. |
| 178 out.println('static ${classname}${SP}create()${SP}=>' |
| 179 '${SP}new ${classname}();'); |
| 180 out.println('static PbList<${classname}>${SP}createRepeated()${SP}=>' |
| 181 '${SP}new PbList<${classname}>();'); |
| 182 |
175 generateFieldsAccessorsMutators(out); | 183 generateFieldsAccessorsMutators(out); |
176 }); | 184 }); |
177 out.println(); | 185 out.println(); |
178 } | 186 } |
179 | 187 |
180 // Returns true if the message type has any required fields. If it doesn't, | 188 // Returns true if the message type has any required fields. If it doesn't, |
181 // we can optimize out calls to its isInitialized()/_findInvalidFields() | 189 // we can optimize out calls to its isInitialized()/_findInvalidFields() |
182 // methods. | 190 // methods. |
183 // | 191 // |
184 // already_seen is used to avoid checking the same type multiple times | 192 // 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}' | 258 '(${fieldTypeString} v)${SP}' |
251 '{${SP}setField(${field.number},${SP}v);${SP}}'); | 259 '{${SP}setField(${field.number},${SP}v);${SP}}'); |
252 out.println('bool $hasIdentifier()${SP}=>' | 260 out.println('bool $hasIdentifier()${SP}=>' |
253 '${SP}hasField(${field.number});'); | 261 '${SP}hasField(${field.number});'); |
254 out.println('void $clearIdentifier()${SP}=>' | 262 out.println('void $clearIdentifier()${SP}=>' |
255 '${SP}clearField(${field.number});'); | 263 '${SP}clearField(${field.number});'); |
256 } | 264 } |
257 } | 265 } |
258 } | 266 } |
259 } | 267 } |
OLD | NEW |