From 63e70650fb8da81fed3d67fb03ea2dce9c861541 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sun, 10 Nov 2024 15:32:33 +0000 Subject: [PATCH] build: Generate c# code for RowTypes Produces structs that implement IBlueRow with strongly typed properties for each column. --- .config/dotnet-tools.json | 8 +- src/DataStreamSuite/Directory.Build.targets | 18 +++ .../SharedStreams/RowTemplates/RefTypeRow.tt | 120 +++++++++++++++++ .../RowTemplates/ValueTypeRow.tt | 123 ++++++++++++++++++ 4 files changed, 268 insertions(+), 1 deletion(-) 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 f21830c0d..c135ce0ad 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..152ab356a --- /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..dde5841bb --- /dev/null +++ b/src/DataStreamSuite/SharedStreams/RowTemplates/RefTypeRow.tt @@ -0,0 +1,120 @@ +<#@ 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.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? jn)) { + throw new ArgumentException($"Failed to get 'Columns' field from '{schemaJ}'"); + } + var columnsJArray = jn.AsArray(); + if (columnsJArray is null) { + throw new ArgumentException($"Failed to get 'Columns' field from '{schemaJ}'"); + } + var columns = new System.Collections.Generic.List>(); + 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; + string getter = columnJ.TryGetPropertyValue("Getter", out var getterJ) ? getterJ.GetValue() : "{ get; init; }"; + string equalsOp = columnJ.TryGetPropertyValue("Equals", out var equalsJ) ? equalsJ.GetValue() : $"EqualityComparer<{type}>.Default.Equals({name}, other.{name})"; + columns.Add(new(name, type, key, getter, equalsOp)); + } + bool isTimeSeries = schemaJ.TryGetPropertyValue("IsTimeSeries", out jn) && jn.GetValue(); +#> +namespace bts.SilverPine.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 type = column.Item2; + var getter = column.Item4; + #> +public <#= type #> <#= 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 IndexOutOfRangeException() + }; + + // 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 KeyNotFoundException() + }; + + 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..e2b2f40d6 --- /dev/null +++ b/src/DataStreamSuite/SharedStreams/RowTemplates/ValueTypeRow.tt @@ -0,0 +1,123 @@ +<#@ 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.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 System.Collections.Generic.List>(); + 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; + string getter = columnJ.TryGetPropertyValue("Getter", out var getterJ) ? getterJ.GetValue() : "{ get; init; }"; + string equalsOp = columnJ.TryGetPropertyValue("Equals", out var equalsJ) ? equalsJ.GetValue() : $"EqualityComparer<{type}>.Default.Equals({name}, other.{name})"; + columns.Add(new(name, type, key, getter, equalsOp)); + } +#> +namespace bts.SilverPine.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 type = column.Item2; + var getter = column.Item4; + #> +public <#= type #> <#= 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 IndexOutOfRangeException() + }; + + // 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 KeyNotFoundException() + }; + + 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; +}