-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
237 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { errors, IConfigStorage, ConfigRef } from "@drewpackages/engine"; | ||
import { IDumpable } from "../dump"; | ||
import z from "zod"; | ||
|
||
const ConfigStorageDumpSchema = z | ||
.object({ | ||
storage: z.record(z.string().min(1), z.any()), | ||
}) | ||
.required({ storage: true }); | ||
|
||
export type IConfigStorageDump = z.infer<typeof ConfigStorageDumpSchema>; | ||
|
||
export class ConfigStorage<T extends object> | ||
implements IConfigStorage, IDumpable<IConfigStorageDump> | ||
{ | ||
private readonly storage: Map<string, object> = new Map(); | ||
|
||
set(api: string, resolvedConfig: T) { | ||
if (this.storage.has(api)) { | ||
throw new errors.ApiConfigAlreadyResolvedError(api); | ||
} | ||
this.storage.set(api, resolvedConfig); | ||
} | ||
|
||
get(api: string): T { | ||
if (!this.storage.has(api)) { | ||
throw new errors.ApiConfigNotResolvedError(api); | ||
} | ||
return this.storage.get(api) as T; | ||
} | ||
|
||
resolve(ref: ConfigRef): string { | ||
if (!this.storage.has(ref.group)) { | ||
throw new errors.ApiConfigNotResolvedError(ref.group); | ||
} | ||
|
||
return (this.storage.get(ref.group) as any)[ref.key]; | ||
} | ||
|
||
isCorrectDump(maybeDump: any): maybeDump is IConfigStorageDump { | ||
return ConfigStorageDumpSchema.safeParse(maybeDump).success; | ||
} | ||
|
||
toDump(): IConfigStorageDump { | ||
return { storage: Object.fromEntries(this.storage.entries()) }; | ||
} | ||
|
||
fromDump(dump: IConfigStorageDump): undefined { | ||
Object.getOwnPropertyNames(dump.storage!).forEach((key) => | ||
this.storage.set(key, dump.storage[key]) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./types"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { StageInstruction } from "@drewpackages/engine"; | ||
import { IConfigStorageDump } from "config/storage"; | ||
import { IStateStorageDump } from "state/dump"; | ||
|
||
export interface IDumpable<D extends object> { | ||
isCorrectDump(maybeDump: any): maybeDump is D; | ||
toDump(): D | Promise<D>; | ||
fromDump(dump: D): void | Promise<void>; | ||
} | ||
|
||
export interface FormulaExecutionDump { | ||
config: IConfigStorageDump; | ||
state: IStateStorageDump; | ||
instructions: Array<StageInstruction>; | ||
executedSteps: number; | ||
formulaName: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import z from "zod"; | ||
|
||
export const StateStorageDumpSchema = z.object({ | ||
outputIds: z.array(z.string().min(1)), | ||
resolvedValues: z | ||
.map(z.string().min(1), z.any()) | ||
.or(z.record(z.string(), z.any())), | ||
}); | ||
|
||
export type IStateStorageDump = z.infer<typeof StateStorageDumpSchema>; |
Oops, something went wrong.