@xrpckit/target-go-server 0.0.1 → 0.0.3

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.
package/dist/index.d.ts CHANGED
@@ -1,39 +1,6 @@
1
- import { BaseCodeGenerator, GeneratorConfig, GeneratedFiles, CodeWriter } from '@xrpckit/codegen';
2
- import { ContractDefinition, TypeReference } from '@xrpckit/parser';
1
+ import { Target, CodeWriter, GeneratedUtility, ContractDefinition, TypeReference, TypeMapperBase, TypeMapping, ValidationMapperBase, ValidationMapping } from '@xrpckit/sdk';
3
2
 
4
- declare class GoCodeGenerator extends BaseCodeGenerator {
5
- private typeGenerator;
6
- private serverGenerator;
7
- private validationGenerator;
8
- constructor(config: GeneratorConfig);
9
- generate(contract: ContractDefinition): GeneratedFiles;
10
- }
11
-
12
- declare class GoTypeGenerator {
13
- private w;
14
- private typeMapper;
15
- private packageName;
16
- constructor(packageName?: string);
17
- generateTypes(contract: ContractDefinition): string;
18
- private generateContextType;
19
- private generateMiddlewareTypes;
20
- private generateType;
21
- private generateJSONTag;
22
- private generateTypedHandlers;
23
- }
24
-
25
- declare class GoServerGenerator {
26
- private w;
27
- private packageName;
28
- constructor(packageName?: string);
29
- generateServer(contract: ContractDefinition): string;
30
- private generateServeHTTP;
31
- }
32
-
33
- declare class GoTypeMapper {
34
- mapType(typeRef: TypeReference): string;
35
- mapPrimitive(type: string): string;
36
- }
3
+ declare const goTarget: Target;
37
4
 
38
5
  /**
39
6
  * Go-specific code builder with fluent DSL for common Go patterns
@@ -63,4 +30,268 @@ declare class GoBuilder extends CodeWriter {
63
30
  blockComment(lines: string[]): this;
64
31
  }
65
32
 
66
- export { GoBuilder, GoCodeGenerator, GoServerGenerator, GoTypeGenerator, GoTypeMapper };
33
+ /**
34
+ * Create a Go enum pattern with type, constants, IsValid, and Parse functions.
35
+ *
36
+ * @param name - The enum type name (PascalCase)
37
+ * @param values - The enum values (strings)
38
+ * @returns Generated utility with enum code
39
+ *
40
+ * @example
41
+ * ```typescript
42
+ * const utility = createGoEnumPattern("Status", ["active", "inactive", "pending"]);
43
+ * // Generates:
44
+ * // type Status string
45
+ * // const (
46
+ * // StatusActive Status = "active"
47
+ * // StatusInactive Status = "inactive"
48
+ * // StatusPending Status = "pending"
49
+ * // )
50
+ * // func (s Status) IsValid() bool { ... }
51
+ * // func ParseStatus(s string) (Status, error) { ... }
52
+ * ```
53
+ */
54
+ declare function createGoEnumPattern(name: string, values: (string | number)[]): GeneratedUtility;
55
+ /**
56
+ * Create a Go BigInt pattern for handling large integers.
57
+ *
58
+ * @returns Generated utility with BigInt wrapper code
59
+ */
60
+ declare function createGoBigIntPattern(): GeneratedUtility;
61
+ /**
62
+ * Create a Go union wrapper pattern for discriminated unions.
63
+ *
64
+ * @param name - The union type name
65
+ * @param variants - The variant types (Go type names)
66
+ * @returns Generated utility with union wrapper code
67
+ *
68
+ * @example
69
+ * ```typescript
70
+ * const utility = createGoUnionPattern("Result", ["string", "int", "Error"]);
71
+ * // Generates a struct with Value interface{} and type assertion helpers
72
+ * ```
73
+ */
74
+ declare function createGoUnionPattern(name: string, variants: string[]): GeneratedUtility;
75
+ /**
76
+ * Create a Go tuple struct pattern.
77
+ *
78
+ * @param name - The tuple type name
79
+ * @param elements - The element types (Go type names)
80
+ * @returns Generated utility with tuple struct code
81
+ *
82
+ * @example
83
+ * ```typescript
84
+ * const utility = createGoTuplePattern("Coordinate", ["float64", "float64"]);
85
+ * // Generates:
86
+ * // type Coordinate struct {
87
+ * // V0 float64 `json:"0"`
88
+ * // V1 float64 `json:"1"`
89
+ * // }
90
+ * ```
91
+ */
92
+ declare function createGoTuplePattern(name: string, elements: string[]): GeneratedUtility;
93
+ /**
94
+ * Create a Go date/time utility pattern.
95
+ *
96
+ * @returns Generated utility with date helpers
97
+ */
98
+ declare function createGoDatePattern(): GeneratedUtility;
99
+
100
+ declare class GoServerGenerator {
101
+ private w;
102
+ private packageName;
103
+ constructor(packageName?: string);
104
+ generateServer(contract: ContractDefinition): string;
105
+ private generateServeHTTP;
106
+ }
107
+
108
+ interface CollectedType {
109
+ name: string;
110
+ typeRef: TypeReference;
111
+ source: string;
112
+ }
113
+ /**
114
+ * GoTypeCollector traverses a contract and collects all types that need
115
+ * Go struct generation, including nested inline objects that don't have names.
116
+ *
117
+ * This solves the problem where inline objects like:
118
+ * tasks: z.array(z.object({ id: z.string() }))
119
+ *
120
+ * Would generate `[]interface{}` instead of a proper typed struct.
121
+ */
122
+ declare class GoTypeCollector {
123
+ private collectedTypes;
124
+ private usedNames;
125
+ /**
126
+ * Collect all types from a contract that need Go struct generation.
127
+ * This includes:
128
+ * - Named types from contract.types
129
+ * - Anonymous inline objects nested in properties
130
+ * - Array element types that are inline objects
131
+ * - Optional/nullable wrapped inline objects
132
+ */
133
+ collectTypes(contract: ContractDefinition): CollectedType[];
134
+ /**
135
+ * Process a type definition and extract any nested inline types
136
+ */
137
+ private processTypeDefinition;
138
+ /**
139
+ * Process a property and extract any nested inline types
140
+ */
141
+ private processProperty;
142
+ /**
143
+ * Process a type reference and extract any nested inline types.
144
+ * This is the core function that handles all the different type kinds.
145
+ */
146
+ private processTypeReference;
147
+ /**
148
+ * Assign a unique name, adding numeric suffix if there's a collision
149
+ */
150
+ private assignUniqueName;
151
+ /**
152
+ * Get all collected nested types (not including the main contract types)
153
+ */
154
+ getCollectedTypes(): CollectedType[];
155
+ }
156
+
157
+ declare class GoTypeGenerator {
158
+ private w;
159
+ private typeMapper;
160
+ private packageName;
161
+ private generatedTypes;
162
+ constructor(packageName?: string);
163
+ /**
164
+ * Generate Go types from a contract definition.
165
+ * @param contract - The contract definition (should have names assigned to inline types)
166
+ * @param collectedTypes - Optional pre-collected nested types from GoTypeCollector
167
+ */
168
+ generateTypes(contract: ContractDefinition, collectedTypes?: CollectedType[]): string;
169
+ private generateContextType;
170
+ private generateMiddlewareTypes;
171
+ private generateType;
172
+ /**
173
+ * Generate a type from a TypeReference (used for nested inline types)
174
+ */
175
+ private generateTypeFromReference;
176
+ /**
177
+ * Generate struct types for tuples
178
+ */
179
+ private generateTupleTypes;
180
+ /**
181
+ * Generate wrapper struct types for unions
182
+ */
183
+ private generateUnionTypes;
184
+ /**
185
+ * Get a descriptive field name for a union variant
186
+ */
187
+ private getUnionFieldName;
188
+ private generateJSONTag;
189
+ private generateTypedHandlers;
190
+ }
191
+
192
+ /**
193
+ * Go type mapper that converts xRPC types to Go types.
194
+ * Extends TypeMapperBase to ensure all type kinds are handled.
195
+ */
196
+ declare class GoTypeMapper extends TypeMapperBase<string> {
197
+ private tupleTypes;
198
+ private unionTypes;
199
+ /**
200
+ * Complete mapping of all type kinds to Go types.
201
+ * TypeScript enforces exhaustiveness at compile time.
202
+ */
203
+ readonly typeMapping: TypeMapping<string>;
204
+ /**
205
+ * Map a primitive base type to Go type.
206
+ */
207
+ mapPrimitive(type: string): string;
208
+ /**
209
+ * Get all registered tuple types that need struct generation
210
+ */
211
+ getTupleTypes(): Map<string, TypeReference>;
212
+ /**
213
+ * Get all registered union types that need wrapper struct generation
214
+ */
215
+ getUnionTypes(): Map<string, TypeReference>;
216
+ /**
217
+ * Reset the type registries (call between generation runs)
218
+ */
219
+ reset(): void;
220
+ private handleObject;
221
+ private handleArray;
222
+ private handlePrimitive;
223
+ private handleOptional;
224
+ private handleNullable;
225
+ private handleUnion;
226
+ private handleEnum;
227
+ private handleLiteral;
228
+ private handleRecord;
229
+ private handleTuple;
230
+ private handleDate;
231
+ }
232
+
233
+ declare class GoValidationGenerator {
234
+ private w;
235
+ private packageName;
236
+ private generatedValidations;
237
+ constructor(packageName?: string);
238
+ /**
239
+ * Generate Go validation functions from a contract definition.
240
+ * @param contract - The contract definition (should have names assigned to inline types)
241
+ * @param collectedTypes - Optional pre-collected nested types from GoTypeCollector
242
+ */
243
+ generateValidation(contract: ContractDefinition, collectedTypes?: CollectedType[]): string;
244
+ private generateErrorTypes;
245
+ private generateTypeValidation;
246
+ private generatePropertyValidation;
247
+ private generatePropertyValidationForValue;
248
+ private generateValidationRules;
249
+ private hasValidationRule;
250
+ private checkTypeForValidationRule;
251
+ private checkTypeRefForValidationRule;
252
+ private generateHelperFunctions;
253
+ private unwrapOptionalNullable;
254
+ private getActualType;
255
+ private getEnumValues;
256
+ private isPointerType;
257
+ }
258
+
259
+ /**
260
+ * Represents a Go validation check with its code and required imports.
261
+ */
262
+ interface GoValidationCode {
263
+ /** The Go condition expression (e.g., "len(field) < 5") */
264
+ condition: string;
265
+ /** The error message to display if validation fails */
266
+ message: string;
267
+ /** Required Go imports for this validation */
268
+ imports?: string[];
269
+ /** Whether this validation should be wrapped in a non-empty check for optional fields */
270
+ skipIfEmpty?: boolean;
271
+ }
272
+ /**
273
+ * Go validation mapper that converts xRPC validation rules to Go validation code.
274
+ * Extends ValidationMapperBase to ensure all validation kinds are handled.
275
+ */
276
+ declare class GoValidationMapper extends ValidationMapperBase<GoValidationCode> {
277
+ /**
278
+ * Complete mapping of all validation kinds to Go validation code.
279
+ * TypeScript enforces exhaustiveness at compile time.
280
+ */
281
+ readonly validationMapping: ValidationMapping<GoValidationCode>;
282
+ private handleMinLength;
283
+ private handleMaxLength;
284
+ private handleEmail;
285
+ private handleUrl;
286
+ private handleUuid;
287
+ private handleRegex;
288
+ private handleMin;
289
+ private handleMax;
290
+ private handleInt;
291
+ private handlePositive;
292
+ private handleNegative;
293
+ private handleMinItems;
294
+ private handleMaxItems;
295
+ }
296
+
297
+ export { type CollectedType, GoBuilder, GoServerGenerator, GoTypeCollector, GoTypeGenerator, GoTypeMapper, type GoValidationCode, GoValidationGenerator, GoValidationMapper, createGoBigIntPattern, createGoDatePattern, createGoEnumPattern, createGoTuplePattern, createGoUnionPattern, goTarget };