Unverified Commit 9bda1f19 authored by Paul Yang's avatar Paul Yang Committed by GitHub

Adopt upb change for timestamp and duration json to php (#5106)

* Adopt upb change for timestamp and duration json to php

* Remove unused code

* Re-sync upb

* Fix php implementation timestamp json parsing

* Fix strptime use local timezone on mac.

* Remove succeeding tests

* Resync

* Add tests for values

* Fix php tests

* Fix encoder handlers change default value

Previously, oneofsubmsg_handler and submsg_handler change zval's default value directly.
The fix use REPLACE_ZVAL_VALUE which create a copy of parsed value and assign it to zval.
parent 47d33e75
......@@ -5,8 +5,6 @@ Recommended.Proto3.JsonInput.BytesFieldBase64Url.JsonOutput
Recommended.Proto3.JsonInput.BytesFieldBase64Url.ProtobufOutput
Recommended.Proto3.JsonInput.DurationHas3FractionalDigits.Validator
Recommended.Proto3.JsonInput.DurationHas6FractionalDigits.Validator
Recommended.Proto3.JsonInput.DurationHas9FractionalDigits.Validator
Recommended.Proto3.JsonInput.DurationHasZeroFractionalDigit.Validator
Recommended.Proto3.JsonInput.Int64FieldBeString.Validator
Recommended.Proto3.JsonInput.MapFieldValueIsNull
Recommended.Proto3.JsonInput.OneofZeroBytes.JsonOutput
......@@ -18,13 +16,12 @@ Recommended.Proto3.JsonInput.StringFieldUnpairedHighSurrogate
Recommended.Proto3.JsonInput.StringFieldUnpairedLowSurrogate
Recommended.Proto3.JsonInput.TimestampHas3FractionalDigits.Validator
Recommended.Proto3.JsonInput.TimestampHas6FractionalDigits.Validator
Recommended.Proto3.JsonInput.TimestampHas9FractionalDigits.Validator
Recommended.Proto3.JsonInput.TimestampHasZeroFractionalDigit.Validator
Recommended.Proto3.JsonInput.TimestampZeroNormalized.Validator
Recommended.Proto3.JsonInput.Uint64FieldBeString.Validator
Recommended.Proto3.ProtobufInput.OneofZeroBytes.JsonOutput
Required.DurationProtoInputTooLarge.JsonOutput
Required.DurationProtoInputTooSmall.JsonOutput
Required.TimestampProtoInputTooLarge.JsonOutput
Required.TimestampProtoInputTooSmall.JsonOutput
Required.Proto3.JsonInput.Any.JsonOutput
Required.Proto3.JsonInput.Any.ProtobufOutput
Required.Proto3.JsonInput.AnyNested.JsonOutput
......@@ -51,12 +48,8 @@ Required.Proto3.JsonInput.DoubleFieldMaxNegativeValue.ProtobufOutput
Required.Proto3.JsonInput.DoubleFieldMinPositiveValue.JsonOutput
Required.Proto3.JsonInput.DoubleFieldMinPositiveValue.ProtobufOutput
Required.Proto3.JsonInput.DoubleFieldNan.JsonOutput
Required.Proto3.JsonInput.DurationMaxValue.JsonOutput
Required.Proto3.JsonInput.DurationMaxValue.ProtobufOutput
Required.Proto3.JsonInput.DurationMinValue.JsonOutput
Required.Proto3.JsonInput.DurationMinValue.ProtobufOutput
Required.Proto3.JsonInput.DurationRepeatedValue.JsonOutput
Required.Proto3.JsonInput.DurationRepeatedValue.ProtobufOutput
Required.Proto3.JsonInput.FieldMask.JsonOutput
Required.Proto3.JsonInput.FieldMask.ProtobufOutput
Required.Proto3.JsonInput.FloatFieldInfinity.JsonOutput
......@@ -73,36 +66,8 @@ Required.Proto3.JsonInput.StringFieldUnicodeEscape.JsonOutput
Required.Proto3.JsonInput.StringFieldUnicodeEscape.ProtobufOutput
Required.Proto3.JsonInput.StringFieldUnicodeEscapeWithLowercaseHexLetters.JsonOutput
Required.Proto3.JsonInput.StringFieldUnicodeEscapeWithLowercaseHexLetters.ProtobufOutput
Required.Proto3.JsonInput.Struct.JsonOutput
Required.Proto3.JsonInput.Struct.ProtobufOutput
Required.Proto3.JsonInput.TimestampMaxValue.JsonOutput
Required.Proto3.JsonInput.TimestampMaxValue.ProtobufOutput
Required.Proto3.JsonInput.TimestampMinValue.JsonOutput
Required.Proto3.JsonInput.TimestampMinValue.ProtobufOutput
Required.Proto3.JsonInput.TimestampRepeatedValue.JsonOutput
Required.Proto3.JsonInput.TimestampRepeatedValue.ProtobufOutput
Required.Proto3.JsonInput.TimestampWithNegativeOffset.JsonOutput
Required.Proto3.JsonInput.TimestampWithNegativeOffset.ProtobufOutput
Required.Proto3.JsonInput.TimestampWithPositiveOffset.JsonOutput
Required.Proto3.JsonInput.TimestampWithPositiveOffset.ProtobufOutput
Required.Proto3.JsonInput.ValueAcceptBool.JsonOutput
Required.Proto3.JsonInput.ValueAcceptBool.ProtobufOutput
Required.Proto3.JsonInput.ValueAcceptFloat.JsonOutput
Required.Proto3.JsonInput.ValueAcceptFloat.ProtobufOutput
Required.Proto3.JsonInput.ValueAcceptInteger.JsonOutput
Required.Proto3.JsonInput.ValueAcceptInteger.ProtobufOutput
Required.Proto3.JsonInput.ValueAcceptList.JsonOutput
Required.Proto3.JsonInput.ValueAcceptList.ProtobufOutput
Required.Proto3.JsonInput.ValueAcceptNull.JsonOutput
Required.Proto3.JsonInput.ValueAcceptNull.ProtobufOutput
Required.Proto3.JsonInput.ValueAcceptObject.JsonOutput
Required.Proto3.JsonInput.ValueAcceptObject.ProtobufOutput
Required.Proto3.JsonInput.ValueAcceptString.JsonOutput
Required.Proto3.JsonInput.ValueAcceptString.ProtobufOutput
Required.Proto3.ProtobufInput.DoubleFieldNormalizeQuietNan.JsonOutput
Required.Proto3.ProtobufInput.DoubleFieldNormalizeSignalingNan.JsonOutput
Required.Proto3.ProtobufInput.FloatFieldNormalizeQuietNan.JsonOutput
Required.Proto3.ProtobufInput.FloatFieldNormalizeSignalingNan.JsonOutput
Required.Proto3.ProtobufInput.ValidDataRepeated.FLOAT.JsonOutput
Required.TimestampProtoInputTooLarge.JsonOutput
Required.TimestampProtoInputTooSmall.JsonOutput
This diff is collapsed.
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -438,8 +438,8 @@ class GPBUtil
$nanoseconds = intval($nanoseconds);
// remove the nanoseconds and preceding period from the timestamp
$date = substr($timestamp, 0, $periodIndex - 1);
$timezone = substr($timestamp, $periodIndex + $nanosecondsLength);
$date = substr($timestamp, 0, $periodIndex);
$timezone = substr($timestamp, $periodIndex + $nanosecondsLength + 1);
$timestamp = $date.$timezone;
}
}
......
......@@ -1157,9 +1157,17 @@ class Message
public function parseFromJsonStream($input)
{
$array = json_decode($input->getData(), true, 512, JSON_BIGINT_AS_STRING);
if ($this instanceof \Google\Protobuf\ListValue) {
$array = ["values"=>$array];
}
if (is_null($array)) {
throw new GPBDecodeException(
"Cannot decode json string.");
if ($this instanceof \Google\Protobuf\Value) {
$this->setNullValue(\Google\Protobuf\NullValue::NULL_VALUE);
return;
} else {
throw new GPBDecodeException(
"Cannot decode json string: " . $input->getData());
}
}
try {
$this->mergeFromJsonArray($array);
......
......@@ -20,6 +20,9 @@ use Google\Protobuf\UInt64Value;
use Google\Protobuf\BoolValue;
use Google\Protobuf\StringValue;
use Google\Protobuf\BytesValue;
use Google\Protobuf\Value;
use Google\Protobuf\ListValue;
use Google\Protobuf\Struct;
class EncodeDecodeTest extends TestBase
{
......@@ -40,6 +43,13 @@ class EncodeDecodeTest extends TestBase
$this->assertEquals(false, $m->getValue());
}
public function testEncodeTopLevelBoolValue()
{
$m = new BoolValue();
$m->setValue(true);
$this->assertSame("true", $m->serializeToJsonString());
}
public function testDecodeTopLevelDoubleValue()
{
$m = new DoubleValue();
......@@ -47,6 +57,13 @@ class EncodeDecodeTest extends TestBase
$this->assertEquals(1.5, $m->getValue());
}
public function testEncodeTopLevelDoubleValue()
{
$m = new DoubleValue();
$m->setValue(1.5);
$this->assertSame("1.5", $m->serializeToJsonString());
}
public function testDecodeTopLevelFloatValue()
{
$m = new FloatValue();
......@@ -54,6 +71,13 @@ class EncodeDecodeTest extends TestBase
$this->assertEquals(1.5, $m->getValue());
}
public function testEncodeTopLevelFloatValue()
{
$m = new FloatValue();
$m->setValue(1.5);
$this->assertSame("1.5", $m->serializeToJsonString());
}
public function testDecodeTopLevelInt32Value()
{
$m = new Int32Value();
......@@ -61,6 +85,13 @@ class EncodeDecodeTest extends TestBase
$this->assertEquals(1, $m->getValue());
}
public function testEncodeTopLevelInt32Value()
{
$m = new Int32Value();
$m->setValue(1);
$this->assertSame("1", $m->serializeToJsonString());
}
public function testDecodeTopLevelUInt32Value()
{
$m = new UInt32Value();
......@@ -68,6 +99,13 @@ class EncodeDecodeTest extends TestBase
$this->assertEquals(1, $m->getValue());
}
public function testEncodeTopLevelUInt32Value()
{
$m = new UInt32Value();
$m->setValue(1);
$this->assertSame("1", $m->serializeToJsonString());
}
public function testDecodeTopLevelInt64Value()
{
$m = new Int64Value();
......@@ -75,6 +113,13 @@ class EncodeDecodeTest extends TestBase
$this->assertEquals(1, $m->getValue());
}
# public function testEncodeTopLevelInt64Value()
# {
# $m = new Int64Value();
# $m->setValue(1);
# $this->assertSame("\"1\"", $m->serializeToJsonString());
# }
public function testDecodeTopLevelUInt64Value()
{
$m = new UInt64Value();
......@@ -82,6 +127,13 @@ class EncodeDecodeTest extends TestBase
$this->assertEquals(1, $m->getValue());
}
# public function testEncodeTopLevelUInt64Value()
# {
# $m = new UInt64Value();
# $m->setValue(1);
# $this->assertSame("\"1\"", $m->serializeToJsonString());
# }
public function testDecodeTopLevelStringValue()
{
$m = new StringValue();
......@@ -89,6 +141,13 @@ class EncodeDecodeTest extends TestBase
$this->assertSame("a", $m->getValue());
}
public function testEncodeTopLevelStringValue()
{
$m = new StringValue();
$m->setValue("a");
$this->assertSame("\"a\"", $m->serializeToJsonString());
}
public function testDecodeTopLevelBytesValue()
{
$m = new BytesValue();
......@@ -96,6 +155,13 @@ class EncodeDecodeTest extends TestBase
$this->assertSame("a", $m->getValue());
}
public function testEncodeTopLevelBytesValue()
{
$m = new BytesValue();
$m->setValue("a");
$this->assertSame("\"YQ==\"", $m->serializeToJsonString());
}
public function testEncode()
{
$from = new TestMessage();
......@@ -603,4 +669,120 @@ class EncodeDecodeTest extends TestBase
$to->mergeFromJsonString($data);
$this->expectFields($to);
}
public function testDecodeDuration()
{
$m = new Google\Protobuf\Duration();
$m->mergeFromJsonString("\"1234.5678s\"");
$this->assertEquals(1234, $m->getSeconds());
$this->assertEquals(567800000, $m->getNanos());
}
public function testEncodeDuration()
{
$m = new Google\Protobuf\Duration();
$m->setSeconds(1234);
$m->setNanos(999999999);
$this->assertEquals("\"1234.999999999s\"", $m->serializeToJsonString());
}
public function testDecodeTimestamp()
{
$m = new Google\Protobuf\Timestamp();
$m->mergeFromJsonString("\"2000-01-01T00:00:00.123456789Z\"");
$this->assertEquals(946684800, $m->getSeconds());
$this->assertEquals(123456789, $m->getNanos());
}
public function testEncodeTimestamp()
{
$m = new Google\Protobuf\Timestamp();
$m->setSeconds(946684800);
$m->setNanos(123456789);
$this->assertEquals("\"2000-01-01T00:00:00.123456789Z\"",
$m->serializeToJsonString());
}
public function testDecodeTopLevelValue()
{
$m = new Value();
$m->mergeFromJsonString("\"a\"");
$this->assertSame("a", $m->getStringValue());
$m = new Value();
$m->mergeFromJsonString("1.5");
$this->assertSame(1.5, $m->getNumberValue());
$m = new Value();
$m->mergeFromJsonString("true");
$this->assertSame(true, $m->getBoolValue());
$m = new Value();
$m->mergeFromJsonString("null");
$this->assertSame("null_value", $m->getKind());
$m = new Value();
$m->mergeFromJsonString("[1]");
$this->assertSame("list_value", $m->getKind());
$m = new Value();
$m->mergeFromJsonString("{\"a\":1}");
$this->assertSame("struct_value", $m->getKind());
}
public function testEncodeTopLevelValue()
{
$m = new Value();
$m->setStringValue("a");
$this->assertSame("\"a\"", $m->serializeToJsonString());
$m = new Value();
$m->setNumberValue(1.5);
$this->assertSame("1.5", $m->serializeToJsonString());
$m = new Value();
$m->setBoolValue(true);
$this->assertSame("true", $m->serializeToJsonString());
$m = new Value();
$m->setNullValue(0);
$this->assertSame("null", $m->serializeToJsonString());
}
public function testDecodeTopLevelListValue()
{
$m = new ListValue();
$m->mergeFromJsonString("[1]");
$this->assertSame(1.0, $m->getValues()[0]->getNumberValue());
}
public function testEncodeTopLevelListValue()
{
$m = new ListValue();
$arr = $m->getValues();
$sub = new Value();
$sub->setNumberValue(1.5);
$arr[] = $sub;
$this->assertSame("[1.5]", $m->serializeToJsonString());
}
public function testDecodeTopLevelStruct()
{
$m = new Struct();
$m->mergeFromJsonString("{\"a\":{\"b\":1}}");
$this->assertSame(1.0, $m->getFields()["a"]
->getStructValue()
->getFields()["b"]->getNumberValue());
}
public function testEncodeTopLevelStruct()
{
$m = new Struct();
$map = $m->getFields();
$sub = new Value();
$sub->setNumberValue(1.5);
$map["a"] = $sub;
$this->assertSame("{\"a\":1.5}", $m->serializeToJsonString());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment