Change ToTypedData logic to match Host
This commit is contained in:
Родитель
f22f6ef98b
Коммит
91b2bf8516
|
@ -12,6 +12,7 @@
|
|||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.1" />
|
||||
<PackageReference Include="Microsoft.PowerShell.SDK" Version="6.0.4" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
@ -135,8 +135,6 @@ Set-Variable -Name '$return' -Value $return -Scope global
|
|||
{
|
||||
Name = binding.Key,
|
||||
Data = TypeConverter.ToTypedData(
|
||||
binding.Key,
|
||||
binding.Value,
|
||||
result[binding.Key])
|
||||
};
|
||||
|
||||
|
|
|
@ -5,12 +5,14 @@ using Microsoft.Azure.WebJobs.Script.Grpc.Messages;
|
|||
using System.Net.Http;
|
||||
using static Microsoft.Azure.WebJobs.Script.Grpc.Messages.TypedData;
|
||||
using System;
|
||||
using Newtonsoft.Json;
|
||||
using System.Collections;
|
||||
|
||||
namespace Microsoft.Azure.Functions.PowerShellWorker.Utility
|
||||
{
|
||||
public class TypeConverter
|
||||
{
|
||||
public static object FromTypedData (TypedData data)
|
||||
public static object ToObject (TypedData data)
|
||||
{
|
||||
switch (data.DataCase)
|
||||
{
|
||||
|
@ -36,103 +38,43 @@ namespace Microsoft.Azure.Functions.PowerShellWorker.Utility
|
|||
}
|
||||
}
|
||||
|
||||
public static TypedData ToTypedData (string bindingName, BindingInfo binding, object psobject)
|
||||
public static TypedData ToTypedData(object value)
|
||||
{
|
||||
switch (binding.Type)
|
||||
{
|
||||
case "json":
|
||||
TypedData typedData = new TypedData();
|
||||
|
||||
if(!LanguagePrimitives.TryConvertTo<string>(
|
||||
psobject,
|
||||
out string jsonVal))
|
||||
if (value == null)
|
||||
{
|
||||
throw new PSInvalidCastException();
|
||||
return typedData;
|
||||
}
|
||||
return new TypedData()
|
||||
{
|
||||
Json = jsonVal
|
||||
};
|
||||
|
||||
case "bytes":
|
||||
|
||||
if(!LanguagePrimitives.TryConvertTo<ByteString>(
|
||||
psobject,
|
||||
out ByteString bytesVal))
|
||||
if (LanguagePrimitives.TryConvertTo<byte[]>(
|
||||
value, out byte[] arr))
|
||||
{
|
||||
throw new PSInvalidCastException();
|
||||
typedData.Bytes = ByteString.CopyFrom(arr);
|
||||
}
|
||||
return new TypedData()
|
||||
else if(LanguagePrimitives.TryConvertTo<HttpResponseContext>(
|
||||
value, out HttpResponseContext http))
|
||||
{
|
||||
Bytes = bytesVal
|
||||
};
|
||||
|
||||
case "double":
|
||||
|
||||
if(!LanguagePrimitives.TryConvertTo<double>(
|
||||
psobject,
|
||||
out double doubleVal))
|
||||
{
|
||||
throw new PSInvalidCastException();
|
||||
typedData.Http = ToRpcHttp(http);
|
||||
}
|
||||
return new TypedData()
|
||||
else if (LanguagePrimitives.TryConvertTo<Hashtable>(
|
||||
value, out Hashtable hashtable))
|
||||
{
|
||||
Double = doubleVal
|
||||
};
|
||||
|
||||
case "http":
|
||||
|
||||
if(!LanguagePrimitives.TryConvertTo<HttpResponseContext>(
|
||||
psobject,
|
||||
out HttpResponseContext httpVal))
|
||||
{
|
||||
throw new PSInvalidCastException();
|
||||
typedData.Json = JsonConvert.SerializeObject(hashtable);
|
||||
}
|
||||
return new TypedData()
|
||||
else if (LanguagePrimitives.TryConvertTo<string>(
|
||||
value, out string str))
|
||||
{
|
||||
Http = ToRpcHttp(httpVal)
|
||||
};
|
||||
|
||||
case "int":
|
||||
|
||||
if(!LanguagePrimitives.TryConvertTo<int>(
|
||||
psobject,
|
||||
out int intVal))
|
||||
try
|
||||
{
|
||||
throw new PSInvalidCastException();
|
||||
typedData.Json = JsonConvert.SerializeObject(str);
|
||||
}
|
||||
return new TypedData()
|
||||
catch
|
||||
{
|
||||
Int = intVal
|
||||
};
|
||||
|
||||
case "stream":
|
||||
|
||||
if(!LanguagePrimitives.TryConvertTo<ByteString>(
|
||||
psobject,
|
||||
out ByteString streamVal))
|
||||
{
|
||||
throw new PSInvalidCastException();
|
||||
typedData.String = str;
|
||||
}
|
||||
return new TypedData()
|
||||
{
|
||||
Stream = streamVal
|
||||
};
|
||||
|
||||
case "string":
|
||||
|
||||
if(!LanguagePrimitives.TryConvertTo<string>(
|
||||
psobject,
|
||||
out string stringVal))
|
||||
{
|
||||
throw new PSInvalidCastException();
|
||||
}
|
||||
return new TypedData()
|
||||
{
|
||||
String = stringVal
|
||||
};
|
||||
default:
|
||||
throw new PSInvalidCastException("could not parse type");
|
||||
}
|
||||
return typedData;
|
||||
}
|
||||
|
||||
public static HttpRequestContext ToHttpContext (RpcHttp rpcHttp)
|
||||
|
@ -149,12 +91,12 @@ namespace Microsoft.Azure.Functions.PowerShellWorker.Utility
|
|||
|
||||
if (rpcHttp.Body != null)
|
||||
{
|
||||
httpRequestContext.Body = FromTypedData(rpcHttp.Body);
|
||||
httpRequestContext.Body = ToObject(rpcHttp.Body);
|
||||
}
|
||||
|
||||
if (rpcHttp.RawBody != null)
|
||||
{
|
||||
httpRequestContext.Body = FromTypedData(rpcHttp.RawBody);
|
||||
httpRequestContext.Body = ToObject(rpcHttp.RawBody);
|
||||
}
|
||||
|
||||
return httpRequestContext;
|
||||
|
|
Загрузка…
Ссылка в новой задаче