Skip to content

Commit

Permalink
More than one foreach on the same collection, using complex content, …
Browse files Browse the repository at this point in the history
…resulted in duplicate generation of the collection item type, which again resulted in fields not being found.
  • Loading branch information
mlyrstad committed Aug 5, 2024
1 parent a693110 commit 3b8b730
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 30 deletions.
6 changes: 0 additions & 6 deletions Source/.idea/.idea.OnyxTemplate/.idea/vcs.xml

This file was deleted.

47 changes: 44 additions & 3 deletions Source/ConsoleApp1/TestTemplateBase.onyx
Original file line number Diff line number Diff line change
@@ -1,5 +1,46 @@
{{ $foreach row in Rows }}
{{ $foreach cell in cells }}
{{ $if .$first }}**{{ $else }} {{ $end }}{{ $if $first }}|{{ $end }}{{ cell }}{{ $if $last }}|{{ $end }}{{ $if .$last }}**{{ $else }} {{ $end }}
using Microsoft.Maui.Controls;
using Microsoft.Maui.Graphics;
using System;
using System.Diagnostics;
using System.Collections.Immutable;
using Go.Themes;

namespace {{ namespace }};

/// <summary>
/// A collection of resources divided by Dark and Light themes.
/// </summary>
public partial class DesignSystem
{
/// <summary>
/// A list of themed <see cref="Color"/>s
/// </summary>
public static readonly ImmutableDictionary<string, ThemedItem<Color?>> Colors;

/// <summary>
/// A list of themed <see cref="Brush"/>es
/// </summary>
public static readonly ImmutableDictionary<string, ThemedItem<Brush?>> Brushes;

static DesignSystem()
{
Colors = ImmutableDictionary.Create<string, ThemedItem<Color?>>(StringComparer.Ordinal)
{{ $foreach color in colors }}
{{ $if hasWarning }}
#warning {{ warning }}
{{ $end }}
.Add("{{ key }}", new ThemedItem<Color?>(new Color({{ darkR }}, {{ darkG }}, {{ darkB }}, {{ darkA }}), new Color({{ lightR }}, {{ lightG }}, {{ lightB }}, {{ lightA }}))){{ $if $last }};{{ $end }} // #{{ darkA }}{{ darkR }}{{ darkG }}{{ darkB }} #{{ lightA }}{{ lightR }}{{ lightG }}{{ lightB }}
{{ $next }}
Brushes = ImmutableDictionary.Create<string, ThemedItem<Brush?>>(StringComparer.Ordinal)
{{ $foreach color in colors }}
.Add("{{ key }}", new ThemedItem<Brush?>(new SolidColorBrush(new Color({{ darkR }}, {{ darkG }}, {{ darkB }}, {{ darkA }})), new SolidColorBrush(new Color({{ lightR }}, {{ lightG }}, {{ lightB }}, {{ lightA }})))){{ $if $last }};{{ $end }} // #{{ darkA }}{{ darkR }}{{ darkG }}{{ darkB }} #{{ lightA }}{{ lightR }}{{ lightG }}{{ lightB }}
{{ $next }}

{{ $if hasBadKeys }}
Debug.WriteLine("Problematic DesignSystem resource keys:");
{{ $end }}
{{ $foreach key in badKeys }}
Debug.WriteLine(" - {{ name }}: {{ reason }}");
{{ $next }}
}
}
43 changes: 24 additions & 19 deletions Source/OnyxTemplate/DocumentModel/Document.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ namespace Mal.OnyxTemplate.DocumentModel
public class Document
{

/// <summary>
/// Parses a document from a string.
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
/// <exception cref="DomException"></exception>
public static Document Parse(string source)
{
var blocks = ImmutableArray.CreateBuilder<DocumentBlock>();
var ptr = new TextPtr(source);
TryReadHeader(ref ptr, out var header);
if (ReadBlocks(ref ptr, blocks) != null)
throw new DomException(ptr, 1, "Unexpected token.");
return new Document(header, blocks.ToImmutable());
}

TemplateTypeDescriptor _typeDescriptor;

Document(DocumentHeader header, ImmutableArray<DocumentBlock> blocks)
Expand Down Expand Up @@ -124,10 +140,15 @@ static void ScanThisScope(TemplateTypeDescriptor.Builder descriptor, IReadOnlyLi
return;
var complexTypeName = Identifier.MakeSafe(loop.Collection.Name + "Item");
var complexType = new TemplateTypeDescriptor.Builder(descriptor)
.WithName(complexTypeName);
var complexType = builder.FindComplexType(complexTypeName);
if (complexType == null)
{
complexType = new TemplateTypeDescriptor.Builder(descriptor)
.WithName(complexTypeName);
builder.WithComplexType(complexType);
}
ScanThisScope(complexType, loop.Blocks);
builder.WithComplexType(complexType);
p.WithType(complexTypeName);
});
}
Expand Down Expand Up @@ -224,22 +245,6 @@ static int CountFields(DocumentBlock block, int scope)
}
}

/// <summary>
/// Parses a document from a string.
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
/// <exception cref="DomException"></exception>
public static Document Parse(string source)
{
var blocks = ImmutableArray.CreateBuilder<DocumentBlock>();
var ptr = new TextPtr(source);
TryReadHeader(ref ptr, out var header);
if (ReadBlocks(ref ptr, blocks) != null)
throw new DomException(ptr, 1, "Unexpected token.");
return new Document(header, blocks.ToImmutable());
}

static IntermediateMacro ReadBlocks(ref TextPtr ptr, ImmutableArray<DocumentBlock>.Builder blocks)
{
var start = ptr;
Expand Down
10 changes: 10 additions & 0 deletions Source/OnyxTemplate/DocumentModel/TemplateTypeDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,16 @@ public Builder WithComplexType(Builder complexType)
return this;
}

/// <summary>
/// Attempts to find a complex type by name.
/// </summary>
/// <param name="complexTypeName"></param>
/// <returns></returns>
public Builder FindComplexType(Identifier complexTypeName)
{
return _complexTypes.FirstOrDefault(c => c.Name == complexTypeName);
}

class TypeResolver : ITypeResolver
{
public Dictionary<Identifier, TemplateTypeDescriptor> Types { get; } = new Dictionary<Identifier, TemplateTypeDescriptor>(Identifier.IgnoreCaseComparer);
Expand Down
2 changes: 1 addition & 1 deletion Source/OnyxTemplate/PackageVersion.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.3.3-alpha
1.3.4-alpha
5 changes: 4 additions & 1 deletion Source/OnyxTemplate/ReleaseNotes.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
v.1.3.3-alpha
v.1.3.4-alpha
- More than one foreach on the same collection, using complex content, resulted in duplicate generation of the collection item type, which again resulted in fields not being found.

v.1.3.3-alpha
- :facepalm: You have tests, Mal. _Use_ them (fixed bugs brought to light by tests).

v.1.3.2-alpha
Expand Down

0 comments on commit 3b8b730

Please sign in to comment.