From 073cfb71e359201a10ba8f0d767dc38026438695 Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Fri, 7 Sep 2018 14:30:09 -0700 Subject: [PATCH] switch to pattern matching and support all other TypedData types --- src/Utility/TypeExtensions.cs | 65 ++++++++++++++++++----------- test/Utility/TypeExtensionsTests.cs | 25 ++++++----- 2 files changed, 54 insertions(+), 36 deletions(-) diff --git a/src/Utility/TypeExtensions.cs b/src/Utility/TypeExtensions.cs index 8c3e01a2..b641248d 100644 --- a/src/Utility/TypeExtensions.cs +++ b/src/Utility/TypeExtensions.cs @@ -5,6 +5,7 @@ using System; using System.Collections; +using System.IO; using System.Management.Automation; using Google.Protobuf; @@ -137,31 +138,45 @@ public static TypedData ToTypedData(this object value) return typedData; } - if (LanguagePrimitives.TryConvertTo(value, out byte[] arr)) - { - typedData.Bytes = ByteString.CopyFrom(arr); - } - else if(LanguagePrimitives.TryConvertTo(value, out HttpResponseContext http)) - { - typedData.Http = http.ToRpcHttp(); - } - else if (LanguagePrimitives.TryConvertTo(value, out IDictionary hashtable)) - { - typedData.Json = JsonConvert.SerializeObject(hashtable); - } - else if (LanguagePrimitives.TryConvertTo(value, out string str)) - { - // Attempt to parse the string into json. If it fails, - // fallback to storing as a string - try - { - JsonConvert.DeserializeObject(str); - typedData.Json = str; - } - catch - { - typedData.String = str; - } + switch (value) + { + case double d: + typedData.Double = d; + break; + case long l: + typedData.Int = l; + break; + case int i: + typedData.Int = i; + break; + case byte[] arr: + typedData.Bytes = ByteString.CopyFrom(arr); + break; + case Stream s: + typedData.Stream = ByteString.FromStream(s); + break; + case HttpResponseContext http: + typedData.Http = http.ToRpcHttp(); + break; + case IDictionary hashtable: + typedData.Json = JsonConvert.SerializeObject(hashtable); + break; + default: + // Handle everything else as a string + var str = value.ToString(); + + // Attempt to parse the string into json. If it fails, + // fallback to storing as a string + try + { + JsonConvert.DeserializeObject(str); + typedData.Json = str; + } + catch + { + typedData.String = str; + } + break; } return typedData; } diff --git a/test/Utility/TypeExtensionsTests.cs b/test/Utility/TypeExtensionsTests.cs index a2237880..30807ee4 100644 --- a/test/Utility/TypeExtensionsTests.cs +++ b/test/Utility/TypeExtensionsTests.cs @@ -5,6 +5,7 @@ using System; using System.Collections; +using System.IO; using Google.Protobuf; using Google.Protobuf.Collections; @@ -314,10 +315,10 @@ public void TestObjectToTypedDataRpcHttpStatusCodeString() Assert.Equal(expected, input.ToTypedData()); } - [Fact(Skip = "Int gets interpreted as byte[]")] + [Fact] public void TestObjectToTypedDataInt() { - var data = (long)1; + var data = 1; var input = (object)data; var expected = new TypedData @@ -328,7 +329,7 @@ public void TestObjectToTypedDataInt() Assert.Equal(expected, input.ToTypedData()); } - [Fact(Skip = "Double gets interpreted as byte[]")] + [Fact] public void TestObjectToTypedDataDouble() { var data = 1.1; @@ -370,18 +371,20 @@ public void TestObjectToTypedDataBytes() Assert.Equal(expected, input.ToTypedData()); } - [Fact(Skip = "Stream gets interpreted as Bytes")] + [Fact] public void TestObjectToTypedDataStream() { - var data = ByteString.CopyFromUtf8("Hello World!").ToByteArray(); - - var input = (object)data; - var expected = new TypedData + using(MemoryStream data = new MemoryStream(100)) { - Stream = ByteString.CopyFrom(data) - }; - Assert.Equal(expected, input.ToTypedData()); + var input = (object)data; + var expected = new TypedData + { + Stream = ByteString.FromStream(data) + }; + + Assert.Equal(expected, input.ToTypedData()); + } } [Fact]