mirror of
https://github.com/denoland/std.git
synced 2024-11-22 04:59:05 +00:00
refactor(yaml): add KindType
generic type argument to Type
(#5461)
This commit is contained in:
parent
d708abee9a
commit
8b8333f1fd
@ -27,7 +27,7 @@ import {
|
||||
VERTICAL_LINE,
|
||||
} from "./_chars.ts";
|
||||
import { DEFAULT_SCHEMA, type Schema } from "./_schema.ts";
|
||||
import type { StyleVariant, Type } from "./_type.ts";
|
||||
import type { KindType, StyleVariant, Type } from "./_type.ts";
|
||||
import { type ArrayObject, getObjectTypeString, isObject } from "./_utils.ts";
|
||||
|
||||
const STYLE_PLAIN = 1;
|
||||
@ -118,7 +118,10 @@ function generateNextLine(indent: number, level: number): string {
|
||||
return `\n${" ".repeat(indent * level)}`;
|
||||
}
|
||||
|
||||
function testImplicitResolving(implicitTypes: Type[], str: string): boolean {
|
||||
function testImplicitResolving(
|
||||
implicitTypes: Type<"scalar">[],
|
||||
str: string,
|
||||
): boolean {
|
||||
return implicitTypes.some((type) => type.resolve(str));
|
||||
}
|
||||
|
||||
@ -472,8 +475,8 @@ export class DumperState {
|
||||
useAnchors: boolean;
|
||||
compatMode: boolean;
|
||||
condenseFlow: boolean;
|
||||
implicitTypes: Type[];
|
||||
explicitTypes: Type[];
|
||||
implicitTypes: Type<"scalar">[];
|
||||
explicitTypes: Type<KindType>[];
|
||||
tag: string | null = null;
|
||||
result = "";
|
||||
duplicates: unknown[] = [];
|
||||
|
@ -37,7 +37,7 @@ import {
|
||||
} from "./_chars.ts";
|
||||
import { Mark } from "./_mark.ts";
|
||||
import { DEFAULT_SCHEMA, type Schema, type TypeMap } from "./_schema.ts";
|
||||
import type { Type } from "./_type.ts";
|
||||
import type { KindType, Type } from "./_type.ts";
|
||||
import { type ArrayObject, getObjectTypeString, isObject } from "./_utils.ts";
|
||||
|
||||
const CONTEXT_FLOW_IN = 1;
|
||||
@ -146,7 +146,7 @@ class LoaderState {
|
||||
line = 0;
|
||||
onWarning?: (error: Error) => void;
|
||||
allowDuplicateKeys: boolean;
|
||||
implicitTypes: Type[];
|
||||
implicitTypes: Type<"scalar">[];
|
||||
typeMap: TypeMap;
|
||||
|
||||
version?: string | null;
|
||||
@ -1412,7 +1412,7 @@ function composeNode(
|
||||
let indentStatus = 1; // 1: this>parent, 0: this=parent, -1: this<parent
|
||||
let atNewLine = false;
|
||||
let hasContent = false;
|
||||
let type: Type;
|
||||
let type: Type<KindType>;
|
||||
let flowIndent: number;
|
||||
let blockIndent: number;
|
||||
|
||||
|
@ -24,11 +24,12 @@ import {
|
||||
undefinedType,
|
||||
} from "./_type/mod.ts";
|
||||
|
||||
function compileList(
|
||||
// deno-lint-ignore no-explicit-any
|
||||
function compileList<K extends KindType, D = any>(
|
||||
schema: Schema,
|
||||
name: "implicit" | "explicit",
|
||||
result: Type[],
|
||||
): Type[] {
|
||||
result: Type<K, D>[],
|
||||
): Type<K, D>[] {
|
||||
const exclude: number[] = [];
|
||||
|
||||
for (const includedSchema of schema.include) {
|
||||
@ -45,14 +46,17 @@ function compileList(
|
||||
}
|
||||
}
|
||||
|
||||
result.push(currentType);
|
||||
result.push(currentType as Type<K, D>);
|
||||
}
|
||||
|
||||
return result.filter((_type, index): unknown => !exclude.includes(index));
|
||||
}
|
||||
|
||||
export type TypeMap = Record<KindType | "fallback", ArrayObject<Type<unknown>>>;
|
||||
function compileMap(...typesList: Type<unknown>[][]): TypeMap {
|
||||
export type TypeMap = Record<
|
||||
KindType | "fallback",
|
||||
ArrayObject<Type<KindType>>
|
||||
>;
|
||||
function compileMap(...typesList: Type<KindType>[][]): TypeMap {
|
||||
const result: TypeMap = {
|
||||
fallback: {},
|
||||
mapping: {},
|
||||
@ -69,17 +73,17 @@ function compileMap(...typesList: Type<unknown>[][]): TypeMap {
|
||||
}
|
||||
|
||||
export class Schema {
|
||||
implicit: Type[];
|
||||
explicit: Type[];
|
||||
implicit: Type<"scalar">[];
|
||||
explicit: Type<KindType>[];
|
||||
include: Schema[];
|
||||
|
||||
compiledImplicit: Type[];
|
||||
compiledExplicit: Type[];
|
||||
compiledImplicit: Type<"scalar">[];
|
||||
compiledExplicit: Type<KindType>[];
|
||||
compiledTypeMap: TypeMap;
|
||||
|
||||
constructor(definition: {
|
||||
implicit?: Type[];
|
||||
explicit?: Type[];
|
||||
implicit?: Type<"scalar">[];
|
||||
explicit?: Type<KindType>[];
|
||||
include?: Schema[];
|
||||
}) {
|
||||
this.explicit = definition.explicit || [];
|
||||
|
@ -22,9 +22,9 @@ export type StyleVariant =
|
||||
export type RepresentFn<D> = (data: D, style?: StyleVariant) => string;
|
||||
|
||||
// deno-lint-ignore no-explicit-any
|
||||
export interface Type<D = any> {
|
||||
export interface Type<K extends KindType, D = any> {
|
||||
tag: string;
|
||||
kind: KindType;
|
||||
kind: K;
|
||||
instanceOf?: new (...args: unknown[]) => D;
|
||||
predicate?: (data: unknown) => data is D;
|
||||
represent?: RepresentFn<D> | ArrayObject<RepresentFn<D>>;
|
||||
|
@ -118,7 +118,7 @@ function isBinary(obj: unknown): obj is Uint8Array {
|
||||
return obj instanceof Uint8Array;
|
||||
}
|
||||
|
||||
export const binary: Type<Uint8Array> = {
|
||||
export const binary: Type<"scalar", Uint8Array> = {
|
||||
tag: "tag:yaml.org,2002:binary",
|
||||
construct: constructYamlBinary,
|
||||
kind: "scalar",
|
||||
|
@ -17,7 +17,7 @@ function constructYamlBoolean(data: string): boolean {
|
||||
return data === "true" || data === "True" || data === "TRUE";
|
||||
}
|
||||
|
||||
export const bool: Type<boolean> = {
|
||||
export const bool: Type<"scalar", boolean> = {
|
||||
tag: "tag:yaml.org,2002:bool",
|
||||
construct: constructYamlBoolean,
|
||||
defaultStyle: "lowercase",
|
||||
|
@ -114,7 +114,7 @@ function isFloat(object: unknown): object is number {
|
||||
(object % 1 !== 0 || isNegativeZero(object));
|
||||
}
|
||||
|
||||
export const float: Type<number> = {
|
||||
export const float: Type<"scalar", number> = {
|
||||
tag: "tag:yaml.org,2002:float",
|
||||
construct: constructYamlFloat,
|
||||
defaultStyle: "lowercase",
|
||||
|
@ -156,7 +156,7 @@ function isInteger(object: unknown): object is number {
|
||||
!isNegativeZero(object);
|
||||
}
|
||||
|
||||
export const int: Type<number> = {
|
||||
export const int: Type<"scalar", number> = {
|
||||
tag: "tag:yaml.org,2002:int",
|
||||
construct: constructYamlInteger,
|
||||
defaultStyle: "decimal",
|
||||
|
@ -5,7 +5,7 @@
|
||||
|
||||
import type { Type } from "../_type.ts";
|
||||
|
||||
export const map: Type<unknown> = {
|
||||
export const map: Type<"mapping", unknown> = {
|
||||
tag: "tag:yaml.org,2002:map",
|
||||
resolve() {
|
||||
return true;
|
||||
|
@ -9,7 +9,7 @@ function resolveYamlMerge(data: string): boolean {
|
||||
return data === "<<" || data === null;
|
||||
}
|
||||
|
||||
export const merge: Type<unknown> = {
|
||||
export const merge: Type<"scalar", unknown> = {
|
||||
tag: "tag:yaml.org,2002:merge",
|
||||
kind: "scalar",
|
||||
resolve: resolveYamlMerge,
|
||||
|
@ -22,7 +22,7 @@ function isNull(object: unknown): object is null {
|
||||
return object === null;
|
||||
}
|
||||
|
||||
export const nil: Type<null> = {
|
||||
export const nil: Type<"scalar", null> = {
|
||||
tag: "tag:yaml.org,2002:null",
|
||||
construct: constructYamlNull,
|
||||
defaultStyle: "lowercase",
|
||||
|
@ -35,7 +35,7 @@ function resolveYamlOmap(data: any): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
export const omap: Type<Record<PropertyKey, unknown>[]> = {
|
||||
export const omap: Type<"sequence", Record<PropertyKey, unknown>[]> = {
|
||||
tag: "tag:yaml.org,2002:omap",
|
||||
kind: "sequence",
|
||||
resolve: resolveYamlOmap,
|
||||
|
@ -43,7 +43,7 @@ function constructYamlPairs(data: string) {
|
||||
return result;
|
||||
}
|
||||
|
||||
export const pairs: Type = {
|
||||
export const pairs: Type<"sequence"> = {
|
||||
tag: "tag:yaml.org,2002:pairs",
|
||||
construct: constructYamlPairs,
|
||||
kind: "sequence",
|
||||
|
@ -7,7 +7,7 @@ import type { Type } from "../_type.ts";
|
||||
|
||||
const REGEXP = /^\/(?<regexp>[\s\S]+)\/(?<modifiers>[gismuy]*)$/;
|
||||
|
||||
export const regexp: Type<RegExp> = {
|
||||
export const regexp: Type<"scalar", RegExp> = {
|
||||
tag: "tag:yaml.org,2002:js/regexp",
|
||||
kind: "scalar",
|
||||
// deno-lint-ignore no-explicit-any
|
||||
|
@ -5,7 +5,7 @@
|
||||
|
||||
import type { Type } from "../_type.ts";
|
||||
|
||||
export const seq: Type<unknown[]> = {
|
||||
export const seq: Type<"sequence", unknown[]> = {
|
||||
tag: "tag:yaml.org,2002:seq",
|
||||
resolve() {
|
||||
return true;
|
||||
|
@ -22,7 +22,7 @@ function constructYamlSet(data: string) {
|
||||
return data !== null ? data : {};
|
||||
}
|
||||
|
||||
export const set: Type<Record<PropertyKey, unknown>> = {
|
||||
export const set: Type<"mapping", Record<PropertyKey, unknown>> = {
|
||||
tag: "tag:yaml.org,2002:set",
|
||||
construct: constructYamlSet,
|
||||
kind: "mapping",
|
||||
|
@ -4,7 +4,7 @@
|
||||
|
||||
import type { Type } from "../_type.ts";
|
||||
|
||||
export const str: Type<string> = {
|
||||
export const str: Type<"scalar", string> = {
|
||||
tag: "tag:yaml.org,2002:str",
|
||||
resolve() {
|
||||
return true;
|
||||
|
@ -87,7 +87,7 @@ function representYamlTimestamp(date: Date): string {
|
||||
return date.toISOString();
|
||||
}
|
||||
|
||||
export const timestamp: Type<Date> = {
|
||||
export const timestamp: Type<"scalar", Date> = {
|
||||
tag: "tag:yaml.org,2002:timestamp",
|
||||
construct: constructYamlTimestamp,
|
||||
instanceOf: Date,
|
||||
|
@ -5,7 +5,7 @@
|
||||
|
||||
import type { Type } from "../_type.ts";
|
||||
|
||||
export const undefinedType: Type<undefined> = {
|
||||
export const undefinedType: Type<"scalar", undefined> = {
|
||||
tag: "tag:yaml.org,2002:js/undefined",
|
||||
kind: "scalar",
|
||||
resolve() {
|
||||
|
Loading…
Reference in New Issue
Block a user