module Google::Protobuf::MessageExts

Public Instance Methods

orig_to_json(options = {})
Alias for: to_json
orig_to_proto()
Alias for: to_proto
to_json(options = {}) click to toggle source
# File lib/protobuf/validate.rb, line 9
def to_json(options = {})
  validate_presence!
  orig_to_json(options)
end
Also aliased as: orig_to_json
to_proto() click to toggle source
# File lib/protobuf/validate.rb, line 14
def to_proto
  validate_presence!
  orig_to_proto
end
Also aliased as: orig_to_proto
validate_presence!() click to toggle source
# File lib/protobuf/validate.rb, line 19
def validate_presence!
  self.class.descriptor.entries.each do |entry|
    next if entry.label != :required

    v = self[entry.name]

    validate_fail = case entry.type
    when :int64, :uint64, :int32, :uint32,  :double, :float
      v.nil? || v == 0
    when :string
      v.nil? || v.empty?
    when :enum
      v.nil? || !entry.subtype.entries.map(&:first).include?(v)
    else
      v.nil?
    end

    raise "#{self.class.name}::#{entry.name} was required, but got 「#{v.inspect}」" if validate_fail
  end
end