Skip to content

Commit

Permalink
refactor(ast): followup changes for YAML 1.2 Schemas
Browse files Browse the repository at this point in the history
Refs #3481
  • Loading branch information
char0n committed Jan 17, 2024
1 parent bb14de0 commit 6ef9c04
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 88 deletions.
1 change: 1 addition & 0 deletions packages/apidom-ast/src/yaml/schemas/ScalarTag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@ class ScalarTag {
return node;
}
}
/* eslint-enable class-methods-use-this */

export default ScalarTag;
21 changes: 18 additions & 3 deletions packages/apidom-ast/src/yaml/schemas/Tag.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
import ScalarTag from './ScalarTag';
/* eslint-disable class-methods-use-this */
class Tag {
public static readonly uri: string = '';

class Tag extends ScalarTag {
public tag: string = '';
public readonly tag: string = '';

constructor() {
this.tag = (this.constructor as typeof Tag).uri;
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
public test(node: any): boolean {
return true;
}

public resolve(node: any): any {
return node;
}
}
/* eslint-enable class-methods-use-this */

export default Tag;
15 changes: 4 additions & 11 deletions packages/apidom-ast/src/yaml/schemas/failsafe/GenericMapping.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import Tag from '../Tag';
import { YamlNodeKind } from '../../nodes/YamlTag';

/* eslint-disable class-methods-use-this */
class GenericMapping extends Tag {
public static uri: string = 'tag:yaml.org,2002:map';
public static readonly uri: string = 'tag:yaml.org,2002:map';

constructor() {
super();
this.tag = GenericMapping.uri;
}

public static test(node: any): boolean {
public test(node: any): boolean {
return node.tag.kind === YamlNodeKind.Mapping;
}

public static resolve(node: any): any {
return node;
}
}
/* eslint-enable class-methods-use-this */

export default GenericMapping;
15 changes: 4 additions & 11 deletions packages/apidom-ast/src/yaml/schemas/failsafe/GenericSequence.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import Tag from '../Tag';
import { YamlNodeKind } from '../../nodes/YamlTag';

/* eslint-disable class-methods-use-this */
class GenericSequence extends Tag {
public static uri: string = 'tag:yaml.org,2002:seq';
public static readonly uri: string = 'tag:yaml.org,2002:seq';

constructor() {
super();
this.tag = GenericSequence.uri;
}

public static test(node: any): boolean {
public test(node: any): boolean {
return node.tag.kind === YamlNodeKind.Sequence;
}

public static resolve(node: any): any {
return node;
}
}
/* eslint-enable class-methods-use-this */

export default GenericSequence;
11 changes: 1 addition & 10 deletions packages/apidom-ast/src/yaml/schemas/failsafe/GenericString.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
import Tag from '../Tag';

class GenericString extends Tag {
public static uri: string = 'tag:yaml.org,2002:str';

constructor() {
super();
this.tag = GenericString.uri;
}

public static resolve(node: any): any {
return node;
}
public static readonly uri: string = 'tag:yaml.org,2002:str';
}

export default GenericString;
24 changes: 12 additions & 12 deletions packages/apidom-ast/src/yaml/schemas/failsafe/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { clone } from 'ramda';
import YamlTagError from '../../errors/YamlTagError';
import YamlDirective from '../../nodes/YamlDirective';
import { YamlNodeKind } from '../../nodes/YamlTag';
import GenericMapping from './GenericMapping';
import GenericSequence from './GenericSequence';
import GenericString from './GenericString';
import GenericMappingTag from './GenericMapping';
import GenericSequenceTag from './GenericSequence';
import GenericStringTag from './GenericString';
import ScalarTag from '../ScalarTag';

class FailsafeSchema {
Expand All @@ -16,9 +16,9 @@ class FailsafeSchema {
constructor() {
this.tags = [];
this.tagDirectives = [];
this.registerTag(new GenericMapping());
this.registerTag(new GenericSequence());
this.registerTag(new GenericString());
this.registerTag(new GenericMappingTag());
this.registerTag(new GenericSequenceTag());
this.registerTag(new GenericStringTag());
}

// eslint-disable-next-line class-methods-use-this
Expand All @@ -28,11 +28,11 @@ class FailsafeSchema {
if (node.tag.explicitName === '!') {
// non-specific tag; we assume tag by kind
if (node.tag.kind === YamlNodeKind.Scalar) {
specificTagName = GenericString.uri;
specificTagName = GenericStringTag.uri;
} else if (node.tag.kind === YamlNodeKind.Sequence) {
specificTagName = GenericSequence.uri;
specificTagName = GenericSequenceTag.uri;
} else if (node.tag.kind === YamlNodeKind.Mapping) {
specificTagName = GenericMapping.uri;
specificTagName = GenericMappingTag.uri;
}
} else if (node.tag.explicitName.startsWith('!<')) {
// verbatim form
Expand Down Expand Up @@ -80,7 +80,7 @@ class FailsafeSchema {

// turn scalar nodes into canonical format before resolving
let canonicalNode = node;
if (node.tag.kind === YamlNodeKind.Scalar) {
if (ScalarTag.test(node)) {
canonicalNode = ScalarTag.canonicalFormat(node);
}

Expand All @@ -98,7 +98,7 @@ class FailsafeSchema {
}

// node content is not compatible with resolving mechanism (tag implementation)
if (!tag.constructor.test(canonicalNode)) {
if (!tag.test(canonicalNode)) {
throw new YamlTagError(`Node couldn't be resolved against the tag "${specificTagName}"`, {
specificTagName,
explicitTagName: node.tag.explicitName,
Expand All @@ -109,7 +109,7 @@ class FailsafeSchema {
});
}

return tag.constructor.resolve(canonicalNode);
return tag.resolve(canonicalNode);
}
}

Expand Down
13 changes: 5 additions & 8 deletions packages/apidom-ast/src/yaml/schemas/json/Boolean.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import Tag from '../Tag';

/* eslint-disable class-methods-use-this */
class Boolean extends Tag {
public static uri: string = 'tag:yaml.org,2002:bool';
public static readonly uri: string = 'tag:yaml.org,2002:bool';

constructor() {
super();
this.tag = Boolean.uri;
}

public static test(node: any): boolean {
public test(node: any): boolean {
return /^(true|false)$/.test(node.content);
}

public static resolve(node: any): any {
public resolve(node: any): any {
const content = node.content === 'true';
const nodeClone = node.clone();

Expand All @@ -21,5 +17,6 @@ class Boolean extends Tag {
return nodeClone;
}
}
/* eslint-enable class-methods-use-this */

export default Boolean;
13 changes: 5 additions & 8 deletions packages/apidom-ast/src/yaml/schemas/json/FloatingPoint.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import Tag from '../Tag';

/* eslint-disable class-methods-use-this */
class FloatingPoint extends Tag {
public static uri: string = 'tag:yaml.org,2002:float';
public static readonly uri: string = 'tag:yaml.org,2002:float';

constructor() {
super();
this.tag = FloatingPoint.uri;
}

public static test(node: any): boolean {
public test(node: any): boolean {
return /^-?(0|[1-9][0-9]*)(\.[0-9]*)?([eE][-+]?[0-9]+)?$/.test(node.content);
}

public static resolve(node: any): any {
public resolve(node: any): any {
const content = parseFloat(node.content);
const nodeClone = node.clone();

Expand All @@ -21,5 +17,6 @@ class FloatingPoint extends Tag {
return nodeClone;
}
}
/* eslint-enable class-methods-use-this */

export default FloatingPoint;
13 changes: 5 additions & 8 deletions packages/apidom-ast/src/yaml/schemas/json/Integer.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import Tag from '../Tag';

/* eslint-disable class-methods-use-this */
class Integer extends Tag {
public static uri: string = 'tag:yaml.org,2002:int';
public static readonly uri: string = 'tag:yaml.org,2002:int';

constructor() {
super();
this.tag = Integer.uri;
}

public static test(node: any): boolean {
public test(node: any): boolean {
return /^-?(0|[1-9][0-9]*)$/.test(node.content);
}

public static resolve(node: any): any {
public resolve(node: any): any {
const content = parseInt(node.content, 10);
const nodeClone = node.clone();

Expand All @@ -21,5 +17,6 @@ class Integer extends Tag {
return nodeClone;
}
}
/* eslint-enable class-methods-use-this */

export default Integer;
13 changes: 5 additions & 8 deletions packages/apidom-ast/src/yaml/schemas/json/Null.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
import Tag from '../Tag';

/* eslint-disable class-methods-use-this */
class Null extends Tag {
public static uri: string = 'tag:yaml.org,2002:null';
public static readonly uri: string = 'tag:yaml.org,2002:null';

constructor() {
super();
this.tag = Null.uri;
}

public static test(node: any): boolean {
public test(node: any): boolean {
return /^null$/.test(node.content);
}

public static resolve(node: any): any {
public resolve(node: any): any {
const nodeClone = node.clone();

nodeClone.content = null;

return nodeClone;
}
}
/* eslint-enable class-methods-use-this */

export default Null;
18 changes: 9 additions & 9 deletions packages/apidom-ast/src/yaml/schemas/json/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import FailsafeSchema from '../failsafe/index';
import Boolean from './Boolean';
import FloatingPoint from './FloatingPoint';
import Integer from './Integer';
import Null from './Null';
import BooleanTag from './Boolean';
import FloatingPointTag from './FloatingPoint';
import IntegerTag from './Integer';
import NullTag from './Null';
import { YamlNodeKind } from '../../nodes/YamlTag';
import GenericSequence from '../failsafe/GenericSequence';
import GenericMapping from '../failsafe/GenericMapping';
Expand All @@ -13,10 +13,10 @@ class JsonSchema extends FailsafeSchema {
/**
* We're registering more specific tags before more generic ones from Failsafe schema.
*/
this.registerTag(new Boolean(), true);
this.registerTag(new FloatingPoint(), true);
this.registerTag(new Integer(), true);
this.registerTag(new Null(), true);
this.registerTag(new BooleanTag(), true);
this.registerTag(new FloatingPointTag(), true);
this.registerTag(new IntegerTag(), true);
this.registerTag(new NullTag(), true);
}

public toSpecificTagName(node: any): any {
Expand All @@ -28,7 +28,7 @@ class JsonSchema extends FailsafeSchema {
} else if (node.tag.kind === YamlNodeKind.Mapping) {
specificTagName = GenericMapping.uri;
} else if (node.tag.kind === YamlNodeKind.Scalar) {
const foundTag = this.tags.find((tag) => tag.constructor.test(node));
const foundTag = this.tags.find((tag) => tag.test(node));
specificTagName = foundTag?.tag || '?';
}
}
Expand Down

0 comments on commit 6ef9c04

Please sign in to comment.