@mora-light/core
Version:
Mora Light Core
119 lines (116 loc) • 2.87 kB
JavaScript
const copyCandidType = type => {
let r;
switch (type.type) {
case 'bool':
case 'nat':
case 'int':
case 'nat8':
case 'nat16':
case 'nat32':
case 'nat64':
case 'int8':
case 'int16':
case 'int32':
case 'int64':
case 'float32':
case 'float64':
case 'null':
case 'text':
case 'principal':
r = {
type: type.type
};
break;
case 'blob':
r = {
type: type.type,
subtype: {
type: 'nat8'
}
};
break;
case 'vec':
r = {
type: type.type,
subtype: copyCandidType(type.subtype)
};
break;
case 'opt':
r = {
type: type.type,
subtype: copyCandidType(type.subtype)
};
break;
case 'record':
case 'variant':
case 'tuple':
r = {
type: type.type,
subitems: copyCandidTypeSubitems(type.subitems)
};
break;
case 'rec':
r = {
type: type.type,
subtype: type.subtype !== undefined ? copyCandidType(type.subtype) : undefined,
id: type.id
};
break;
case 'unknown':
case 'empty':
case 'reserved':
r = {
type: type.type
};
break;
case 'func':
r = {
type: type.type,
annotations: [...type.annotations],
argTypes: copyCandidType(type.argTypes),
retTypes: copyCandidType(type.retTypes)
};
break;
case 'service':
r = {
type: type.type,
apis: type.apis.map(api => {
return {
method: api.method,
func: {
type: api.func.type,
annotations: [...api.func.annotations],
argTypes: copyCandidType(api.func.argTypes),
retTypes: copyCandidType(api.func.retTypes)
}
};
})
};
break;
}
return r;
};
const copyCandidTypeSubitems = subitems => {
return subitems.map(subitem => {
return {
key: subitem.key,
type: copyCandidType(subitem.type)
};
});
};
const copyCandidTypeWithUndefined = type => type !== undefined ? copyCandidType(type) : undefined;
const copyCandidTypeSubitemsWithUndefined = subitems => subitems ? copyCandidTypeSubitems(subitems) : undefined;
const copyCandidTypeApiItems = apis => {
return apis.map(api => {
return {
method: api.method,
func: copyCandidType(api.func)
};
});
};
const copyCandidTypeApiItemsWithUndefined = apiItems => apiItems ? copyCandidTypeApiItems(apiItems) : undefined;
exports.copyCandidType = copyCandidType;
exports.copyCandidTypeApiItemsWithUndefined = copyCandidTypeApiItemsWithUndefined;
exports.copyCandidTypeSubitemsWithUndefined = copyCandidTypeSubitemsWithUndefined;
exports.copyCandidTypeWithUndefined = copyCandidTypeWithUndefined;
;