Module: Pb::Serializer

Defined in:
lib/pb/serializer.rb,
lib/pb/serializer/base.rb,
lib/pb/serializer/version.rb

Defined Under Namespace

Classes: Base, Configuration, ConflictOneofError, Error, InvalidAttributeOptionError, InvalidConfigurationError, MissingFieldError, MissingMessageTypeError, UnknownFieldError, ValidationError

Constant Summary collapse

VERSION =
"0.5.2".freeze

Class Method Summary collapse

Class Method Details

.build_default_mask(descriptor) ⇒ Object

Parameters:

  • descriptor (Google::Protobuf::Descriptor)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/pb/serializer.rb', line 67

def build_default_mask(descriptor)
  set =
    descriptor.each_with_object(Set[]) do |fd, m|
      case fd.type
      when :message
        case fd.submsg_name
        when "google.protobuf.Timestamp", 
          "google.protobuf.StringValue",
          "google.protobuf.Int32Value" ,
          "google.protobuf.Int64Value" ,
          "google.protobuf.UInt32Value",
          "google.protobuf.UInt64Value",
          "google.protobuf.FloatValue" ,
          "google.protobuf.DoubleValue",
          "google.protobuf.BoolValue"  ,
          "google.protobuf.BytesValue" then m << fd.name.to_sym
        else
          m << { fd.name.to_sym => -> { build_default_mask(fd.subtype) } }
        end
      else
        m << fd.name.to_sym
      end
    end
  set.to_a
end

.configurationPb::Serializer::Configuration



57
58
59
# File 'lib/pb/serializer.rb', line 57

def configuration
  @configuraiton ||= Configuration.new
end

.configure {|c| ... } ⇒ void

This method returns an undefined value.

Examples:

Pb::Serializer.configuration do |c|
  c.missing_field_behavior = :raise  # :raise, :warn or :ignore (defualt: :raise)
end

Yields:

  • (c)

Yield Parameters:



52
53
54
# File 'lib/pb/serializer.rb', line 52

def configure
  yield configuration
end

.loggerLogger

Returns:

  • (Logger)


62
63
64
# File 'lib/pb/serializer.rb', line 62

def logger
  configuration.logger
end

.normalize_mask(input) ⇒ Hash{Symbol=>(Array,Hash,Proc)}

Parameters:

  • input (Google::Protobuf::FieldMask, Symbol, Array<(Symbol,Hash)>, Hash{Symbol=>(Array,Symbol,Hash,Proc)}, Proc)

Returns:

  • (Hash{Symbol=>(Array,Hash,Proc)})


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/pb/serializer.rb', line 107

def normalize_mask(input)
  if input.kind_of?(Google::Protobuf::FieldMask)
    input = parse_field_mask(input)
  end

  input = input.call if input.kind_of?(Proc)
  input = [input] if input.kind_of?(Hash)

  normalized = {}
  Array(input).each do |el|
    case el
    when Symbol
      normalized[el] ||= []
    when Hash
      el.each do |k, v|
        v = v.call if v.kind_of?(Proc)
        v = [v] if v.kind_of?(Hash)
        normalized[k] ||= []
        normalized[k].push(*Array(v))
      end
    else
      raise "not supported field mask type: #{input.class}"
    end
  end

  normalized
end

.parse_field_mask(field_mask) ⇒ Array

Parameters:

  • field_mask (Google::Protobuf::FieldMask)

Returns:

  • (Array)


95
96
97
98
99
100
101
102
103
# File 'lib/pb/serializer.rb', line 95

def parse_field_mask(field_mask)
  unless field_mask.kind_of?(Google::Protobuf::FieldMask)
    raise ArgumentError, "expected Google::Protobuf::FieldMask, but got #{field_mask.class}"
  end

  field_mask.paths.map do |path|
    path.split(".").reverse.inject(nil) { |h, key| h.nil? ? key.to_sym : { key.to_sym => [h].compact } }
  end
end