Skip to content

Commit a8313c1

Browse files
committed
Fix AlltinnRowIdsGenerator
1 parent 044c7e6 commit a8313c1

File tree

4 files changed

+91
-97
lines changed

4 files changed

+91
-97
lines changed
Lines changed: 59 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
using System.Text;
22
using Altinn.App.SourceGenerator;
3-
using Altinn.App.SourceGenerator.SourceTextGenerator;
43

54
namespace Altinn.App.Analyzers.SourceTextGenerator;
65

76
internal static class AltinnRowIdsGenerator
87
{
98
public static void Generate(StringBuilder builder, ModelPathNode rootNode)
109
{
11-
var listProperties = ModelPathNodeTools.GetListProperties(rootNode);
12-
if (listProperties.IsDefaultOrEmpty)
10+
var property = GetListProperties(rootNode);
11+
if (property?.Children is not { Count: > 0 })
1312
{
1413
builder.Append(
1514
"""
@@ -43,59 +42,69 @@ public void InitializeAltinnRowIds()
4342
"""
4443
);
4544

46-
GenerateSetAltinnRowIds(builder, rootNode, listProperties.ToImmutableArray(), []);
45+
GenerateSetAltinnRowIds(builder, property, []);
4746
}
4847

4948
private static void GenerateSetAltinnRowIds(
5049
StringBuilder builder,
51-
ModelPathNode node,
52-
ImmutableArray<ModelPathNodeTools.Property> listProperties,
50+
PropertyWithListChildren property,
5351
HashSet<string> classes
5452
)
5553
{
56-
if (!classes.Add(node.TypeName))
54+
if (!classes.Add(property.Node.TypeName))
5755
{
5856
return;
5957
}
6058
builder.Append(
6159
$$"""
6260
63-
private static void SetAltinnRowIds({{node.TypeName}} dataModel, bool initialize)
61+
private static void SetAltinnRowIds({{property.Node.TypeName}} dataModel, bool initialize)
6462
{
6563
6664
"""
6765
);
6866

69-
var groupIndex = -1;
70-
var altinnRowId = node.Properties.FirstOrDefault(IsAltinnRowId);
71-
if (altinnRowId is not null)
67+
foreach (var child in property.Children)
7268
{
73-
builder.Append(
74-
$$"""
75-
dataModel.{{altinnRowId.CSharpName}} = initialize ? Guid.NewGuid() : Guid.Empty;
76-
77-
"""
78-
);
79-
}
80-
81-
foreach (var property in listProperties.Where(p => p.Node.Properties.Any(IsAltinnRowId)))
82-
{
83-
groupIndex++;
84-
builder.Append(
85-
$$"""
86-
if (dataModel.{{(string.Join("?.", property.CSharp))}} is { } group{{groupIndex}})
87-
{
88-
foreach (var row in group{{groupIndex}})
69+
if (child.IsRowId)
70+
{
71+
builder.Append(
72+
$$"""
73+
dataModel.{{child.Node.CSharpName}} = initialize ? Guid.NewGuid() : Guid.Empty;
74+
75+
"""
76+
);
77+
}
78+
else if (child.Node.ListType is null)
79+
{
80+
builder.Append(
81+
$$"""
82+
if(dataModel.{{child.Node.CSharpName}} is not null)
83+
{
84+
SetAltinnRowIds(dataModel.{{child.Node.CSharpName}}, initialize);
85+
}
86+
87+
"""
88+
);
89+
}
90+
else
91+
{
92+
builder.Append(
93+
$$"""
94+
if(dataModel.{{child.Node.CSharpName}} is not null)
8995
{
90-
if (row is not null)
96+
foreach (var item in dataModel.{{child.Node.CSharpName}})
9197
{
92-
SetAltinnRowIds(row, initialize);
98+
if (item is not null)
99+
{
100+
SetAltinnRowIds(item, initialize);
101+
}
93102
}
94103
}
95-
}
96-
97-
"""
98-
);
104+
105+
"""
106+
);
107+
}
99108
}
100109
builder.Append(
101110
"""
@@ -104,14 +113,29 @@ private static void SetAltinnRowIds({{node.TypeName}} dataModel, bool initialize
104113
"""
105114
);
106115

107-
foreach (var property in listProperties)
116+
foreach (var child in property.Children)
108117
{
109-
GenerateSetAltinnRowIds(builder, property.Node, property.Children, classes);
118+
if (!child.IsRowId)
119+
GenerateSetAltinnRowIds(builder, child, classes);
110120
}
111121
}
112122

113123
private static bool IsAltinnRowId(ModelPathNode c)
114124
{
115125
return c is { JsonName: "altinnRowId", CSharpName: "AltinnRowId", TypeName: "global::System.Guid" };
116126
}
127+
128+
private record PropertyWithListChildren(ModelPathNode Node, List<PropertyWithListChildren> Children, bool IsRowId);
129+
130+
private static PropertyWithListChildren? GetListProperties(ModelPathNode node)
131+
{
132+
var children = node.Properties.Select(GetListProperties).OfType<PropertyWithListChildren>().ToList();
133+
var isAltinnRowId = IsAltinnRowId(node);
134+
if (children.Count == 0 && !isAltinnRowId)
135+
{
136+
return null;
137+
}
138+
139+
return new PropertyWithListChildren(node, children, isAltinnRowId);
140+
}
117141
}

src/Altinn.App.Analyzers/SourceTextGenerator/ModelPathNodeTools.cs

Lines changed: 0 additions & 46 deletions
This file was deleted.

test/Altinn.App.SourceGenerator.Tests/FullTests.Run#Altinn_App_SourceGenerator_Tests_SkjemaFormDataWrapper.g.verified.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -661,28 +661,36 @@ public void InitializeAltinnRowIds()
661661

662662
private static void SetAltinnRowIds(global::Altinn.App.SourceGenerator.Tests.Skjema dataModel, bool initialize)
663663
{
664-
if (dataModel.Skjemainnhold is { } group0)
664+
if(dataModel.Skjemainnhold is not null)
665665
{
666-
foreach (var row in group0)
666+
foreach (var item in dataModel.Skjemainnhold)
667667
{
668-
if (row is not null)
668+
if (item is not null)
669669
{
670-
SetAltinnRowIds(row, initialize);
670+
SetAltinnRowIds(item, initialize);
671671
}
672672
}
673673
}
674+
if(dataModel.EierAdresse is not null)
675+
{
676+
SetAltinnRowIds(dataModel.EierAdresse, initialize);
677+
}
674678
}
675679

676680
private static void SetAltinnRowIds(global::Altinn.App.SourceGenerator.Tests.SkjemaInnhold dataModel, bool initialize)
677681
{
678682
dataModel.AltinnRowId = initialize ? Guid.NewGuid() : Guid.Empty;
679-
if (dataModel.TidligereAdresse is { } group0)
683+
if(dataModel.Adresse is not null)
684+
{
685+
SetAltinnRowIds(dataModel.Adresse, initialize);
686+
}
687+
if(dataModel.TidligereAdresse is not null)
680688
{
681-
foreach (var row in group0)
689+
foreach (var item in dataModel.TidligereAdresse)
682690
{
683-
if (row is not null)
691+
if (item is not null)
684692
{
685-
SetAltinnRowIds(row, initialize);
693+
SetAltinnRowIds(item, initialize);
686694
}
687695
}
688696
}

test/Altinn.App.SourceGenerator.Tests/SourceTextGeneratorTests.Generate.verified.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -660,28 +660,36 @@ public void InitializeAltinnRowIds()
660660

661661
private static void SetAltinnRowIds(global::Altinn.App.SourceGenerator.Tests.Skjema dataModel, bool initialize)
662662
{
663-
if (dataModel.Skjemainnhold is { } group0)
663+
if(dataModel.Skjemainnhold is not null)
664664
{
665-
foreach (var row in group0)
665+
foreach (var item in dataModel.Skjemainnhold)
666666
{
667-
if (row is not null)
667+
if (item is not null)
668668
{
669-
SetAltinnRowIds(row, initialize);
669+
SetAltinnRowIds(item, initialize);
670670
}
671671
}
672672
}
673+
if(dataModel.EierAdresse is not null)
674+
{
675+
SetAltinnRowIds(dataModel.EierAdresse, initialize);
676+
}
673677
}
674678

675679
private static void SetAltinnRowIds(global::Altinn.App.SourceGenerator.Tests.SkjemaInnhold dataModel, bool initialize)
676680
{
677681
dataModel.AltinnRowId = initialize ? Guid.NewGuid() : Guid.Empty;
678-
if (dataModel.TidligereAdresse is { } group0)
682+
if(dataModel.Adresse is not null)
683+
{
684+
SetAltinnRowIds(dataModel.Adresse, initialize);
685+
}
686+
if(dataModel.TidligereAdresse is not null)
679687
{
680-
foreach (var row in group0)
688+
foreach (var item in dataModel.TidligereAdresse)
681689
{
682-
if (row is not null)
690+
if (item is not null)
683691
{
684-
SetAltinnRowIds(row, initialize);
692+
SetAltinnRowIds(item, initialize);
685693
}
686694
}
687695
}

0 commit comments

Comments
 (0)