Sha256: 35aa40f6173a9119fd041027068def2cd3f6a0b851435b09a527cce53c1a25bc

Contents?: true

Size: 1.35 KB

Versions: 5

Compression:

Stored size: 1.35 KB

Contents

require 'protobuf/field/base_field'

module Protobuf
  module Field
    class MessageField < BaseField

      ##
      # Public Instance Methods
      #

      def acceptable?(val)
        val.is_a?(type_class) || val.respond_to?(:to_hash)
      end

      def decode(bytes)
        type_class.decode(bytes)
      end

      def encode(value)
        bytes = value.encode
        result = ::Protobuf::Field::VarintField.encode(bytes.size)
        result << bytes
      end

      def message?
        true
      end

      def wire_type
        ::Protobuf::WireType::LENGTH_DELIMITED
      end

      private

      ##
      # Private Instance Methods
      #

      def define_setter
        field = self
        message_class.class_eval do
          define_method("#{field.name}=") do |val|
            case
            when val.nil?
              @values.delete(field.name)
            when val.is_a?(field.type_class)
              @values[field.name] = val
            when val.respond_to?(:to_proto)
              @values[field.name] = val.to_proto
            when val.respond_to?(:to_hash)
              @values[field.name] = field.type_class.new(val.to_hash)
            else
              fail TypeError, "Expected value of type '#{field.type_class}' for field #{field.name}, but got '#{val.class}'"
            end
          end
        end
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
protobuf-3.6.12 lib/protobuf/field/message_field.rb
protobuf-3.6.11 lib/protobuf/field/message_field.rb
protobuf-3.6.10 lib/protobuf/field/message_field.rb
protobuf-3.6.9 lib/protobuf/field/message_field.rb
protobuf-3.6.7 lib/protobuf/field/message_field.rb