Search code examples
rubyprotocol-buffers

Getting enum values from compiled protobuffer message in Ruby


I have a compiled Ruby protobuf message like this:

  require 'google/protobuf'

  Google::Protobuf::DescriptorPool.generated_pool.build do
    add_message "PingPacket" do
      optional :message_counter, :int32, 1
      optional :message_type, :enum, 2, "PingPacket.MessageType"
    end
    add_enum "PingPacket.MessageType" do
      value :REPORT, 0
      value :LOW_BATTERY, 1
      value :LOCATE_REQUEST, 2
      value :CHECK_IN, 3
      value :SOS, 4
      value :RESTING, 5
      value :MOVING, 6
      value :EVENT, 7
      value :SYSTEM_TEST, 8
    end
  end

  PingPacket = Google::Protobuf::DescriptorPool.generated_pool.lookup("PingPacket").msgclass
  PingPacket::MessageType = Google::Protobuf::DescriptorPool.generated_pool.lookup("PingPacket.MessageType").enummodule

and Im trying to get an array with all the MessageType values. I've tried the obvious:

PingPacket::MessageType.enums
PingPacket::MessageType.values
PingPacket::MessageType.to_s

But nothing works. How can I get those values?


Solution

  • I like to inspect things with Pry, if I load the code in the pry console I get:

    1) Your class is a Module

    [2] pry(main)> PingPacket::MessageType.class
    => Module
    

    If I go inside the class I get:

    [4] pry(main)> cd PingPacket::MessageType
    [5] pry(PingPacket::MessageType):1> ls
    constants: 
      CHECK_IN  LOCATE_REQUEST  MOVING  RESTING  SYSTEM_TEST
      EVENT     LOW_BATTERY     REPORT  SOS    
    PingPacket::MessageType.methods: descriptor  lookup  resolve
    locals: _  __  _dir_  _ex_  _file_  _in_  _out_  _pry_
    

    Then I can inspect all the constants:

    [6] pry(PingPacket::MessageType):1> constants
    => [:CHECK_IN,
     :SOS,
     :RESTING,
     :MOVING,
     :EVENT,
     :SYSTEM_TEST,
     :REPORT,
     :LOW_BATTERY,
     :LOCATE_REQUEST]
    

    Finally I can get, the constants value form a module with this trick:

    [9] pry(PingPacket::MessageType):1> constants(false).map &method(:const_get)
    => [3, 4, 5, 6, 7, 8, 0, 1, 2]
    

    So this will do the trick

    [12] pry(main)> PingPacket::MessageType.constants(false).map &PingPacket::MessageType.method(:const_get)
    => [3, 4, 5, 6, 7, 8, 0, 1, 2]
    

    also ypu can see that has three methods, that work as follow:

    [31] pry(PingPacket::MessageType):1> resolve :CHECK_IN
    => 3
    [33] pry(PingPacket::MessageType):1> lookup 3
    => :CHECK_IN
    [37] pry(PingPacket::MessageType):1> descriptor.each do |i|
    [37] pry(PingPacket::MessageType):1* puts i
    [37] pry(PingPacket::MessageType):1* end
    LOCATE_REQUEST
    SOS
    SYSTEM_TEST
    LOW_BATTERY
    EVENT
    CHECK_IN
    RESTING
    MOVING
    REPORT
    => nil
    

    check for example this:

    [42] pry(PingPacket::MessageType):1> descriptor.each do |i|
    [42] pry(PingPacket::MessageType):1* puts resolve i
    [42] pry(PingPacket::MessageType):1* end
    2
    4
    8
    1
    7
    3
    5
    6
    0
    => nil
    

    finally conbining all together let's put all the keys a values in a hash

    [54] pry(main)> Hash[PingPacket::MessageType.descriptor.collect do |i| [i, PingPacket::MessageType.resolve(i)] end]
    => {:LOCATE_REQUEST=>2,
     :SOS=>4,
     :SYSTEM_TEST=>8,
     :LOW_BATTERY=>1,
     :EVENT=>7,
     :CHECK_IN=>3,
     :RESTING=>5,
     :MOVING=>6,
     :REPORT=>0}