Allow nested struct generation (#3760)
* Allow nested struct generation * CHANGELOG * Update packages/typegen/src/util/formatting.ts * Rebuild
This commit is contained in:
@@ -50,12 +50,13 @@ const tsTuple = tsExport;
|
||||
function tsEnum (definitions: Record<string, ModuleTypes>, { name: enumName, sub }: TypeDef, imports: TypeImports): string {
|
||||
setImports(definitions, imports, ['Enum']);
|
||||
|
||||
const keys = (sub as TypeDef[]).map(({ info, name = '', type }): string => {
|
||||
const keys = (sub as TypeDef[]).map((def, index): string => {
|
||||
const { info, name = `unknown${index}`, type } = def;
|
||||
const getter = stringUpperFirst(stringCamelCase(name.replace(' ', '_')));
|
||||
const isComplexType = [TypeDefInfo.Tuple, TypeDefInfo.Vec, TypeDefInfo.VecFixed].includes(info);
|
||||
const isComplex = [TypeDefInfo.Struct, TypeDefInfo.Tuple, TypeDefInfo.Vec, TypeDefInfo.VecFixed].includes(info);
|
||||
const asGetter = type === 'Null' || info === TypeDefInfo.DoNotConstruct
|
||||
? ''
|
||||
: createGetter(definitions, `as${getter}`, isComplexType ? formatType(definitions, type, imports) : type, imports);
|
||||
: createGetter(definitions, `as${getter}`, isComplex ? formatType(definitions, info === TypeDefInfo.Struct ? def : type, imports) : type, imports);
|
||||
const isGetter = info === TypeDefInfo.DoNotConstruct
|
||||
? ''
|
||||
: createGetter(definitions, `is${getter}`, 'boolean', imports);
|
||||
@@ -63,6 +64,7 @@ function tsEnum (definitions: Record<string, ModuleTypes>, { name: enumName, sub
|
||||
switch (info) {
|
||||
case TypeDefInfo.Compact:
|
||||
case TypeDefInfo.Plain:
|
||||
case TypeDefInfo.Struct:
|
||||
case TypeDefInfo.Tuple:
|
||||
case TypeDefInfo.Vec:
|
||||
case TypeDefInfo.Option:
|
||||
|
||||
@@ -131,6 +131,14 @@ function formatLinkage (inner: string): string {
|
||||
return paramsNotation('Linkage', inner);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given the inner `T`, return a `Option<T>` string
|
||||
*/
|
||||
/** @internal */
|
||||
function formatOption (inner: string): string {
|
||||
return paramsNotation('Option', inner);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given the inner `O` & `E`, return a `Result<O, E>` string
|
||||
*/
|
||||
@@ -140,11 +148,11 @@ function formatResult (innerOk: string, innerError: string): string {
|
||||
}
|
||||
|
||||
/**
|
||||
* Given the inner `T`, return a `Option<T>` string
|
||||
* Given the entries `[string, T][]`, return a `{ name: T; ... }` string
|
||||
*/
|
||||
/** @internal */
|
||||
function formatOption (inner: string): string {
|
||||
return paramsNotation('Option', inner);
|
||||
function formatStruct (inners: [string, string][]): string {
|
||||
return `{ ${inners.map(([k, t]) => `${k}: ${t};`).join(' ')} } & Struct`;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -211,10 +219,15 @@ export function formatType (definitions: Record<string, ModuleTypes>, type: stri
|
||||
return typeDef.type;
|
||||
}
|
||||
|
||||
case TypeDefInfo.Vec: {
|
||||
setImports(definitions, imports, ['Vec']);
|
||||
case TypeDefInfo.Struct: {
|
||||
setImports(definitions, imports, ['Struct']);
|
||||
|
||||
return formatVec(formatType(definitions, (typeDef.sub as TypeDef).type, imports));
|
||||
return formatStruct(
|
||||
((typeDef.sub as TypeDef[]).map(({ name, type }, index) => [
|
||||
name || `unknown${index}`,
|
||||
formatType(definitions, type, imports)
|
||||
]))
|
||||
);
|
||||
}
|
||||
|
||||
case TypeDefInfo.Tuple: {
|
||||
@@ -222,10 +235,16 @@ export function formatType (definitions: Record<string, ModuleTypes>, type: stri
|
||||
|
||||
// `(a,b)` gets transformed into `ITuple<[a, b]>`
|
||||
return formatTuple(
|
||||
((typeDef.sub as TypeDef[]).map((sub) => formatType(definitions, sub.type, imports)))
|
||||
((typeDef.sub as TypeDef[]).map(({ type }) => formatType(definitions, type, imports)))
|
||||
);
|
||||
}
|
||||
|
||||
case TypeDefInfo.Vec: {
|
||||
setImports(definitions, imports, ['Vec']);
|
||||
|
||||
return formatVec(formatType(definitions, (typeDef.sub as TypeDef).type, imports));
|
||||
}
|
||||
|
||||
case TypeDefInfo.VecFixed: {
|
||||
const type = (typeDef.sub as TypeDef).type;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user