chore: Add in the ability to remove the bad sections?

This commit is contained in:
J H
2024-02-23 15:32:01 -07:00
parent 3bd7596873
commit 4e3075aaba

View File

@@ -826,9 +826,11 @@ export class SystemForEmbassy implements System {
}
async function removePointers(value: T.ConfigRes): Promise<T.ConfigRes> {
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<T>(mutSpec: T): T {
return mutSpec
}
function isKeyOf<O extends object>(
key: string,
ofObject: O,
): key is keyof O & string {
return key in ofObject
}
// prettier-ignore
type CleanConfigFromPointers<C, S> =
[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], B> :
C[K]
)
} :
null
function cleanConfigFromPointers<C, S>(
config: C,
spec: S,
): CleanConfigFromPointers<C, S> {
const newConfig = {} as CleanConfigFromPointers<C, S>
if (!(object.test(config) && object.test(spec)) || newConfig == null)
return null as CleanConfigFromPointers<C, S>
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<C, S>
}
async function updateConfig(
effects: HostSystemStartOs,