From 435f61102bf23b9c9be2592d27b8935a8717e610 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Thu, 9 Nov 2017 14:23:14 +0100 Subject: allow message parsing from an array slice --- csharp/src/Google.Protobuf/MessageExtensions.cs | 16 ++++++++++++++++ csharp/src/Google.Protobuf/MessageParser.cs | 15 +++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/csharp/src/Google.Protobuf/MessageExtensions.cs b/csharp/src/Google.Protobuf/MessageExtensions.cs index 047156c3..9dbc49d6 100644 --- a/csharp/src/Google.Protobuf/MessageExtensions.cs +++ b/csharp/src/Google.Protobuf/MessageExtensions.cs @@ -53,6 +53,22 @@ namespace Google.Protobuf input.CheckReadEndOfStreamTag(); } + /// + /// Merges data from the given byte array slice into an existing message. + /// + /// The message to merge the data into. + /// The data containing the slice to merge, which must be protobuf-encoded binary data. + /// The offset of the slice to merge. + /// The length of the slice to merge. + public static void MergeFrom(this IMessage message, byte[] data, int offset, int length) + { + ProtoPreconditions.CheckNotNull(message, "message"); + ProtoPreconditions.CheckNotNull(data, "data"); + CodedInputStream input = new CodedInputStream(data, offset, length); + message.MergeFrom(input); + input.CheckReadEndOfStreamTag(); + } + /// /// Merges data from the given byte string into an existing message. /// diff --git a/csharp/src/Google.Protobuf/MessageParser.cs b/csharp/src/Google.Protobuf/MessageParser.cs index 8889638b..569414b0 100644 --- a/csharp/src/Google.Protobuf/MessageParser.cs +++ b/csharp/src/Google.Protobuf/MessageParser.cs @@ -70,6 +70,21 @@ namespace Google.Protobuf return message; } + /// + /// Parses a message from a byte array slice. + /// + /// The byte array containing the message. Must not be null. + /// The offset of the slice to parse. + /// The length of the slice to parse. + /// The newly parsed message. + public IMessage ParseFrom(byte[] data, int offset, int length) + { + ProtoPreconditions.CheckNotNull(data, "data"); + IMessage message = factory(); + message.MergeFrom(data, offset, length); + return message; + } + /// /// Parses a message from the given byte string. /// -- cgit v1.2.3 From 4a5e1bd043c8ed01b172ca0ad91af00ed45b0640 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Thu, 9 Nov 2017 18:25:25 +0100 Subject: check already performed by MergeFrom --- csharp/src/Google.Protobuf/MessageParser.cs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/csharp/src/Google.Protobuf/MessageParser.cs b/csharp/src/Google.Protobuf/MessageParser.cs index 569414b0..e5334591 100644 --- a/csharp/src/Google.Protobuf/MessageParser.cs +++ b/csharp/src/Google.Protobuf/MessageParser.cs @@ -64,7 +64,6 @@ namespace Google.Protobuf /// The newly parsed message. public IMessage ParseFrom(byte[] data) { - ProtoPreconditions.CheckNotNull(data, "data"); IMessage message = factory(); message.MergeFrom(data); return message; @@ -79,7 +78,6 @@ namespace Google.Protobuf /// The newly parsed message. public IMessage ParseFrom(byte[] data, int offset, int length) { - ProtoPreconditions.CheckNotNull(data, "data"); IMessage message = factory(); message.MergeFrom(data, offset, length); return message; @@ -92,7 +90,6 @@ namespace Google.Protobuf /// The parsed message. public IMessage ParseFrom(ByteString data) { - ProtoPreconditions.CheckNotNull(data, "data"); IMessage message = factory(); message.MergeFrom(data); return message; @@ -206,7 +203,6 @@ namespace Google.Protobuf /// The newly parsed message. public new T ParseFrom(byte[] data) { - ProtoPreconditions.CheckNotNull(data, "data"); T message = factory(); message.MergeFrom(data); return message; @@ -219,7 +215,6 @@ namespace Google.Protobuf /// The parsed message. public new T ParseFrom(ByteString data) { - ProtoPreconditions.CheckNotNull(data, "data"); T message = factory(); message.MergeFrom(data); return message; -- cgit v1.2.3 From 5f961914594abed32180d3c664cc04fe5f7b242c Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Thu, 9 Nov 2017 18:33:15 +0100 Subject: ParseFrom for array slice is missing --- csharp/src/Google.Protobuf/MessageParser.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/csharp/src/Google.Protobuf/MessageParser.cs b/csharp/src/Google.Protobuf/MessageParser.cs index e5334591..66d44135 100644 --- a/csharp/src/Google.Protobuf/MessageParser.cs +++ b/csharp/src/Google.Protobuf/MessageParser.cs @@ -208,6 +208,20 @@ namespace Google.Protobuf return message; } + /// + /// Parses a message from a byte array slice. + /// + /// The byte array containing the message. Must not be null. + /// The offset of the slice to parse. + /// The length of the slice to parse. + /// The newly parsed message. + public new T ParseFrom(byte[] data, int offset, int length) + { + T message = factory(); + message.MergeFrom(data, offset, length); + return message; + } + /// /// Parses a message from the given byte string. /// -- cgit v1.2.3 From 07df2307b2f339e57b4e7f42b9f87bc1575a5ca1 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Fri, 10 Nov 2017 18:37:33 +0100 Subject: update changelog --- CHANGES.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.txt b/CHANGES.txt index 9fe31583..fe90cb9a 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -62,6 +62,7 @@ C# * Added unknown field support in JsonParser. * Fixed oneof message field merge. + * Simplify parsing messages from array slices. Ruby * Unknown fields are now preserved by default. -- cgit v1.2.3 From 188f18044feb1ca82ecc922f3e59e37078473ddf Mon Sep 17 00:00:00 2001 From: Paul Yang Date: Mon, 13 Nov 2017 09:31:28 -0800 Subject: All integer types should accept null in json. (#3869) --- conformance/conformance_test.cc | 16 ++++++++++++++++ php/src/Google/Protobuf/Internal/Message.php | 11 ++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/conformance/conformance_test.cc b/conformance/conformance_test.cc index c54d4ccd..98c2eae4 100644 --- a/conformance/conformance_test.cc +++ b/conformance/conformance_test.cc @@ -1842,6 +1842,14 @@ bool ConformanceTestSuite::RunSuite(ConformanceTestRunner* runner, "optionalInt64": null, "optionalUint32": null, "optionalUint64": null, + "optionalSint32": null, + "optionalSint64": null, + "optionalFixed32": null, + "optionalFixed64": null, + "optionalSfixed32": null, + "optionalSfixed64": null, + "optionalFloat": null, + "optionalDouble": null, "optionalBool": null, "optionalString": null, "optionalBytes": null, @@ -1851,6 +1859,14 @@ bool ConformanceTestSuite::RunSuite(ConformanceTestRunner* runner, "repeatedInt64": null, "repeatedUint32": null, "repeatedUint64": null, + "repeatedSint32": null, + "repeatedSint64": null, + "repeatedFixed32": null, + "repeatedFixed64": null, + "repeatedSfixed32": null, + "repeatedSfixed64": null, + "repeatedFloat": null, + "repeatedDouble": null, "repeatedBool": null, "repeatedString": null, "repeatedBytes": null, diff --git a/php/src/Google/Protobuf/Internal/Message.php b/php/src/Google/Protobuf/Internal/Message.php index 9785be30..a7a4f272 100644 --- a/php/src/Google/Protobuf/Internal/Message.php +++ b/php/src/Google/Protobuf/Internal/Message.php @@ -833,6 +833,8 @@ class Message } return $value; case GPBType::INT32: + case GPBType::SINT32: + case GPBType::SFIXED32: if (is_null($value)) { return $this->defaultValue($field); } @@ -850,6 +852,7 @@ class Message } return $value; case GPBType::UINT32: + case GPBType::FIXED32: if (is_null($value)) { return $this->defaultValue($field); } @@ -863,6 +866,8 @@ class Message } return $value; case GPBType::INT64: + case GPBType::SINT64: + case GPBType::SFIXED64: if (is_null($value)) { return $this->defaultValue($field); } @@ -880,6 +885,7 @@ class Message } return $value; case GPBType::UINT64: + case GPBType::FIXED64: if (is_null($value)) { return $this->defaultValue($field); } @@ -895,11 +901,6 @@ class Message $value = bcsub($value, "18446744073709551616"); } return $value; - case GPBType::FIXED64: - if (is_null($value)) { - return $this->defaultValue($field); - } - return $value; default: return $value; } -- cgit v1.2.3