Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support hashes for struct initializers #5716

Merged
merged 4 commits into from Jul 25, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions ruby/ext/google/protobuf_c/map.c
Expand Up @@ -395,6 +395,11 @@ VALUE Map_index_set(VALUE _self, VALUE key, VALUE value) {
void* mem;
key = table_key(self, key, keybuf, &keyval, &length);

if (TYPE(value) == T_HASH) {
VALUE args[1] = { value };
value = rb_class_new_instance(1, args, self->value_type_class);
}

mem = value_memory(&v);
native_slot_set("", self->value_type, self->value_type_class, mem, value);

Expand Down
27 changes: 27 additions & 0 deletions ruby/tests/well_known_types_test.rb
Expand Up @@ -139,4 +139,31 @@ def test_any
assert any.is(Google::Protobuf::Timestamp)
assert_equal ts, any.unpack(Google::Protobuf::Timestamp)
end

def test_struct_assign
s = Google::Protobuf::Struct.new(fields: {'a' => Google::Protobuf::Value.new({number_value: 4.4})})
assert_equal 4.4, s['a']

s = Google::Protobuf::Struct.new(fields: {'a' => {number_value: 2.2}})
jbolinger marked this conversation as resolved.
Show resolved Hide resolved
assert_equal 2.2, s['a']
jbolinger marked this conversation as resolved.
Show resolved Hide resolved
end

def test_struct_nested_assign
s = Google::Protobuf::Struct.new(
fields: {
'a' => {string_value: 'A'},
'b' => {struct_value: {
fields: {
'x' => {list_value: {values: [{number_value: 1.0}, {string_value: "ok"}]}},
'y' => {bool_value: true}}}
},
'c' => {struct_value: {}}
}
)
assert_equal 'A', s['a']
expected_b_x = [Google::Protobuf::Value.new(number_value: 1.0), Google::Protobuf::Value.new(string_value: "ok")]
assert_equal expected_b_x, s['b']['x'].values
assert_equal true, s['b']['y']
jbolinger marked this conversation as resolved.
Show resolved Hide resolved
assert_equal Google::Protobuf::Struct.new, s['c']
end
end