diff options
author | 2017-01-27 13:17:54 -0800 | |
---|---|---|
committer | 2017-01-27 13:17:54 -0800 | |
commit | a323f1e65da2c512f971a2edf1918a0cca340015 (patch) | |
tree | 22ae538979fd53567f144a112b069ae2706a03e0 /php/ext/google/protobuf/storage.c | |
parent | 5af0b547de7aba6d6206943546262cb21fedc721 (diff) |
Oneof accessor should return the field name that is actually set. (#2631)
Diffstat (limited to 'php/ext/google/protobuf/storage.c')
-rw-r--r-- | php/ext/google/protobuf/storage.c | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/php/ext/google/protobuf/storage.c b/php/ext/google/protobuf/storage.c index 8a2b3a22..42bcce59 100644 --- a/php/ext/google/protobuf/storage.c +++ b/php/ext/google/protobuf/storage.c @@ -527,7 +527,7 @@ zval* layout_get(MessageLayout* layout, const void* storage, } void layout_set(MessageLayout* layout, MessageHeader* header, - const upb_fielddef* field, zval* val TSRMLS_DC) { + const upb_fielddef* field, zval* val TSRMLS_DC) { void* storage = message_data(header); void* memory = slot_memory(layout, storage, field); uint32_t* oneof_case = slot_oneof_case(layout, storage, field); @@ -582,3 +582,22 @@ void layout_set(MessageLayout* layout, MessageHeader* header, native_slot_set(type, ce, value_memory(field, memory), val TSRMLS_CC); } } + +const char* layout_get_oneof_case(MessageLayout* layout, const void* storage, + const upb_oneofdef* oneof TSRMLS_DC) { + upb_oneof_iter i; + const upb_fielddef* first_field; + + // Oneof is guaranteed to have at least one field. Get the first field. + for(upb_oneof_begin(&i, oneof); !upb_oneof_done(&i); upb_oneof_next(&i)) { + first_field = upb_oneof_iter_field(&i); + break; + } + + uint32_t* oneof_case = slot_oneof_case(layout, storage, first_field); + if (*oneof_case == 0) { + return ""; + } + const upb_fielddef* field = upb_oneofdef_itof(oneof, *oneof_case); + return upb_fielddef_name(field); +} |