Skip to content

Commit ca1a4c6

Browse files
authored
Merge pull request #36 from navtech-io/develop
Develop
2 parents f750c7a + dfd44cf commit ca1a4c6

10 files changed

+287
-194
lines changed

src/Simpleflow/CodeGenerator/SimpleflowCodeVisitor.Helpers.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,18 @@ private Expression GetNumberExpression(string value, Type targetType)
2727
targetType == typeof(decimal) ||
2828
targetType == typeof(double) ||
2929
targetType == typeof(float) ||
30+
targetType == typeof(object) ||
3031
targetType == typeof(byte))
3132
{
3233
return Expression.Constant(Convert.ChangeType(value, targetType), targetType);
3334
}
3435
throw new Exceptions.ValueTypeMismatchException(value, targetType.Name);
3536
}
3637

37-
private Expression GetBoolExpression(string value)
38+
private Expression GetBoolExpression(string value, Type targetType)
3839
{
39-
return Expression.Constant(Convert.ToBoolean(value), typeof(bool));
40+
return Expression.Constant(Convert.ToBoolean(value), targetType == null || targetType == typeof(bool)
41+
? typeof(bool) : targetType );
4042
}
4143

4244
private string GetUnquotedEscapeText(string @string)

src/Simpleflow/CodeGenerator/SimpleflowCodeVisitor.VisitExpression.Predicate.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,21 @@ public override Expression VisitNotExpression([NotNull] SimpleflowParser.NotExpr
7676
private Expression InOperatorExpression(Expression left, Expression right)
7777
{
7878
if (right.Type.GenericTypeArguments.Length == 0
79-
|| right.Type != typeof(List<>).MakeGenericType(right.Type.GenericTypeArguments[0]))
79+
|| right.Type.IsAssignableFrom( typeof(IList<>).MakeGenericType(right.Type.GenericTypeArguments[0])))
8080
{
8181
throw new Exceptions.SimpleflowException(Resources.Message.InOperatorOnList);
8282
}
83-
83+
84+
// Try perform automatic conversion TODO Handle nulls
85+
if (right.Type.GenericTypeArguments[0] == typeof(string))
86+
{
87+
left = ToStringExpression(left);
88+
}
89+
else if (right.Type.GenericTypeArguments[0] != left.Type)
90+
{
91+
left = Expression.Convert(left, right.Type.GenericTypeArguments[0]);
92+
}
93+
8494
return Expression.Call(
8595
right,
8696
right.Type.GetMethod("Contains", right.Type.GenericTypeArguments),

src/Simpleflow/CodeGenerator/SimpleflowCodeVisitor.VisitFunction.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,9 @@ private void CheckInvalidParameters(ParameterInfo[] actualMethodParameters, Simp
128128
private Expression VisitObjectIdentiferAsPerTargetType(SimpleflowParser.ObjectIdentiferExpressionContext objectIdentifier, Type targetType)
129129
{
130130
var objectIdentieferText = objectIdentifier.GetText();
131-
132-
if (objectIdentieferText.Contains(".")
131+
132+
if ( objectIdentieferText.Contains(".") // accessing property
133+
|| objectIdentifier.objectIdentifier()?.identifierIndex()[0]?.index() != null
133134
|| Variables.Any(v => string.Equals(v.Name, objectIdentieferText, StringComparison.OrdinalIgnoreCase)))
134135
{
135136
return Visit(objectIdentifier); // regular object identifier used from variables

src/Simpleflow/CodeGenerator/SimpleflowCodeVisitor.VisitLiterals.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ public override Expression VisitBoolLeteral([NotNull] SimpleflowParser.BoolLeter
4848

4949
var value = context.GetText();
5050

51-
if (targetType == null || targetType == typeof(bool))
51+
if (targetType == null || targetType == typeof(bool) || targetType == typeof(object))
5252
{
53-
return GetBoolExpression(value);
53+
return GetBoolExpression(value, targetType);
5454
}
5555

5656
throw new ValueTypeMismatchException(value);
@@ -62,11 +62,7 @@ public override Expression VisitStringLiteral([NotNull] SimpleflowParser.StringL
6262

6363
var value = GetUnquotedEscapeText(context.String().GetText());
6464

65-
if (targetType == null || targetType == typeof(string))
66-
{
67-
return Expression.Constant(value);
68-
}
69-
else if (targetType != null && targetType.IsEnum) // Handle enum
65+
if (targetType != null && targetType.IsEnum) // Handle Enum
7066
{
7167
if (!TryParseEnum(targetType, value, out object result))
7268
{
@@ -75,6 +71,11 @@ public override Expression VisitStringLiteral([NotNull] SimpleflowParser.StringL
7571
return Expression.Constant(result, targetType);
7672
}
7773

74+
if (targetType == null || targetType == typeof(string) || targetType == typeof(object))
75+
{
76+
return Expression.Constant(value);
77+
}
78+
7879
throw new ValueTypeMismatchException(value);
7980
}
8081

src/Simpleflow/FunctionRegister.BuiltIn.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public static FunctionRegister Default
3939
.Add("Length", (Func<string, int>)StringFunctions.Length)
4040
.Add("Match", (Func<string, string, bool>)StringFunctions.Match)
4141
.Add("Concat", (Func<string, string, string, string, string, string>)StringFunctions.Concat)
42+
43+
.Add("Str", (Func<object, string>)DataTypeConversionFunctions.Str)
44+
4245
;
4346
}
4447
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright (c) navtech.io. All rights reserved.
2+
// See License in the project root for license information.
3+
4+
namespace Simpleflow.Functions
5+
{
6+
internal static class DataTypeConversionFunctions
7+
{
8+
public static string Str(object value) => value?.ToString() ?? string.Empty;
9+
}
10+
}

src/Simpleflow/Simpleflow.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
<PropertyGroup>
33
<TargetFrameworks>net48;netcoreapp3.1;net6.0;</TargetFrameworks>
44
<PackageIcon>PackageIcon.png</PackageIcon>
5-
<VersionPrefix>1.0.11</VersionPrefix>
6-
<VersionSuffix></VersionSuffix>
5+
<VersionPrefix>1.0.12</VersionPrefix>
6+
<VersionSuffix>beta1</VersionSuffix>
77
<PackageReadmeFile>README.md</PackageReadmeFile>
88
</PropertyGroup>
99

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
// Copyright (c) navtech.io. All rights reserved.
2+
// See License in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Text;
7+
using Xunit;
8+
9+
namespace Simpleflow.Tests.Functions
10+
{
11+
public class StringFunctionsTest
12+
{
13+
[Fact]
14+
public void CheckContains()
15+
{
16+
// Arrange
17+
var script =
18+
@"
19+
20+
let hasValue = $Contains(input: arg.Text, value: ""here"" )
21+
let hasValue2 = $Contains(input: arg.Text, value: ""no"" )
22+
23+
output hasValue
24+
output hasValue2
25+
";
26+
27+
// Act
28+
FlowOutput output = SimpleflowEngine.Run(script, new { Text = "its here" });
29+
30+
// Assert
31+
Assert.True((bool)output.Output["hasValue"]);
32+
Assert.False((bool)output.Output["hasValue2"]);
33+
}
34+
35+
[Fact]
36+
public void CheckStartsWithEndsWith()
37+
{
38+
// Arrange
39+
var script =
40+
@"
41+
42+
let hasValue = $StartsWith(input: arg.Text, value: ""its"" )
43+
let hasValue2 = $EndsWith(input: arg.Text, value: ""here"" )
44+
45+
output hasValue
46+
output hasValue2
47+
";
48+
49+
// Act
50+
FlowOutput output = SimpleflowEngine.Run(script, new { Text = "its here" });
51+
52+
// Assert
53+
Assert.True((bool)output.Output["hasValue"]);
54+
Assert.True((bool)output.Output["hasValue2"]);
55+
}
56+
57+
[Fact]
58+
public void CheckTrim()
59+
{
60+
// Arrange
61+
var script =
62+
@"
63+
64+
let trim = $Trim(input: arg.Text, value: "" @"" )
65+
66+
output trim
67+
";
68+
69+
// Act
70+
FlowOutput output = SimpleflowEngine.Run(script, new { Text = " its here@ " });
71+
72+
// Assert
73+
Assert.Equal(expected: "its here", actual: output.Output["trim"]);
74+
}
75+
76+
[Fact]
77+
public void CheckIndexOf()
78+
{
79+
// Arrange
80+
var script =
81+
@"
82+
83+
let index = $IndexOf(input: arg.Text, value: ""here"" )
84+
85+
output index
86+
";
87+
88+
// Act
89+
FlowOutput output = SimpleflowEngine.Run(script, new { Text = " its here@ " });
90+
91+
// Assert
92+
Assert.Equal(expected: 5, actual: output.Output["index"]);
93+
}
94+
95+
[Fact]
96+
public void CheckRegexMatch()
97+
{
98+
// Arrange
99+
var script =
100+
@"
101+
102+
let matched = $match(input: arg.Text, pattern: ""[0-9]*"" )
103+
104+
output matched
105+
";
106+
107+
// Act
108+
FlowOutput output = SimpleflowEngine.Run(script, new { Text = " its here 995 " });
109+
110+
// Assert
111+
Assert.True((bool)output.Output["matched"]);
112+
}
113+
114+
[Fact]
115+
public void CheckSubstring()
116+
{
117+
// Arrange
118+
var script =
119+
@"
120+
121+
let text = $Substring(input: arg.Text, startIndex: 4 )
122+
let text2 = $Substring(input: arg.Text, startIndex: 4, length: 2 )
123+
124+
output text
125+
output text2
126+
";
127+
128+
// Act
129+
FlowOutput output = SimpleflowEngine.Run(script, new { Text = "its here" });
130+
131+
// Assert
132+
Assert.Equal(actual: output.Output["text"], expected: "here");
133+
Assert.Equal(actual: output.Output["text2"], expected: "he");
134+
135+
}
136+
137+
[Fact]
138+
public void CheckStringLength()
139+
{
140+
// Arrange
141+
var script =
142+
@"
143+
144+
let len = $Length(input: arg.Text)
145+
146+
output len
147+
";
148+
149+
// Act
150+
FlowOutput output = SimpleflowEngine.Run(script, new { Text = "its here" });
151+
152+
// Assert
153+
Assert.Equal(actual: output.Output["len"], expected: 8);
154+
}
155+
156+
[Fact]
157+
public void CheckStringConcat()
158+
{
159+
// Arrange
160+
var script =
161+
@"
162+
163+
let val = $Concat(value1: arg.Text, value2: "" abc"")
164+
165+
output val
166+
";
167+
168+
// Act
169+
FlowOutput output = SimpleflowEngine.Run(script, new { Text = "its here" });
170+
171+
// Assert
172+
Assert.Equal(actual: output.Output["val"], expected: "its here abc");
173+
}
174+
175+
[Fact]
176+
public void CheckSubStringAndIndexOf()
177+
{
178+
// Arrange
179+
var script =
180+
@"
181+
182+
let text = $Substring(input: arg.Text,
183+
startIndex: $IndexOf(input: arg.Text, value: '@') + 1
184+
)
185+
output text
186+
";
187+
188+
// Act
189+
FlowOutput output = SimpleflowEngine.Run(script, new { Text = " its here@com" });
190+
191+
// Assert
192+
Assert.Equal(expected: "com", actual: output.Output["text"]);
193+
}
194+
195+
}
196+
}

0 commit comments

Comments
 (0)