(serializable: TypeSchema, importer: (name: string, module: string) => void)
| 4 | import type { TypeSchema } from './TypeSchema'; |
| 5 | |
| 6 | function generateToJsonBody(serializable: TypeSchema, importer: (name: string, module: string) => void) { |
| 7 | const statements: ts.Statement[] = []; |
| 8 | |
| 9 | statements.push( |
| 10 | createNodeFromSource<ts.IfStatement>( |
| 11 | ` |
| 12 | if(!obj) { |
| 13 | return null; |
| 14 | } |
| 15 | `, |
| 16 | ts.SyntaxKind.IfStatement |
| 17 | ) |
| 18 | ); |
| 19 | |
| 20 | statements.push( |
| 21 | createNodeFromSource<ts.VariableStatement>( |
| 22 | ` |
| 23 | const o = new Map<string, unknown>(); |
| 24 | `, |
| 25 | ts.SyntaxKind.VariableStatement |
| 26 | ) |
| 27 | ); |
| 28 | |
| 29 | for (const prop of serializable.properties) { |
| 30 | const fieldName = prop.name; |
| 31 | const jsonName = prop.jsonNames.filter(n => n !== '')[0]; |
| 32 | |
| 33 | if (!jsonName || prop.isJsonReadOnly) { |
| 34 | continue; |
| 35 | } |
| 36 | |
| 37 | let propertyStatements: ts.Statement[] = []; |
| 38 | |
| 39 | if (prop.asRaw || prop.type.isPrimitiveType) { |
| 40 | propertyStatements.push( |
| 41 | createNodeFromSource<ts.ExpressionStatement>( |
| 42 | ` |
| 43 | o.set(${JSON.stringify(jsonName)}, obj.${fieldName}); |
| 44 | `, |
| 45 | ts.SyntaxKind.ExpressionStatement |
| 46 | ) |
| 47 | ); |
| 48 | } else if (prop.type.isEnumType) { |
| 49 | if (prop.type.isNullable) { |
| 50 | propertyStatements.push( |
| 51 | createNodeFromSource<ts.ExpressionStatement>( |
| 52 | ` |
| 53 | o.set(${JSON.stringify(jsonName)}, obj.${fieldName} as number|null); |
| 54 | `, |
| 55 | ts.SyntaxKind.ExpressionStatement |
| 56 | ) |
| 57 | ); |
| 58 | } else if (prop.type.isOptional) { |
| 59 | propertyStatements.push( |
| 60 | createNodeFromSource<ts.ExpressionStatement>( |
| 61 | ` |
| 62 | o.set(${JSON.stringify(jsonName)}, obj.${fieldName} as number|undefined); |
| 63 | `, |
no test coverage detected