refactor: 抽取 YAML field reader factory

This commit is contained in:
Codex
2026-06-14 05:24:28 +00:00
parent cd76411969
commit c10f5c2189
3 changed files with 85 additions and 175 deletions
+21 -90
View File
@@ -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<string, unknown> | null, key: string, fallback:
return typeof value?.[key] === "boolean" ? value[key] : fallback;
}
function asRecord(value: unknown, path: string): Record<string, unknown> {
return yamlRecord(value, path);
}
function objectField(obj: Record<string, unknown>, key: string, path: string): Record<string, unknown> {
return yamlObjectField(obj, key, configLabel, path);
}
function arrayOfRecords(value: unknown, path: string): Record<string, unknown>[] {
return yamlArrayOfRecords(value, configLabel, path);
}
function stringField(obj: Record<string, unknown>, key: string, path: string): string {
return yamlStringField(obj, key, configLabel, path);
}
function integerField(obj: Record<string, unknown>, key: string, path: string): number {
return yamlIntegerField(obj, key, configLabel, path);
}
function booleanField(obj: Record<string, unknown>, key: string, path: string): boolean {
return yamlBooleanField(obj, key, configLabel, path);
}
function stringArrayField(obj: Record<string, unknown>, key: string, path: string): string[] {
return yamlStringArrayField(obj, key, configLabel, path);
}
function numberArrayField(obj: Record<string, unknown>, key: string, path: string): number[] {
return yamlIntegerArrayField(obj, key, configLabel, path);
}
function enumField<const T extends readonly string[]>(obj: Record<string, unknown>, key: string, path: string, values: T): T[number] {
return yamlEnumField(obj, key, configLabel, path, values);
}
function kubernetesNameField(obj: Record<string, unknown>, key: string, path: string): string {
return yamlKubernetesNameField(obj, key, configLabel, path);
}
function sourceRefField(obj: Record<string, unknown>, key: string, path: string): string {
return yamlSourceRefField(obj, key, configLabel, path);
}
function envKeyField(obj: Record<string, unknown>, key: string, path: string): string {
return yamlEnvKeyField(obj, key, configLabel, path);
}
function pgIdentifierField(obj: Record<string, unknown>, key: string, path: string): string {
return yamlPgIdentifierField(obj, key, configLabel, path);
}
function hostField(obj: Record<string, unknown>, key: string, path: string): string {
return yamlHostField(obj, key, configLabel, path);
}
function portField(obj: Record<string, unknown>, key: string, path: string): number {
return yamlPortField(obj, key, configLabel, path);
}
function absolutePathField(obj: Record<string, unknown>, key: string, path: string): string {
return yamlAbsolutePathField(obj, key, configLabel, path);
}
function apiPathField(obj: Record<string, unknown>, key: string, path: string): string {
return yamlApiPathField(obj, key, configLabel, path);
}
function httpsUrlField(obj: Record<string, unknown>, key: string, path: string): string {
return yamlHttpsUrlField(obj, key, configLabel, path);
}
function pvcSpec(record: Record<string, unknown>, path: string): PvcSpec {
const name = kubernetesNameField(record, "name", path);
const size = stringField(record, "size", path);
+20 -85
View File
@@ -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<string, unknown> | null, key: string, fallback:
return typeof value?.[key] === "boolean" ? value[key] : fallback;
}
function asRecord(value: unknown, path: string): Record<string, unknown> {
return yamlRecord(value, path);
}
function objectField(obj: Record<string, unknown>, key: string, path: string): Record<string, unknown> {
return yamlObjectField(obj, key, configLabel, path);
}
function arrayOfRecords(value: unknown, path: string): Record<string, unknown>[] {
return yamlArrayOfRecords(value, configLabel, path);
}
function stringField(obj: Record<string, unknown>, key: string, path: string): string {
return yamlStringField(obj, key, configLabel, path);
}
function integerField(obj: Record<string, unknown>, key: string, path: string): number {
return yamlIntegerField(obj, key, configLabel, path);
}
function booleanField(obj: Record<string, unknown>, key: string, path: string): boolean {
return yamlBooleanField(obj, key, configLabel, path);
}
function stringArrayField(obj: Record<string, unknown>, key: string, path: string): string[] {
return yamlStringArrayField(obj, key, configLabel, path);
}
function numberArrayField(obj: Record<string, unknown>, key: string, path: string): number[] {
return yamlIntegerArrayField(obj, key, configLabel, path);
}
function enumField<const T extends readonly string[]>(obj: Record<string, unknown>, key: string, path: string, values: T): T[number] {
return yamlEnumField(obj, key, configLabel, path, values);
}
function kubernetesNameField(obj: Record<string, unknown>, key: string, path: string): string {
return yamlKubernetesNameField(obj, key, configLabel, path);
}
function sourceRefField(obj: Record<string, unknown>, key: string, path: string): string {
return yamlSourceRefField(obj, key, configLabel, path);
}
function envKeyField(obj: Record<string, unknown>, key: string, path: string): string {
return yamlEnvKeyField(obj, key, configLabel, path);
}
function pgIdentifierField(obj: Record<string, unknown>, key: string, path: string): string {
return yamlPgIdentifierField(obj, key, configLabel, path);
}
function hostField(obj: Record<string, unknown>, key: string, path: string): string {
return yamlHostField(obj, key, configLabel, path);
}
function portField(obj: Record<string, unknown>, key: string, path: string): number {
return yamlPortField(obj, key, configLabel, path);
}
function absolutePathField(obj: Record<string, unknown>, key: string, path: string): string {
return yamlAbsolutePathField(obj, key, configLabel, path);
}
function httpsUrlField(obj: Record<string, unknown>, key: string, path: string): string {
return yamlHttpsUrlField(obj, key, configLabel, path);
}
function pvcSpec(record: Record<string, unknown>, path: string): PvcSpec {
const name = kubernetesNameField(record, "name", path);
const size = stringField(record, "size", path);
+44
View File
@@ -294,6 +294,50 @@ export function yamlHttpsUrlField(obj: Record<string, unknown>, key: string, con
return value.replace(/\/+$/u, "");
}
export interface YamlFieldReader {
asRecord(value: unknown, label: string): Record<string, unknown>;
objectField(obj: Record<string, unknown>, key: string, path: string): Record<string, unknown>;
arrayOfRecords(value: unknown, path: string): Record<string, unknown>[];
stringField(obj: Record<string, unknown>, key: string, path: string): string;
integerField(obj: Record<string, unknown>, key: string, path: string): number;
booleanField(obj: Record<string, unknown>, key: string, path: string): boolean;
stringArrayField(obj: Record<string, unknown>, key: string, path: string): string[];
numberArrayField(obj: Record<string, unknown>, key: string, path: string): number[];
enumField<const T extends readonly string[]>(obj: Record<string, unknown>, key: string, path: string, values: T): T[number];
kubernetesNameField(obj: Record<string, unknown>, key: string, path: string): string;
sourceRefField(obj: Record<string, unknown>, key: string, path: string): string;
envKeyField(obj: Record<string, unknown>, key: string, path: string): string;
pgIdentifierField(obj: Record<string, unknown>, key: string, path: string): string;
hostField(obj: Record<string, unknown>, key: string, path: string): string;
portField(obj: Record<string, unknown>, key: string, path: string): number;
absolutePathField(obj: Record<string, unknown>, key: string, path: string): string;
apiPathField(obj: Record<string, unknown>, key: string, path: string): string;
httpsUrlField(obj: Record<string, unknown>, 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;