Skip to content
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

fix(dotnet-sdk): refactor task signature to not build schema output with no return type #1170

Merged
merged 1 commit into from
Dec 2, 2024
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
19 changes: 4 additions & 15 deletions sdk-dotnet/LittleHorse.Sdk.Tests/Helper/LHMappingHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using LittleHorse.Common.Proto;
using LittleHorse.Sdk.Helper;
using Google.Protobuf.WellKnownTypes;
using LittleHorse.Sdk.Exceptions;
using LittleHorse.Sdk.Tests;
using Xunit;
using Type = System.Type;
Expand Down Expand Up @@ -70,16 +69,6 @@ public void LHHelper_WithSystemBytesVariableType_ShouldReturnLHVariableBytesType
Assert.True(result == VariableType.Bytes);
}

[Fact]
public void LHHelper_WithSystemVoidVariableType_ShouldReturnLHVariableJsonObjType()
{
var type = typeof(void);

var result = LHMappingHelper.MapDotNetTypeToLHVariableType(type);

Assert.True(result == VariableType.JsonObj);
}

[Fact]
public void LHHelper_WithSystemArrayObjectVariableType_ShouldReturnLHVariableJsonArrType()
{
Expand All @@ -94,15 +83,15 @@ public void LHHelper_WithSystemArrayObjectVariableType_ShouldReturnLHVariableJso
}

[Fact]
public void LHHelper_WithoutSystemVariableType_ShouldThrowException()
public void LHHelper_WithNotAllowedSystemVariableTypes_ShouldReturnLHJsonObj()
{
var test_not_allowed_types = new List<Type>() { typeof(decimal), typeof(char) };
var test_not_allowed_types = new List<Type>() { typeof(decimal), typeof(char), typeof(void) };

foreach (var type in test_not_allowed_types)
{
var exception = Assert.Throws<Exception>(() => LHMappingHelper.MapDotNetTypeToLHVariableType(type));
var result = LHMappingHelper.MapDotNetTypeToLHVariableType(type);

Assert.Equal($"Unaccepted variable type.", exception.Message);
Assert.Equal(VariableType.JsonObj, result);
}
}

Expand Down
33 changes: 33 additions & 0 deletions sdk-dotnet/LittleHorse.Sdk.Tests/LHTaskSignatureTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
public class LHTaskSignatureTest
{
const string TASK_DEF_NAME_ADD = "add-task-worker";
const string TASK_DEF_NAME_INFORM = "inform-task-worker";
const string TASK_DEF_NAME_UPDATE = "update-task-worker";
const string TASK_DEF_NAME_DELETE = "delete-task-worker";
const string TASK_DEF_NAME_GET = "get-task-worker";
Expand Down Expand Up @@ -63,6 +64,32 @@ public void TaskSignature_WithLHTaskMethodAndLHTypeAttributes_ShouldBuildLHSigna
Assert.False(taskSignature.HasWorkerContextAtEnd);
}

[Fact]
public void TaskSignature_WithoutReturnTypeInLHTaskMethod_ShouldBuildLHSignatureWithoutSchemaOutput()
{
int number_of_method_params = 1;

var taskSignature = new LHTaskSignature<TestWorker>(TASK_DEF_NAME_INFORM, new TestWorker());

var expectedLHMethodParam = new LHMethodParam
{
Type = VariableType.Str,
Name = "name",
IsMasked = TRUE_IS_MASKET
};

Assert.True(taskSignature.LhMethodParams.Count == number_of_method_params);
foreach (var actualLHMethodParam in taskSignature.LhMethodParams)
{
Assert.Equal(expectedLHMethodParam.Name, actualLHMethodParam.Name);
Assert.Equal(expectedLHMethodParam.Type, actualLHMethodParam.Type);
Assert.Equal(expectedLHMethodParam.IsMasked, actualLHMethodParam.IsMasked);
}

Assert.Null(taskSignature.TaskDefOutputSchema);
Assert.False(taskSignature.HasWorkerContextAtEnd);
}

[Fact]
public void TaskSignature_WithLHTaskMethodAndLHTypeAttributes_ShouldBuildSignatureWithOutputResult()
{
Expand Down Expand Up @@ -202,6 +229,12 @@ public string Add([LHType(masked: TRUE_IS_MASKET)] string name)
return $"Output value: {name}";
}

[LHTaskMethod(TASK_DEF_NAME_INFORM)]
public void Inform([LHType(masked: TRUE_IS_MASKET)] string name)
{
var test_variable = "test_variable" + name;
}

[LHTaskMethod(TASK_DEF_NAME_UPDATE)]
[LHType(masked: TRUE_IS_MASKET, name: "result")]
public string Update(int value)
Expand Down
13 changes: 1 addition & 12 deletions sdk-dotnet/LittleHorse.Sdk/Helper/LHMappingHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections;
using System.Net;
using Google.Protobuf;
using Google.Protobuf.WellKnownTypes;
using LittleHorse.Common.Proto;
Expand Down Expand Up @@ -45,17 +44,7 @@ public static VariableType MapDotNetTypeToLHVariableType(Type type)
return VariableType.JsonArr;
}

if (!type.Namespace!.StartsWith("System"))
{
return VariableType.JsonObj;
}

if (type.IsAssignableFrom(typeof(void)))
{
return VariableType.JsonObj;
}

throw new Exception("Unaccepted variable type.");
return VariableType.JsonObj;
}

public static DateTime? MapDateTimeFromProtoTimeStamp(Timestamp protoTimestamp)
Expand Down
9 changes: 5 additions & 4 deletions sdk-dotnet/LittleHorse.Sdk/Worker/LHTaskSignature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ public LHTaskSignature(string taskDefName, T executable)

var methodParams = TaskMethod.GetParameters();

CreateInputVarsSignature(methodParams);
BuildInputVarsSignature(methodParams);

CreateOutputSchemaSignature();
if (!TaskMethod.ReturnType.IsAssignableFrom(typeof(void)))
BuildOutputSchemaSignature();
}

private bool IsValidLHTaskWorkerValue(Attribute? lhtaskWorkerAttribute, string taskDefName)
Expand All @@ -65,7 +66,7 @@ private bool IsValidLHTaskWorkerValue(Attribute? lhtaskWorkerAttribute, string t
return false;
}

private void CreateInputVarsSignature(ParameterInfo[] methodParams)
private void BuildInputVarsSignature(ParameterInfo[] methodParams)
{
for (int i = 0; i < methodParams.Length; i++)
{
Expand Down Expand Up @@ -107,7 +108,7 @@ private void CreateInputVarsSignature(ParameterInfo[] methodParams)
}
}

private void CreateOutputSchemaSignature()
private void BuildOutputSchemaSignature()
{
var returnType = LHMappingHelper.MapDotNetTypeToLHVariableType(TaskMethod.ReturnType);
var maskedValue = false;
Expand Down
13 changes: 13 additions & 0 deletions sdk-dotnet/Littlehorse.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BasicExample", "Examples\Ba
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MaskedFieldsExample", "Examples\MaskedFieldsExample\MaskedFieldsExample.csproj", "{AA9767C3-9F03-4414-A4C2-A0C3761A975C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LittleHorse.Sdk.Tests", "LittleHorse.Sdk.Tests\LittleHorse.Sdk.Tests.csproj", "{AC276D3F-ABAB-4E2D-B721-099681083390}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExceptionsHandlerExample", "Examples\ExceptionsHandlerExample\ExceptionsHandlerExample.csproj", "{6B7A8034-21C0-465D-8708-4F47A1780972}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -32,9 +36,18 @@ Global
{AA9767C3-9F03-4414-A4C2-A0C3761A975C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AA9767C3-9F03-4414-A4C2-A0C3761A975C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AA9767C3-9F03-4414-A4C2-A0C3761A975C}.Release|Any CPU.Build.0 = Release|Any CPU
{AC276D3F-ABAB-4E2D-B721-099681083390}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AC276D3F-ABAB-4E2D-B721-099681083390}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AC276D3F-ABAB-4E2D-B721-099681083390}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AC276D3F-ABAB-4E2D-B721-099681083390}.Release|Any CPU.Build.0 = Release|Any CPU
{6B7A8034-21C0-465D-8708-4F47A1780972}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6B7A8034-21C0-465D-8708-4F47A1780972}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6B7A8034-21C0-465D-8708-4F47A1780972}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6B7A8034-21C0-465D-8708-4F47A1780972}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{88CB23B3-1174-45F5-9F72-46AB57BAB661} = {EEC3BB78-BFA5-4525-A965-4EAA3455BBAE}
{AA9767C3-9F03-4414-A4C2-A0C3761A975C} = {EEC3BB78-BFA5-4525-A965-4EAA3455BBAE}
{6B7A8034-21C0-465D-8708-4F47A1780972} = {EEC3BB78-BFA5-4525-A965-4EAA3455BBAE}
EndGlobalSection
EndGlobal