Skip to content

Instantly share code, notes, and snippets.

@ismith
Last active August 29, 2015 14:03
Show Gist options
  • Save ismith/b17e9b31d696195169df to your computer and use it in GitHub Desktop.
Save ismith/b17e9b31d696195169df to your computer and use it in GitHub Desktop.
require 'protobuf'
class SampleEnum < ::Protobuf::Enum
define :NEGATIVE, -1
define :ZERO, 0
define :POSITIVE, 1
end
class SampleMessage < ::Protobuf::Message
required SampleEnum, :my_enum, 1
end
def roundtrip(msg)
begin
encoded_msg = msg.encode
decoded_msg = SampleMessage.decode(encoded_msg)
puts "Successful round trip"
rescue StandardError => e
puts "Failed round trip: #{e.message}, #{e.backtrace}"
end
end
if __FILE__ == $0
puts "About to round trip a message with an enum with a positive value:"
roundtrip(SampleMessage.new(:my_enum => SampleEnum::POSITIVE))
puts "About to round trip a message with an enum with a zero value:"
roundtrip(SampleMessage.new(:my_enum => SampleEnum::ZERO))
puts "About to round trip a message with an enum with a negative value:"
roundtrip(SampleMessage.new(:my_enum => SampleEnum::NEGATIVE))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment