THRIFT-5591 Add uuid type to IDL and implement reference code (+ improved self-tests)
Client: compiler general, netstd, Delphi
Patch: Jens Geyer
diff --git a/lib/delphi/src/Thrift.Protocol.JSON.pas b/lib/delphi/src/Thrift.Protocol.JSON.pas
index 52909b7..2a9682c 100644
--- a/lib/delphi/src/Thrift.Protocol.JSON.pas
+++ b/lib/delphi/src/Thrift.Protocol.JSON.pas
@@ -198,6 +198,7 @@
procedure WriteDouble( const d: Double); override;
procedure WriteString( const s: string ); override;
procedure WriteBinary( const b: TBytes); override;
+ procedure WriteUuid( const uuid: TGuid); override;
//
function ReadMessageBegin: TThriftMessage; override;
procedure ReadMessageEnd(); override;
@@ -219,6 +220,7 @@
function ReadDouble:Double; override;
function ReadString : string; override;
function ReadBinary: TBytes; override;
+ function ReadUuid: TGuid; override;
strict private
@@ -288,6 +290,7 @@
NAME_MAP = 'map';
NAME_LIST = 'lst';
NAME_SET = 'set';
+ NAME_UUID = 'uid';
INVARIANT_CULTURE : TFormatSettings
= ( ThousandSeparator: ',';
@@ -317,6 +320,7 @@
TType.Map: result := NAME_MAP;
TType.Set_: result := NAME_SET;
TType.List: result := NAME_LIST;
+ TType.Uuid: result := NAME_UUID;
else
raise TProtocolExceptionNotImplemented.Create('Unrecognized type ('+IntToStr(Ord(typeID))+')');
end;
@@ -336,6 +340,7 @@
else if name = NAME_MAP then result := TType.Map
else if name = NAME_LIST then result := TType.List
else if name = NAME_SET then result := TType.Set_
+ else if name = NAME_UUID then result := TType.Uuid
else raise TProtocolExceptionNotImplemented.Create('Unrecognized type ('+name+')');
end;
@@ -831,6 +836,11 @@
WriteJSONBase64( b);
end;
+procedure TJSONProtocolImpl.WriteUuid( const uuid: TGuid);
+begin
+ WriteString( Copy( GuidToString(uuid), 2, 36)); // strip off the { braces }
+end;
+
function TJSONProtocolImpl.ReadJSONString( skipContext : Boolean) : TBytes;
var buffer : TThriftMemoryStream;
@@ -1237,6 +1247,12 @@
end;
+function TJSONProtocolImpl.ReadUuid: TGuid;
+begin
+ result := StringToGUID( '{' + ReadString + '}');
+end;
+
+
function TJSONProtocolImpl.GetMinSerializedSize( const aType : TType) : Integer;
// Return the minimum number of bytes a type will consume on the wire
begin
@@ -1254,6 +1270,7 @@
TType.Map: result := 2; // empty map
TType.Set_: result := 2; // empty set
TType.List: result := 2; // empty list
+ TType.Uuid: result := 36; // "E236974D-F0B0-4E05-8F29-0B455D41B1A1"
else
raise TTransportExceptionBadArgs.Create('Unhandled type code');
end;