|
| 1 | +import { $ } from '../../../py-dsl'; |
| 2 | +import type { KwargPyDsl } from '../../../py-dsl/expr/kwarg'; |
| 3 | +import type { PydanticPlugin } from '../types'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Field constraint keys that map to Pydantic Field() parameters. |
| 7 | + */ |
| 8 | +export interface FieldConstraints { |
| 9 | + /** Alias for the field name in serialization */ |
| 10 | + alias?: string; |
| 11 | + /** Default value for the field */ |
| 12 | + default?: unknown; |
| 13 | + /** Default factory function (for mutable defaults) */ |
| 14 | + default_factory?: string; |
| 15 | + /** Description of the field */ |
| 16 | + description?: string; |
| 17 | + /** Greater than or equal constraint for numbers */ |
| 18 | + ge?: number; |
| 19 | + /** Greater than constraint for numbers */ |
| 20 | + gt?: number; |
| 21 | + /** Less than or equal constraint for numbers */ |
| 22 | + le?: number; |
| 23 | + /** Less than constraint for numbers */ |
| 24 | + lt?: number; |
| 25 | + /** Maximum length constraint for strings/arrays */ |
| 26 | + max_length?: number; |
| 27 | + /** Minimum length constraint for strings/arrays */ |
| 28 | + min_length?: number; |
| 29 | + /** Multiple of constraint for numbers */ |
| 30 | + multiple_of?: number; |
| 31 | + /** Regex pattern constraint for strings */ |
| 32 | + pattern?: string; |
| 33 | + /** Title for the field */ |
| 34 | + title?: string; |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Checks if any constraints require a Field() call. |
| 39 | + */ |
| 40 | +export function hasConstraints(constraints: Record<string, unknown>): boolean { |
| 41 | + const relevantKeys: Array<keyof FieldConstraints> = [ |
| 42 | + 'alias', |
| 43 | + 'default', |
| 44 | + 'default_factory', |
| 45 | + 'description', |
| 46 | + 'ge', |
| 47 | + 'gt', |
| 48 | + 'le', |
| 49 | + 'lt', |
| 50 | + 'max_length', |
| 51 | + 'min_length', |
| 52 | + 'multiple_of', |
| 53 | + 'pattern', |
| 54 | + 'title', |
| 55 | + ]; |
| 56 | + |
| 57 | + return relevantKeys.some((key) => constraints[key] !== undefined); |
| 58 | +} |
| 59 | + |
| 60 | +type FieldArg = ReturnType<typeof $.literal> | ReturnType<typeof $> | KwargPyDsl; |
| 61 | + |
| 62 | +/** |
| 63 | + * Creates a Pydantic Field() call expression with the given constraints. |
| 64 | + * |
| 65 | + * @example |
| 66 | + * // With constraints |
| 67 | + * createFieldCall({ min_length: 1, description: "Name" }, plugin) |
| 68 | + * // Returns: Field(..., min_length=1, description="Name") |
| 69 | + * |
| 70 | + * // Without constraints but with default |
| 71 | + * createFieldCall({ default: "test" }, plugin) |
| 72 | + * // Returns: Field(default="test") |
| 73 | + */ |
| 74 | +export function createFieldCall( |
| 75 | + constraints: Record<string, unknown>, |
| 76 | + plugin: PydanticPlugin['Instance'], |
| 77 | + options?: { |
| 78 | + /** If true, the field is required (default behavior) */ |
| 79 | + required?: boolean; |
| 80 | + }, |
| 81 | +): ReturnType<typeof $.call> { |
| 82 | + const field = plugin.external('pydantic.Field'); |
| 83 | + const args: Array<FieldArg> = []; |
| 84 | + |
| 85 | + // Handle required vs optional |
| 86 | + const isRequired = options?.required !== false && constraints.default === undefined; |
| 87 | + |
| 88 | + // For required fields with no default, use ... as first arg |
| 89 | + if (isRequired && constraints.default === undefined) { |
| 90 | + args.push($('...')); |
| 91 | + } |
| 92 | + |
| 93 | + // Add constraint arguments in a consistent order |
| 94 | + const orderedKeys: Array<keyof FieldConstraints> = [ |
| 95 | + 'default', |
| 96 | + 'default_factory', |
| 97 | + 'alias', |
| 98 | + 'title', |
| 99 | + 'description', |
| 100 | + 'gt', |
| 101 | + 'ge', |
| 102 | + 'lt', |
| 103 | + 'le', |
| 104 | + 'multiple_of', |
| 105 | + 'min_length', |
| 106 | + 'max_length', |
| 107 | + 'pattern', |
| 108 | + ]; |
| 109 | + |
| 110 | + for (const key of orderedKeys) { |
| 111 | + const value = constraints[key]; |
| 112 | + if (value === undefined) continue; |
| 113 | + |
| 114 | + // Skip default if we already added ... for required fields |
| 115 | + if (key === 'default' && isRequired) continue; |
| 116 | + |
| 117 | + // Create keyword argument using $.kwarg |
| 118 | + args.push($.kwarg(key, toKwargValue(value))); |
| 119 | + } |
| 120 | + |
| 121 | + // Type assertion needed because args include KwargPyDsl which produces KeywordArgument |
| 122 | + return $(field).call(...(args as Parameters<typeof $.call>[1][])); |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | + * Converts a constraint value to a kwarg-compatible value. |
| 127 | + */ |
| 128 | +function toKwargValue(value: unknown): string | number | boolean | null { |
| 129 | + if (value === null) return null; |
| 130 | + if (typeof value === 'string') return value; |
| 131 | + if (typeof value === 'number') return value; |
| 132 | + if (typeof value === 'boolean') return value; |
| 133 | + // For complex types, stringify |
| 134 | + return String(value); |
| 135 | +} |
| 136 | + |
| 137 | +/** |
| 138 | + * Merges multiple constraint objects, with later objects taking precedence. |
| 139 | + */ |
| 140 | +export function mergeConstraints( |
| 141 | + ...constraintSets: Array<Record<string, unknown>> |
| 142 | +): Record<string, unknown> { |
| 143 | + const merged: Record<string, unknown> = {}; |
| 144 | + |
| 145 | + for (const constraints of constraintSets) { |
| 146 | + for (const [key, value] of Object.entries(constraints)) { |
| 147 | + if (value !== undefined) { |
| 148 | + merged[key] = value; |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + return merged; |
| 154 | +} |
0 commit comments