Skip to content

switch to pattern matching and support all other TypedData types #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 7, 2018
Merged
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
65 changes: 40 additions & 25 deletions src/Utility/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using System;
using System.Collections;
using System.IO;
using System.Management.Automation;

using Google.Protobuf;
Expand Down Expand Up @@ -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;
}
Expand Down
25 changes: 14 additions & 11 deletions test/Utility/TypeExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using System;
using System.Collections;
using System.IO;

using Google.Protobuf;
using Google.Protobuf.Collections;
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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]
Expand Down