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/netstd/Thrift/Protocol/Utilities/TGuidExtensions.cs b/lib/netstd/Thrift/Protocol/Utilities/TGuidExtensions.cs
new file mode 100644
index 0000000..190ddbb
--- /dev/null
+++ b/lib/netstd/Thrift/Protocol/Utilities/TGuidExtensions.cs
@@ -0,0 +1,82 @@
+// Licensed to the Apache Software Foundation(ASF) under one
+// or more contributor license agreements.See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Text;
+
+namespace Thrift.Protocol.Utilities
+{
+ public static class TGuidExtensions
+ {
+ public static Guid SwapByteOrder(this Guid self)
+ {
+ var bytes = self.ToByteArray();
+
+ // already network order on BigEndian machines
+ if (BitConverter.IsLittleEndian)
+ {
+ SwapBytes(ref bytes[0], ref bytes[3]);
+ SwapBytes(ref bytes[1], ref bytes[2]);
+ SwapBytes(ref bytes[4], ref bytes[5]);
+ SwapBytes(ref bytes[6], ref bytes[7]);
+ }
+
+ return new Guid(bytes);
+ }
+
+ private static void SwapBytes(ref byte one, ref byte two)
+ {
+ var tmp = one;
+ one = two;
+ two = tmp;
+ }
+
+ #region SelfTest
+#if DEBUG
+ static private readonly Guid TEST_GUID = new Guid("{00112233-4455-6677-8899-aabbccddeeff}");
+
+ static TGuidExtensions()
+ {
+ SelfTest();
+ }
+
+ private static void SelfTest()
+ {
+ // host to network
+ var guid = TEST_GUID;
+ guid = guid.SwapByteOrder();
+
+ // validate network order
+ var bytes = guid.ToByteArray();
+ for (var i = 0; i < 10; ++i)
+ {
+ var expected = i * 0x11;
+ Debug.Assert( bytes[i] == expected);
+ }
+
+ // network to host and final validation
+ guid = guid.SwapByteOrder();
+ Debug.Assert(guid.Equals(TEST_GUID));
+ }
+
+#endif
+ #endregion
+
+ }
+}
diff --git a/lib/netstd/Thrift/Protocol/Utilities/TJsonProtocolConstants.cs b/lib/netstd/Thrift/Protocol/Utilities/TJsonProtocolConstants.cs
index 6cc1302..f8c261a 100644
--- a/lib/netstd/Thrift/Protocol/Utilities/TJsonProtocolConstants.cs
+++ b/lib/netstd/Thrift/Protocol/Utilities/TJsonProtocolConstants.cs
@@ -1,4 +1,4 @@
-// Licensed to the Apache Software Foundation(ASF) under one
+// Licensed to the Apache Software Foundation(ASF) under one
// or more contributor license agreements.See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.The ASF licenses this file
@@ -56,6 +56,7 @@
public static readonly byte[] NameMap = { (byte)'m', (byte)'a', (byte)'p' };
public static readonly byte[] NameList = { (byte)'l', (byte)'s', (byte)'t' };
public static readonly byte[] NameSet = { (byte)'s', (byte)'e', (byte)'t' };
+ public static readonly byte[] NameUuid = { (byte)'u', (byte)'i', (byte)'d' };
}
}
-}
\ No newline at end of file
+}
diff --git a/lib/netstd/Thrift/Protocol/Utilities/TJsonProtocolHelper.cs b/lib/netstd/Thrift/Protocol/Utilities/TJsonProtocolHelper.cs
index ff49ebe..67c7bc0 100644
--- a/lib/netstd/Thrift/Protocol/Utilities/TJsonProtocolHelper.cs
+++ b/lib/netstd/Thrift/Protocol/Utilities/TJsonProtocolHelper.cs
@@ -1,4 +1,4 @@
-// Licensed to the Apache Software Foundation(ASF) under one
+// Licensed to the Apache Software Foundation(ASF) under one
// or more contributor license agreements.See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.The ASF licenses this file
@@ -48,6 +48,8 @@
return TJSONProtocolConstants.TypeNames.NameSet;
case TType.List:
return TJSONProtocolConstants.TypeNames.NameList;
+ case TType.Uuid:
+ return TJSONProtocolConstants.TypeNames.NameUuid;
default:
throw new TProtocolException(TProtocolException.NOT_IMPLEMENTED, "Unrecognized exType");
}
@@ -102,6 +104,9 @@
case (byte) 't':
result = TType.Bool;
break;
+ case (byte)'u':
+ result = TType.Uuid;
+ break;
}
}
if (result == TType.Stop)
@@ -173,4 +178,4 @@
return (byte)((char)val + 'a');
}
}
-}
\ No newline at end of file
+}
diff --git a/lib/netstd/Thrift/Protocol/Utilities/TProtocolUtil.cs b/lib/netstd/Thrift/Protocol/Utilities/TProtocolUtil.cs
index 832e46e..3c8b37a 100644
--- a/lib/netstd/Thrift/Protocol/Utilities/TProtocolUtil.cs
+++ b/lib/netstd/Thrift/Protocol/Utilities/TProtocolUtil.cs
@@ -55,6 +55,9 @@
// Don't try to decode the string, just skip it.
await protocol.ReadBinaryAsync(cancellationToken);
break;
+ case TType.Uuid:
+ await protocol.ReadUuidAsync(cancellationToken);
+ break;
case TType.Struct:
await protocol.ReadStructBeginAsync(cancellationToken);
while (true)