From 4e3075aabad02fb2b6f878fd01269937c3dbd0d8 Mon Sep 17 00:00:00 2001 From: J H Date: Fri, 23 Feb 2024 15:32:01 -0700 Subject: [PATCH] chore: Add in the ability to remove the bad sections? --- .../Systems/SystemForEmbassy/index.ts | 42 ++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/container-runtime/src/Adapters/Systems/SystemForEmbassy/index.ts b/container-runtime/src/Adapters/Systems/SystemForEmbassy/index.ts index 339df6460..4814da9b2 100644 --- a/container-runtime/src/Adapters/Systems/SystemForEmbassy/index.ts +++ b/container-runtime/src/Adapters/Systems/SystemForEmbassy/index.ts @@ -826,9 +826,11 @@ export class SystemForEmbassy implements System { } async function removePointers(value: T.ConfigRes): Promise { const startingSpec = structuredClone(value.spec) + const config = + value.config && cleanConfigFromPointers(value.config, startingSpec) const spec = cleanSpecOfPointers(startingSpec) - return { ...value, spec } + return { config, spec } } const matchPointer = object({ @@ -871,6 +873,44 @@ function cleanSpecOfPointers(mutSpec: T): T { return mutSpec } +function isKeyOf( + key: string, + ofObject: O, +): key is keyof O & string { + return key in ofObject +} + +// prettier-ignore +type CleanConfigFromPointers = + [C, S] extends [object, object] ? { + [K in (keyof C & keyof S ) & string]: ( + S[K] extends {type: "pointer"} ? never : + S[K] extends {spec: object & infer B} ? CleanConfigFromPointers : + C[K] + ) + } : + null + +function cleanConfigFromPointers( + config: C, + spec: S, +): CleanConfigFromPointers { + const newConfig = {} as CleanConfigFromPointers + + if (!(object.test(config) && object.test(spec)) || newConfig == null) + return null as CleanConfigFromPointers + + for (const key of Object.keys(spec)) { + if (!isKeyOf(key, spec)) continue + if (!isKeyOf(key, config)) continue + const partSpec = spec[key] + if (matchPointer.test(partSpec)) continue + ;(newConfig as any)[key] = matchSpec.test(partSpec) + ? cleanConfigFromPointers(config[key], partSpec.spec) + : config[key] + } + return newConfig as CleanConfigFromPointers +} async function updateConfig( effects: HostSystemStartOs,