Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
P
protobuf
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
protobuf
Commits
545527e8
Commit
545527e8
authored
May 18, 2016
by
Josh Haberman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Ruby oneofs: return default instead of nil for unset fields.
parent
32e3d7a1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
20 deletions
+42
-20
storage.c
ruby/ext/google/protobuf_c/storage.c
+23
-1
basic.rb
ruby/tests/basic.rb
+19
-19
No files found.
ruby/ext/google/protobuf_c/storage.c
View file @
545527e8
...
...
@@ -57,6 +57,28 @@ size_t native_slot_size(upb_fieldtype_t type) {
}
}
VALUE
value_from_default
(
const
upb_fielddef
*
field
)
{
switch
(
upb_fielddef_type
(
field
))
{
case
UPB_TYPE_FLOAT
:
return
DBL2NUM
(
upb_fielddef_defaultfloat
(
field
));
case
UPB_TYPE_DOUBLE
:
return
DBL2NUM
(
upb_fielddef_defaultdouble
(
field
));
case
UPB_TYPE_BOOL
:
return
upb_fielddef_defaultbool
(
field
)
?
Qtrue
:
Qfalse
;
case
UPB_TYPE_MESSAGE
:
return
Qnil
;
case
UPB_TYPE_ENUM
:
return
INT2NUM
(
upb_fielddef_defaultint32
(
field
));
case
UPB_TYPE_INT32
:
return
INT2NUM
(
upb_fielddef_defaultint32
(
field
));
case
UPB_TYPE_INT64
:
return
LL2NUM
(
upb_fielddef_defaultint64
(
field
));;
case
UPB_TYPE_UINT32
:
return
UINT2NUM
(
upb_fielddef_defaultuint32
(
field
));
case
UPB_TYPE_UINT64
:
return
ULL2NUM
(
upb_fielddef_defaultuint64
(
field
));
case
UPB_TYPE_STRING
:
case
UPB_TYPE_BYTES
:
{
size_t
size
;
const
char
*
str
=
upb_fielddef_defaultstr
(
field
,
&
size
);
return
rb_str_new
(
str
,
size
);
}
default:
return
Qnil
;
}
}
static
bool
is_ruby_num
(
VALUE
value
)
{
return
(
TYPE
(
value
)
==
T_FLOAT
||
TYPE
(
value
)
==
T_FIXNUM
||
...
...
@@ -537,7 +559,7 @@ VALUE layout_get(MessageLayout* layout,
if
(
upb_fielddef_containingoneof
(
field
))
{
if
(
*
oneof_case
!=
upb_fielddef_number
(
field
))
{
return
Qnil
;
return
value_from_default
(
field
)
;
}
return
native_slot_get
(
upb_fielddef_type
(
field
),
field_type_class
(
field
),
...
...
ruby/tests/basic.rb
View file @
545527e8
...
...
@@ -703,36 +703,36 @@ module BasicTest
def
test_oneof
d
=
OneofMessage
.
new
assert
d
.
a
==
nil
assert
d
.
b
==
nil
assert
d
.
a
==
""
assert
d
.
b
==
0
assert
d
.
c
==
nil
assert
d
.
d
==
nil
assert
d
.
d
==
0
assert
d
.
my_oneof
==
nil
d
.
a
=
"hi"
assert
d
.
a
==
"hi"
assert
d
.
b
==
nil
assert
d
.
b
==
0
assert
d
.
c
==
nil
assert
d
.
d
==
nil
assert
d
.
d
==
0
assert
d
.
my_oneof
==
:a
d
.
b
=
42
assert
d
.
a
==
nil
assert
d
.
a
==
""
assert
d
.
b
==
42
assert
d
.
c
==
nil
assert
d
.
d
==
nil
assert
d
.
d
==
0
assert
d
.
my_oneof
==
:b
d
.
c
=
TestMessage2
.
new
(
:foo
=>
100
)
assert
d
.
a
==
nil
assert
d
.
b
==
nil
assert
d
.
a
==
""
assert
d
.
b
==
0
assert
d
.
c
.
foo
==
100
assert
d
.
d
==
nil
assert
d
.
d
==
0
assert
d
.
my_oneof
==
:c
d
.
d
=
:C
assert
d
.
a
==
nil
assert
d
.
b
==
nil
assert
d
.
a
==
""
assert
d
.
b
==
0
assert
d
.
c
==
nil
assert
d
.
d
==
:C
assert
d
.
my_oneof
==
:d
...
...
@@ -748,23 +748,23 @@ module BasicTest
d3
=
OneofMessage
.
decode
(
encoded_field_c
+
encoded_field_a
+
encoded_field_d
)
assert
d3
.
a
==
nil
assert
d3
.
b
==
nil
assert
d3
.
a
==
""
assert
d3
.
b
==
0
assert
d3
.
c
==
nil
assert
d3
.
d
==
:B
d4
=
OneofMessage
.
decode
(
encoded_field_c
+
encoded_field_a
+
encoded_field_d
+
encoded_field_c
)
assert
d4
.
a
==
nil
assert
d4
.
b
==
nil
assert
d4
.
a
==
""
assert
d4
.
b
==
0
assert
d4
.
c
.
foo
==
1
assert
d4
.
d
==
nil
assert
d4
.
d
==
0
d5
=
OneofMessage
.
new
(
:a
=>
"hello"
)
assert
d5
.
a
!=
nil
assert
d5
.
a
==
"hello"
d5
.
a
=
nil
assert
d5
.
a
==
nil
assert
d5
.
a
==
""
assert
OneofMessage
.
encode
(
d5
)
==
''
assert
d5
.
my_oneof
==
nil
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment