Skip to content
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
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<Product>Simpra</Product>
<Company>ALTA Software llc.</Company>
<Copyright>Copyright © 2024 ALTA Software llc.</Copyright>
<Version>1.0.12</Version>
<Version>1.0.13</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down
25 changes: 25 additions & 0 deletions src/AltaSoft.Simpra/Visitor/SimpraParserVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,14 @@ public Expression VisitIndexAccess(SimpraParser.IndexAccessContext context)
objectType = left.Type;
}

// Handle IDictionary<TKey, TValue> (including Dictionary<,>)
var isDictionary = objectType.IsIReadOnlyDictionaryT();

if (isDictionary)
{
return HandleDictionaryIndexAccess(context, objectType, index, left);
}

// Handle indexable types (e.g., List<T>)
var defaultIndexer = objectType
.GetProperties()
Expand Down Expand Up @@ -552,4 +560,21 @@ public Expression VisitArray(SimpraParser.ArrayContext context)
{
return NewSimpraList(context.exp().Select(Visit), context);
}

private static Expression HandleDictionaryIndexAccess(SimpraParser.IndexAccessContext context, Type objectType, Expression index, Expression left)
{
var keyType = objectType.GetGenericArguments()[0];
var valueType = objectType.GetGenericArguments()[1];
var tryGetValueMethod = objectType.GetMethod("TryGetValue", [keyType, valueType.MakeByRefType()])!;

var keyExpr = index.Type != keyType ? Expression.Convert(index, keyType) : index;
var valueVar = Expression.Variable(valueType, "dictValue");

var tryGetValueCall = Expression.Call(left, tryGetValueMethod, keyExpr, valueVar);

var defaultValue = Expression.Default(valueType);
var block = Expression.Block([valueVar], Expression.Condition(tryGetValueCall, valueVar, defaultValue));

return ConvertToSimpraType(block, false, context);
}
}
3 changes: 3 additions & 0 deletions tests/AltaSoft.Simpra.tests/Models/TestModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public class TestModel : BaseModel, ITransferModel
public required Transfer? Transfer { get; set; }
public required Customer Customer { get; set; }
public required string Remittance { get; set; }
public Dictionary<string, string>? Countries { get; set; }

public Dictionary<string, Customer>? DictionaryOfObjects { get; set; }

public ColorX ColorX { get; set; } = ColorX.Green;
public ColorM ColorM { get; set; } = ColorM.Green;
Expand Down
54 changes: 54 additions & 0 deletions tests/AltaSoft.Simpra.tests/SimpraExpressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,60 @@ namespace AltaSoft.Simpra.Tests;

public class SimpraExpressionTests
{

[Fact]
public void DictionaryIndexer_MissingKey_ReturnsDefaultValueForProperty()
{
const string expressionCode =
"""
return DictionaryOfObjects['test'].Id
""";

var simpra = new Simpra();
var model = GetTestModel();
model.DictionaryOfObjects = new Dictionary<string, Customer> { { "Georgia", new Customer
{
Id = 1,
Status = 10
}} };

var result = simpra.Execute<int, TestModel, IFunctions>(model, new TestFunctions(), expressionCode);
Assert.Equal(0, result);

}

[Fact]
public void CallGetValueFromDictionaryWhenKeyDoesNotExist_ShouldReturnDefault()
{
const string expressionCode =
"""
return Countries['test'] is 'Test'
""";

var simpra = new Simpra();
var model = GetTestModel();
model.Countries = new Dictionary<string, string> { { "Georgia", "Test" } };
var result = simpra.Execute<bool, TestModel, IFunctions>(model, new TestFunctions(), expressionCode);
Assert.False(result);

}

[Fact]
public void CallGetValueFromDictionaryWhenKeyExist_ShouldReturnValue()
{
const string expressionCode =
"""
return Countries['Georgia'] is 'Test'
""";

var simpra = new Simpra();
var model = GetTestModel();
model.Countries = new Dictionary<string, string> { { "Georgia", "Test" } };
var result = simpra.Execute<bool, TestModel, IFunctions>(model, new TestFunctions(), expressionCode);
Assert.True(result);

}

[Fact]
public void CallInterfaceFunctionFromSimpra_ShouldReturnCorrectValue()
{
Expand Down
Loading