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
e00266a7
Unverified
Commit
e00266a7
authored
Jul 02, 2018
by
Joshua Haberman
Committed by
GitHub
Jul 02, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #3847 from FX-HAO/master
Google::Protobuf::Struct can access a missing key (#3846)
parents
e0579ac6
8695997e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
12 additions
and
0 deletions
+12
-0
well_known_types.rb
ruby/lib/google/protobuf/well_known_types.rb
+6
-0
well_known_types_test.rb
ruby/tests/well_known_types_test.rb
+6
-0
No files found.
ruby/lib/google/protobuf/well_known_types.rb
View file @
e00266a7
...
...
@@ -155,6 +155,8 @@ module Google
Struct
.
class_eval
do
def
[]
(
key
)
self
.
fields
[
key
].
to_ruby
rescue
NoMethodError
nil
end
def
[]=
(
key
,
value
)
...
...
@@ -176,6 +178,10 @@ module Google
hash
.
each
{
|
key
,
val
|
ret
[
key
]
=
val
}
ret
end
def
has_key?
(
key
)
self
.
fields
.
has_key?
(
key
)
end
end
ListValue
.
class_eval
do
...
...
ruby/tests/well_known_types_test.rb
View file @
e00266a7
...
...
@@ -60,6 +60,9 @@ class TestWellKnownTypes < Test::Unit::TestCase
assert_equal
(
Google
::
Protobuf
::
ListValue
.
from_a
(
sublist
),
struct
[
"sublist"
])
assert_equal
true
,
struct
.
has_key?
(
"null"
)
assert_equal
false
,
struct
.
has_key?
(
"missing_key"
)
should_equal
=
{
"number"
=>
12345
,
"boolean-true"
=>
true
,
...
...
@@ -82,6 +85,9 @@ class TestWellKnownTypes < Test::Unit::TestCase
# to_h returns a fully-flattened Ruby structure (Hash and Array).
assert_equal
(
should_equal
,
struct
.
to_h
)
# Test that we can safely access a missing key
assert_equal
(
nil
,
struct
[
"missing_key"
])
# Test that we can assign Struct and ListValue directly.
struct
[
"substruct"
]
=
Google
::
Protobuf
::
Struct
.
from_hash
(
substruct
)
struct
[
"sublist"
]
=
Google
::
Protobuf
::
ListValue
.
from_a
(
sublist
)
...
...
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