def generateClient( dest: FilePython, *, classname: str, funcspecs: List[Tuple[str, FuncSpec]], adv: Advanced, flavour: Flavour, ) -> None: dest.filecomment(HEADER) # we're always going to need typing module dest.contents.alsoImportPy("typing") appendFailureModeClasses(dest) # make copies of all our dataclasses for dc in adv.getAllDataclasses(): dcspec = _getDataclassSpec(dc, adv) dest.contents.also(dcspec) # generate function wrappers dest.contents.also(_generateWrappers(classname, funcspecs, adv, flavour)) dest.writefile() dest.makepretty()
def _generateAdvancedTypes(dest: FileTS, adv: Advanced) -> None: for name, baseType, children in adv.getNewTypeDetails(): try: tsprimitive = PRIMITIVES[baseType.__name__] except KeyError: raise Exception( f'Cannot generate a typescript alias matching {name}' f'; no known primitive type for {baseType.__name__}') typeExpr = tsprimitive + ' & {readonly brand?: unique symbol}' if children: typeExpr = ' | '.join(children) + ' | (' + typeExpr + ')' dest.contents.blank() dest.contents.also(tsexpr(f'export type {name} = {typeExpr}')) for dc in adv.getAllDataclasses(): dest.contents.blank() iface = InterfaceSpec(dc.__name__, tsexport=True, appendto=dest.contents) for field in dataclasses.fields(dc): generated = _generateCrossType(getTypeSpec(field.type, adv), adv) iface.addProperty(field.name, generated)