Skip to content

Commit c8efbbb

Browse files
author
Rajesh Jinaga
committed
Add support to .NET48
1 parent e514fda commit c8efbbb

16 files changed

+132
-37
lines changed

src/Simpleflow/ArgumentException.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ namespace Simpleflow
88
{
99
internal static class ArgumentException
1010
{
11-
public static void ThrowIfNull(object argument, [CallerArgumentExpression("argument")]string paramName = default)
11+
public static void ThrowIfNull(object argument,
12+
#if NETCOREAPP
13+
[CallerArgumentExpression("argument")]
14+
#endif
15+
string paramName = default)
1216
{
1317
if (argument == null)
1418
{
@@ -23,7 +27,11 @@ public static void ThrowIfNull(object argument, [CallerArgumentExpression("argum
2327
}
2428
}
2529

26-
public static void ThrowIfNullOrEmpty(string argument, [CallerArgumentExpression("argument")] string paramName = default)
30+
public static void ThrowIfNullOrEmpty(string argument,
31+
#if NETCOREAPP
32+
[CallerArgumentExpression("argument")]
33+
#endif
34+
string paramName = default)
2735
{
2836
if (string.IsNullOrWhiteSpace(argument))
2937
{

src/Simpleflow/CodeGenerator/SimpleflowCodeVisitor.Helpers.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ private System.Reflection.PropertyInfo GetPropertyInfo(Type type, string name)
6262
return type.GetProperties().FirstOrDefault(p => string.Equals(p.Name, name, StringComparison.OrdinalIgnoreCase));
6363
}
6464

65+
private System.Reflection.FieldInfo GetFieldInfo(Type type, string name)
66+
{
67+
return type.GetFields().FirstOrDefault(p => string.Equals(p.Name, name, StringComparison.OrdinalIgnoreCase));
68+
}
69+
6570

6671
private Expression ToStringExpression(Expression obj)
6772
{

src/Simpleflow/CodeGenerator/SimpleflowCodeVisitor.VisitLiterals.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public override Expression VisitStringLiteral([NotNull] SimpleflowParser.StringL
6868
}
6969
else if (targetType != null && targetType.IsEnum) // Handle enum
7070
{
71-
if (!Enum.TryParse(targetType, value, out object result))
71+
if (!TryParseEnum(targetType, value, out object result))
7272
{
7373
throw new SimpleflowException(String.Format(Resources.Message.RequestedEnumValueNotFound, value, targetType.Name));
7474
}
@@ -141,6 +141,22 @@ public override Expression VisitTemplateStringLiteral([NotNull] SimpleflowParser
141141
return Expression.Call(concatMethod, newArrayExpression);
142142
}
143143

144-
144+
private bool TryParseEnum(Type targetType, string value, out object result)
145+
{
146+
#if NETCOREAPP
147+
return Enum.TryParse(targetType, value, out result);
148+
#else
149+
try
150+
{
151+
result = Enum.Parse(targetType, value);
152+
return true;
153+
}
154+
catch
155+
{
156+
result = null;
157+
return false;
158+
}
159+
#endif
160+
}
145161
}
146162
}

src/Simpleflow/CodeGenerator/SimpleflowCodeVisitor.VisitObjectIdentifier.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Linq;
55
using System.Linq.Expressions;
6+
using System.Reflection;
67
using Antlr4.Runtime.Misc;
78
using Simpleflow.Exceptions;
89
using Simpleflow.Parser;
@@ -51,7 +52,7 @@ var indexProperty
5152
.GetProperties()
5253
.SingleOrDefault(p => p.GetIndexParameters().Length == 1 &&
5354
p.GetIndexParameters()[0].ParameterType == indexExpression.Type);
54-
55+
5556
return Expression.MakeIndex(objectExp, indexProperty, new[] { indexExpression });
5657
}
5758

@@ -68,16 +69,31 @@ private Expression GetFinalPropertyValue(Expression propExp, SimpleflowParser.Id
6869
var propName = property.Identifier().GetText();
6970
var prop = GetPropertyInfo(propExp.Type, propName);
7071

72+
// Support property or field
73+
74+
FieldInfo field = null;
7175
if (prop == null)
7276
{
73-
throw new InvalidPropertyException($"Invalid property '{propName}'");
77+
field = GetFieldInfo(propExp.Type, propName);
78+
79+
if (field == null)
80+
{
81+
throw new InvalidPropertyException($"Invalid property or field '{propName}'");
82+
}
7483
}
7584

7685
// Get indexed object
7786
propExp = GetIndexObjectExpIfDefined(propExp, property.index());
7887

7988
// Get property of indexed object
80-
propExp = Expression.Property(propExp, prop);
89+
if (prop != null)
90+
{
91+
propExp = Expression.Property(propExp, prop);
92+
}
93+
else
94+
{
95+
propExp = Expression.Field(propExp, field);
96+
}
8197
}
8298
return propExp;
8399
}

src/Simpleflow/Exceptions/DuplicateParametersException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ public class DuplicateParametersException : SimpleflowException
1616
/// a specified function name.
1717
/// </summary>
1818
/// <param name="repeatedParameters">The repeated parameters that caused the exception.</param>
19-
public DuplicateParametersException(IEnumerable<string> repeatedParameters) : base($"Duplicate parameters '{string.Join(',', repeatedParameters)}' cannot be allowed." ) { }
19+
public DuplicateParametersException(IEnumerable<string> repeatedParameters) : base($"Duplicate parameters '{string.Join(",", repeatedParameters)}' cannot be allowed." ) { }
2020
}
2121
}

src/Simpleflow/FunctionRegister.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,20 @@ public FunctionPointer GetFunction(string name, ArgumentInfo[] argumentInfo) //
9898
var bitmapIndex = _bitmapIndex[name];
9999
(int storeIndex, int valueIndex) = GetStoreAndValueIndex(bitmapIndex);
100100

101-
return storeIndex switch
102-
{
103-
MethodStoreIndex => new FunctionPointer { Reference = _methodStore[valueIndex] },
104-
ProviderStoreIndex => _providerStore[valueIndex].GetFunction(name, argumentInfo),
105-
_ => null
106-
};
101+
// Change this code, in order to support .NET 48
102+
return storeIndex == MethodStoreIndex
103+
? new FunctionPointer { Reference = _methodStore[valueIndex] }
104+
: storeIndex == ProviderStoreIndex
105+
? _providerStore[valueIndex].GetFunction(name, argumentInfo)
106+
: null;
107+
108+
//return storeIndex switch
109+
//{
110+
// MethodStoreIndex => new FunctionPointer { Reference = _methodStore[valueIndex] },
111+
// ProviderStoreIndex => _providerStore[valueIndex].GetFunction(name, argumentInfo),
112+
// _ => null
113+
//};
114+
107115
}
108116
return null;
109117
}

src/Simpleflow/IContextOptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ public interface IContextOptions : IOptions
2020
/// <summary>
2121
/// Reset cache allows to remove the item from cache if exists and add it once its compiled.
2222
/// </summary>
23-
public bool ResetCache { get; set; }
23+
bool ResetCache { get; set; }
2424

2525
/// <summary>
2626
/// Gets or sets <see cref="System.Threading.CancellationToken"/>
2727
/// </summary>
28-
public CancellationToken CancellationToken { get; set; }
28+
CancellationToken CancellationToken { get; set; }
2929
}
3030
}

src/Simpleflow/IOptions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ public interface IOptions
1111
/// <summary>
1212
/// Gets or sets AllowFunctions
1313
/// </summary>
14-
public string[] AllowFunctions { get; set; }
14+
string[] AllowFunctions { get; set; }
1515

1616

1717
/// <summary>
1818
/// Gets or sets DenyFunctions
1919
/// </summary>
20-
public string[] DenyFunctions { get; set; }
20+
string[] DenyFunctions { get; set; }
2121

2222
/// <summary>
2323
/// Gets or sets cache options
2424
/// </summary>
25-
public CacheOptions CacheOptions { get; set; }
25+
CacheOptions CacheOptions { get; set; }
2626
}
2727
}

src/Simpleflow/Services/CacheService.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class CacheService : IFlowPipelineService
2424
/// </param>
2525
public CacheService(CacheOptions cacheOptions)
2626
{
27-
_cacheOptions = cacheOptions ?? throw new ArgumentNullException(nameof(cacheOptions));
27+
_cacheOptions = cacheOptions ?? throw new ArgumentNullException(nameof(cacheOptions));
2828

2929
// validate hashing algorithm for unique id generation
3030
if (string.IsNullOrWhiteSpace(cacheOptions.HashingAlgToIdentifyScriptUniquely))
@@ -43,7 +43,7 @@ public CacheService(CacheOptions cacheOptions)
4343
public CacheService() : this(new CacheOptions())
4444
{
4545
}
46-
46+
4747
/// <inheritdoc />
4848
public void Run<TArg>(FlowContext<TArg> context, NextPipelineService<TArg> next)
4949
{
@@ -78,8 +78,10 @@ public void Run<TArg>(FlowContext<TArg> context, NextPipelineService<TArg> next)
7878
protected virtual string GetScriptUniqueId(CacheOptions contextCacheOptions, string script)
7979
{
8080
// Calculate id for script
81-
using var sha1 = HashAlgorithm.Create(contextCacheOptions?.HashingAlgToIdentifyScriptUniquely ?? _cacheOptions.HashingAlgToIdentifyScriptUniquely);
82-
return System.Convert.ToBase64String(sha1.ComputeHash(Encoding.UTF8.GetBytes(script)));
81+
using (var sha1 = HashAlgorithm.Create(contextCacheOptions?.HashingAlgToIdentifyScriptUniquely ?? _cacheOptions.HashingAlgToIdentifyScriptUniquely))
82+
{
83+
return System.Convert.ToBase64String(sha1.ComputeHash(Encoding.UTF8.GetBytes(script)));
84+
}
8385
}
8486

8587

src/Simpleflow/Simpleflow.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFrameworks>netcoreapp3.1;net6.0</TargetFrameworks>
3+
<TargetFrameworks>net48;netcoreapp3.1;net6.0;</TargetFrameworks>
44
<PackageIcon>PackageIcon.png</PackageIcon>
5-
<VersionPrefix>1.0.10</VersionPrefix>
6-
<VersionSuffix></VersionSuffix>
5+
<VersionPrefix>1.0.11</VersionPrefix>
6+
<VersionSuffix>beta1</VersionSuffix>
77
<PackageReadmeFile>README.md</PackageReadmeFile>
88
</PropertyGroup>
99

0 commit comments

Comments
 (0)