Skip to content

Commit

Permalink
Fix binder.
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed Nov 27, 2023
1 parent 57bb214 commit d96663e
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 70 deletions.
64 changes: 32 additions & 32 deletions Mjml.Net.Generator/Template.handlebar
Original file line number Diff line number Diff line change
Expand Up @@ -63,43 +63,43 @@ public partial class {{className}}
if (source{{name}} != null)
{
{{#if isCustom }}
source{{name}} = {{customName}}.Coerce(source{{name}});
this.{{name}} = {{customName}}.Coerce(source{{name}});
{{/if}}
{{#unless isCustom }}
source{{name}} = AttributeTypes.{{defaultType}}.Coerce(source{{name}});
this.{{name}} = AttributeTypes.{{defaultType}}.Coerce(source{{name}});
{{/unless}}
{{#if isExpanded}}
this.{{name}} = source{{name}};
if ({{name}}Top == null || {{name}}Right == null || {{name}}Bottom == null || {{name}}Left == null)
}
{{/each}}

{{#each expandedFields}}
if ({{name}} != null && ({{name}}Top == null || {{name}}Right == null || {{name}}Bottom == null || {{name}}Left == null))
{
{{#if isBorder }}
var (t, r, b, l) = BindingHelper.ParseShorthandBorder({{name}});
{{/if}}
{{#unless isBorder }}
var (t, r, b, l) = BindingHelper.ParseShorthandValue({{name}});
{{/unless}}

if ({{name}}Top == null)
{
{{#if isBorder }}
var (t, r, b, l) = BindingHelper.ParseShorthandBorder(source{{name}});
{{/if}}
{{#unless isBorder }}
var (t, r, b, l) = BindingHelper.ParseShorthandValue(source{{name}});
{{/unless}}

if ({{name}}Top == null)
{
{{name}}Top = t;
}

if ({{name}}Right == null)
{
{{name}}Right = r;
}

if ({{name}}Bottom == null)
{
{{name}}Bottom = b;
}

if ({{name}}Left == null)
{
{{name}}Left = l;
}
{{name}}Top = t;
}

if ({{name}}Right == null)
{
{{name}}Right = r;
}

if ({{name}}Bottom == null)
{
{{name}}Bottom = b;
}

if ({{name}}Left == null)
{
{{name}}Left = l;
}
{{/if}}
}
{{/each}}
{{#each textFields}}
Expand Down
2 changes: 0 additions & 2 deletions Mjml.Net.Generator/TemplateField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ internal sealed record TemplateField

public bool IsText { get; set; }

public bool IsExpanded { get; set; }

public bool IsBorder => Attribute == "border";

public bool IsCustom => CustomType != null;
Expand Down
74 changes: 40 additions & 34 deletions Mjml.Net.Generator/TemplateModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,67 @@ public IEnumerable<TemplateField> CustomTypes
{
get
{
return Fields.Values.Where(x => x.IsCustom).OrderByDescending(x => x.Name);
return Fields.Values.Where(x => x.IsCustom).OrderBy(x => x.Name);
}
}

public IEnumerable<TemplateField> DefaultTypes
{
get
{
return Fields.Values.Where(x => !x.IsCustom).OrderByDescending(x => x.Name);
return Fields.Values.Where(x => !x.IsCustom).OrderBy(x => x.Name);
}
}

public IEnumerable<TemplateField> NormalFields
{
get
{
return Fields.Values.Where(x => !x.IsText).OrderByDescending(x => x.Name);
return Fields.Values.Where(x => !x.IsText).OrderBy(x => x.Name);
}
}

public IEnumerable<TemplateField> TextFields
{
get
{
return Fields.Values.Where(x => x.IsText).OrderByDescending(x => x.Name);
return Fields.Values.Where(x => x.IsText).OrderBy(x => x.Name);
}
}

public IEnumerable<TemplateField> ExpandedFields
{
get
{
foreach (var field in NormalFields)
{
bool IsCandidate(string name)
{
if (field.Attribute.Contains(name) &&
!field.Attribute.EndsWith($"{name}-top", StringComparison.Ordinal) &&
!field.Attribute.EndsWith($"{name}-right", StringComparison.Ordinal) &&
!field.Attribute.EndsWith($"{name}-bottom", StringComparison.Ordinal) &&
!field.Attribute.EndsWith($"{name}-left", StringComparison.Ordinal))
{
return true;
}

return false;
}

if (!IsCandidate("margin") && !IsCandidate("padding") && !IsCandidate("border"))
{
continue;
}

if (Fields.ContainsKey($"{field.Name}Top") &&
Fields.ContainsKey($"{field.Name}Right") &&
Fields.ContainsKey($"{field.Name}Bottom") &&
Fields.ContainsKey($"{field.Name}Left"))
{
yield return field;
}
}
}
}

Expand Down Expand Up @@ -90,36 +126,6 @@ public static TemplateModel Build(INamedTypeSymbol classSymbol, IEnumerable<Fiel
allFields[fieldInfo.Name] = fieldInfo;
}

foreach (var field in allFields.Values)
{
bool IsCandidate(string name)
{
if (field.Attribute.Contains(name) &&
!field.Attribute.EndsWith($"{name}-top", StringComparison.Ordinal) &&
!field.Attribute.EndsWith($"{name}-right", StringComparison.Ordinal) &&
!field.Attribute.EndsWith($"{name}-bottom", StringComparison.Ordinal) &&
!field.Attribute.EndsWith($"{name}-left", StringComparison.Ordinal))
{
return true;
}

return false;
}

if (!IsCandidate("margin") && !IsCandidate("padding") && !IsCandidate("border"))
{
continue;
}

if (allFields.ContainsKey($"{field.Name}Top") &&
allFields.ContainsKey($"{field.Name}Right") &&
allFields.ContainsKey($"{field.Name}Bottom") &&
allFields.ContainsKey($"{field.Name}Left"))
{
field.IsExpanded = true;
}
}

return new TemplateModel
{
Fields = allFields,
Expand Down
2 changes: 1 addition & 1 deletion Mjml.Net/IType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public interface IType
{
bool Validate(string value, ref ValidationContext context);

string Coerce(string value)
public string Coerce(string value)
{
return value;
}
Expand Down
2 changes: 1 addition & 1 deletion Mjml.Net/Types/ColorType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public bool Validate(string value, ref ValidationContext context)
#endif
}

public string CoerceColor(string value)
public string Coerce(string value)
{
var trimmed = value.AsSpan().Trim();

Expand Down

0 comments on commit d96663e

Please sign in to comment.