Module: Protobug::Message
- Defined in:
- lib/protobug/message.rb
Defined Under Namespace
Modules: InstanceMethods
Instance Attribute Summary collapse
-
#fields_by_json_name ⇒ Object
readonly
Returns the value of attribute fields_by_json_name.
-
#fields_by_name ⇒ Object
readonly
Returns the value of attribute fields_by_name.
-
#fields_by_number ⇒ Object
readonly
Returns the value of attribute fields_by_number.
-
#full_name ⇒ Object
Returns the value of attribute full_name.
-
#oneofs ⇒ Object
readonly
Returns the value of attribute oneofs.
-
#reserved_ranges ⇒ Object
readonly
Returns the value of attribute reserved_ranges.
Class Method Summary collapse
Instance Method Summary collapse
- #decode(binary, registry:, object: new) ⇒ Object
- #decode_json(json, registry:, ignore_unknown_fields: false) ⇒ Object
- #decode_json_hash(json, registry:, ignore_unknown_fields: false) ⇒ Object
- #encode(message) ⇒ Object
- #field(number, name, type:, **kwargs) ⇒ Object
- #freeze ⇒ Object
- #map(number, name, **kwargs) ⇒ Object
- #optional(number, name, **kwargs) ⇒ Object
- #repeated(number, name, **kwargs) ⇒ Object
- #required(number, name, **kwargs) ⇒ Object
- #reserved_range(range) ⇒ Object
Instance Attribute Details
#fields_by_json_name ⇒ Object (readonly)
Returns the value of attribute fields_by_json_name.
34 35 36 |
# File 'lib/protobug/message.rb', line 34 def fields_by_json_name @fields_by_json_name end |
#fields_by_name ⇒ Object (readonly)
Returns the value of attribute fields_by_name.
34 35 36 |
# File 'lib/protobug/message.rb', line 34 def fields_by_name @fields_by_name end |
#fields_by_number ⇒ Object (readonly)
Returns the value of attribute fields_by_number.
34 35 36 |
# File 'lib/protobug/message.rb', line 34 def fields_by_number @fields_by_number end |
#full_name ⇒ Object
Returns the value of attribute full_name.
33 34 35 |
# File 'lib/protobug/message.rb', line 33 def full_name @full_name end |
#oneofs ⇒ Object (readonly)
Returns the value of attribute oneofs.
34 35 36 |
# File 'lib/protobug/message.rb', line 34 def oneofs @oneofs end |
#reserved_ranges ⇒ Object (readonly)
Returns the value of attribute reserved_ranges.
34 35 36 |
# File 'lib/protobug/message.rb', line 34 def reserved_ranges @reserved_ranges end |
Class Method Details
.extended(base) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/protobug/message.rb', line 20 def self.extended(base) base.class_eval do @full_name = nil @fields_by_number = {} @fields_by_json_name = {} @fields_by_name = {} @reserved_ranges = [] @oneofs = {} extend BaseDescriptor include Protobug::Message::InstanceMethods end end |
Instance Method Details
#decode(binary, registry:, object: new) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/protobug/message.rb', line 121 def decode(binary, registry:, object: new) binary.binmode loop do header = BinaryEncoding.decode_varint(binary) break if header.nil? wire_type = header & 0b111 number = (header ^ wire_type) >> 3 unless number.positive? raise DecodeError, "unexpected field number #{number} in #{full_name || fields_by_name.inspect}" end field = fields_by_number[number] if field field.binary_decode(binary, object, registry, wire_type) else object.unknown_fields << [number, wire_type, BinaryEncoding.read_field_value(binary, wire_type)] end end object end |
#decode_json(json, registry:, ignore_unknown_fields: false) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/protobug/message.rb', line 85 def decode_json(json, registry:, ignore_unknown_fields: false) require "json" hash = begin JSON.parse(json, allow_blank: false, create_additions: false, allow_nan: false, allow_infinity: false) rescue JSON::ParserError => e raise DecodeError, "JSON failed to parse: #{e.}" end raise DecodeError, "expected hash, got #{hash.inspect}" unless hash.is_a? Hash decode_json_hash(hash, registry: registry, ignore_unknown_fields: ignore_unknown_fields) end |
#decode_json_hash(json, registry:, ignore_unknown_fields: false) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/protobug/message.rb', line 97 def decode_json_hash(json, registry:, ignore_unknown_fields: false) return UNSET if json.nil? raise DecodeError, "expected hash for #{self} (#{full_name}), got #{json.inspect}" unless json.is_a? Hash = new json.each do |key, value| field = fields_by_json_name[key] unless field next if ignore_unknown_fields raise(UnknownFieldError, "unknown field #{key.inspect} in #{full_name}") end if field.oneof && .send(field.oneof) && !value.nil? raise DecodeError, "multiple oneof fields set in #{full_name}: #{.send(field.oneof)} and #{field.name}" end field.json_decode(value, , ignore_unknown_fields, registry) end end |
#encode(message) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/protobug/message.rb', line 146 def encode() raise EncodeError, "expected #{self}, got #{.inspect}" unless .is_a? self buf = fields_by_number.each_with_object("".b) do |(_number, field), outbuf| next unless .send(field.haser) value = .instance_variable_get(field.ivar) field.binary_encode(value, outbuf) end .unknown_fields.each_with_object(buf) do |(number, wire_type, value), outbuf| BinaryEncoding.encode_varint((number << 3) | wire_type, outbuf) case wire_type when 0, 5 BinaryEncoding.encode_varint(value, outbuf) when 2 BinaryEncoding.encode_length(value, outbuf) else raise EncodeError, "unknown wire_type: #{wire_type}" end end end |
#field(number, name, type:, **kwargs) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/protobug/message.rb', line 169 def field(number, name, type:, **kwargs) field = case type when :message Field::MessageField when :enum Field::EnumField when :bytes Field::BytesField when :string Field::StringField when :map kwargs.delete(:cardinality) if kwargs[:cardinality] == :repeated Field::MapField when :int64 Field::Int64Field when :uint64 Field::UInt64Field when :sint64 Field::SInt64Field when :fixed64 Field::Fixed64Field when :sfixed64 Field::SFixed64Field when :int32 Field::Int32Field when :uint32 Field::UInt32Field when :sint32 Field::SInt32Field when :fixed32 Field::Fixed32Field when :sfixed32 Field::SFixed32Field when :bool Field::BoolField when :float Field::FloatField when :double Field::DoubleField when :group Field::GroupField else raise ArgumentError, "Unknown field type #{type.inspect}" end.new(number, name, **kwargs).freeze raise DefinitionError, "duplicate field number #{number}" if fields_by_number[number] fields_by_number[number] = field raise DefinitionError, "duplicate field name #{name}" if fields_by_name[name] fields_by_name[name] = field fields_by_json_name[name] = field fields_by_json_name[field.json_name] = field define_method(field.setter) do |value| return instance_variable_set(field.ivar, UNSET) if value.nil? && field.optional? && field.proto3_optional? field.validate!(value, self) instance_variable_set(field.ivar, value) end define_method(name) do value = instance_variable_get(field.ivar) UNSET == value ? field.default : value end define_method(field.haser) do value = instance_variable_get(field.ivar) return false if UNSET == value return false if (!field.optional? || !field.proto3_optional?) && !field.oneof && field.default == value if field.repeated? !value.empty? else true end end define_method(field.clearer) do instance_variable_set(field.ivar, UNSET) end field.define_adder(self) if field.repeated? return unless field.oneof unless oneofs[field.oneof] oneofs[field.oneof] = ary = [] define_method(field.oneof) do ary.find { |f| send(f.haser) }&.name end end oneofs[field.oneof] << field end |
#freeze ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/protobug/message.rb', line 36 def freeze fields_by_number.freeze fields_by_name.freeze fields_by_json_name.freeze full_name.freeze reserved_ranges.freeze oneofs.each_value(&:freeze) oneofs.freeze super end |
#map(number, name, **kwargs) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/protobug/message.rb', line 63 def map(number, name, **kwargs) if kwargs[:type] && kwargs[:type] != :map raise DefinitionError, "expected type: :map, got #{kwargs[:type].inspect}" end repeated(number, name, type: :map, **kwargs) end |
#optional(number, name, **kwargs) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/protobug/message.rb', line 47 def optional(number, name, **kwargs) if kwargs[:cardinality] && kwargs[:cardinality] != :optional raise DefinitionError, "expected cardinality: :optional, got #{kwargs[:cardinality].inspect}" end field(number, name, cardinality: :optional, **kwargs) end |
#repeated(number, name, **kwargs) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/protobug/message.rb', line 55 def repeated(number, name, **kwargs) if kwargs[:cardinality] && kwargs[:cardinality] != :repeated raise DefinitionError, "expected cardinality: :repeated, got #{kwargs[:cardinality].inspect}" end field(number, name, cardinality: :repeated, **kwargs) end |
#required(number, name, **kwargs) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/protobug/message.rb', line 71 def required(number, name, **kwargs) if kwargs[:cardinality] && kwargs[:cardinality] != :required raise DefinitionError, "expected cardinality: :required, got #{kwargs[:cardinality].inspect}" end field(number, name, cardinality: :required, **kwargs) end |
#reserved_range(range) ⇒ Object
79 80 81 82 83 |
# File 'lib/protobug/message.rb', line 79 def reserved_range(range) raise DefinitionError, "expected Range, got #{range.inspect}" unless range.is_a? Range reserved_ranges << range end |