refactor: 抽取 YAML field reader factory
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user