Skip to content

Commit b59440b

Browse files
KyllianAubryGitHub Enterprise
authored andcommitted
SKA-778: multidimensional arrays (#188)
* SKA-778: add multidimensional array ser/des * SKA-778: handle optional nested arrays
1 parent 5acc4f3 commit b59440b

File tree

5 files changed

+264
-57
lines changed

5 files changed

+264
-57
lines changed

.github/workflows/build-test-internal.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,6 @@ jobs:
9999
uses: pnd/sil-kit-fmu-importer-supplements/.github/workflows/testAutomation.yaml@main
100100
with:
101101
fmu-importer-version: 1.5.0
102-
branch-name: ${{ github.ref_name }}
102+
branch-name: ${{ github.head_ref }}
103103
commit-full-sha: ${{ github.sha }}
104104
secrets: inherit

FmuImporter/FmiBridge/Binding/Helper/ReturnVariable.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public struct Variable
1414
public IntPtr[] ValueSizes;
1515
public bool IsScalar;
1616
public VariableTypes Type;
17+
public ulong[]? Dimensions;
1718
}
1819

1920
public Variable[] ResultArray;
@@ -47,7 +48,8 @@ public static ReturnVariable CreateReturnVariable<T>(
4748
ValueSizes = Array.Empty<IntPtr>(),
4849
Values = new object[arrayLength],
4950
Type = modelVar.VariableType,
50-
IsScalar = modelVar.IsScalar
51+
IsScalar = modelVar.IsScalar,
52+
Dimensions = modelVar.Dimensions
5153
};
5254
for (ulong j = 0; j < arrayLength; j++)
5355
{
@@ -88,7 +90,8 @@ public static ReturnVariable CreateReturnVariable<T>(
8890
// T ~ Array of Binaries -> IntPtr[]
8991
Values = new object[arrayLength],
9092
Type = modelVar.VariableType,
91-
IsScalar = modelVar.IsScalar
93+
IsScalar = modelVar.IsScalar,
94+
Dimensions = modelVar.Dimensions
9295
};
9396
for (ulong j = 0; j < arrayLength; j++)
9497
{

FmuImporter/FmiBridge/Supplements/Serializer.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: MIT
22
// Copyright (c) Vector Informatik GmbH. All rights reserved.
33

4+
using System.Collections;
45
using System.Text;
56
using Fmi.Exceptions;
67
using Fmi.FmiModel.Internal;
@@ -11,6 +12,18 @@ public class Serializer
1112
{
1213
public static byte[] Serialize(object data, Variable variable, List<int> binSizes)
1314
{
15+
// handle nested lists recursively
16+
if (data is IList and not Array)
17+
{
18+
var nestedList = (IList)data;
19+
var result = new List<byte>();
20+
foreach (var entry in nestedList)
21+
{
22+
result.AddRange(Serialize(entry!, variable, binSizes));
23+
}
24+
return result.ToArray();
25+
}
26+
1427
// there are no binSizes by default
1528
switch (variable.VariableType)
1629
{

FmuImporter/FmuImporter/Config/ParserExtensions/ParameterValueTypeConverter.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public object ReadYaml(IParser parser, Type type)
2727
return new ParameterValue(ParseScalar(scalar));
2828
}
2929

30-
throw new NotSupportedException("A parameter's \"Value\" attribute supports scalars or list of scalars only.");
30+
throw new NotSupportedException("A parameter's \"Value\" attribute supports scalars or arrays only.");
3131
}
3232

3333
private static object ParseScalar(Scalar scalar)
@@ -63,22 +63,34 @@ private static object ParseScalar(Scalar scalar)
6363
}
6464

6565
private static ParameterValue StartParsingSequence(IParser parser)
66+
{
67+
return new ParameterValue(ParseSequence(parser));
68+
}
69+
70+
private static List<object> ParseSequence(IParser parser)
6671
{
6772
var resultList = new List<object>();
6873
while (!parser.Accept<SequenceEnd>(out _))
6974
{
70-
if (parser.TryConsume<Scalar>(out var scalar))
75+
if (parser.TryConsume<SequenceStart>(out _))
76+
{
77+
resultList.Add(ParseSequence(parser));
78+
}
79+
else if (parser.TryConsume<Scalar>(out var scalar))
7180
{
7281
resultList.Add(ParseScalar(scalar));
7382
}
7483
else
7584
{
76-
throw new NotSupportedException("A parameter's \"Value\" attribute supports scalars or list of scalars only.");
85+
var current = parser.Current;
86+
throw new NotSupportedException(
87+
$"A parameter's \"Value\" attribute supports scalars or arrays only. " +
88+
$"Unexpected member '{current?.GetType().Name}' at {current?.Start}.");
7789
}
7890
}
7991

8092
parser.MoveNext();
81-
return new ParameterValue(resultList);
93+
return resultList;
8294
}
8395

8496
public void WriteYaml(IEmitter emitter, object? value, Type type)

0 commit comments

Comments
 (0)