Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(yaml): merge yaml/_dumper/ files #5260

Merged
merged 4 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 124 additions & 2 deletions yaml/_dumper/dumper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@
VERTICAL_LINE,
} from "../_chars.ts";
import { YamlError } from "../_error.ts";
import type { RepresentFn } from "../_type.ts";
import type { Schema } from "../_schema.ts";
import { State } from "../_state.ts";
import type { RepresentFn, StyleVariant, Type } from "../_type.ts";
import * as common from "../_utils.ts";
import { DumperState, type DumperStateOptions } from "./dumper_state.ts";

type Any = common.Any;
type ArrayObject<T = Any> = common.ArrayObject<T>;
Expand Down Expand Up @@ -72,6 +73,127 @@
"OFF",
];

function compileStyleMap(
schema: Schema,
map?: ArrayObject<StyleVariant> | null,
): ArrayObject<StyleVariant> {
if (typeof map === "undefined" || map === null) return {};

const result: ArrayObject<StyleVariant> = {};
for (let tag of Object.keys(map)) {
let style = String(map[tag]) as StyleVariant;
if (tag.slice(0, 2) === "!!") {
tag = `tag:yaml.org,2002:${tag.slice(2)}`;
}
const type = schema.compiledTypeMap.fallback[tag];

if (type?.styleAliases && Object.hasOwn(type.styleAliases, style)) {
style = type.styleAliases[style];
}

Check warning on line 92 in yaml/_dumper/dumper.ts

View check run for this annotation

Codecov / codecov/patch

yaml/_dumper/dumper.ts#L91-L92

Added lines #L91 - L92 were not covered by tests

result[tag] = style;
}

return result;
}

export interface DumperStateOptions {
/** indentation width to use (in spaces). */
indent?: number;
/** when true, will not add an indentation level to array elements */
noArrayIndent?: boolean;
/**
* do not throw on invalid types (like function in the safe schema)
* and skip pairs and single values with such types.
*/
skipInvalid?: boolean;
/**
* specifies level of nesting, when to switch from
* block to flow style for collections. -1 means block style everywhere
*/
flowLevel?: number;
/** Each tag may have own set of styles. - "tag" => "style" map. */
styles?: ArrayObject<StyleVariant> | null;
/** specifies a schema to use. */
schema?: Schema;
/**
* If true, sort keys when dumping YAML in ascending, ASCII character order.
* If a function, use the function to sort the keys. (default: false)
* If a function is specified, the function must return a negative value
* if first argument is less than second argument, zero if they're equal
* and a positive value otherwise.
*/
sortKeys?: boolean | ((a: string, b: string) => number);
/** set max line width. (default: 80) */
lineWidth?: number;
/**
* if true, don't convert duplicate objects
* into references (default: false)
*/
noRefs?: boolean;
/**
* if true don't try to be compatible with older yaml versions.
* Currently: don't quote "yes", "no" and so on,
* as required for YAML 1.1 (default: false)
*/
noCompatMode?: boolean;
/**
* if true flow sequences will be condensed, omitting the
* space between `key: value` or `a, b`. Eg. `'[a,b]'` or `{a:{b:c}}`.
* Can be useful when using yaml for pretty URL query params
* as spaces are %-encoded. (default: false).
*/
condenseFlow?: boolean;
}

export class DumperState extends State {
indent: number;
noArrayIndent: boolean;
skipInvalid: boolean;
flowLevel: number;
sortKeys: boolean | ((a: Any, b: Any) => number);
lineWidth: number;
noRefs: boolean;
noCompatMode: boolean;
condenseFlow: boolean;
implicitTypes: Type[];
explicitTypes: Type[];
tag: string | null = null;
result = "";
duplicates: Any[] = [];
usedDuplicates: Any[] = []; // changed from null to []
styleMap: ArrayObject<StyleVariant>;
dump: Any;

constructor({
schema,
indent = 2,
noArrayIndent = false,
skipInvalid = false,
flowLevel = -1,
styles = null,
sortKeys = false,
lineWidth = 80,
noRefs = false,
noCompatMode = false,
condenseFlow = false,
}: DumperStateOptions) {
super(schema);
this.indent = Math.max(1, indent);
this.noArrayIndent = noArrayIndent;
this.skipInvalid = skipInvalid;
this.flowLevel = flowLevel;
this.styleMap = compileStyleMap(this.schema, styles);
this.sortKeys = sortKeys;
this.lineWidth = lineWidth;
this.noRefs = noRefs;
this.noCompatMode = noCompatMode;
this.condenseFlow = condenseFlow;
this.implicitTypes = this.schema.compiledImplicit;
this.explicitTypes = this.schema.compiledExplicit;
}
}

function encodeHex(character: number): string {
const string = character.toString(16).toUpperCase();

Expand Down
130 changes: 0 additions & 130 deletions yaml/_dumper/dumper_state.ts

This file was deleted.