From c10f5c2189e8b31cd623562af280f35b6bc1ca07 Mon Sep 17 00:00:00 2001 From: Codex Date: Sun, 14 Jun 2026 05:24:28 +0000 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=8A=BD=E5=8F=96=20YAML=20field?= =?UTF-8?q?=20reader=20factory?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/src/platform-infra-langbot.ts | 111 ++++------------------ scripts/src/platform-infra-n8n.ts | 105 ++++---------------- scripts/src/platform-infra-ops-library.ts | 44 +++++++++ 3 files changed, 85 insertions(+), 175 deletions(-) diff --git a/scripts/src/platform-infra-langbot.ts b/scripts/src/platform-infra-langbot.ts index 90298851..d8e27f71 100644 --- a/scripts/src/platform-infra-langbot.ts +++ b/scripts/src/platform-infra-langbot.ts @@ -19,24 +19,7 @@ import { type FrpcSecretMaterial, } from "./platform-infra-public-service"; import { - yamlAbsolutePathField, - yamlApiPathField, - yamlArrayOfRecords, - yamlBooleanField, - yamlEnumField, - yamlEnvKeyField, - yamlHostField, - yamlHttpsUrlField, - yamlIntegerArrayField, - yamlIntegerField, - yamlKubernetesNameField, - yamlObjectField, - yamlPgIdentifierField, - yamlPortField, - yamlRecord, - yamlSourceRefField, - yamlStringArrayField, - yamlStringField, + createYamlFieldReader, } from "./platform-infra-ops-library"; import { fingerprintSecretValues, parseEnvFile, readEnvSourceFile, readTextFile, redactRepoPath, requiredEnvValue } from "./secrets"; @@ -45,6 +28,26 @@ const configLabel = "config/platform-infra/langbot.yaml"; const serviceName = "langbot"; const pluginRuntimeServiceName = "langbot-plugin-runtime"; const fieldManager = "unidesk-platform-langbot"; +const { + asRecord, + objectField, + arrayOfRecords, + stringField, + integerField, + booleanField, + stringArrayField, + numberArrayField, + enumField, + kubernetesNameField, + sourceRefField, + envKeyField, + pgIdentifierField, + hostField, + portField, + absolutePathField, + apiPathField, + httpsUrlField, +} = createYamlFieldReader(configLabel); interface LangBotConfig { version: number; @@ -1461,78 +1464,6 @@ function boolField(value: Record | null, key: string, fallback: return typeof value?.[key] === "boolean" ? value[key] : fallback; } -function asRecord(value: unknown, path: string): Record { - return yamlRecord(value, path); -} - -function objectField(obj: Record, key: string, path: string): Record { - return yamlObjectField(obj, key, configLabel, path); -} - -function arrayOfRecords(value: unknown, path: string): Record[] { - return yamlArrayOfRecords(value, configLabel, path); -} - -function stringField(obj: Record, key: string, path: string): string { - return yamlStringField(obj, key, configLabel, path); -} - -function integerField(obj: Record, key: string, path: string): number { - return yamlIntegerField(obj, key, configLabel, path); -} - -function booleanField(obj: Record, key: string, path: string): boolean { - return yamlBooleanField(obj, key, configLabel, path); -} - -function stringArrayField(obj: Record, key: string, path: string): string[] { - return yamlStringArrayField(obj, key, configLabel, path); -} - -function numberArrayField(obj: Record, key: string, path: string): number[] { - return yamlIntegerArrayField(obj, key, configLabel, path); -} - -function enumField(obj: Record, key: string, path: string, values: T): T[number] { - return yamlEnumField(obj, key, configLabel, path, values); -} - -function kubernetesNameField(obj: Record, key: string, path: string): string { - return yamlKubernetesNameField(obj, key, configLabel, path); -} - -function sourceRefField(obj: Record, key: string, path: string): string { - return yamlSourceRefField(obj, key, configLabel, path); -} - -function envKeyField(obj: Record, key: string, path: string): string { - return yamlEnvKeyField(obj, key, configLabel, path); -} - -function pgIdentifierField(obj: Record, key: string, path: string): string { - return yamlPgIdentifierField(obj, key, configLabel, path); -} - -function hostField(obj: Record, key: string, path: string): string { - return yamlHostField(obj, key, configLabel, path); -} - -function portField(obj: Record, key: string, path: string): number { - return yamlPortField(obj, key, configLabel, path); -} - -function absolutePathField(obj: Record, key: string, path: string): string { - return yamlAbsolutePathField(obj, key, configLabel, path); -} - -function apiPathField(obj: Record, key: string, path: string): string { - return yamlApiPathField(obj, key, configLabel, path); -} - -function httpsUrlField(obj: Record, key: string, path: string): string { - return yamlHttpsUrlField(obj, key, configLabel, path); -} - function pvcSpec(record: Record, path: string): PvcSpec { const name = kubernetesNameField(record, "name", path); const size = stringField(record, "size", path); diff --git a/scripts/src/platform-infra-n8n.ts b/scripts/src/platform-infra-n8n.ts index 40e9dcf1..68a1d792 100644 --- a/scripts/src/platform-infra-n8n.ts +++ b/scripts/src/platform-infra-n8n.ts @@ -21,23 +21,7 @@ import { type PublicServiceTarget, } from "./platform-infra-public-service"; import { - yamlAbsolutePathField, - yamlArrayOfRecords, - yamlBooleanField, - yamlEnumField, - yamlEnvKeyField, - yamlHostField, - yamlHttpsUrlField, - yamlIntegerArrayField, - yamlIntegerField, - yamlKubernetesNameField, - yamlObjectField, - yamlPgIdentifierField, - yamlPortField, - yamlRecord, - yamlSourceRefField, - yamlStringArrayField, - yamlStringField, + createYamlFieldReader, } from "./platform-infra-ops-library"; import { fingerprintSecretValues, parseEnvFile, readEnvSourceFile, readTextFile, redactRepoPath, requiredEnvValue } from "./secrets"; @@ -45,6 +29,25 @@ const configFile = rootPath("config", "platform-infra", "n8n.yaml"); const configLabel = "config/platform-infra/n8n.yaml"; const serviceName = "n8n"; const fieldManager = "unidesk-platform-n8n"; +const { + asRecord, + objectField, + arrayOfRecords, + stringField, + integerField, + booleanField, + stringArrayField, + numberArrayField, + enumField, + kubernetesNameField, + sourceRefField, + envKeyField, + pgIdentifierField, + hostField, + portField, + absolutePathField, + httpsUrlField, +} = createYamlFieldReader(configLabel); interface N8nConfig { version: number; @@ -1065,74 +1068,6 @@ function boolField(value: Record | null, key: string, fallback: return typeof value?.[key] === "boolean" ? value[key] : fallback; } -function asRecord(value: unknown, path: string): Record { - return yamlRecord(value, path); -} - -function objectField(obj: Record, key: string, path: string): Record { - return yamlObjectField(obj, key, configLabel, path); -} - -function arrayOfRecords(value: unknown, path: string): Record[] { - return yamlArrayOfRecords(value, configLabel, path); -} - -function stringField(obj: Record, key: string, path: string): string { - return yamlStringField(obj, key, configLabel, path); -} - -function integerField(obj: Record, key: string, path: string): number { - return yamlIntegerField(obj, key, configLabel, path); -} - -function booleanField(obj: Record, key: string, path: string): boolean { - return yamlBooleanField(obj, key, configLabel, path); -} - -function stringArrayField(obj: Record, key: string, path: string): string[] { - return yamlStringArrayField(obj, key, configLabel, path); -} - -function numberArrayField(obj: Record, key: string, path: string): number[] { - return yamlIntegerArrayField(obj, key, configLabel, path); -} - -function enumField(obj: Record, key: string, path: string, values: T): T[number] { - return yamlEnumField(obj, key, configLabel, path, values); -} - -function kubernetesNameField(obj: Record, key: string, path: string): string { - return yamlKubernetesNameField(obj, key, configLabel, path); -} - -function sourceRefField(obj: Record, key: string, path: string): string { - return yamlSourceRefField(obj, key, configLabel, path); -} - -function envKeyField(obj: Record, key: string, path: string): string { - return yamlEnvKeyField(obj, key, configLabel, path); -} - -function pgIdentifierField(obj: Record, key: string, path: string): string { - return yamlPgIdentifierField(obj, key, configLabel, path); -} - -function hostField(obj: Record, key: string, path: string): string { - return yamlHostField(obj, key, configLabel, path); -} - -function portField(obj: Record, key: string, path: string): number { - return yamlPortField(obj, key, configLabel, path); -} - -function absolutePathField(obj: Record, key: string, path: string): string { - return yamlAbsolutePathField(obj, key, configLabel, path); -} - -function httpsUrlField(obj: Record, key: string, path: string): string { - return yamlHttpsUrlField(obj, key, configLabel, path); -} - function pvcSpec(record: Record, path: string): PvcSpec { const name = kubernetesNameField(record, "name", path); const size = stringField(record, "size", path); diff --git a/scripts/src/platform-infra-ops-library.ts b/scripts/src/platform-infra-ops-library.ts index 07871a32..ec5defb6 100644 --- a/scripts/src/platform-infra-ops-library.ts +++ b/scripts/src/platform-infra-ops-library.ts @@ -294,6 +294,50 @@ export function yamlHttpsUrlField(obj: Record, key: string, con return value.replace(/\/+$/u, ""); } +export interface YamlFieldReader { + asRecord(value: unknown, label: string): Record; + objectField(obj: Record, key: string, path: string): Record; + arrayOfRecords(value: unknown, path: string): Record[]; + stringField(obj: Record, key: string, path: string): string; + integerField(obj: Record, key: string, path: string): number; + booleanField(obj: Record, key: string, path: string): boolean; + stringArrayField(obj: Record, key: string, path: string): string[]; + numberArrayField(obj: Record, key: string, path: string): number[]; + enumField(obj: Record, key: string, path: string, values: T): T[number]; + kubernetesNameField(obj: Record, key: string, path: string): string; + sourceRefField(obj: Record, key: string, path: string): string; + envKeyField(obj: Record, key: string, path: string): string; + pgIdentifierField(obj: Record, key: string, path: string): string; + hostField(obj: Record, key: string, path: string): string; + portField(obj: Record, key: string, path: string): number; + absolutePathField(obj: Record, key: string, path: string): string; + apiPathField(obj: Record, key: string, path: string): string; + httpsUrlField(obj: Record, key: string, path: string): string; +} + +export function createYamlFieldReader(configLabel: string): YamlFieldReader { + return { + asRecord: (value, label) => yamlRecord(value, label), + objectField: (obj, key, path) => yamlObjectField(obj, key, configLabel, path), + arrayOfRecords: (value, path) => yamlArrayOfRecords(value, configLabel, path), + stringField: (obj, key, path) => yamlStringField(obj, key, configLabel, path), + integerField: (obj, key, path) => yamlIntegerField(obj, key, configLabel, path), + booleanField: (obj, key, path) => yamlBooleanField(obj, key, configLabel, path), + stringArrayField: (obj, key, path) => yamlStringArrayField(obj, key, configLabel, path), + numberArrayField: (obj, key, path) => yamlIntegerArrayField(obj, key, configLabel, path), + enumField: (obj, key, path, values) => yamlEnumField(obj, key, configLabel, path, values), + kubernetesNameField: (obj, key, path) => yamlKubernetesNameField(obj, key, configLabel, path), + sourceRefField: (obj, key, path) => yamlSourceRefField(obj, key, configLabel, path), + envKeyField: (obj, key, path) => yamlEnvKeyField(obj, key, configLabel, path), + pgIdentifierField: (obj, key, path) => yamlPgIdentifierField(obj, key, configLabel, path), + hostField: (obj, key, path) => yamlHostField(obj, key, configLabel, path), + portField: (obj, key, path) => yamlPortField(obj, key, configLabel, path), + absolutePathField: (obj, key, path) => yamlAbsolutePathField(obj, key, configLabel, path), + apiPathField: (obj, key, path) => yamlApiPathField(obj, key, configLabel, path), + httpsUrlField: (obj, key, path) => yamlHttpsUrlField(obj, key, configLabel, path), + }; +} + export function repoRelative(path: string): string { const rel = relative(rootPath(), path); return rel.startsWith("..") ? path : rel;