Skip to content

Commit

Permalink
candidTypeObject, candidTypeAnnotation, variableAliasDeclarations
Browse files Browse the repository at this point in the history
  • Loading branch information
lastmjs committed Dec 5, 2023
1 parent 3805e2f commit 5757962
Show file tree
Hide file tree
Showing 50 changed files with 540 additions and 257 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,14 @@ export type ServiceCandidDefinition = {
export type ServiceMethodDefinition = {
name: string;
imports: Set<string>;
typeAliasDeclarations: string[];
variableAliasDeclarations: string[];
src: string;
};

type CandidMeta = {
typeAnnotation: string; // Either a type reference or type literal
typeAliasDeclarations: string[];
candidTypeAnnotation: string; // Either a type reference or type literal
candidTypeObject: string;
variableAliasDeclarations: string[];
imports: Set<string>;
candidType: CandidType;
};
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ export type CandidValueAndMeta<T extends CorrespondingJSType, E = T> = {
agentArgumentValue: T;
agentResponseValue: E;
src: {
typeAnnotation: string;
typeAliasDeclarations: string[];
candidTypeAnnotation: string;
candidTypeObject: string;
variableAliasDeclarations: string[];
imports: Set<string>;
valueLiteral: string;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@ export function CandidValueAndMetaArbGenerator<
).map(
([
{
candidMeta: { typeAnnotation, typeAliasDeclarations, imports }
candidMeta: {
candidTypeAnnotation,
candidTypeObject,
variableAliasDeclarations,
imports
}
},
{ agentArgumentValue, agentResponseValue, valueLiteral }
]) => {
return {
src: {
typeAnnotation,
typeAliasDeclarations,
candidTypeAnnotation,
candidTypeObject,
variableAliasDeclarations,
imports,
valueLiteral
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ export function _VecNat8DefinitionArb(): fc.Arbitrary<BlobCandidDefinition> {
return fc
.tuple(UniqueIdentifierArb('typeDeclaration'), fc.boolean())
.map(([name, useTypeDeclaration]): BlobCandidDefinition => {
const typeAnnotation = 'Vec(nat8)';
const typeAliasDeclarations = useTypeDeclaration
? [`const ${name} = ${typeAnnotation}`]
const candidTypeAnnotation = 'Vec<nat8>';
const candidTypeObject = 'Vec(nat8)';
const variableAliasDeclarations = useTypeDeclaration
? [`const ${name} = ${candidTypeObject}`]
: [];
const imports = new Set(['Vec', 'nat8']);
return {
candidMeta: {
typeAnnotation,
typeAliasDeclarations,
candidTypeAnnotation,
candidTypeObject,
variableAliasDeclarations,
imports,
candidType: 'blob'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,31 @@ export function OptDefinitionArb(
fc.boolean()
)
.map(([name, innerType, useTypeDeclaration]): OptCandidDefinition => {
const typeAnnotation = useTypeDeclaration
? name
: generateTypeAnnotation(innerType);
const candidTypeAnnotation = generateCandidTypeAnnotation(
useTypeDeclaration,
name,
innerType
);

const candidTypeObject = generateCandidTypeObject(
useTypeDeclaration,
name,
innerType
);

const typeAliasDeclarations = generateTypeAliasDeclarations(
const variableAliasDeclarations = generateVariableAliasDeclarations(
useTypeDeclaration,
name,
innerType,
useTypeDeclaration
innerType
);

const imports = generateImports(innerType);

return {
candidMeta: {
typeAnnotation,
typeAliasDeclarations,
candidTypeAnnotation,
candidTypeObject,
variableAliasDeclarations,
imports,
candidType: 'Opt'
},
Expand All @@ -39,22 +48,46 @@ export function OptDefinitionArb(
});
}

function generateTypeAliasDeclarations(
function generateVariableAliasDeclarations(
useTypeDeclaration: boolean,
name: string,
innerType: CandidDefinition,
useTypeDeclaration: boolean
innerType: CandidDefinition
): string[] {
if (useTypeDeclaration) {
return [
...innerType.candidMeta.typeAliasDeclarations,
`const ${name} = ${generateTypeAnnotation(innerType)};`
...innerType.candidMeta.variableAliasDeclarations,
`const ${name} = ${generateCandidTypeObject(
false,
name,
innerType
)};`
];
}
return innerType.candidMeta.typeAliasDeclarations;
return innerType.candidMeta.variableAliasDeclarations;
}

function generateTypeAnnotation(innerType: CandidDefinition): string {
return `Opt(${innerType.candidMeta.typeAnnotation})`;
function generateCandidTypeAnnotation(
useTypeDeclaration: boolean,
name: string,
innerType: CandidDefinition
): string {
if (useTypeDeclaration) {
return `typeof ${name}.tsType`;
}

return `Opt<${innerType.candidMeta.candidTypeAnnotation}>`;
}

function generateCandidTypeObject(
useTypeDeclaration: boolean,
name: string,
innerType: CandidDefinition
): string {
if (useTypeDeclaration === true) {
return name;
}

return `Opt(${innerType.candidMeta.candidTypeObject})`;
}

function generateImports(innerType: CandidDefinition): Set<string> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,31 @@ export function RecordDefinitionArb(
fc.boolean()
)
.map(([name, fields, useTypeDeclaration]): RecordCandidDefinition => {
const typeAnnotation = useTypeDeclaration
? name
: generateTypeAnnotation(fields);
const candidTypeAnnotation = generateCandidTypeAnnotation(
useTypeDeclaration,
name,
fields
);

const candidTypeObject = generateCandidTypeObject(
useTypeDeclaration,
name,
fields
);

const typeAliasDeclarations = generateTypeAliasDeclarations(
const variableAliasDeclarations = generateVariableAliasDeclarations(
useTypeDeclaration,
name,
fields,
useTypeDeclaration
fields
);

const imports = generateImports(fields);

return {
candidMeta: {
typeAnnotation,
typeAliasDeclarations,
candidTypeAnnotation,
candidTypeObject,
variableAliasDeclarations,
imports,
candidType: 'Record'
},
Expand All @@ -53,28 +62,53 @@ function generateImports(fields: Field[]): Set<string> {
return new Set([...fieldImports, 'Record']);
}

function generateTypeAnnotation(fields: Field[]): string {
function generateCandidTypeAnnotation(
useTypeDeclaration: boolean,
name: string,
fields: Field[]
): string {
if (useTypeDeclaration === true) {
return name;
}

return `{${fields
.map(
([fieldName, fieldDefinition]) =>
`${fieldName}: ${fieldDefinition.candidMeta.candidTypeAnnotation}`
)
.join(',')}`;
}

function generateCandidTypeObject(
useTypeDeclaration: boolean,
name: string,
fields: Field[]
): string {
if (useTypeDeclaration === true) {
return name;
}

return `Record({${fields
.map(
([fieldName, fieldDefinition]) =>
`${fieldName}: ${fieldDefinition.candidMeta.typeAnnotation}`
`${fieldName}: ${fieldDefinition.candidMeta.candidTypeObject}`
)
.join(',')}})`;
}

function generateTypeAliasDeclarations(
function generateVariableAliasDeclarations(
useTypeDeclaration: boolean,
name: string,
fields: Field[],
useTypeDeclaration: boolean
fields: Field[]
): string[] {
const fieldTypeAliasDeclarations = fields.flatMap(
(field) => field[1].candidMeta.typeAliasDeclarations
const fieldVariableAliasDefinitions = fields.flatMap(
(field) => field[1].candidMeta.variableAliasDeclarations
);
if (useTypeDeclaration) {
return [
...fieldTypeAliasDeclarations,
`const ${name} = ${generateTypeAnnotation(fields)};`
...fieldVariableAliasDefinitions,
`const ${name} = ${generateCandidTypeObject(false, name, fields)};`
];
}
return fieldTypeAliasDeclarations;
return fieldVariableAliasDefinitions;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,31 @@ export function TupleDefinitionArb(
fc.boolean()
)
.map(([name, fields, useTypeDeclaration]): TupleCandidDefinition => {
const typeAnnotation = useTypeDeclaration
? name
: generateTypeAnnotation(fields);
const candidTypeAnnotation = generateCandidTypeAnnotation(
useTypeDeclaration,
name,
fields
);

const candidTypeObject = generateCandidTypeObject(
useTypeDeclaration,
name,
fields
);

const typeAliasDeclarations = generateTypeAliasDeclarations(
const variableAliasDeclarations = generateVariableAliasDeclarations(
useTypeDeclaration,
name,
fields,
useTypeDeclaration
fields
);

const imports = generateImports(fields);

return {
candidMeta: {
typeAnnotation,
typeAliasDeclarations,
candidTypeAnnotation,
candidTypeObject,
variableAliasDeclarations,
imports,
candidType: 'Tuple'
},
Expand All @@ -42,25 +51,49 @@ export function TupleDefinitionArb(
});
}

function generateTypeAliasDeclarations(
function generateVariableAliasDeclarations(
useTypeDeclaration: boolean,
name: string,
fields: CandidDefinition[],
useTypeDeclaration: boolean
fields: CandidDefinition[]
): string[] {
const fieldTypeAliasDeclarations = fields.flatMap(
(field) => field.candidMeta.typeAliasDeclarations
const fieldVariableAliasDeclarations = fields.flatMap(
(field) => field.candidMeta.variableAliasDeclarations
);
if (useTypeDeclaration) {
return [
...fieldTypeAliasDeclarations,
`const ${name} = ${generateTypeAnnotation(fields)};`
...fieldVariableAliasDeclarations,
`const ${name} = ${generateCandidTypeObject(false, name, fields)};`
];
}
return fieldTypeAliasDeclarations;
return fieldVariableAliasDeclarations;
}

function generateTypeAnnotation(fields: CandidDefinition[]) {
const innerTypes = fields.map((field) => field.candidMeta.typeAnnotation);
function generateCandidTypeAnnotation(
useTypeDeclaration: boolean,
name: string,
fields: CandidDefinition[]
) {
if (useTypeDeclaration === true) {
return `typeof ${name}.tsType`;
}

const innerTypes = fields.map(
(field) => field.candidMeta.candidTypeAnnotation
);

return `Tuple<[${innerTypes.join(', ')}]>`;
}

function generateCandidTypeObject(
useTypeDeclaration: boolean,
name: string,
fields: CandidDefinition[]
) {
if (useTypeDeclaration === true) {
return name;
}

const innerTypes = fields.map((field) => field.candidMeta.candidTypeObject);

return `Tuple(${innerTypes.join(', ')})`;
}
Expand Down
Loading

0 comments on commit 5757962

Please sign in to comment.