Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions compiler/cpp/src/thrift/generate/t_php_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,9 @@ string t_php_generator::render_const_value(t_type* type, t_const_value* value) {
out << value->get_double();
}
break;
case t_base_type::TYPE_UUID:
out << '"' << get_escaped_string(value) << '"';
break;
default:
throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase);
}
Expand Down Expand Up @@ -2175,6 +2178,15 @@ void t_php_generator::generate_deserialize_field(ostream& out,
out << indent() << "$arr = unpack('d', strrev(" << itrans << "->readAll(8)));" << '\n'
<< indent() << "$" << name << " = $arr[1];" << '\n';
break;
case t_base_type::TYPE_UUID:
out << indent() << "$_uuid_bin = " << itrans << "->readAll(16);" << '\n'
<< indent() << "$_uuid_hex = bin2hex($_uuid_bin);" << '\n'
<< indent() << "$" << name << " = substr($_uuid_hex, 0, 8) . '-' . "
<< "substr($_uuid_hex, 8, 4) . '-' . "
<< "substr($_uuid_hex, 12, 4) . '-' . "
<< "substr($_uuid_hex, 16, 4) . '-' . "
<< "substr($_uuid_hex, 20, 12);" << '\n';
break;
default:
throw "compiler error: no PHP name for base type " + t_base_type::t_base_name(tbase)
+ tfield->get_name();
Expand Down Expand Up @@ -2216,6 +2228,9 @@ void t_php_generator::generate_deserialize_field(ostream& out,
case t_base_type::TYPE_DOUBLE:
out << "readDouble($" << name << ");";
break;
case t_base_type::TYPE_UUID:
out << "readUuid($" << name << ");";
break;
default:
throw "compiler error: no PHP name for base type " + t_base_type::t_base_name(tbase);
}
Expand Down Expand Up @@ -2417,6 +2432,9 @@ void t_php_generator::generate_serialize_field(ostream& out, t_field* tfield, st
case t_base_type::TYPE_DOUBLE:
out << indent() << "$output .= strrev(pack('d', $" << name << "));" << '\n';
break;
case t_base_type::TYPE_UUID:
out << indent() << "$output .= hex2bin(str_replace('-', '', $" << name << "));" << '\n';
break;
default:
throw "compiler error: no PHP name for base type " + t_base_type::t_base_name(tbase);
}
Expand Down Expand Up @@ -2454,6 +2472,9 @@ void t_php_generator::generate_serialize_field(ostream& out, t_field* tfield, st
case t_base_type::TYPE_DOUBLE:
out << "writeDouble($" << name << ");";
break;
case t_base_type::TYPE_UUID:
out << "writeUuid($" << name << ");";
break;
default:
throw "compiler error: no PHP name for base type " + t_base_type::t_base_name(tbase);
}
Expand Down Expand Up @@ -2782,6 +2803,8 @@ string t_php_generator::type_to_cast(t_type* type) {
return "(double)";
case t_base_type::TYPE_STRING:
return "(string)";
case t_base_type::TYPE_UUID:
return "(string)";
default:
return "";
}
Expand Down Expand Up @@ -2816,6 +2839,8 @@ string t_php_generator::type_to_enum(t_type* type) {
return "TType::I64";
case t_base_type::TYPE_DOUBLE:
return "TType::DOUBLE";
case t_base_type::TYPE_UUID:
return "TType::UUID";
default:
throw "compiler error: unhandled type";
}
Expand Down Expand Up @@ -2859,6 +2884,8 @@ string t_php_generator::type_to_phpdoc(t_type* type) {
return "int";
case t_base_type::TYPE_DOUBLE:
return "double";
case t_base_type::TYPE_UUID:
return "string";
default:
throw "compiler error: unhandled type";
}
Expand Down
3 changes: 2 additions & 1 deletion lib/php/lib/Base/TBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ abstract class TBase
TType::I32 => 'I32',
TType::I64 => 'I64',
TType::DOUBLE => 'Double',
TType::STRING => 'String'
TType::STRING => 'String',
TType::UUID => 'Uuid'
);

abstract public function read($input);
Expand Down
22 changes: 22 additions & 0 deletions lib/php/lib/Protocol/TBinaryProtocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,15 @@ public function writeString($value)
return $result + $len;
}

public function writeUuid($uuid)
{
self::validateUuid($uuid);
$data = hex2bin(str_replace('-', '', $uuid));
$this->trans_->write($data, 16);

return 16;
}

public function readMessageBegin(&$name, &$type, &$seqid)
{
$result = $this->readI32($sz);
Expand Down Expand Up @@ -450,4 +459,17 @@ public function readString(&$value)

return $result + $len;
}

public function readUuid(&$value)
{
$data = $this->trans_->readAll(16);
$hex = bin2hex($data);
$value = substr($hex, 0, 8) . '-' .
substr($hex, 8, 4) . '-' .
substr($hex, 12, 4) . '-' .
substr($hex, 16, 4) . '-' .
substr($hex, 20, 12);

return 16;
}
}
25 changes: 25 additions & 0 deletions lib/php/lib/Protocol/TCompactProtocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class TCompactProtocol extends TProtocol
const COMPACT_SET = 0x0A;
const COMPACT_MAP = 0x0B;
const COMPACT_STRUCT = 0x0C;
const COMPACT_UUID = 0x0D;

const STATE_CLEAR = 0;
const STATE_FIELD_WRITE = 1;
Expand Down Expand Up @@ -76,6 +77,7 @@ class TCompactProtocol extends TProtocol
TType::LST => TCompactProtocol::COMPACT_LIST,
TType::SET => TCompactProtocol::COMPACT_SET,
TType::MAP => TCompactProtocol::COMPACT_MAP,
TType::UUID => TCompactProtocol::COMPACT_UUID,
);

protected static $ttypes = array(
Expand All @@ -92,6 +94,7 @@ class TCompactProtocol extends TProtocol
TCompactProtocol::COMPACT_LIST => TType::LST,
TCompactProtocol::COMPACT_SET => TType::SET,
TCompactProtocol::COMPACT_MAP => TType::MAP,
TCompactProtocol::COMPACT_UUID => TType::UUID,
);

protected $state = TCompactProtocol::STATE_CLEAR;
Expand Down Expand Up @@ -370,6 +373,15 @@ public function writeString($value)
return $result + $len;
}

public function writeUuid($uuid)
{
self::validateUuid($uuid);
$data = hex2bin(str_replace('-', '', $uuid));
$this->trans_->write($data, 16);

return 16;
}

public function readFieldBegin(&$name, &$field_type, &$field_id)
{
$result = $this->readUByte($compact_type_and_delta);
Expand Down Expand Up @@ -587,6 +599,19 @@ public function readString(&$value)
return $result + $len;
}

public function readUuid(&$value)
{
$data = $this->trans_->readAll(16);
$hex = bin2hex($data);
$value = substr($hex, 0, 8) . '-' .
substr($hex, 8, 4) . '-' .
substr($hex, 12, 4) . '-' .
substr($hex, 16, 4) . '-' .
substr($hex, 20, 12);

return 16;
}

public function getTType($byte)
{
return self::$ttypes[$byte & 0x0f];
Expand Down
20 changes: 20 additions & 0 deletions lib/php/lib/Protocol/TJSONProtocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class TJSONProtocol extends TProtocol
const NAME_MAP = "map";
const NAME_LIST = "lst";
const NAME_SET = "set";
const NAME_UUID = "uid";

private function getTypeNameForTypeID($typeID)
{
Expand All @@ -100,6 +101,8 @@ private function getTypeNameForTypeID($typeID)
return self::NAME_SET;
case TType::LST:
return self::NAME_LIST;
case TType::UUID:
return self::NAME_UUID;
default:
throw new TProtocolException("Unrecognized type", TProtocolException::UNKNOWN);
}
Expand Down Expand Up @@ -149,6 +152,9 @@ private function getTypeIDForTypeName($name)
case 't':
$result = TType::BOOL;
break;
case 'u':
$result = TType::UUID;
break;
}
}
if ($result == TType::STOP) {
Expand Down Expand Up @@ -574,6 +580,12 @@ public function writeString($str)
$this->writeJSONString($str);
}

public function writeUuid($uuid)
{
self::validateUuid($uuid);
$this->writeJSONString($uuid);
}

/**
* Reads the message header
*
Expand Down Expand Up @@ -730,4 +742,12 @@ public function readString(&$str)

return true;
}

public function readUuid(&$uuid)
{
$uuid = $this->readJSONString(false);
self::validateUuid($uuid);

return true;
}
}
15 changes: 15 additions & 0 deletions lib/php/lib/Protocol/TProtocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ abstract public function writeDouble($dub);

abstract public function writeString($str);

abstract public function writeUuid($uuid);

protected static function validateUuid($uuid)
{
if (!is_string($uuid) || !preg_match('/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/', $uuid)) {
throw new TProtocolException('Invalid UUID format', TProtocolException::INVALID_DATA);
}
}

/**
* Reads the message header
*
Expand Down Expand Up @@ -177,6 +186,8 @@ abstract public function readDouble(&$dub);

abstract public function readString(&$str);

abstract public function readUuid(&$uuid);

/**
* The skip function is a utility to parse over unrecognized date without
* causing corruption.
Expand All @@ -200,6 +211,8 @@ public function skip($type)
return $this->readDouble($dub);
case TType::STRING:
return $this->readString($str);
case TType::UUID:
return $this->readUuid($uuid);
case TType::STRUCT:
$result = $this->readStructBegin($name);
while (true) {
Expand Down Expand Up @@ -271,6 +284,8 @@ public static function skipBinary($itrans, $type)
return $itrans->readAll(8);
case TType::DOUBLE:
return $itrans->readAll(8);
case TType::UUID:
return $itrans->readAll(16);
case TType::STRING:
$len = unpack('N', $itrans->readAll(4));
$len = $len[1];
Expand Down
10 changes: 10 additions & 0 deletions lib/php/lib/Protocol/TProtocolDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ public function writeString($str)
return $this->concreteProtocol_->writeString($str);
}

public function writeUuid($uuid)
{
return $this->concreteProtocol_->writeUuid($uuid);
}

/**
* Reads the message header
*
Expand Down Expand Up @@ -282,4 +287,9 @@ public function readString(&$str)
{
return $this->concreteProtocol_->readString($str);
}

public function readUuid(&$uuid)
{
return $this->concreteProtocol_->readUuid($uuid);
}
}
10 changes: 10 additions & 0 deletions lib/php/lib/Protocol/TSimpleJSONProtocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@ public function writeString($str)
$this->writeJSONString($str);
}

public function writeUuid($uuid)
{
$this->writeJSONString($uuid);
}

/**
* Reading methods.
*
Expand Down Expand Up @@ -372,4 +377,9 @@ public function readString(&$str)
{
throw new TException("Not implemented");
}

public function readUuid(&$uuid)
{
throw new TException("Not implemented");
}
}
1 change: 1 addition & 0 deletions lib/php/lib/Type/TType.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class TType
const MAP = 13;
const SET = 14;
const LST = 15; // N.B. cannot use LIST keyword in PHP!
const UUID = 16;
const UTF8 = 16;
const UTF16 = 17;
}
2 changes: 1 addition & 1 deletion lib/php/test/Resources/ThriftTest.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

namespace php TestValidators

include "../../../../test/v0.16/ThriftTest.thrift"
include "../../../../test/ThriftTest.thrift"

union UnionOfStrings {
1: string aa;
Expand Down
Loading