Skip to content

Instantly share code, notes, and snippets.

@film42
Created September 15, 2015 03:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save film42/694611999ab72065b9ed to your computer and use it in GitHub Desktop.
Save film42/694611999ab72065b9ed to your computer and use it in GitHub Desktop.
class EnumDeliveryType < ::Protobuf::Enum
define :STANDARD, 1
define :PRIORITY, 2
define :NEXT_DAY, 3
define :OVERNIGHT, 4
end
# # # # # # # # # # # # # PART 1 # # # # # # # # # # # # #
##
# ENCODE
#
irb(main):008:0> s = Dora::Warehouse::Shipment.new
#=> #<Dora::Warehouse::Shipment ... delivery_type=#<Protobuf::Enum(Dora::Warehouse::EnumDeliveryType)::STANDARD=1>>
irb(main):010:0> s.delivery_type = 4
#=> #<Dora::Warehouse::Shipment ... delivery_type=#<Protobuf::Enum(Dora::Warehouse::EnumDeliveryType)::OVERNIGHT=4>>
irb(main):014:0> s.encode
#=> "(\x04"
irb(main):027:0> s.delivery_type = 5
####################################
# NOTE: This breaks here
####################################
# TypeError: Invalid Enum value: 5 for delivery_type
# from /Users/garrettthornburg/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/protobuf-3.5.1/lib/protobuf/field/enum_field.rb:50:in `block (2 levels) in define_setter'
# from (irb):27
# from /Users/garrettthornburg/.rbenv/versions/2.2.2/bin/irb:11:in `<main>'
# # # # # # # # # # # # # PART 2 # # # # # # # # # # # # #
##
# DECODE
#
# From above we know that 4 is a valid enum
irb(main):029:0* Dora::Warehouse::Shipment.decode("(\x04")
#=> #<Dora::Warehouse::Shipment ... delivery_type=#<Protobuf::Enum(Dora::Warehouse::EnumDeliveryType)::OVERNIGHT=4>>
# What about hard coding to 5?
irb(main):030:0* Dora::Warehouse::Shipment.decode("(\x05")
###############################################
# NOTE: The enum is thrown away and 1 is chosen
###############################################
#=> #<Dora::Warehouse::Shipment ... delivery_type=#<Protobuf::Enum(Dora::Warehouse::EnumDeliveryType)::STANDARD=1>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment