Skip to content

Commit

Permalink
Fix Services
Browse files Browse the repository at this point in the history
  • Loading branch information
dansteren committed Dec 12, 2023
1 parent 0ae609e commit f4f10d6
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fc from 'fast-check';
import { CandidType } from '../candid_type';
import { CandidType as AzleCandidType } from '../../../../src/lib';
import { CandidType as AzleCandidType } from '../../../../src/lib/candid/candid_type';
import { ServiceMethodDefinition } from '../reference/service_arb/service_method_arb';

export type CandidDefinitionArb = fc.Arbitrary<CandidDefinition>;

Expand Down Expand Up @@ -60,12 +61,6 @@ export type ServiceCandidDefinition = {
candidMeta: CandidMeta;
funcs: ServiceMethodDefinition[];
};
export type ServiceMethodDefinition = {
name: string;
imports: Set<string>;
variableAliasDeclarations: string[];
src: string;
};

type CandidMeta = {
candidTypeAnnotation: string; // Either a type reference or type literal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import fc from 'fast-check';

import { JsFunctionNameArb } from '../../../js_function_name_arb';
import { UniqueIdentifierArb } from '../../../unique_identifier_arb';
import {
CandidDefinition,
ServiceCandidDefinition,
ServiceMethodArb,
ServiceMethodDefinition
} from './service_method_arb';
import {
CandidDefinition,
ServiceCandidDefinition
} from '../../candid_definition_arb/types';
import { VoidDefinitionArb } from '../../primitive/void';
import { UniqueIdentifierArb } from '../../../unique_identifier_arb';
import { Canister } from '../../../../../src/lib/candid/types/reference/service';

export function ServiceDefinitionArb(
fieldCandidDefArb: fc.Arbitrary<CandidDefinition>
Expand All @@ -34,6 +36,8 @@ export function ServiceDefinitionArb(
fields
);

const azleCandidTypeObject = generateAzleCandidTypeObject(fields);

const variableAliasDeclarations = generateVariableAliasDeclarations(
useTypeDeclaration,
name,
Expand All @@ -47,6 +51,7 @@ export function ServiceDefinitionArb(
candidMeta: {
candidTypeAnnotation,
candidTypeObject,
azleCandidTypeObject,
variableAliasDeclarations,
imports,
candidType: 'Service'
Expand All @@ -56,49 +61,6 @@ export function ServiceDefinitionArb(
});
}

function ServiceMethodArb(
candidDefArb: fc.Arbitrary<CandidDefinition>
): fc.Arbitrary<ServiceMethodDefinition> {
return fc
.tuple(
JsFunctionNameArb,
fc.constantFrom('query', 'update'),
fc.array(candidDefArb),
fc.oneof(candidDefArb, VoidDefinitionArb())
)
.map(([name, mode, params, returnType]): ServiceMethodDefinition => {
const paramCandidTypeObjects = params.map(
(param) => param.candidMeta.candidTypeObject
);

const variableAliasDeclarations = params.reduce(
(
acc,
{ candidMeta: { variableAliasDeclarations } }
): string[] => {
return [...acc, ...variableAliasDeclarations];
},
returnType.candidMeta.variableAliasDeclarations
);

const src = `${name}: ${mode}([${paramCandidTypeObjects}], ${returnType.candidMeta.candidTypeObject})`;

const imports = params.reduce(
(acc, param) => {
return new Set([...acc, ...param.candidMeta.imports]);
},
new Set([mode, ...returnType.candidMeta.imports])
);

return {
name,
imports,
variableAliasDeclarations: variableAliasDeclarations,
src
};
});
}

function generateVariableAliasDeclarations(
useTypeDeclaration: boolean,
name: string,
Expand Down Expand Up @@ -148,6 +110,19 @@ function generateCandidTypeObject(
return `Canister({${methods}})`;
}

function generateAzleCandidTypeObject(
serviceMethods: ServiceMethodDefinition[]
) {
const methods = serviceMethods.reduce((acc, serviceMethod) => {
return {
...acc,
[serviceMethod.name]: serviceMethod.azleCandidTypeObject
};
}, {});

return Canister(methods);
}

function generateImports(
serviceMethods: ServiceMethodDefinition[]
): Set<string> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import fc from 'fast-check';

import { CandidType } from '../../candid_type';
import { CandidDefinition } from '../../candid_definition_arb/types';
import { VoidDefinitionArb } from '../../primitive/void';
import { JsFunctionNameArb } from '../../../js_function_name_arb';
import {
query,
update
} from '../../../../../src/lib/canister_methods/methods/';
import { CanisterMethodInfo } from '../../../../../src/lib/canister_methods/types/canister_method_info';

type Mode = 'query' | 'update';

export type ServiceMethodDefinition = {
name: string;
azleCandidTypeObject: CanisterMethodInfo<CandidType[], CandidType>;
imports: Set<string>;
variableAliasDeclarations: string[];
src: string;
};

export function ServiceMethodArb(
candidDefArb: fc.Arbitrary<CandidDefinition>
): fc.Arbitrary<ServiceMethodDefinition> {
return fc
.tuple(
JsFunctionNameArb,
fc.constantFrom('query', 'update') as fc.Arbitrary<Mode>,
fc.array(candidDefArb),
fc.oneof(candidDefArb, VoidDefinitionArb())
)
.map(([name, mode, params, returnType]): ServiceMethodDefinition => {
const paramCandidTypeObjects = params.map(
(param) => param.candidMeta.candidTypeObject
);

const variableAliasDeclarations = params.reduce(
(
acc,
{ candidMeta: { variableAliasDeclarations } }
): string[] => {
return [...acc, ...variableAliasDeclarations];
},
returnType.candidMeta.variableAliasDeclarations
);

const src = `${name}: ${mode}([${paramCandidTypeObjects}], ${returnType.candidMeta.candidTypeObject})`;

const imports = params.reduce(
(acc, param) => {
return new Set([...acc, ...param.candidMeta.imports]);
},
new Set([mode, ...returnType.candidMeta.imports])
);

const azleCandidTypeObject = generateAzleCandidTypeObject(
mode,
params,
returnType
);

return {
name,
azleCandidTypeObject: azleCandidTypeObject,
imports,
variableAliasDeclarations: variableAliasDeclarations,
src
};
});
}

function generateAzleCandidTypeObject(
mode: Mode,
params: CandidDefinition[],
returnType: CandidDefinition
) {
const modeFunction = mode === 'query' ? query : update;
const paramCandidTypeObjects = params.map(
(param) => param.candidMeta.azleCandidTypeObject
);
const returnCandidTypeObject = returnType.candidMeta.azleCandidTypeObject;

return modeFunction(paramCandidTypeObjects, returnCandidTypeObject);
}

0 comments on commit f4f10d6

Please sign in to comment.