OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2014 Google Inc. All rights reserved. | 2 // Copyright 2014 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 name = RSTRING_PTR(method_str); | 144 name = RSTRING_PTR(method_str); |
145 name_len = RSTRING_LEN(method_str); | 145 name_len = RSTRING_LEN(method_str); |
146 setter = false; | 146 setter = false; |
147 | 147 |
148 // Setters have names that end in '='. | 148 // Setters have names that end in '='. |
149 if (name[name_len - 1] == '=') { | 149 if (name[name_len - 1] == '=') { |
150 setter = true; | 150 setter = true; |
151 name_len--; | 151 name_len--; |
152 } | 152 } |
153 | 153 |
154 // Check for a oneof name first. | 154 // See if this name corresponds to either a oneof or field in this message. |
155 o = upb_msgdef_ntoo(self->descriptor->msgdef, | 155 if (!upb_msgdef_lookupname(self->descriptor->msgdef, name, name_len, &f, |
156 name, name_len); | 156 &o)) { |
| 157 return rb_call_super(argc, argv); |
| 158 } |
| 159 |
157 if (o != NULL) { | 160 if (o != NULL) { |
| 161 // This is a oneof -- return which field inside the oneof is set. |
158 if (setter) { | 162 if (setter) { |
159 rb_raise(rb_eRuntimeError, "Oneof accessors are read-only."); | 163 rb_raise(rb_eRuntimeError, "Oneof accessors are read-only."); |
160 } | 164 } |
161 return which_oneof_field(self, o); | 165 return which_oneof_field(self, o); |
162 } | 166 } else { |
163 | 167 // This is a field -- get or set the field's value. |
164 // Otherwise, check for a field with that name. | 168 assert(f); |
165 f = upb_msgdef_ntof(self->descriptor->msgdef, | 169 if (setter) { |
166 name, name_len); | 170 if (argc < 2) { |
167 | 171 rb_raise(rb_eArgError, "No value provided to setter."); |
168 if (f == NULL) { | 172 } |
169 rb_raise(rb_eArgError, "Unknown field"); | 173 layout_set(self->descriptor->layout, Message_data(self), f, argv[1]); |
170 } | 174 return Qnil; |
171 | 175 } else { |
172 if (setter) { | 176 return layout_get(self->descriptor->layout, Message_data(self), f); |
173 if (argc < 2) { | |
174 rb_raise(rb_eArgError, "No value provided to setter."); | |
175 } | 177 } |
176 layout_set(self->descriptor->layout, Message_data(self), f, argv[1]); | |
177 return Qnil; | |
178 } else { | |
179 return layout_get(self->descriptor->layout, Message_data(self), f); | |
180 } | 178 } |
181 } | 179 } |
182 | 180 |
183 int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) { | 181 int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) { |
184 MessageHeader* self; | 182 MessageHeader* self; |
185 VALUE method_str; | 183 VALUE method_str; |
186 char* name; | 184 char* name; |
187 const upb_fielddef* f; | 185 const upb_fielddef* f; |
188 TypedData_Get_Struct(_self, MessageHeader, &Message_type, self); | 186 TypedData_Get_Struct(_self, MessageHeader, &Message_type, self); |
189 | 187 |
190 if (!SYMBOL_P(key)) { | 188 if (!SYMBOL_P(key)) { |
191 rb_raise(rb_eArgError, | 189 rb_raise(rb_eArgError, |
192 "Expected symbols as hash keys in initialization map."); | 190 "Expected symbols as hash keys in initialization map."); |
193 } | 191 } |
194 | 192 |
195 method_str = rb_id2str(SYM2ID(key)); | 193 method_str = rb_id2str(SYM2ID(key)); |
196 name = RSTRING_PTR(method_str); | 194 name = RSTRING_PTR(method_str); |
197 f = upb_msgdef_ntofz(self->descriptor->msgdef, name); | 195 f = upb_msgdef_ntofz(self->descriptor->msgdef, name); |
198 if (f == NULL) { | 196 if (f == NULL) { |
199 rb_raise(rb_eArgError, | 197 rb_raise(rb_eArgError, |
200 "Unknown field name in initialization map entry."); | 198 "Unknown field name '%s' in initialization map entry.", name); |
201 } | 199 } |
202 | 200 |
203 if (is_map_field(f)) { | 201 if (is_map_field(f)) { |
204 VALUE map; | 202 VALUE map; |
205 | 203 |
206 if (TYPE(val) != T_HASH) { | 204 if (TYPE(val) != T_HASH) { |
207 rb_raise(rb_eArgError, | 205 rb_raise(rb_eArgError, |
208 "Expected Hash object as initializer value for map field."); | 206 "Expected Hash object as initializer value for map field '%s'.",
name); |
209 } | 207 } |
210 map = layout_get(self->descriptor->layout, Message_data(self), f); | 208 map = layout_get(self->descriptor->layout, Message_data(self), f); |
211 Map_merge_into_self(map, val); | 209 Map_merge_into_self(map, val); |
212 } else if (upb_fielddef_label(f) == UPB_LABEL_REPEATED) { | 210 } else if (upb_fielddef_label(f) == UPB_LABEL_REPEATED) { |
213 VALUE ary; | 211 VALUE ary; |
214 | 212 |
215 if (TYPE(val) != T_ARRAY) { | 213 if (TYPE(val) != T_ARRAY) { |
216 rb_raise(rb_eArgError, | 214 rb_raise(rb_eArgError, |
217 "Expected array as initializer value for repeated field."); | 215 "Expected array as initializer value for repeated field '%s'.", n
ame); |
218 } | 216 } |
219 ary = layout_get(self->descriptor->layout, Message_data(self), f); | 217 ary = layout_get(self->descriptor->layout, Message_data(self), f); |
220 for (int i = 0; i < RARRAY_LEN(val); i++) { | 218 for (int i = 0; i < RARRAY_LEN(val); i++) { |
221 RepeatedField_push(ary, rb_ary_entry(val, i)); | 219 RepeatedField_push(ary, rb_ary_entry(val, i)); |
222 } | 220 } |
223 } else { | 221 } else { |
224 layout_set(self->descriptor->layout, Message_data(self), f, val); | 222 layout_set(self->descriptor->layout, Message_data(self), f, val); |
225 } | 223 } |
226 return 0; | 224 return 0; |
227 } | 225 } |
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
468 rb_define_method(klass, "==", Message_eq, 1); | 466 rb_define_method(klass, "==", Message_eq, 1); |
469 rb_define_method(klass, "hash", Message_hash, 0); | 467 rb_define_method(klass, "hash", Message_hash, 0); |
470 rb_define_method(klass, "to_h", Message_to_h, 0); | 468 rb_define_method(klass, "to_h", Message_to_h, 0); |
471 rb_define_method(klass, "to_hash", Message_to_h, 0); | 469 rb_define_method(klass, "to_hash", Message_to_h, 0); |
472 rb_define_method(klass, "inspect", Message_inspect, 0); | 470 rb_define_method(klass, "inspect", Message_inspect, 0); |
473 rb_define_method(klass, "[]", Message_index, 1); | 471 rb_define_method(klass, "[]", Message_index, 1); |
474 rb_define_method(klass, "[]=", Message_index_set, 2); | 472 rb_define_method(klass, "[]=", Message_index_set, 2); |
475 rb_define_singleton_method(klass, "decode", Message_decode, 1); | 473 rb_define_singleton_method(klass, "decode", Message_decode, 1); |
476 rb_define_singleton_method(klass, "encode", Message_encode, 1); | 474 rb_define_singleton_method(klass, "encode", Message_encode, 1); |
477 rb_define_singleton_method(klass, "decode_json", Message_decode_json, 1); | 475 rb_define_singleton_method(klass, "decode_json", Message_decode_json, 1); |
478 rb_define_singleton_method(klass, "encode_json", Message_encode_json, 1); | 476 rb_define_singleton_method(klass, "encode_json", Message_encode_json, -1); |
479 rb_define_singleton_method(klass, "descriptor", Message_descriptor, 0); | 477 rb_define_singleton_method(klass, "descriptor", Message_descriptor, 0); |
480 | 478 |
481 return klass; | 479 return klass; |
482 } | 480 } |
483 | 481 |
484 /* | 482 /* |
485 * call-seq: | 483 * call-seq: |
486 * Enum.lookup(number) => name | 484 * Enum.lookup(number) => name |
487 * | 485 * |
488 * This module method, provided on each generated enum module, looks up an enum | 486 * This module method, provided on each generated enum module, looks up an enum |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
571 VALUE Google_Protobuf_deep_copy(VALUE self, VALUE obj) { | 569 VALUE Google_Protobuf_deep_copy(VALUE self, VALUE obj) { |
572 VALUE klass = CLASS_OF(obj); | 570 VALUE klass = CLASS_OF(obj); |
573 if (klass == cRepeatedField) { | 571 if (klass == cRepeatedField) { |
574 return RepeatedField_deep_copy(obj); | 572 return RepeatedField_deep_copy(obj); |
575 } else if (klass == cMap) { | 573 } else if (klass == cMap) { |
576 return Map_deep_copy(obj); | 574 return Map_deep_copy(obj); |
577 } else { | 575 } else { |
578 return Message_deep_copy(obj); | 576 return Message_deep_copy(obj); |
579 } | 577 } |
580 } | 578 } |
OLD | NEW |