Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: format code with dotnet-format #18

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
367 changes: 200 additions & 167 deletions src/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,199 +64,232 @@ internal static partial class Constants
"""";

private const string GeneratedCodeAttributesList =
$"[GeneratedCode(\"{ThisAssemblyName}\", \"{RegexDtoGeneratorVersion}\"), CompilerGenerated]";
private const string RegexDtoAttributeDeclaration = $$$""""
$"[GeneratedCode(\"{ThisAssemblyName}\", \"{RegexDtoGeneratorVersion}\"), CompilerGenerated]";

private const string RegexDtoAttributeDeclaration = $$$""""
[AttributeUsage(@Class | @Struct, Inherited = false, AllowMultiple = false)]
{{{GeneratedCodeAttributesList}}}
internal sealed class RegexDtoAttribute(
#if NET7_0_OR_GREATER
{{{GeneratedCodeAttributesList
}}}
internal sealed class RegexDtoAttribute(
#if NET7_0_OR_GREATER
[StringSyntax(SS.Regex)]
#endif
string Regex, System.Type? BaseType = null, RegexOptions RegexOptions = Compiled | CultureInvariant | ExplicitCapture | IgnoreCase | IgnorePatternWhitespace | Singleline) : System.Attribute
{
}
"""";
private const string RegexDtoBaseTypeDeclaration = $$$""""
#endif
string Regex, System.Type ? BaseType = null, RegexOptions RegexOptions = Compiled | CultureInvariant | ExplicitCapture | IgnoreCase | IgnorePatternWhitespace | Singleline) : System.Attribute
{
}
"""";

private const string RegexDtoBaseTypeDeclaration = $$$""""
namespace {{ namespace_name }}
{
{{{GeneratedCodeAttributesList}}}
public abstract partial {{ target_data_structure_type }} {{ type_name }}Base {{ if base_type != "" }} : {{ base_type ~}}{{ end }}
{
/// <summary>The default <see cref="RegexOptions" /></summary>
/// <value>(RegexOptions)({{ regex_options | string.replace "," " | " }})</value>
const RegexOptions RegexOptions = (RegexOptions)({{ regex_options | string.replace "," " | " }});

{
{ { { GeneratedCodeAttributesList} } }
public abstract partial
{ { target_data_structure_type } }
{ { type_name } }
Base
{ { if base_type != "" } } : { { base_type ~} }
{ { end } }
{
/// <summary>The default <see cref="RegexOptions" /></summary>
/// <value>(RegexOptions)({{ regex_options | string.replace "," " | " }})</value>
const RegexOptions RegexOptions = (RegexOptions)({ { regex_options | string.replace "," " | " } });

#if NET7_0_OR_GREATER
[StringSyntax(SS.Regex)]
#endif
public const string RegexString = @"{{ regex }}";

// #if NET7_0_OR_GREATER
// [GeneratedRegex(RegexString, RegexOptions)]
// public static partial Regex Regex();
// #else
private static readonly Regex _regex = new (RegexString, RegexOptions);
/// <summary>The <see cref="Regex" /> that will be used to validate and recognize the DTO</summary>
public static Regex Regex() => _regex;
// #endif

public {{ if is_class }}virtual{{ end }} string OriginalString { get; init; }

{{ properties_declarations }}

{{ parse_declaration }}

{{ constructor_declaration }}
#endif
public const string RegexString = @"{{ regex }}";

// #if NET7_0_OR_GREATER
// [GeneratedRegex(RegexString, RegexOptions)]
// public static partial Regex Regex();
// #else
private static readonly Regex _regex = new(RegexString, RegexOptions);
/// <summary>The <see cref="Regex" /> that will be used to validate and recognize the DTO</summary>
public static Regex Regex() => _regex;
// #endif

public
{ { if is_class } }
virtual
{ { end } }
string OriginalString { get; init; }

{ { properties_declarations } }

{ { parse_declaration } }

{ { constructor_declaration } }
}
}
"""";
private const string RegexDtoTypeDeclaration = $$$$""""
"""";

private const string RegexDtoTypeDeclaration = $$$$""""
namespace {{ namespace_name }}
{
{{{{GeneratedCodeAttributesList}}}}
public partial {{ target_data_structure_type }} {{ type_name }} {{ if base_type != "" }} : {{ base_type ~}}{{ end }}
{
const RegexOptions RegexOptions = (RegexOptions)({{ regex_options | string.replace "," " | " }});

{
{ { { { GeneratedCodeAttributesList} } } }
public partial
{ { target_data_structure_type } }
{ { type_name } }
{ { if base_type != "" } } : { { base_type ~} }
{ { end } }
{
const RegexOptions RegexOptions = (RegexOptions)({ { regex_options | string.replace "," " | " } });

#if NET7_0_OR_GREATER
[StringSyntax(SS.Regex)]
#endif
/// <summary>The regex string that will be used to validate and recognize the DTO</summary>
public const string RegexString = @"{{ regex }}";

// #if NET7_0_OR_GREATER
// [GeneratedRegex(RegexString, RegexOptions)]
// public static partial Regex Regex();
// #else
private static readonly Regex _regex = new (RegexString, RegexOptions);
/// <summary>The <see cref="Regex" /> that will be used to validate and recognize the DTO</summary>
public static {{ if base_type != "" }}new{{ end }} Regex Regex() => _regex;
// #endif

public {{ if is_class }}override{{ end }} string OriginalString { get; init; }

{{ properties_declarations }}

{{ parse_declaration }}

{{ constructor_declaration }}
#endif
/// <summary>The regex string that will be used to validate and recognize the DTO</summary>
public const string RegexString = @"{{ regex }}";

// #if NET7_0_OR_GREATER
// [GeneratedRegex(RegexString, RegexOptions)]
// public static partial Regex Regex();
// #else
private static readonly Regex _regex = new(RegexString, RegexOptions);
/// <summary>The <see cref="Regex" /> that will be used to validate and recognize the DTO</summary>
public static
{ { if base_type != "" } }
new {{ end }} Regex Regex() => _regex;
// #endif

public
{ { if is_class } }
override
{ { end } }
string OriginalString { get; init; }

{ { properties_declarations } }

{ { parse_declaration } }

{ { constructor_declaration } }
}
}
"""";
private const string RegexDtoParseDeclaration = """
"""";

private const string RegexDtoParseDeclaration = """
/// <summary>Parses the <paramref name="s">string</paramref> into an instance of <see cref="{{ type_name }}">the DTO</see></summary>
/// <param name="s">The <see langword="string" /> to parse</param>
/// <returns>The parsed <see cref="{{ type_name }}">DTO<see></returns>
public static {{ type_name }} Parse(string s)
{
return new {{ type_name }}(s);
public static
{ { type_name } }
Parse(string s)
{
return new {{ type_name }
} (s);
}
""";

private const string RegexDtoConstructorDeclaration = """
/// <summary>Instantiates a new, empty <see cref="{{ type_name }}" /></summary>
{{ parameterless_constructor_visibility }} {{ type_name }} () { }

/// <summary>Instantiates a new <see cref="{{ type_name }}" /> and parses the <paramref name="s" /></summary>
{{ parameterized_constructor_visibility }} {{ type_name }} (string s)
{
var match = Regex().Match(s);
if (!match.Success)
{
throw new ArgumentException($"The string \"{s}\" does not match the regular expression \"{RegexString}\".", nameof(s));
}

{{~ for property in properties ~}}
{{~ if property.is_nullable ~}}
{{ property.name }} = string.IsNullOrEmpty(match.Groups["{{ property.name }}"]?.Value) ? null : ({{ property.type }}?)System.Convert.ChangeType(match.Groups["{{ property.name }}"]?.Value, typeof({{ property.type }}));
{{~ else ~}}
{{ property.name }} = ({{ property.type }})System.Convert.ChangeType(match.Groups["{{ property.name }}"]?.Value, typeof({{ property.type }}));
{{~ end ~}}
{{~ end ~}}

OriginalString = s;
}
""";
{ { parameterless_constructor_visibility } }
{ { type_name } } () { }

/// <summary>Instantiates a new <see cref="{{ type_name }}" /> and parses the <paramref name="s" /></summary>
{ { parameterized_constructor_visibility } }
{ { type_name } } (string s)
{
var match = Regex().Match(s);
if (!match.Success)
{
throw new ArgumentException($"The string \"{s}\" does not match the regular expression \"{RegexString}\".", nameof(s));
}

{ { ~ for property in properties ~} }
{ { ~ if property.is_nullable ~} }
{ { property.name } } = string.IsNullOrEmpty(match.Groups["{{ property.name }}"]?.Value) ? null : ({ { property.type } }?)System.Convert.ChangeType(match.Groups["{{ property.name }}"]?.Value, typeof({ { property.type } }));
{ { ~ else ~} }
{ { property.name } } = ({ { property.type } })System.Convert.ChangeType(match.Groups["{{ property.name }}"]?.Value, typeof({ { property.type } }));
{ { ~end ~} }
{ { ~end ~} }

OriginalString = s;
}
""";

private const string RegexDtoPropertyDeclaration = """
{{~ if is_nullable ~}}
public {{ overridability }} {{ type }}? {{ name }} { get; set; }
{{~ else ~}}
public {{ overridability }} {{ type }} {{ name }} { get; set; }
{{~end ~}}
""";
{ { ~ if is_nullable ~} }
public
{ { overridability } }
{ { type } }?
{ { name } }
{ get; set; }
{ { ~ else ~} }
public
{ { overridability } }
{ { type } }
{ { name } }
{ get; set; }
{ { ~end ~} }
""";

private const string RegexDtoPropertiesDeclaration = $$$"""
{{~ for property in properties ~}}
{{{RegexDtoPropertyDeclaration}}}
{{~end ~}}
""";
{ { ~ for property in properties ~} }
{ { { RegexDtoPropertyDeclaration} } }
{ { ~end ~} }
""";

private static readonly Template RegexDtoDeclarationTemplate = Parse(
RegexDtoTypeDeclaration,
nameof(RegexDtoTypeDeclaration)
);
private static readonly Template RegexDtoBaseTypeDeclarationTemplate = Parse(
RegexDtoBaseTypeDeclaration,
nameof(RegexDtoBaseTypeDeclaration)
);
private static readonly Template RegexDtoParseDeclarationTemplate = Parse(
RegexDtoParseDeclaration,
nameof(RegexDtoParseDeclaration)
);
private static readonly Template RegexDtoPropertiesDeclarationTemplate = Parse(
RegexDtoPropertiesDeclaration,
nameof(RegexDtoPropertiesDeclaration)
);
private static readonly Template RegexDtoConstructorDeclarationTemplate = Parse(
RegexDtoConstructorDeclaration,
nameof(RegexDtoConstructorDeclaration)
);
private static readonly Template RegexDtoAttributeDeclarationTemplate = Parse(
RegexDtoAttributeDeclaration,
nameof(RegexDtoAttributeDeclaration)
);
private static readonly Template RegexDtoPropertyDeclarationTemplate = Parse(
RegexDtoPropertyDeclaration,
nameof(RegexDtoPropertyDeclaration)
);
private static Template HeaderTemplate => Parse(Header, nameof(Header));
public static string RenderedRegexDtoAttributeDeclaration =>
HeaderTemplate.Render(new FilenameAndTimestamp(RegexDtoAttributeFilename))
+ RegexDtoAttributeDeclarationTemplate.Render();
public static string RenderDtoConstructorDeclaration(
RegexDtoConstructorDeclarationModel model
) => RegexDtoConstructorDeclarationTemplate.Render(model);
public static string RenderRegexDtoPropertiesDeclaration(
RegexDtoPropertiesDeclarationModel model
) => RegexDtoPropertiesDeclarationTemplate.Render(model);
public static string RenderRegexDtoParseDeclaration(RegexDtoPropertiesDeclarationModel model) =>
RegexDtoParseDeclarationTemplate.Render(model);
public static string RenderRegexDtoDeclaration(RegexDtoDeclarationModel model) =>
RegexDtoDeclarationTemplate.Render(model);
public static string RenderHeader(RegexDtoFileModel model) => HeaderTemplate.Render(model);
public static string RenderRegexDtoBaseTypeDeclaration(
RegexDtoBaseTypeDeclarationModel model
) => RegexDtoBaseTypeDeclarationTemplate.Render(model);
public static string RenderRegexDtoPropertyDeclaration(
RegexDtoPropertyDeclarationModel model
) => RegexDtoPropertyDeclarationTemplate.Render(model);
);

private static readonly Template RegexDtoBaseTypeDeclarationTemplate = Parse(
RegexDtoBaseTypeDeclaration,
nameof(RegexDtoBaseTypeDeclaration)
);

private static readonly Template RegexDtoParseDeclarationTemplate = Parse(
RegexDtoParseDeclaration,
nameof(RegexDtoParseDeclaration)
);

private static readonly Template RegexDtoPropertiesDeclarationTemplate = Parse(
RegexDtoPropertiesDeclaration,
nameof(RegexDtoPropertiesDeclaration)
);

private static readonly Template RegexDtoConstructorDeclarationTemplate = Parse(
RegexDtoConstructorDeclaration,
nameof(RegexDtoConstructorDeclaration)
);

private static readonly Template RegexDtoAttributeDeclarationTemplate = Parse(
RegexDtoAttributeDeclaration,
nameof(RegexDtoAttributeDeclaration)
);
private static readonly Template RegexDtoPropertyDeclarationTemplate = Parse(
RegexDtoPropertyDeclaration,
nameof(RegexDtoPropertyDeclaration)
);

private static Template HeaderTemplate => Parse(Header, nameof(Header));

public static string RenderedRegexDtoAttributeDeclaration =>
HeaderTemplate.Render(new FilenameAndTimestamp(RegexDtoAttributeFilename))
+ RegexDtoAttributeDeclarationTemplate.Render();

public static string RenderDtoConstructorDeclaration(
RegexDtoConstructorDeclarationModel model
) => RegexDtoConstructorDeclarationTemplate.Render(model);

public static string RenderRegexDtoPropertiesDeclaration(
RegexDtoPropertiesDeclarationModel model
) => RegexDtoPropertiesDeclarationTemplate.Render(model);

public static string RenderRegexDtoParseDeclaration(RegexDtoPropertiesDeclarationModel model) =>
RegexDtoParseDeclarationTemplate.Render(model);

public static string RenderRegexDtoDeclaration(RegexDtoDeclarationModel model) =>
RegexDtoDeclarationTemplate.Render(model);

public static string RenderHeader(RegexDtoFileModel model) => HeaderTemplate.Render(model);

public static string RenderRegexDtoBaseTypeDeclaration(
RegexDtoBaseTypeDeclarationModel model
) => RegexDtoBaseTypeDeclarationTemplate.Render(model);

public static string RenderRegexDtoPropertyDeclaration(
RegexDtoPropertyDeclarationModel model
) => RegexDtoPropertyDeclarationTemplate.Render(model);
}
Loading
Loading