Fix overwriting fields

This commit is contained in:
Omar Roth 2019-09-18 22:16:42 -04:00
parent 89b2ee0d29
commit bfe9627836
No known key found for this signature in database
GPG Key ID: B8254FB7EC3D37F2

View File

@ -52,7 +52,7 @@ struct ProtoBuf::Any
Float64 | Float64 |
Array(UInt8) | Array(UInt8) |
String | String |
Hash(Int32, Type) Hash(String, Type)
getter raw : Type getter raw : Type
@ -73,7 +73,8 @@ struct ProtoBuf::Any
end end
def self.from_io(io : IO, format = IO::ByteFormat::NetworkEndian, ignore_exceptions = false) def self.from_io(io : IO, format = IO::ByteFormat::NetworkEndian, ignore_exceptions = false)
item = new({} of Int32 => Type) item = new({} of String => Type)
index = 0
begin begin
until io.pos == io.size until io.pos == io.size
@ -136,7 +137,8 @@ struct ProtoBuf::Any
raise "Invalid type #{type}" raise "Invalid type #{type}"
end end
item[field] = value.as(Type) item["#{field}:#{index}"] = value.as(Type)
index += 1
end end
rescue ex rescue ex
if !ignore_exceptions if !ignore_exceptions
@ -147,12 +149,12 @@ struct ProtoBuf::Any
item item
end end
def []=(key : Int32, value : Type) def []=(key : String, value : Type)
case object = @raw case object = @raw
when Hash when Hash
object[key] = value object[key] = value
else else
raise "Expected Hash for #[]=(key : Int32, value : Type), not #{object.class}" raise "Expected Hash for #[]=(key : String, value : Type), not #{object.class}"
end end
end end