{"version":3,"sources":["struct.ts"],"sourcesContent":["import { dc } from \"../js_adapter/js_class_adapter.ts\";\nimport type { ObjectRef } from \"../runtime/pointers.ts\";\nimport { Runtime } from \"../runtime/runtime.ts\";\nimport { Class } from \"../utils/global_types.ts\";\nimport { sha256 } from \"../utils/sha256.ts\";\nimport { Type } from \"./type.ts\";\nimport { Decorators } from \"../js_adapter/js_class_adapter.ts\";\nimport { getCallerFile } from \"../utils/caller_metadata.ts\";\nimport { client_type } from \"../utils/constants.ts\";\n\nexport type StructuralTypeDefIn = {\n\t[key: string]: Type|Type[]|(new () => unknown)|StructuralTypeDefIn|StructuralTypeDefIn[]\n}\nexport type StructuralTypeDef = {\n\t[key: string]: Type|StructuralTypeDef\n}\n\nexport type collapseTypeDef<Def extends StructuralTypeDefIn> = {\n\t[K in keyof Def]: collapseType<Def[K]>\n}\n\nexport type collapseType<TT> = \n\tTT extends Type<infer T> ? \n\t\t// if Type\n\t\tT : \n\t\t// else\n\t\tTT extends Array<infer DT> ?\n\t\t\t// if Array\n\t\t\tArray<DT extends StructuralTypeDefIn ? collapseTypeDef<DT> : collapseType<DT>> :\n\t\t\t// else \n\t\t\tTT extends (new () => infer T) ? \n\t\t\t\t// if constructor\n\t\t\t\tT :\n\t\t\t\t// else\n\t\t\t\tTT extends StructuralTypeDefIn ? \n\t\t\t\t\t// is StructuralTypeDefIn\n\t\t\t\t\tcollapseType<TT> :\n\t\t\t\t\tnever\n\n\n\nexport type inferType<DXTypeOrClass extends Type|Class> = \n\tDXTypeOrClass extends Type<infer Def> ? \n\t\tObjectRef<Def> : \n\tDXTypeOrClass extends Class ? \n\t\tInstanceType<DXTypeOrClass> : \n\tnever;\n\n\n/**\n * Define a structural type without a class or prototype.\n * Instanes are plain objects mapped with DATEX type mapping and\n * DATEX runtime type validation.\n * \n * This is more efficient than just using plain objects, because\n * the properties are known at creation time.\n * \n * ```ts\n * \n * // struct definition\n * const MyStruct = struct({\n *    a: string,\n *    b: Set,\n *    c: Array<number>,\n *    x: {\n *       xx: string,\n *       yy: Set<string>\n *    }\n * })\n * // inferred TS definition\n * type MyStruct = inferType<typeof MyStruct>\n * \n * // instantiation\n * const myStruct: MyStruct = MyStruct({\n *    a: \"aaaaaa\",\n *    b: new Set(),\n *    c: [1,2,3],\n *    x: {\n *       xx: \"xxxxxxx\",\n *       yy: new Set(['1','2'])\n *    }\n * })\n * ```\n */\n\nexport function struct<T extends Record<string, any> & Class>(classDefinition: T): dc<T>\nexport function struct<T extends Record<string, any> & Class>(type: string, classDefinition: T): dc<T>\nexport function struct<Def extends StructuralTypeDefIn>(typeName: string, def: Def): Type<ObjectRef<collapseTypeDef<Def>>> & ((val: collapseTypeDef<Def>)=>ObjectRef<collapseTypeDef<Def>>)\nexport function struct<Def extends StructuralTypeDefIn>(def: Def): Type<ObjectRef<collapseTypeDef<Def>>> & ((val: collapseTypeDef<Def>)=>ObjectRef<collapseTypeDef<Def>>)\nexport function struct(defOrTypeName: StructuralTypeDefIn|Class|string, def?: StructuralTypeDefIn|Class): any {\n\t// create unique type name from template hash\n\n\tconst callerFile = client_type == \"deno\" ? getCallerFile() : undefined;\n\n\tconst hasType = typeof defOrTypeName == \"string\";\n\tconst typeName = hasType ? defOrTypeName : undefined;\n\tdef = hasType ? def : defOrTypeName;\n\n\t// is class definition\n\tif (typeof def == \"function\") {\n\t\treturn Decorators.sync(typeName, def, undefined, callerFile);\n\t}\n\n\t// is struct definition\n\tif (!def || typeof def !== \"object\") throw new Error(\"Struct definition must of type object\");\n\n\tconst template:StructuralTypeDef = {};\n\tfor (const [key, val] of Object.entries(def)) {\n\t\ttemplate[key] = convertDefinitionToType(val, key);\n\t}\n\n\tconst hash = typeName ?? sha256(Runtime.valueToDatexString(template, false, false, false))\n\tconst type = new Type(\"struct\", hash).setTemplate(template);\n\n    // custom instanceof handling for structs\n\t// TODO: does not work in Deno (throws runtime error when checking instanceof)\n    (type as any)[Symbol.hasInstance] = (val: unknown) => {\n        return Type.ofValue(val).matchesType(type);\n    }\n\n\tif (callerFile) type.jsTypeDefModule = callerFile;\n\ttype.proxify_children = true;\n\treturn type as any\n}\n\n\nfunction convertDefinitionToType(val: StructuralTypeDefIn[string], key: string): Type {\n\t// Datex type\n\tif (val instanceof Type) {\n\t\treturn val\n\t}\n\t// Array\n\telse if (val instanceof Array) {\n\t\tif (val.length != 1) throw new Error(\"Invalid struct definition value for property '\"+key+\"'. Array must have exactly one element.\");\n\t\tconst type = convertDefinitionToType(val[0], key);\n\t\treturn Type.std.Array.getParametrized([type]);\n\t}\n\t// constructor\n\telse if (typeof val == \"function\") {\n\t\tconst type = Type.getClassDatexType(val);\n\t\treturn type;\n\t}\n\t// object\n\telse if (val && typeof val == \"object\") {\n\t\tconst type = struct(val);\n\t\treturn type;\n\t}\n\telse {\n\t\tthrow new Error(\"Invalid struct definition value for property '\"+key+\"' (type \" + (typeof val) + \")\")\n\t}\n}"],"names":["Runtime","sha256","Type","Decorators","getCallerFile","client_type","struct","defOrTypeName","def","callerFile","undefined","hasType","typeName","sync","Error","template","key","val","Object","entries","convertDefinitionToType","type","valueToDatexString","setTemplate","Symbol","hasInstance","ofValue","matchesType","jsTypeDefModule","proxify_children","Array","length","std","getParametrized","getClassDatexType"],"mappings":"AAEA,SAASA,OAAO,QAAQ,wBAAwB;AAEhD,SAASC,MAAM,QAAQ,qBAAqB;AAC5C,SAASC,IAAI,QAAQ,YAAY;AACjC,SAASC,UAAU,QAAQ,oCAAoC;AAC/D,SAASC,aAAa,QAAQ,8BAA8B;AAC5D,SAASC,WAAW,QAAQ,wBAAwB;AAiFpD,OAAO,SAASC,OAAOC,aAA+C,EAAEC,GAA+B;IAGtG,IAAMC,aAAaJ,AAAe,UAAfA,cAAwBD,kBAAkBM,KAAAA,GAEvDC,UAAU,AAAwB,YAAxB,OAAOJ,eACjBK,WAAWD,UAAUJ,gBAAgBG,KAAAA;IAI3C,IAAI,AAAc,cAAd,OAHJF,CAAAA,MAAMG,UAAUH,MAAMD,aAAY,GAIjC,OAAOJ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