Skip to content

Commit 3cbb399

Browse files
TylerLeonhardtdaxian-dbw
authored andcommitted
Switch to pattern matching and support all other TypedData types (#39)
1 parent db89422 commit 3cbb399

File tree

2 files changed

+54
-36
lines changed

2 files changed

+54
-36
lines changed

src/Utility/TypeExtensions.cs

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
using System;
77
using System.Collections;
8+
using System.IO;
89
using System.Management.Automation;
910

1011
using Google.Protobuf;
@@ -137,31 +138,45 @@ public static TypedData ToTypedData(this object value)
137138
return typedData;
138139
}
139140

140-
if (LanguagePrimitives.TryConvertTo(value, out byte[] arr))
141-
{
142-
typedData.Bytes = ByteString.CopyFrom(arr);
143-
}
144-
else if(LanguagePrimitives.TryConvertTo(value, out HttpResponseContext http))
145-
{
146-
typedData.Http = http.ToRpcHttp();
147-
}
148-
else if (LanguagePrimitives.TryConvertTo(value, out IDictionary hashtable))
149-
{
150-
typedData.Json = JsonConvert.SerializeObject(hashtable);
151-
}
152-
else if (LanguagePrimitives.TryConvertTo(value, out string str))
153-
{
154-
// Attempt to parse the string into json. If it fails,
155-
// fallback to storing as a string
156-
try
157-
{
158-
JsonConvert.DeserializeObject(str);
159-
typedData.Json = str;
160-
}
161-
catch
162-
{
163-
typedData.String = str;
164-
}
141+
switch (value)
142+
{
143+
case double d:
144+
typedData.Double = d;
145+
break;
146+
case long l:
147+
typedData.Int = l;
148+
break;
149+
case int i:
150+
typedData.Int = i;
151+
break;
152+
case byte[] arr:
153+
typedData.Bytes = ByteString.CopyFrom(arr);
154+
break;
155+
case Stream s:
156+
typedData.Stream = ByteString.FromStream(s);
157+
break;
158+
case HttpResponseContext http:
159+
typedData.Http = http.ToRpcHttp();
160+
break;
161+
case IDictionary hashtable:
162+
typedData.Json = JsonConvert.SerializeObject(hashtable);
163+
break;
164+
default:
165+
// Handle everything else as a string
166+
var str = value.ToString();
167+
168+
// Attempt to parse the string into json. If it fails,
169+
// fallback to storing as a string
170+
try
171+
{
172+
JsonConvert.DeserializeObject(str);
173+
typedData.Json = str;
174+
}
175+
catch
176+
{
177+
typedData.String = str;
178+
}
179+
break;
165180
}
166181
return typedData;
167182
}

test/Utility/TypeExtensionsTests.cs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
using System;
77
using System.Collections;
8+
using System.IO;
89

910
using Google.Protobuf;
1011
using Google.Protobuf.Collections;
@@ -314,10 +315,10 @@ public void TestObjectToTypedDataRpcHttpStatusCodeString()
314315
Assert.Equal(expected, input.ToTypedData());
315316
}
316317

317-
[Fact(Skip = "Int gets interpreted as byte[]")]
318+
[Fact]
318319
public void TestObjectToTypedDataInt()
319320
{
320-
var data = (long)1;
321+
var data = 1;
321322

322323
var input = (object)data;
323324
var expected = new TypedData
@@ -328,7 +329,7 @@ public void TestObjectToTypedDataInt()
328329
Assert.Equal(expected, input.ToTypedData());
329330
}
330331

331-
[Fact(Skip = "Double gets interpreted as byte[]")]
332+
[Fact]
332333
public void TestObjectToTypedDataDouble()
333334
{
334335
var data = 1.1;
@@ -370,18 +371,20 @@ public void TestObjectToTypedDataBytes()
370371
Assert.Equal(expected, input.ToTypedData());
371372
}
372373

373-
[Fact(Skip = "Stream gets interpreted as Bytes")]
374+
[Fact]
374375
public void TestObjectToTypedDataStream()
375376
{
376-
var data = ByteString.CopyFromUtf8("Hello World!").ToByteArray();
377-
378-
var input = (object)data;
379-
var expected = new TypedData
377+
using(MemoryStream data = new MemoryStream(100))
380378
{
381-
Stream = ByteString.CopyFrom(data)
382-
};
383379

384-
Assert.Equal(expected, input.ToTypedData());
380+
var input = (object)data;
381+
var expected = new TypedData
382+
{
383+
Stream = ByteString.FromStream(data)
384+
};
385+
386+
Assert.Equal(expected, input.ToTypedData());
387+
}
385388
}
386389

387390
[Fact]

0 commit comments

Comments
 (0)