8000 Supports Trimming by fallenwood · Pull Request #7 · fallenwood/minijinja-csharp · GitHub
[go: up one dir, main page]

Skip to content
Merged
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
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
**/.vs
**/bin
**/obj
**/.build
**/.build
**/output

# AI
.claude
7 changes: 6 additions & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,10 @@
<BaseIntermediateOutputPath>$(MSBuildThisFileDirectory).build\$(MSBuildProjectName)\obj\</BaseIntermediateOutputPath>
<!-- Hide results files in Solution Explorer -->
<DefaultItemExcludes>$(DefaultItemExcludes);TestResults\**</DefaultItemExcludes>

<!-- Enable trimming and AOT compatibility -->
<IsAotCompatible>true</IsAotCompatible>
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
<EnableSingleFileAnalyzer>true</EnableSingleFileAnalyzer>
</PropertyGroup>
</Project>
</Project>
2 changes: 2 additions & 0 deletions Directory.Packages.Props → Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@
<PackageVersion Include="xunit" Version="2.9.3" />
<PackageVersion Include="xunit.runner.visualstudio" Version="3.1.5" />
<PackageVersion Include="FluentAssertions" Version="8.8.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" />
</ItemGroup>
</Project>
204 changes: 199 additions & 5 deletions example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,29 @@ static void Section(string title) {
using var env = new MJEnvironment();
env.AddTemplate("hello", "Hello {{ name }}!");
var tmpl = env.GetTemplate("hello");
var result = tmpl.Render(new { name = "World" });
// Using dictionary for context (AOT-compatible)
var result = tmpl.Render(new Dictionary<string, object?> { ["name"] = "World" });
Console.WriteLine(result);
}

Section("Template From String"); {
using var env = new MJEnvironment();
var tmpl = env.TemplateFromString("Hello {{ name }}!");
Console.WriteLine(tmpl.Render(new { name = "World" }));
Console.WriteLine(tmpl.Render(new Dictionary<string, object?> { ["name"] = "World" }));
}

Section("Custom Type with ITemplateSerializable"); {
using var env = new MJEnvironment();
var tmpl = env.TemplateFromString("{{ name }} is {{ age }} years old");
var person = new Person { Name = "Alice", Age = 30 };
Console.WriteLine(tmpl.Render(person));
}

Section("Custom Type with Source Generator"); {
using var env = new MJEnvironment();
var tmpl = env.TemplateFromString("{{ name }} is {{ age }} years old and lives in {{ city }}");
var employee = new Employee { Name = "Bob", Age = 25, City = "Seattle" };
Console.WriteLine(tmpl.Render(employee));
}

Section("Template Syntax (Variables, Blocks, Filters)"); {
Expand All @@ -46,7 +61,10 @@ No items.
);

var tmpl = env.GetTemplate("syntax");
var output = tmpl.Render(new { name = "world", items = new[] { "a", "b", "c" } });
var output = tmpl.Render(new Dictionary<string, object?> {
["name"] = "world",
["items"] = new[] { "a", "b", "c" }
});
Console.WriteLine(output.Trim());
}

Expand All @@ -62,7 +80,7 @@ No items.

env.AddTemplate("reverse-demo", "{{ value|reverse }}");
var tmpl = env.GetTemplate("reverse-demo");
Console.WriteLine(tmpl.Render(new { value = "hello" }));
Console.WriteLine(tmpl.Render(new Dictionary<string, object?> { ["value"] = "hello" }));
}

Section("Custom Function (repeat)"); {
Expand All @@ -77,7 +95,7 @@ No items.

env.AddTemplate("repeat-demo", "{{ repeat(word, 3) }}");
var tmpl = env.GetTemplate("repeat-demo");
Console.WriteLine(tmpl.Render(new { word = "ha" }));
Console.WriteLine(tmpl.Render(new Dictionary<string, object?> { ["word"] = "ha" }));
}

Section("Built-in Function (range)"); {
Expand Down Expand Up @@ -107,6 +125,43 @@ No items.
Console.WriteLine(tmpl.Render(ctx));
}

Section("Complex Object with Nested Data"); {
using var env = new MJEnvironment();
var tmpl = env.TemplateFromString("""
Company: {{ name }}
Departments:
{% for dept in departments %}
- {{ dept.name }} ({{ dept.employees|length }} employees)
{% for emp in dept.employees %}
* {{ emp.name }}, {{ emp.role }}{% if emp.isManager %} (Manager){% endif %}
{% endfor %}
{% endfor %}
""");

var company = new Company {
Name = "Tech Corp",
Departments = new[] {
new Department {
Name = "Engineering",
Employees = new[] {
new EmployeeInfo { Name = "Alice", Role = "Senior Engineer", IsManager = true },
new EmployeeInfo { Name = "Bob", Role = "Engineer", IsManager = false },
new EmployeeInfo { Name = "Carol", Role = "Junior Engineer", IsManager = false }
}
},
new Department {
Name = "Sales",
Employees = new[] {
new EmployeeInfo { Name = "Dave", Role = "Sales Lead", IsManager = true },
new EmployeeInfo { Name = "Eve", Role = "Account Manager", IsManager = false }
}
}
}
};

Console.WriteLine(tmpl.Render(company).Trim());
}

Section("Error Handling"); {
using var env = new MJEnvironment();
try {
Expand All @@ -123,3 +178,142 @@ No items.
Console.WriteLine($"TemplateError: {e.Message}");
}
}

Section("Property Attributes and Naming Strategies"); {
using var env = new MJEnvironment();

// Example 1: Custom property names
var tmpl1 = env.TemplateFromString("User: {{ user_id }} - {{ display_name }}");
var user = new UserWithCustomNames { Id = 123, Name = "Alice" };
Console.WriteLine(tmpl1.Render(user));

// Example 2: Snake case naming strategy
var tmpl2 = env.TemplateFromString("{{ first_name }} {{ last_name }} ({{ email_address }})");
var contact = new ContactWithSnakeCase {
FirstName = "Bob",
LastName = "Smith",
EmailAddress = "bob@example.com"
};
Console.WriteLine(tmpl2.Render(contact));

// Example 3: Ignored properties
var tmpl3 = env.TemplateFromString("Username: {{ username }}, Password: {{ password }}");
8 var credentials = new CredentialsWithIgnore {
Username = "admin",
Password = "secret123"
};
Console.WriteLine(tmpl3.Render(credentials));
}

Section("JSON Serialization with Special Characters in Keys"); {
using var env = new MJEnvironment();

// Create a dictionary with keys containing special characters
var data = new Dictionary<string, Value> {
["normal_key"] = Value.FromString("value1"),
["key\"with\"quotes"] = Value.FromString("value2"),
["key\\with\\backslashes"] = Value.FromString("value3"),
["key\nwith\nnewlines"] = Value.FromString("value4"),
["key\twith\ttabs"] = Value.FromString("value5"),
["key\u0001with\u001fcontrol"] = Value.FromString("value6")
};

var value = Value.FromDict(data);
var json = value.ToJson(false);
Console.WriteLine("Compact JSON:");
Console.WriteLine(json);
Console.WriteLine();
Console.WriteLine("Pretty JSON:");
Console.WriteLine(value.ToJson(true));

// Verify the JSON is valid by checking it can be parsed
try {
System.Text.Json.JsonDocument.Parse(json);
Console.WriteLine("✓ JSON is valid");
} catch {
Console.WriteLine("✗ JSON is invalid!");
}
}

// Removed collision test to allow build to succeed.
// To test collision detection, create a class like:
// [MiniJinjaContext]
// partial class TestCollision {
// public int UserId { get; set; }
// public int userId { get; set; }
// }
// This will generate warning MINIJINJA001

// To test missing partial modifier detection:
// [MiniJinjaContext]
// class NonPartialTest { // Missing 'partial' keyword
// public string Name { get; set; } = "";
// }
// This will generate error MINIJINJA002

// Example of a custom type that implements ITemplateSerializable for AOT compatibility
class Person : ITemplateSerializable {
public string Name { get; set; } = "";
public int Age { get; set; }

public Dictionary<string, Value> ToTemplateValues() {
return new Dictionary<string, Value> {
["name"] = Value.FromString(Name),
["age"] = Value.FromInt(Age)
};
}
}

// Example of using the source generator - just mark the class with [MiniJinjaContext]
// and make it partial. The ToTemplateValues method will be generated automatically.
[MiniJinjaContext]
partial class Employee {
public string Name { get; set; } = "";
public int Age { get; set; }
public string City { get; set; } = "";
}

// Complex nested object example with collections
[MiniJinjaContext]
partial class Company {
public string Name { get; set; } = "";
public Department[] Departments { get; set; } = Array.Empty<Department>();
}

[MiniJinjaContext]
partial class Department {
public string Name { get; set; } = "";
public EmployeeInfo[] Employees { get; set; } = Array.Empty<EmployeeInfo>();
}

[MiniJinjaContext]
partial class EmployeeInfo {
public string Name { get; set; } = "";
public string Role { get; set; } = "";
public bool IsManager { get; set; }
}

// Examples for property attributes and naming strategies
[MiniJinjaContext]
partial class UserWithCustomNames {
[MiniJinjaProperty(Name = "user_id")]
public int Id { get; set; }

[MiniJinjaProperty(Name = "display_name")]
public string Name { get; set; } = "";
}

[MiniJinjaContext(KeyNamingStrategy = KeyNamingStrategy.SnakeCase)]
partial class ContactWithSnakeCase {
public string FirstName { get; set; } = "";
public string LastName { get; set; } = "";
public string EmailAddress { get; set; } = "";
}

[MiniJinjaContext]
partial class CredentialsWithIgnore {
public string Username { get; set; } = "";

[MiniJinjaProperty(Ignore = true)]
public string Password { get; set; } = "";
}
5 changes: 5 additions & 0 deletions example/example.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<IsPackable>false</IsPackable>
<PublishTrimmed>true</PublishTrimmed>
<InvariantGlobalization>true</InvariantGlobalization>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\minijinja\minijinja.csproj" />
<ProjectReference Include="..\minijinja.generator\MiniJinja.SourceGenerator.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "10.0.102",
"version": "10.0.101",
Comment thread
fallenwood marked this conversation as resolved.
Resolved
"rollForward": "latestFeature"
},
"msbuild-sdks": {
Expand Down
3 changes: 3 additions & 0 deletions minijinja.generator/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<Project>
<!-- Override parent Directory.Build.props for the source generator -->
</Project>
21 changes: 21 additions & 0 deletions minijinja.generator/MiniJinja.SourceGenerator.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Authors>Fallenwood</Authors>
<Company>Net.Fallenwood</Company>
<OutputType>Library</OutputType>
<TargetFramework>netstandard2.0</TargetFramework>
<Description>MSBuild targets to aid in crosscompiling Native AOT publishing.</Description>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<IsRoslynComponent>true</IsRoslynComponent>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<IsPackable>true</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" PrivateAssets="all" />
</ItemGroup>

</Project>
Loading
Loading
0