Skip to content

Commit 6b24e2d

Browse files
committed
add comments
1 parent fbbfe0b commit 6b24e2d

File tree

5 files changed

+89
-27
lines changed

5 files changed

+89
-27
lines changed

ColumnInfo.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using System.Collections.Generic;
2-
31
namespace T4Sample
42
{
53
public class ColumnInfo

ITextTemplate.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,26 @@
22

33
namespace T4Sample
44
{
5+
/// <summary>
6+
/// Preprocessed text template.
7+
/// </summary>
8+
/// <remarks>
9+
/// This interface is used to avoid compile errors in code that calls T4 classes,
10+
/// and uses the "default implementation of interfaces" feature.
11+
/// Learn More: https://devblogs.microsoft.com/dotnet/default-implementations-in-interfaces/
12+
/// </remarks>
513
public interface ITextTemplate
614
{
15+
/// <summary>
16+
/// Generate the output text of the transformation.
17+
/// </summary>
18+
/// <returns>
19+
/// Throws <see cref="NotImplementedException"/>.
20+
/// After build, it should be overridden by the implementation.
21+
/// </returns>
22+
/// <exception cref="NotImplementedException">
23+
/// Throws when the default implementation is called.
24+
/// </exception>
725
string TransformText() => throw new NotImplementedException();
826
}
927
}

Program.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ class Program
77
{
88
static void Main()
99
{
10+
// DB Type => .NET Type
1011
var typeDef = new Dictionary<string, string>()
1112
{
1213
{ "integer", "int" },
1314
{ "varchar", "string" },
1415
{ "date", "DateTime" },
1516
};
1617

18+
// Mock Table Data
1719
var table = new TableInfo()
1820
{
1921
Name = "staff_master",
@@ -27,8 +29,11 @@ static void Main()
2729
}
2830
};
2931

32+
// This variable type should be an interface because avoid CS1061 compile error.
33+
// At runtime, the implementation's TransformText() is called.
3034
ITextTemplate template = new TableEntityTemplate(typeDef, "MyNameSpace", table);
3135

36+
// Actually, you can output to a file like "StaffMaster.cs".
3237
Console.WriteLine(template.TransformText());
3338
}
3439
}

StringHelper.cs

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,60 @@
11
using System;
2-
using System.Buffers;
32

43
namespace T4Sample
54
{
5+
/// <summary>
6+
/// Extension methods for <see cref="string"/>.
7+
/// </summary>
68
public static class StringHelper
79
{
10+
/// <summary>
11+
/// Convert snake_case to PascalCase(UpperCamel).
12+
/// </summary>
13+
/// <param name="snakeCaseString">snake_case string.</param>
14+
/// <returns>PascalCase string.</returns>
815
public static string ToPascalCase(this string snakeCaseString)
9-
=> string.IsNullOrEmpty(snakeCaseString) ? snakeCaseString : ToCamelCaseInner(snakeCaseString.AsSpan(), true);
16+
=> string.IsNullOrEmpty(snakeCaseString) ?
17+
snakeCaseString : ToCamelCaseInner(snakeCaseString.AsSpan(), true);
18+
19+
/// <summary>
20+
/// Convert snake_case to camelCase(lowerCamel).
21+
/// </summary>
22+
/// <param name="snakeCaseString">snake_case string.</param>
23+
/// <returns>camelCase string.</returns>
1024
public static string ToCamelCase(this string snakeCaseString)
11-
=> string.IsNullOrEmpty(snakeCaseString) ? snakeCaseString : ToCamelCaseInner(snakeCaseString.AsSpan(), false);
25+
=> string.IsNullOrEmpty(snakeCaseString) ?
26+
snakeCaseString : ToCamelCaseInner(snakeCaseString.AsSpan(), false);
1227

28+
/// <summary>
29+
/// Inner Method for <see cref="ToPascalCase(string)"/> and <see cref="ToCamelCase(string)"/>.
30+
/// </summary>
31+
/// <param name="source">snake_case chars span.</param>
32+
/// <param name="isUpper">First Letter is Upper or Lower.</param>
33+
/// <returns>(Upper|Lower) camelCase string.</returns>
1334
private static string ToCamelCaseInner(ReadOnlySpan<char> source, bool isUpper)
1435
{
15-
var buffer = ArrayPool<char>.Shared.Rent(source.Length);
16-
try
17-
{
18-
var bufferSpan = buffer.AsSpan();
36+
// if length is short, use stackalloc to avoid `new`.
37+
// TODO: use ArrayPool to avoid `new` completely.
38+
var buffer = source.Length <= 100 ?
39+
stackalloc char[source.Length] : new char[source.Length];
1940

20-
int written = 0;
21-
foreach (var c in source)
41+
int written = 0;
42+
foreach (var c in source)
43+
{
44+
if (c == '_')
2245
{
23-
if (c == '_')
24-
{
25-
// (written != 0) means "Is Not First Letter".
26-
// if camelCase, isUpper switches `true` when "Is Not First Letter" and comes "_".
27-
// if PascalCase, isUpper switches `true` when comes "_".
28-
isUpper |= (written != 0);
29-
continue;
30-
}
31-
32-
bufferSpan[written++] = isUpper ? char.ToUpperInvariant(c) : char.ToLowerInvariant(c);
33-
isUpper = false;
46+
// (written != 0) means "Is Not First Letter".
47+
// if camelCase and first letter, (isUpper | "Is Not First Letter") is false.
48+
// if PascalCase and first letter, (isUpper | "Is Not First Letter") is true.
49+
// if not first letter, (isUpper | "Is Not First Letter") is true whether isUpper is true or not.
50+
isUpper |= (written != 0);
51+
continue;
3452
}
35-
return (written == 0) ? "" : new string(bufferSpan.Slice(0, written));
36-
}
37-
finally
38-
{
39-
ArrayPool<char>.Shared.Return(buffer);
53+
54+
buffer[written++] = isUpper ? char.ToUpperInvariant(c) : char.ToLowerInvariant(c);
55+
isUpper = false;
4056
}
57+
return (written == 0) ? "" : new string(buffer.Slice(0, written));
4158
}
4259
}
4360
}

TableEntityTemplate.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,39 @@ namespace T4Sample
44
{
55
public partial class TableEntityTemplate : ITextTemplate
66
{
7+
/// <summary>
8+
/// Assigns Databases Column Type Name to .NET Type Name.
9+
/// </summary>
710
private readonly Dictionary<string, string> _typeDictionary;
811

12+
/// <summary>
13+
/// Entity's Namespace.
14+
/// </summary>
915
public string NameSpace { get; }
1016

17+
/// <summary>
18+
/// Table Data.
19+
/// </summary>
1120
public TableInfo Table { get; }
1221

22+
/// <summary>
23+
/// Creates an Instance of TableEntityTemplate.
24+
/// </summary>
25+
/// <param name="typeDictionary">Assigns Databases Column Type Name to .NET Type Name.</param>
26+
/// <param name="nameSpace">Entity's Namespace.</param>
27+
/// <param name="table">Table Data.</param>
1328
public TableEntityTemplate(Dictionary<string, string> typeDictionary, string nameSpace, TableInfo table)
1429
=> (_typeDictionary, NameSpace, Table) = (typeDictionary, nameSpace, table);
1530

31+
/// <summary>
32+
/// Get .NET Type Name from typeDictionary.
33+
/// </summary>
34+
/// <param name="column">Column's Data.</param>
35+
/// <returns>
36+
/// Returns typeDictionary's value if found.
37+
/// Returns <see cref="ColumnInfo.Type"/> if not found.
38+
/// If the column is nullable, add "?" To the return value.
39+
/// </returns>
1640
public string GetColumnType(ColumnInfo column)
1741
=> _typeDictionary.TryGetValue(column.Type, out var n) ? n : column.Type
1842
+ (column.NotNull ? "" : "?");

0 commit comments

Comments
 (0)