Skip to content

Commit

Permalink
simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
skatkov committed Oct 20, 2023
1 parent 3936978 commit 464a514
Showing 1 changed file with 27 additions and 28 deletions.
55 changes: 27 additions & 28 deletions lib/pbbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,20 @@ def set!(field, *args, &block)
descriptor = _descriptor_for_field(name)
::Kernel.raise ::ArgumentError, "Unknown field #{name}" if descriptor.nil?

if block
if ::Kernel.block_given?
::Kernel.raise ::ArgumentError, "can't pass block to non-message field" unless descriptor.type == :message

if descriptor.label == :repeated
# pb.field @array { |element| pb.name element.name }
::Kernel.raise ::ArgumentError, "wrong number of arguments #{args.length} (expected 1)" unless args.length == 1
collection = args.first
_append_repeated(name, descriptor, collection, &block)
return
else
# pb.field { pb.name "hello" }
::Kernel.raise ::ArgumentError, "wrong number of arguments (expected 0)" unless args.empty?
message = (@message[name] ||= _new_message_from_descriptor(descriptor))
_scope(message, &block)
end

::Kernel.raise ::ArgumentError, "wrong number of arguments (expected 0)" unless args.empty?
# pb.field { pb.name "hello" }
message = (@message[name] ||= _new_message_from_descriptor(descriptor))
_scope(message, &block)
elsif args.length == 1
arg = args.first
if descriptor.label == :repeated
Expand Down Expand Up @@ -123,11 +122,11 @@ def extract!(element, *args)
def merge!(object)
::Kernel.raise Pbbuilder::MergeError.build(target!, object) unless object.class == ::Hash

object.each_key do |key|
next if object[key].respond_to?(:empty?) && object[key].empty?
object.each do |key, value|
next if value.respond_to?(:empty?) && value.empty?

descriptor = _descriptor_for_field(key)
::Kernel.raise ::ArgumentError, "Unknown field #{name}" if descriptor.nil?
::Kernel.raise ::ArgumentError, "Unknown field #{key}" if descriptor.nil?

if descriptor.label == :repeated
# optional empty fields don't show up in @message object,
Expand All @@ -136,48 +135,48 @@ def merge!(object)
@message[key.to_s] = _new_message_from_descriptor(descriptor)
end

if object[key].respond_to?(:to_hash)
object[key].to_hash.each {|k, v| @message[key.to_s][k] = v}
elsif object[key].respond_to?(:to_ary)
elements = object[key].map do |obj|
if value.respond_to?(:to_hash)
value.to_hash.each {|k, v| @message[key.to_s][k] = v}
elsif value.respond_to?(:to_ary)
elements = value.map do |obj|
descriptor.subtype ? descriptor.subtype.msgclass.new(obj) : obj
end

@message[key.to_s].replace(elements)
end
else
if object[key].class == ::String
if value.class == ::String
# pb.fields {"one" => "two"}
@message[key.to_s] = object[key]
elsif object[key].class == ::TrueClass || object[key].class == ::FalseClass
@message[key.to_s] = value
elsif value.class == ::TrueClass || value.class == ::FalseClass
# pb.boolean true || false
@message[key.to_s] = object[key]
elsif object[key].class == ::Array
@message[key.to_s] = value
elsif value.class == ::Array
# pb.field_name do
# pb.tags ["ok", "cool"]
# end

@message[key.to_s] = object[key]
elsif object[key].class == ::Hash
@message[key.to_s] = value
elsif descriptor.type == :message
if @message[key.to_s].nil?
@message[key.to_s] = _new_message_from_descriptor(descriptor)
end

object[key].each do |k, v|
if object[key][k].respond_to?(:to_hash)
value.each do |k, v|
if value[k].respond_to?(:to_hash)
if @message[key.to_s][k.to_s].nil?
descriptor = @message[key.to_s].class.descriptor.lookup(k.to_s)
@message[key.to_s][k.to_s] = _new_message_from_descriptor(descriptor)
end

_scope(@message[key.to_s][k.to_s]) { self.merge!(object[key][k]) }
elsif object[key][k].respond_to?(:to_ary)
@message[key.to_s][k.to_s].replace object[key][k]
_scope(@message[key.to_s][k.to_s]) { self.merge!(value[k]) }
elsif value[k].respond_to?(:to_ary)
@message[key.to_s][k.to_s].replace value[k]
else
# Throws an error, if we try to merge nil object into empty value.
next if object[key][k].nil? && @message[key.to_s][k.to_s].nil?
next if value[k].nil? && @message[key.to_s][k.to_s].nil?

@message[key.to_s][k.to_s] = object[key][k]
@message[key.to_s][k.to_s] = value[k]
end
end
end
Expand Down

0 comments on commit 464a514

Please sign in to comment.