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
3cec2ea8
Commit
3cec2ea8
authored
Aug 01, 2016
by
Josh Haberman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Ruby: added custom Struct exception type and fixed Makefile.am.
parent
a207a2bd
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
42 additions
and
11 deletions
+42
-11
Makefile.am
Makefile.am
+1
-0
well_known_types.rb
ruby/lib/google/protobuf/well_known_types.rb
+17
-11
well_known_types_test.rb
ruby/tests/well_known_types_test.rb
+24
-0
No files found.
Makefile.am
View file @
3cec2ea8
...
...
@@ -681,6 +681,7 @@ ruby_EXTRA_DIST= \
ruby/google-protobuf.gemspec
\
ruby/lib/google/protobuf/message_exts.rb
\
ruby/lib/google/protobuf/repeated_field.rb
\
ruby/lib/google/protobuf/well_known_types.rb
\
ruby/lib/google/protobuf.rb
\
ruby/pom.xml
\
ruby/src/main/java/com/google/protobuf/jruby/RubyBuilder.java
\
...
...
ruby/lib/google/protobuf/well_known_types.rb
View file @
3cec2ea8
...
...
@@ -90,6 +90,8 @@ module Google
end
end
class
UnexpectedStructType
<
Google
::
Protobuf
::
Error
;
end
Value
.
class_eval
do
def
to_ruby
(
recursive
=
false
)
case
self
.
kind
...
...
@@ -114,31 +116,32 @@ module Google
when
:bool_value
self
.
bool_value
else
raise
"Value not set"
raise
UnexpectedStructType
end
end
def
from_ruby
(
value
)
if
value
.
nil?
case
value
when
NilClass
self
.
null_value
=
0
elsif
value
.
is_a?
(
Numeric
)
when
Numeric
self
.
number_value
=
value
elsif
value
.
is_a?
(
String
)
when
String
self
.
string_value
=
value
elsif
value
.
is_a?
(
TrueClass
)
when
TrueClass
self
.
bool_value
=
true
elsif
value
.
is_a?
(
FalseClass
)
when
FalseClass
self
.
bool_value
=
false
elsif
value
.
is_a?
(
Struct
)
when
Struct
self
.
struct_value
=
value
elsif
value
.
is_a?
(
Hash
)
when
Hash
self
.
struct_value
=
Struct
.
from_hash
(
value
)
elsif
value
.
is_a?
(
ListValue
)
when
ListValue
self
.
list_value
=
value
elsif
value
.
is_a?
(
Array
)
when
Array
self
.
list_value
=
ListValue
.
from_a
(
value
)
else
raise
"Unexpected type"
raise
UnexpectedStructType
end
end
end
...
...
@@ -149,6 +152,9 @@ module Google
end
def
[]=
(
key
,
value
)
unless
key
.
is_a?
(
String
)
raise
UnexpectedStructType
,
"Struct keys must be strings."
end
self
.
fields
[
key
]
||=
Google
::
Protobuf
::
Value
.
new
self
.
fields
[
key
].
from_ruby
(
value
)
end
...
...
ruby/tests/well_known_types_test.rb
View file @
3cec2ea8
...
...
@@ -85,6 +85,30 @@ class TestWellKnownTypes < Test::Unit::TestCase
assert_equal
(
should_equal
,
struct
.
to_h
)
assert_equal
(
should_equal
[
"sublist"
].
length
,
struct
[
"sublist"
].
length
)
assert_raise
Google
::
Protobuf
::
UnexpectedStructType
do
struct
[
123
]
=
5
end
assert_raise
Google
::
Protobuf
::
UnexpectedStructType
do
struct
[
5
]
=
Time
.
new
end
assert_raise
Google
::
Protobuf
::
UnexpectedStructType
do
struct
[
5
]
=
[
Time
.
new
]
end
assert_raise
Google
::
Protobuf
::
UnexpectedStructType
do
struct
[
5
]
=
{
123
=>
456
}
end
assert_raise
Google
::
Protobuf
::
UnexpectedStructType
do
struct
=
Google
::
Protobuf
::
Struct
.
new
struct
.
fields
[
"foo"
]
=
Google
::
Protobuf
::
Value
.
new
# Tries to return a Ruby value for a Value class whose type
# hasn't been filled in.
struct
[
"foo"
]
end
end
def
test_any
...
...
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