build: Generate c# code for RowTypes
Produces structs that implement IBlueRow with strongly typed properties for each column.
This commit is contained in:
1 parent
d6d919afe4
commit
63e70650fb
4 files changed
+268
-1
No files matched your search
@@ -25,6 +25,12 @@
|
||||
"commands": [
|
||||
"fake"
|
||||
]
|
||||
},
|
||||
"dotnet-t4": {
|
||||
"version": "3.0.0",
|
||||
"commands": [
|
||||
"t4"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<Project>
|
||||
|
||||
<Import Project="..\Directory.Build.targets" />
|
||||
|
||||
<Target
|
||||
Name="GenerateRowTypes"
|
||||
BeforeTargets="BeforeCompile"
|
||||
Condition="'@(RowType)'!=''"
|
||||
Inputs="%(RowType.SchemaJson);%(RowType.Identity)"
|
||||
Outputs="$(IntermediateOutputPath)/RowTypes/%(RowType.TypeName).cs">
|
||||
<MakeDir Directories="$(IntermediateOutputPath)/RowTypes" />
|
||||
<Exec Command="dotnet t4 %(RowType.Identity) -p SchemaJson=%(RowType.SchemaJson) -p TypeName=%(RowType.TypeName) -p TableName=%(RowType.TableName) -o $(IntermediateOutputPath)/RowTypes/%(RowType.TypeName).cs" />
|
||||
<ItemGroup>
|
||||
<Compile Include="$(IntermediateOutputPath)/RowTypes/%(RowType.TypeName).cs" />
|
||||
</ItemGroup>
|
||||
</Target>
|
||||
|
||||
</Project>
|
||||
@@ -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<JsonObject>(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<System.Tuple<string, string, string, string, string>>();
|
||||
for (int colIdx = 0; colIdx < columnsJArray.Count; colIdx++) {
|
||||
var columnJ = columnsJArray[colIdx].AsObject();
|
||||
string name = columnJ["Name"].GetValue<string>();
|
||||
string type = columnJ["Type"].GetValue<string>();
|
||||
string key = columnJ.TryGetPropertyValue("Key", out var keyJ) ? keyJ.GetValue<string>() : name;
|
||||
string getter = columnJ.TryGetPropertyValue("Getter", out var getterJ) ? getterJ.GetValue<string>() : "{ get; init; }";
|
||||
string equalsOp = columnJ.TryGetPropertyValue("Equals", out var equalsJ) ? equalsJ.GetValue<string>() : $"EqualityComparer<{type}>.Default.Equals({name}, other.{name})";
|
||||
columns.Add(new(name, type, key, getter, equalsOp));
|
||||
}
|
||||
bool isTimeSeries = schemaJ.TryGetPropertyValue("IsTimeSeries", out jn) && jn.GetValue<bool>();
|
||||
#>
|
||||
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;
|
||||
}
|
||||
@@ -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<JsonObject>(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<System.Tuple<string, string, string, string, string>>();
|
||||
for (int colIdx = 0; colIdx < columnsJArray.Count; colIdx++) {
|
||||
var columnJ = columnsJArray[colIdx].AsObject();
|
||||
string name = columnJ["Name"].GetValue<string>();
|
||||
string type = columnJ["Type"].GetValue<string>();
|
||||
string key = columnJ.TryGetPropertyValue("Key", out var keyJ) ? keyJ.GetValue<string>() : name;
|
||||
string getter = columnJ.TryGetPropertyValue("Getter", out var getterJ) ? getterJ.GetValue<string>() : "{ get; init; }";
|
||||
string equalsOp = columnJ.TryGetPropertyValue("Equals", out var equalsJ) ? equalsJ.GetValue<string>() : $"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;
|
||||
}
|
||||
Reference in new issue
Block a user