Skip to content

Commit

Permalink
Generate the default mask lazily
Browse files Browse the repository at this point in the history
  • Loading branch information
fetburner committed Sep 6, 2023
1 parent dfefdef commit ff227c9
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/pb/serializable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def self.included(base)
# @param with [
# Google::Protobuf::FieldMask,
# Array<(Symbol, Hash)>,
# Hash{Symbol=>(Array,Symbol,Hash)},
# Hash{Symbol=>(Array,Symbol,Hash,Proc)},
# ]
# Specifies the list of fields to be serialized in the Proto message object.
# `nil` means that all fields defined in .proto will be serialized.
Expand Down
7 changes: 4 additions & 3 deletions lib/pb/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def build_default_mask(descriptor)
"google.protobuf.BoolValue" ,
"google.protobuf.BytesValue" then m << fd.name.to_sym
else
m << { fd.name.to_sym => build_default_mask(fd.subtype) }
m << { fd.name.to_sym => -> { build_default_mask(fd.subtype) } }
end
else
m << fd.name.to_sym
Expand All @@ -102,8 +102,8 @@ def parse_field_mask(field_mask)
end
end

# @param input [Google::Protobuf::FieldMask, Symbol, Array<(Symbol,Hash)>, Hash{Symbol=>(Array,Symbol,Hash)}]
# @return [Hash{Symbol=>(Array,Hash)}]
# @param input [Google::Protobuf::FieldMask, Symbol, Array<(Symbol,Hash)>, Hash{Symbol=>(Array,Symbol,Hash,Proc)}]
# @return [Hash{Symbol=>(Array,Hash,Proc)}]
def normalize_mask(input)
if input.kind_of?(Google::Protobuf::FieldMask)
input = parse_field_mask(input)
Expand All @@ -118,6 +118,7 @@ def normalize_mask(input)
normalized[el] ||= []
when Hash
el.each do |k, v|
v = v.call if input.kind_of?(Proc)
v = [v] if v.kind_of?(Hash)
normalized[k] ||= []
normalized[k].push(*Array(v))
Expand Down

0 comments on commit ff227c9

Please sign in to comment.