Created
December 18, 2014 08:40
-
-
Save rjnienaber/bf43a677e374504799ab to your computer and use it in GitHub Desktop.
Example protocol buffer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'protocol_buffers' | |
class User < ProtocolBuffers::Message | |
required :string, :name, 1 | |
required :string, :email, 2 | |
optional :int32, :logins, 3 | |
end | |
class Group < ProtocolBuffers::Message | |
repeated User, :users, 1 | |
repeated Group, :subgroups, 2 | |
module GroupType | |
include ProtocolBuffers::Enum | |
Study = 1 | |
Play = 2 | |
end | |
optional GroupType, :group_type, 3 | |
end | |
user = User.new(name: 'Richard', email: 'haha@email.com', logins: 1) | |
group = Group.new(:group_type => Group::GroupType::Play) | |
group.users << user | |
serialized = group.serialize_to_string | |
# "\n\e\n\aRichard\x12\x0Ehaha@email.com\x18\x01\x18\x02" | |
puts "SERIALIZED: #{serialized.inspect}" | |
deserialized = Group.parse(serialized) | |
#<Group users=[#<User name="Richard" email="haha@email.com" logins=1>] subgroups=[] group_type=Play(2)> | |
puts "DESERIALIZED: #{deserialized.inspect}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment