@wictorwilen/cocogen 1.0.0 → 1.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/README.md +88 -35
  2. package/dist/cli.d.ts.map +1 -1
  3. package/dist/cli.js +84 -14
  4. package/dist/cli.js.map +1 -1
  5. package/dist/init/init.d.ts.map +1 -1
  6. package/dist/init/init.js +302 -156
  7. package/dist/init/init.js.map +1 -1
  8. package/dist/init/templates/dotnet/AGENTS.md.ejs +20 -0
  9. package/dist/init/templates/dotnet/Core/ConnectorCore.cs.ejs +602 -0
  10. package/dist/init/templates/dotnet/Datasource/CsvItemSource.cs.ejs +12 -3
  11. package/dist/init/templates/dotnet/Datasource/IItemSource.cs.ejs +10 -4
  12. package/dist/init/templates/dotnet/Generated/Constants.cs.ejs +5 -1
  13. package/dist/init/templates/dotnet/Generated/CsvParser.cs.ejs +61 -0
  14. package/dist/init/templates/dotnet/Generated/FromCsvRow.cs.ejs +11 -3
  15. package/dist/init/templates/dotnet/Generated/ItemPayload.cs.ejs +13 -3
  16. package/dist/init/templates/dotnet/Generated/Model.cs.ejs +5 -22
  17. package/dist/init/templates/dotnet/Generated/PropertyTransformBase.cs.ejs +45 -0
  18. package/dist/init/templates/dotnet/Generated/SchemaPayload.cs.ejs +9 -1
  19. package/dist/init/templates/dotnet/Program.commandline.cs.ejs +76 -278
  20. package/dist/init/templates/dotnet/PropertyTransform.cs.ejs +10 -0
  21. package/dist/init/templates/dotnet/README.md.ejs +6 -7
  22. package/dist/init/templates/dotnet/appsettings.json.ejs +1 -1
  23. package/dist/init/templates/ts/.env.example.ejs +1 -1
  24. package/dist/init/templates/ts/AGENTS.md.ejs +20 -0
  25. package/dist/init/templates/ts/README.md.ejs +6 -7
  26. package/dist/init/templates/ts/src/cli.ts.ejs +85 -173
  27. package/dist/init/templates/ts/src/core/connectorCore.ts.ejs +384 -0
  28. package/dist/init/templates/ts/src/datasource/csvItemSource.ts.ejs +12 -4
  29. package/dist/init/templates/ts/src/datasource/itemSource.ts.ejs +12 -5
  30. package/dist/init/templates/ts/src/generated/constants.ts.ejs +16 -0
  31. package/dist/init/templates/ts/src/generated/csv.ts.ejs +10 -0
  32. package/dist/init/templates/ts/src/generated/fromCsvRow.ts.ejs +12 -37
  33. package/dist/init/templates/ts/src/generated/index.ts.ejs +3 -0
  34. package/dist/init/templates/ts/src/generated/itemPayload.ts.ejs +12 -3
  35. package/dist/init/templates/ts/src/generated/model.ts.ejs +3 -11
  36. package/dist/init/templates/ts/src/generated/propertyTransformBase.ts.ejs +40 -0
  37. package/dist/init/templates/ts/src/generated/schemaPayload.ts.ejs +3 -0
  38. package/dist/init/templates/ts/src/index.ts.ejs +4 -1
  39. package/dist/init/templates/ts/src/propertyTransform.ts.ejs +16 -0
  40. package/dist/ir.d.ts +2 -0
  41. package/dist/ir.d.ts.map +1 -1
  42. package/dist/tsp/init-tsp.d.ts.map +1 -1
  43. package/dist/tsp/init-tsp.js +50 -7
  44. package/dist/tsp/init-tsp.js.map +1 -1
  45. package/dist/tsp/loader.d.ts.map +1 -1
  46. package/dist/tsp/loader.js +23 -9
  47. package/dist/tsp/loader.js.map +1 -1
  48. package/dist/typespec/decorators.d.ts +1 -0
  49. package/dist/typespec/decorators.d.ts.map +1 -1
  50. package/dist/typespec/decorators.js +7 -2
  51. package/dist/typespec/decorators.js.map +1 -1
  52. package/dist/typespec/state.d.ts +2 -0
  53. package/dist/typespec/state.d.ts.map +1 -1
  54. package/dist/typespec/state.js +1 -0
  55. package/dist/typespec/state.js.map +1 -1
  56. package/dist/validate/validator.d.ts.map +1 -1
  57. package/dist/validate/validator.js +127 -14
  58. package/dist/validate/validator.js.map +1 -1
  59. package/package.json +2 -1
  60. package/typespec/main.tsp +6 -2
  61. package/dist/init/templates/dotnet/Generated/PersonEntityDefaults.cs.ejs +0 -48
  62. package/dist/init/templates/dotnet/Generated/PropertyTransforms.cs.ejs +0 -22
  63. package/dist/init/templates/dotnet/PersonEntityOverrides.cs.ejs +0 -49
  64. package/dist/init/templates/dotnet/Program.cs.ejs +0 -487
  65. package/dist/init/templates/ts/src/generated/personEntityDefaults.ts.ejs +0 -33
  66. package/dist/init/templates/ts/src/generated/propertyTransforms.ts.ejs +0 -23
  67. package/dist/init/templates/ts/src/personEntityOverrides.ts.ejs +0 -36
@@ -1,72 +1,115 @@
1
+ // CSV parsing helpers used by generated transforms.
1
2
  namespace <%= namespaceName %>.Datasource;
2
3
 
4
+ /// <summary>
5
+ /// Helpers for parsing typed values from CSV rows.
6
+ /// </summary>
3
7
  public static class CsvParser
4
8
  {
9
+ /// <summary>
10
+ /// Read the first matching header value from the row.
11
+ /// </summary>
5
12
  public static string ReadValue(IReadOnlyDictionary<string, string?> row, string[] headers)
6
13
  {
7
14
  if (headers.Length == 0) return "";
8
15
  return row.TryGetValue(headers[0], out var v) ? (v ?? "") : "";
9
16
  }
10
17
 
18
+ /// <summary>
19
+ /// Parse a nullable string value into a string.
20
+ /// </summary>
11
21
  public static string ParseString(string? value)
12
22
  {
13
23
  return value ?? "";
14
24
  }
15
25
 
26
+ /// <summary>
27
+ /// Parse a string from a CSV row using the provided headers.
28
+ /// </summary>
16
29
  public static string ParseString(IReadOnlyDictionary<string, string?> row, string[] headers)
17
30
  {
18
31
  return ParseString(ReadValue(row, headers));
19
32
  }
20
33
 
34
+ /// <summary>
35
+ /// Parse a boolean from a CSV row using the provided headers.
36
+ /// </summary>
21
37
  public static bool ParseBoolean(IReadOnlyDictionary<string, string?> row, string[] headers)
22
38
  {
23
39
  return ParseBoolean(ParseString(row, headers));
24
40
  }
25
41
 
42
+ /// <summary>
43
+ /// Parse a boolean from a string value.
44
+ /// </summary>
26
45
  public static bool ParseBoolean(string? value)
27
46
  {
28
47
  var v = ParseString(value);
29
48
  return v.Equals("true", StringComparison.OrdinalIgnoreCase) || v.Equals("1");
30
49
  }
31
50
 
51
+ /// <summary>
52
+ /// Parse an Int64 from a CSV row using the provided headers.
53
+ /// </summary>
32
54
  public static long ParseInt64(IReadOnlyDictionary<string, string?> row, string[] headers)
33
55
  {
34
56
  return ParseInt64(ParseString(row, headers));
35
57
  }
36
58
 
59
+ /// <summary>
60
+ /// Parse an Int64 from a string value.
61
+ /// </summary>
37
62
  public static long ParseInt64(string? value)
38
63
  {
39
64
  var v = ParseString(value);
40
65
  return long.TryParse(v, out var n) ? n : 0;
41
66
  }
42
67
 
68
+ /// <summary>
69
+ /// Parse a double from a CSV row using the provided headers.
70
+ /// </summary>
43
71
  public static double ParseDouble(IReadOnlyDictionary<string, string?> row, string[] headers)
44
72
  {
45
73
  return ParseDouble(ParseString(row, headers));
46
74
  }
47
75
 
76
+ /// <summary>
77
+ /// Parse a double from a string value.
78
+ /// </summary>
48
79
  public static double ParseDouble(string? value)
49
80
  {
50
81
  var v = ParseString(value);
51
82
  return double.TryParse(v, out var n) ? n : 0;
52
83
  }
53
84
 
85
+ /// <summary>
86
+ /// Parse a DateTimeOffset from a CSV row using the provided headers.
87
+ /// </summary>
54
88
  public static DateTimeOffset ParseDateTime(IReadOnlyDictionary<string, string?> row, string[] headers)
55
89
  {
56
90
  return ParseDateTime(ParseString(row, headers));
57
91
  }
58
92
 
93
+ /// <summary>
94
+ /// Parse a DateTimeOffset from a string value.
95
+ /// </summary>
59
96
  public static DateTimeOffset ParseDateTime(string? value)
60
97
  {
61
98
  var v = ParseString(value);
62
99
  return DateTimeOffset.TryParse(v, out var dt) ? dt : DateTimeOffset.MinValue;
63
100
  }
64
101
 
102
+ /// <summary>
103
+ /// Parse a string collection from a CSV row using the provided headers.
104
+ /// </summary>
65
105
  public static List<string> ParseStringCollection(IReadOnlyDictionary<string, string?> row, string[] headers)
66
106
  {
67
107
  return ParseStringCollection(ParseString(row, headers));
68
108
  }
69
109
 
110
+ /// <summary>
111
+ /// Parse a string collection from a string value.
112
+ /// </summary>
70
113
  public static List<string> ParseStringCollection(string? value)
71
114
  {
72
115
  var v = ParseString(value);
@@ -75,11 +118,17 @@ public static class CsvParser
75
118
  : v.Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).ToList();
76
119
  }
77
120
 
121
+ /// <summary>
122
+ /// Parse an Int64 collection from a CSV row using the provided headers.
123
+ /// </summary>
78
124
  public static List<long> ParseInt64Collection(IReadOnlyDictionary<string, string?> row, string[] headers)
79
125
  {
80
126
  return ParseInt64Collection(ParseString(row, headers));
81
127
  }
82
128
 
129
+ /// <summary>
130
+ /// Parse an Int64 collection from a string value.
131
+ /// </summary>
83
132
  public static List<long> ParseInt64Collection(string? value)
84
133
  {
85
134
  var v = ParseString(value);
@@ -89,11 +138,17 @@ public static class CsvParser
89
138
  .ToList();
90
139
  }
91
140
 
141
+ /// <summary>
142
+ /// Parse a double collection from a CSV row using the provided headers.
143
+ /// </summary>
92
144
  public static List<double> ParseDoubleCollection(IReadOnlyDictionary<string, string?> row, string[] headers)
93
145
  {
94
146
  return ParseDoubleCollection(ParseString(row, headers));
95
147
  }
96
148
 
149
+ /// <summary>
150
+ /// Parse a double collection from a string value.
151
+ /// </summary>
97
152
  public static List<double> ParseDoubleCollection(string? value)
98
153
  {
99
154
  var v = ParseString(value);
@@ -103,11 +158,17 @@ public static class CsvParser
103
158
  .ToList();
104
159
  }
105
160
 
161
+ /// <summary>
162
+ /// Parse a DateTimeOffset collection from a CSV row using the provided headers.
163
+ /// </summary>
106
164
  public static List<DateTimeOffset> ParseDateTimeCollection(IReadOnlyDictionary<string, string?> row, string[] headers)
107
165
  {
108
166
  return ParseDateTimeCollection(ParseString(row, headers));
109
167
  }
110
168
 
169
+ /// <summary>
170
+ /// Parse a DateTimeOffset collection from a string value.
171
+ /// </summary>
111
172
  public static List<DateTimeOffset> ParseDateTimeCollection(string? value)
112
173
  {
113
174
  var v = ParseString(value);
@@ -1,13 +1,21 @@
1
+ // Map CSV rows into the schema model.
1
2
  using <%= namespaceName %>;
2
3
  using <%= namespaceName %>.Datasource;
3
4
 
4
- namespace <%= namespaceName %>.Schema;
5
+ namespace <%= schemaNamespace %>;
5
6
 
7
+ /// <summary>
8
+ /// Maps CSV rows into the schema model using generated transforms.
9
+ /// </summary>
6
10
  public static class FromCsvRow
7
11
  {
8
- public static Item Parse(IReadOnlyDictionary<string, string?> row)
12
+ /// <summary>
13
+ /// Convert a CSV row dictionary into a schema model instance.
14
+ /// </summary>
15
+ public static <%= itemTypeName %> Parse(IReadOnlyDictionary<string, string?> row)
9
16
  {
10
- return new Item(
17
+ var transforms = new PropertyTransform();
18
+ return new <%= itemTypeName %>(
11
19
  <%- constructorArgLines %>
12
20
  );
13
21
  }
@@ -1,16 +1,26 @@
1
1
  <% const graphNs = graphApiVersion === "beta" ? "Microsoft.Graph.Beta" : "Microsoft.Graph"; -%>
2
+ // Build ExternalItem payloads for Graph ingestion.
2
3
  using <%= graphNs %>.Models.ExternalConnectors;
3
4
 
4
- namespace <%= namespaceName %>.Schema;
5
+ namespace <%= schemaNamespace %>;
5
6
 
7
+ /// <summary>
8
+ /// Maps schema model instances into Graph ExternalItem payloads.
9
+ /// </summary>
6
10
  public static class ItemPayload
7
11
  {
8
- public static string GetItemId(Item item)
12
+ /// <summary>
13
+ /// Resolve the external item ID for a schema model instance.
14
+ /// </summary>
15
+ public static string GetItemId(<%= itemTypeName %> item)
9
16
  {
10
17
  return <%- itemIdExpression %>;
11
18
  }
12
19
 
13
- public static ExternalItem ToExternalItem(Item item)
20
+ /// <summary>
21
+ /// Convert a schema model instance into a Graph ExternalItem payload.
22
+ /// </summary>
23
+ public static ExternalItem ToExternalItem(<%= itemTypeName %> item)
14
24
  {
15
25
  var props = new Properties
16
26
  {
@@ -1,28 +1,11 @@
1
- namespace <%= namespaceName %>.Schema;
1
+ // C# representation of the external item schema.
2
+ namespace <%= schemaNamespace %>;
2
3
 
3
- <% if (itemTypeName === "Item") { -%>
4
- public sealed record Item(
5
- <% for (let i = 0; i < properties.length; i++) { -%>
6
- <%= properties[i].csType %> <%= properties[i].csName %><%= i < properties.length - 1 ? "," : "" %>
7
- <% } -%>
8
- );
9
- <% } else { -%>
4
+ /// <summary>
5
+ /// Schema model generated from TypeSpec.
6
+ /// </summary>
10
7
  public sealed record <%= itemTypeName %>(
11
8
  <% for (let i = 0; i < properties.length; i++) { -%>
12
9
  <%= properties[i].csType %> <%= properties[i].csName %><%= i < properties.length - 1 ? "," : "" %>
13
10
  <% } -%>
14
11
  );
15
-
16
- public sealed record Item(
17
- <% for (let i = 0; i < properties.length; i++) { -%>
18
- <%= properties[i].csType %> <%= properties[i].csName %><%= i < properties.length - 1 ? "," : "" %>
19
- <% } -%>
20
- )
21
- {
22
- public static implicit operator Item(<%= itemTypeName %> item) => new(
23
- <% for (let i = 0; i < properties.length; i++) { -%>
24
- item.<%= properties[i].csName %><%= i < properties.length - 1 ? "," : "" %>
25
- <% } -%>
26
- );
27
- }
28
- <% } -%>
@@ -0,0 +1,45 @@
1
+ // Generated property transforms derived from TypeSpec.
2
+ using System;
3
+ using System.Collections.Generic;
4
+ <% if (usesPersonEntity) { -%>
5
+ using System.Linq;
6
+ using System.Text.Json;
7
+ <% } -%>
8
+ using <%= namespaceName %>.Datasource;
9
+
10
+ namespace <%= schemaNamespace %>;
11
+
12
+ /// <summary>
13
+ /// Base class for CSV-to-model property transforms.
14
+ /// </summary>
15
+ public abstract class PropertyTransformBase
16
+ {
17
+ /// <summary>
18
+ /// Transform a property by name using the generated transform methods.
19
+ /// </summary>
20
+ public object TransformProperty(string name, IReadOnlyDictionary<string, string?> row)
21
+ {
22
+ return name switch
23
+ {
24
+ <% for (const prop of properties) { -%>
25
+ <%= JSON.stringify(prop.name) %> => Transform<%= prop.csName %>(row),
26
+ <% } -%>
27
+ _ => throw new ArgumentOutOfRangeException(nameof(name), $"Unknown property '{name}'."),
28
+ };
29
+ }
30
+
31
+ <% for (const prop of properties) { -%>
32
+ /// <summary>
33
+ /// Transform the <%= prop.name %> property from a CSV row.
34
+ /// </summary>
35
+ protected virtual <%= prop.csType %> Transform<%= prop.csName %>(IReadOnlyDictionary<string, string?> row)
36
+ {
37
+ <% if (prop.transformThrows) { -%>
38
+ <%- prop.transformExpression %>;
39
+ <% } else { -%>
40
+ return <%- prop.transformExpression %>;
41
+ <% } -%>
42
+ }
43
+
44
+ <% } -%>
45
+ }
@@ -1,14 +1,22 @@
1
1
  <% const graphNs = graphApiVersion === "beta" ? "Microsoft.Graph.Beta" : "Microsoft.Graph"; -%>
2
+ // Graph schema payload derived from the TypeSpec model.
2
3
  using <%= graphNs %>.Models.ExternalConnectors;
3
4
 
4
- namespace <%= namespaceName %>.Schema;
5
+ namespace <%= schemaNamespace %>;
5
6
 
7
+ /// <summary>
8
+ /// Builds the Graph schema payload for this connector.
9
+ /// </summary>
6
10
  public static class SchemaPayload
7
11
  {
12
+ /// <summary>
13
+ /// Build the schema payload for the external connection.
14
+ /// </summary>
8
15
  public static global::<%= graphNs %>.Models.ExternalConnectors.Schema BuildSchema()
9
16
  {
10
17
  return new global::<%= graphNs %>.Models.ExternalConnectors.Schema
11
18
  {
19
+ BaseType = "microsoft.graph.externalItem",
12
20
  Properties = new List<Property>
13
21
  {
14
22
  <%- schemaPropertyLines %>