Skip to content

Commit

Permalink
Fixed lambda map in map variable #2410 (#2412)
Browse files Browse the repository at this point in the history
  • Loading branch information
BernieWhite authored Sep 1, 2023
1 parent 3cf5ea3 commit 1fa7275
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
3 changes: 3 additions & 0 deletions docs/CHANGELOG-v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ What's changed since pre-release v1.30.0-B0026:
- Engineering:
- Bump Microsoft.CodeAnalysis.NetAnalyzers to v7.0.4.
[#2405](https://github.com/Azure/PSRule.Rules.Azure/pull/2405)
- Bug fixes:
- Fixed lambda map in map variable by @BernieWhite.
[#2410](https://github.com/Azure/PSRule.Rules.Azure/issues/2410)

## v1.30.0-B0026 (pre-release)

Expand Down
4 changes: 2 additions & 2 deletions src/PSRule.Rules.Azure/Data/Template/LambdaExpressionFn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ private LambdaContext GetArgs2(ITemplateContext context, out string varName1, ou
return null;

fn = arg1;
return new LambdaContext(context);
return context is LambdaContext existing ? existing : new LambdaContext(context);
}

private LambdaContext GetArgs3(ITemplateContext context, out string varName1, out string varName2, out ExpressionFnOuter fn)
Expand All @@ -210,7 +210,7 @@ private LambdaContext GetArgs3(ITemplateContext context, out string varName1, ou
return null;

fn = arg2;
return new LambdaContext(context);
return context is LambdaContext existing ? existing : new LambdaContext(context);
}

private static object ObjectMapper(object input, LambdaContext context, string varName, ExpressionFnOuter mapper)
Expand Down
6 changes: 6 additions & 0 deletions tests/PSRule.Rules.Azure.Tests/TemplateVisitorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,12 @@ public void LambdaFunctions()
Assert.Equal(123, objectMapNull["value"]["123"].Value<int>());
Assert.Equal(456, objectMapNull["value"]["456"].Value<int>());
Assert.Equal(789, objectMapNull["value"]["789"].Value<int>());

// Additional test cases
Assert.True(templateContext.RootDeployment.TryOutput("mapInMap", out JObject mapInMap));
Assert.Equal(2, mapInMap["value"].Value<JArray>().Count);
Assert.Equal("value1", mapInMap["value"][0].Value<string>());
Assert.Equal("value1", mapInMap["value"][1].Value<string>());
}

[Fact]
Expand Down
14 changes: 14 additions & 0 deletions tests/PSRule.Rules.Azure.Tests/Tests.Bicep.13.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,17 @@ output objectMap2 object = toObject(numbers, i => '${i}', i => {
}
)
output objectMapNull object = toObject([ 123, 456, 789, null ], i => '${i}')

// Additional cases map in map

var mapNested = [
{
item: [
'item1'
'item2'
]
value: 'value1'
}
]

output mapInMap array = flatten(map(mapNested, a => map(a.item, b => a.value)))
17 changes: 15 additions & 2 deletions tests/PSRule.Rules.Azure.Tests/Tests.Bicep.13.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"_generator": {
"name": "bicep",
"version": "0.20.4.51522",
"templateHash": "7710253816038152126"
"templateHash": "12689734029213343259"
}
},
"parameters": {
Expand Down Expand Up @@ -71,7 +71,16 @@
]
}
],
"ages": "[map(variables('dogs'), lambda('dog', lambdaVariables('dog').age))]"
"ages": "[map(variables('dogs'), lambda('dog', lambdaVariables('dog').age))]",
"mapNested": [
{
"item": [
"item1",
"item2"
],
"value": "value1"
}
]
},
"resources": [
{
Expand Down Expand Up @@ -233,6 +242,10 @@
"objectMapNull": {
"type": "object",
"value": "[toObject(createArray(123, 456, 789, null()), lambda('i', format('{0}', lambdaVariables('i'))))]"
},
"mapInMap": {
"type": "array",
"value": "[flatten(map(variables('mapNested'), lambda('a', map(lambdaVariables('a').item, lambda('b', lambdaVariables('a').value)))))]"
}
}
}

0 comments on commit 1fa7275

Please sign in to comment.