From a1c6e0ccabbfe0b31b6982d855e57f66cd7b697a Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sun, 10 Nov 2024 15:32:33 +0000 Subject: [PATCH] build: Add t4 templates to generate c# row types Produces classes/structs that implement IBlueRow with strongly typed properties for each column. --- .config/dotnet-tools.json | 6 + src/DataStreamSuite/Directory.Build.targets | 18 +++ .../SharedStreams/RowTemplates/RefTypeRow.tt | 127 +++++++++++++++++ .../RowTemplates/ValueTypeRow.tt | 131 ++++++++++++++++++ 4 files changed, 282 insertions(+) create mode 100644 src/DataStreamSuite/Directory.Build.targets create mode 100644 src/DataStreamSuite/SharedStreams/RowTemplates/RefTypeRow.tt create mode 100644 src/DataStreamSuite/SharedStreams/RowTemplates/ValueTypeRow.tt diff --git a/.config/dotnet-tools.json b/.config/dotnet-tools.json index ceeb06ae8..02a9e91bd 100644 --- a/.config/dotnet-tools.json +++ b/.config/dotnet-tools.json @@ -25,6 +25,12 @@ "commands": [ "fake" ] + }, + "dotnet-t4": { + "version": "3.0.0", + "commands": [ + "t4" + ] } } } \ No newline at end of file diff --git a/src/DataStreamSuite/Directory.Build.targets b/src/DataStreamSuite/Directory.Build.targets new file mode 100644 index 000000000..ce26f3228 --- /dev/null +++ b/src/DataStreamSuite/Directory.Build.targets @@ -0,0 +1,18 @@ + + + + + + + + + + + + + diff --git a/src/DataStreamSuite/SharedStreams/RowTemplates/RefTypeRow.tt b/src/DataStreamSuite/SharedStreams/RowTemplates/RefTypeRow.tt new file mode 100644 index 000000000..0143b8c28 --- /dev/null +++ b/src/DataStreamSuite/SharedStreams/RowTemplates/RefTypeRow.tt @@ -0,0 +1,127 @@ +<#@ template language="C#" #> +<#@ output extension=".cs" #> +<#@ parameter name="TableName" type="System.String" #> +<#@ parameter name="TypeName" type="System.String" #> +<#@ parameter name="SchemaJson" type="System.String" #> +<#@ import namespace="System.Collections.Generic" #> +<#@ import namespace="System.Text.Json" #> +<#@ import namespace="System.Text.Json.Nodes" #> +<# + System.Console.WriteLine($"Reading json file '{SchemaJson}'"); + var schemaStr = System.IO.File.ReadAllText(SchemaJson); + var schemaJ = JsonSerializer.Deserialize(schemaStr); + if (schemaJ is null) { + throw new ArgumentException($"Failed to parse json from '{schemaJ}'"); + } + if (!schemaJ.TryGetPropertyValue("Columns", out JsonNode? columnsJNode)) { + throw new ArgumentException($"Failed to get 'Columns' field from '{schemaJ}'"); + } + var columnsJArray = columnsJNode.AsArray(); + if (columnsJArray is null) { + throw new ArgumentException($"Failed to get 'Columns' field from '{schemaJ}'"); + } + var columns = new List, string, string>>(); + for (int colIdx = 0; colIdx < columnsJArray.Count; colIdx++) { + var columnJ = columnsJArray[colIdx].AsObject(); + string name = columnJ["Name"].GetValue(); + string type = columnJ["Type"].GetValue(); + string key = columnJ.TryGetPropertyValue("Key", out var keyJ) ? keyJ.GetValue() : name; + columnJ.TryGetPropertyValue("Getter", out var getterJ); + List getterLines = getterJ?.GetValueKind() switch { + JsonValueKind.Array => new(getterJ.AsArray().GetValues()), + _ => new(1) { getterJ?.GetValue() ?? "{ get; init; }" }, + }; + string equalsOp = columnJ.TryGetPropertyValue("Equals", out var equalsJ) ? equalsJ.GetValue() : $"EqualityComparer<{type}>.Default.Equals({name}, other.{name})"; + string propType = columnJ.TryGetPropertyValue("PropType", out var propTypeJ) ? propTypeJ.GetValue() : type; + columns.Add(new(name, type, key, getterLines, equalsOp, propType)); + } + bool isTimeSeries = schemaJ.TryGetPropertyValue("IsTimeSeries", out columnsJNode) && columnsJNode.GetValue(); +#> +namespace Bts.SharedStreams.Tables; + +using BCG; + +using System; + +public class <#=TypeName#> : <#=isTimeSeries ? "ITimeSeriesRow" : "IBlueRow" #>, IEquatable<<#=TypeName#>> { + public static IBlueTableSchema Schema { get; } = + new BlueTableSchema( + new ColumnInfo[] { + <# + // Generate properties + for (int i = 0; i < columns.Count; i++) { + var type = columns[i].Item2; + var key = columns[i].Item3; + #> + new("<#=key#>", <#=i#>, typeof(<#=type#>)), + <# + } + #> + }, + "<#= TableName #>"); + + IBlueTableSchema IBlueRow.schema => Schema; + + <# + // Generate properties + foreach (var column in columns) { + var name = column.Item1; + var propType = column.Item6; + var getterLines = column.Item4; + var getter = string.Join("\n ", getterLines); + #> +public <#= propType #> <#= name #> <#= getter #> + <# + } + #> + + // Indexer by column name + public object? this[string col] => col switch { + <# + // Generate case statements for each column name + for (int i = 0; i < columns.Count; i++) { + var name = columns[i].Item1; + var key = columns[i].Item3; + #> +"<#= key #>" => <#= name #>, + <# +} + #> +_ => throw new KeyNotFoundException() + }; + + // Indexer by column index + public object? this[int col] => col switch { + <# + // Generate case statements for each column index + for (int i = 0; i < columns.Count; i++) { + var name = columns[i].Item1; + #> +<#= i #> => <#= name #>, + <# +} + #> +_ => throw new IndexOutOfRangeException() + }; + + public bool Equals(<#= TypeName #>? other) => + other is not null + <# + // Generate properties + for (int i = 0; i < columns.Count; i++) { + var equalsOp = columns[i].Item5; + if (string.IsNullOrEmpty(equalsOp)) { + continue; + } + equalsOp = equalsOp.Insert(0, "&& "); + #> + <#=equalsOp#> + <#}#> + ; + + public override bool Equals(object? obj) => + obj is <#=TypeName#> other && Equals(other); + + public override int GetHashCode() => + Id; +} diff --git a/src/DataStreamSuite/SharedStreams/RowTemplates/ValueTypeRow.tt b/src/DataStreamSuite/SharedStreams/RowTemplates/ValueTypeRow.tt new file mode 100644 index 000000000..864554f30 --- /dev/null +++ b/src/DataStreamSuite/SharedStreams/RowTemplates/ValueTypeRow.tt @@ -0,0 +1,131 @@ +<#@ template language="C#" #> +<#@ output extension=".cs" #> +<#@ parameter name="TableName" type="System.String" #> +<#@ parameter name="TypeName" type="System.String" #> +<#@ parameter name="SchemaJson" type="System.String" #> +<#@ import namespace="System.Collections.Generic" #> +<#@ import namespace="System.Text.Json" #> +<#@ import namespace="System.Text.Json.Nodes" #> +<# + System.Console.WriteLine($"Reading json file '{SchemaJson}'"); + var schemaStr = System.IO.File.ReadAllText(SchemaJson); + var schemaJ = JsonSerializer.Deserialize(schemaStr); + if (schemaJ is null) { + throw new ArgumentException($"Failed to parse json from '{schemaJ}'"); + } + if (!schemaJ.TryGetPropertyValue("Columns", out JsonNode? columnsJNode)) { + throw new ArgumentException($"Failed to get 'Columns' field from '{schemaJ}'"); + } + var columnsJArray = columnsJNode.AsArray(); + if (columnsJArray is null) { + throw new ArgumentException($"Failed to get 'Columns' field from '{schemaJ}'"); + } + var columns = new List, string, string>>(); + for (int colIdx = 0; colIdx < columnsJArray.Count; colIdx++) { + var columnJ = columnsJArray[colIdx].AsObject(); + string name = columnJ["Name"].GetValue(); + string type = columnJ["Type"].GetValue(); + string key = columnJ.TryGetPropertyValue("Key", out var keyJ) ? keyJ.GetValue() : name; + columnJ.TryGetPropertyValue("Getter", out var getterJ); + List getterLines = getterJ?.GetValueKind() switch { + JsonValueKind.Array => new(getterJ.AsArray().GetValues()), + _ => new(1) { getterJ?.GetValue() ?? "{ get; init; }" }, + }; + string equalsOp = columnJ.TryGetPropertyValue("Equals", out var equalsJ) ? equalsJ.GetValue() : $"EqualityComparer<{type}>.Default.Equals({name}, other.{name})"; + string propType = columnJ.TryGetPropertyValue("PropType", out var propTypeJ) ? propTypeJ.GetValue() : type; + columns.Add(new(name, type, key, getterLines, equalsOp, propType)); + } + bool isTimeSeries = schemaJ.TryGetPropertyValue("IsTimeSeries", out columnsJNode) && columnsJNode.GetValue(); +#> +namespace Bts.SharedStreams.Tables; + +using BCG; + +using System; + +public readonly struct <#=TypeName#> : IBlueRow, IEquatable<<#=TypeName#>> { + public static IBlueTableSchema Schema { get; } = + new BlueTableSchema( + new ColumnInfo[] { + <# + // Generate properties + for (int i = 0; i < columns.Count; i++) { + var type = columns[i].Item2; + var key = columns[i].Item3; + #> + new("<#=key#>", <#=i#>, typeof(<#=type#>)), + <# + } + #> + }, + "<#= TableName #>"); + + IBlueTableSchema IBlueRow.schema => Schema; + + <# + // Generate properties + foreach (var column in columns) { + var name = column.Item1; + var propType = column.Item6; + var getterLines = column.Item4; + var getter = string.Join("\n ", getterLines); + #> +public <#= propType #> <#= name #> <#= getter #> + <# + } + #> + + // Indexer by column name + public readonly object? this[string col] => col switch { + <# + // Generate case statements for each column name + for (int i = 0; i < columns.Count; i++) { + var name = columns[i].Item1; + var key = columns[i].Item3; + #> +"<#= key #>" => <#= name #>, + <# +} + #> +_ => throw new KeyNotFoundException() + }; + + // Indexer by column index + public readonly object? this[int col] => col switch { + <# + // Generate case statements for each column index + for (int i = 0; i < columns.Count; i++) { + var name = columns[i].Item1; + #> +<#= i #> => <#= name #>, + <# +} + #> +_ => throw new IndexOutOfRangeException() + }; + + public bool Equals(<#= TypeName #> other) => + <# + // Generate properties + bool isFirstComp = true; + for (int i = 0; i < columns.Count; i++) { + var equalsOp = columns[i].Item5; + if (string.IsNullOrEmpty(equalsOp)) { + continue; + } + if (!isFirstComp) { + equalsOp = equalsOp.Insert(0, "&& "); + } else { + isFirstComp = false; + } + #> + <#=equalsOp#> + <#}#> + ; + + public override bool Equals(object? obj) => + obj is <#=TypeName#> other && Equals(other); + + public override int GetHashCode() => + Id; +}