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

Support JSON Schema in determineScalarType #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
58 changes: 52 additions & 6 deletions src/scalarInference.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { YAMLScalar } from './yamlAST'
import YAMLException = require('./exception');

export function parseYamlBoolean(input: string): boolean {
if (["true", "True", "TRUE"].lastIndexOf(input) >= 0) {
Expand Down Expand Up @@ -54,15 +55,60 @@ export enum ScalarType {
null, bool, int, float, string
}

export enum DetermineScalarSchema {
Core = 'core',
Json = 'json',
}


export function determineScalarType(node: YAMLScalar, schema: DetermineScalarSchema = DetermineScalarSchema.Core): ScalarType {
if (node === void 0) {
// we should throw here, but for backwards compatibility purposes we need to have it here
return ScalarType.null;
}

switch (schema) {
case DetermineScalarSchema.Core:
return determineScalarTypeForCoreSchema(node);
case DetermineScalarSchema.Json:
return determineScalarTypeForJsonSchema(node);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw on default to protect from future mistakes?

}
}

/** Determines the type of a scalar according to
* the YAML 1.2 Core Schema (http://www.yaml.org/spec/1.2/spec.html#id2804923)
*/
export function determineScalarType(node: YAMLScalar): ScalarType {
if (node === undefined) {
* the YAML 1.2 JSON Schema (https://yaml.org/spec/1.2/spec.html#id2804356)
*/
export function determineScalarTypeForJsonSchema(node: YAMLScalar): ScalarType {
if (node.doubleQuoted || node.singleQuoted) {
return ScalarType.string
}

const value = node.value;

if (value === 'null') {
return ScalarType.null;
}

if (node.doubleQuoted || !node.plainScalar || node['singleQuoted']) {
if (value === 'true' || value === 'false') {
return ScalarType.bool;
}

if (/^-?(?:0|[1-9][0-9]*)$/.test(value)) {
return ScalarType.int;
}

if (/^-?(?:0|[1-9][0-9]*)(?:\.[0-9]*)?(?:[eE][-+]?[0-9]+)?$/.test(value)) {
return ScalarType.float;
}

throw new YAMLException('could not determine scalar type');
}

/** Determines the type of a scalar according to
* the YAML 1.2 Core Schema (http://www.yaml.org/spec/1.2/spec.html#id2804923)
*/
export function determineScalarTypeForCoreSchema(node: YAMLScalar): ScalarType {
if (node.doubleQuoted || !node.plainScalar || node.singleQuoted) {
return ScalarType.string
}

Expand Down Expand Up @@ -95,4 +141,4 @@ export function determineScalarType(node: YAMLScalar): ScalarType {
}

return ScalarType.string;
}
}
128 changes: 93 additions & 35 deletions test/scalarInference.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as chai from 'chai'
const assert = chai.assert
import { determineScalarType as sut, ScalarType, parseYamlBoolean, parseYamlInteger, parseYamlFloat } from '../src/scalarInference'
import { DetermineScalarSchema, determineScalarType as sut, ScalarType, parseYamlBoolean, parseYamlInteger, parseYamlFloat } from '../src/scalarInference'

import * as Yaml from '../src/index'

suite('determineScalarType', () => {

function determineScalarType(scalar: Yaml.YAMLDocument) {
return sut(<Yaml.YAMLScalar>scalar)
function determineScalarType(scalar: Yaml.YAMLDocument, schema: DetermineScalarSchema = DetermineScalarSchema.Core) {
return sut(<Yaml.YAMLScalar>scalar, schema)
}

function safeLoad(input) {
Expand All @@ -16,40 +16,97 @@ suite('determineScalarType', () => {

let _test = test;

// http://www.yaml.org/spec/1.2/spec.html#id2805071
suite('Plain Tag Resolution', () => {
suite('JSON Schema', () => {
suite('Plain Tag Resolution', () => {

function test(name, type, acceptable) {
_test(name, function () {
for (const word of acceptable) {
assert.strictEqual(determineScalarType(safeLoad(word)), type, word)
}
function test(name, type, acceptable) {
_test(name, function () {
for (const word of acceptable) {
assert.strictEqual(determineScalarType(safeLoad(word), DetermineScalarSchema.Json), type, word)
}
})
}

test('boolean', ScalarType.bool, ["true", "false"])

test("null", ScalarType.null, ["null"])

_test("null as from an array", function () {
const node = Yaml.newScalar('');
node.plainScalar = true;
assert.strictEqual(determineScalarType(node), ScalarType.null, "unquoted empty string")
})
};

test('boolean', ScalarType.bool, ["true", "True", "TRUE", "false", "False", "FALSE"])
test("integer", ScalarType.int, ["0", "-0", "3", "-19"])

test("float", ScalarType.float, ["0.", "-0.0", "12e03", "-2E+05"])

test("null", ScalarType.null, ["null", "Null", "NULL", "~", ""])
_test("null as from an array", function () {
const node = Yaml.newScalar('');
node.plainScalar = true;
assert.strictEqual(determineScalarType(node), ScalarType.null, "unquoted empty string")
test("string", ScalarType.string, ["'true'", "'TrUe'", "''", "'0'", '"1"', '" .5"'])
})

test("integer", ScalarType.int, ["0", "0o7", "0x3A", "-19"])
suite('Flow style', () => {
test('still recognizes types', function () {
const node = <Yaml.YAMLSequence>safeLoad(`[ null,
true,
0,
0.,
.inf,
.nan,
"-123\n345"
]`)

test("float", ScalarType.float, ["0.", "-0.0", ".5", "+12e03", "-2E+05"])
const expected = [ScalarType.null, ScalarType.bool, ScalarType.int, ScalarType.float, ScalarType.float, ScalarType.float, ScalarType.string]

test("float-infinity", ScalarType.float, [".inf", "-.Inf", "+.INF"])
assert.deepEqual(node.items.map(d => determineScalarType(d)), expected)
})
})

test("float-NaN", ScalarType.float, [".nan", ".NaN", ".NAN"])
suite('Block styles', () => {
var variations = ['>', '|', '>8', '|+1', '>-', '>+', '|-', '|+']

test("string", ScalarType.string, ["'true'", "TrUe", "nULl", "''", "'0'", '"1"', '" .5"', ".inF", ".nAn"])
test('are always strings', function () {
for (const variant of variations) {
assert.deepEqual(determineScalarType(safeLoad(variant + "\n 123")), ScalarType.string);
}
})
})
})

suite('Flow style', () => {
test('still recognizes types', function () {
const node = <Yaml.YAMLSequence>safeLoad(`[ null,
// http://www.yaml.org/spec/1.2/spec.html#id2805071
suite('Core Schema', () => {
suite('Plain Tag Resolution', () => {

function test(name, type, acceptable) {
_test(name, function () {
for (const word of acceptable) {
assert.strictEqual(determineScalarType(safeLoad(word)), type, word)
}
})
};

test('boolean', ScalarType.bool, ["true", "True", "TRUE", "false", "False", "FALSE"])

test("null", ScalarType.null, ["null", "Null", "NULL", "~", ""])
_test("null as from an array", function () {
const node = Yaml.newScalar('');
node.plainScalar = true;
assert.strictEqual(determineScalarType(node), ScalarType.null, "unquoted empty string")
})

test("integer", ScalarType.int, ["0", "0o7", "0x3A", "-19"])

test("float", ScalarType.float, ["0.", "-0.0", ".5", "+12e03", "-2E+05"])

test("float-infinity", ScalarType.float, [".inf", "-.Inf", "+.INF"])

test("float-NaN", ScalarType.float, [".nan", ".NaN", ".NAN"])

test("string", ScalarType.string, ["'true'", "TrUe", "nULl", "''", "'0'", '"1"', '" .5"', ".inF", ".nAn"])
})

suite('Flow style', () => {
test('still recognizes types', function () {
const node = <Yaml.YAMLSequence>safeLoad(`[ null,
true,
0,
0.,
Expand All @@ -58,19 +115,20 @@ suite('determineScalarType', () => {
"-123\n345"
]`)

const expected = [ScalarType.null, ScalarType.bool, ScalarType.int, ScalarType.float, ScalarType.float, ScalarType.float, ScalarType.string]
const expected = [ScalarType.null, ScalarType.bool, ScalarType.int, ScalarType.float, ScalarType.float, ScalarType.float, ScalarType.string]

assert.deepEqual(node.items.map(d => determineScalarType(d)), expected)
assert.deepEqual(node.items.map(d => determineScalarType(d)), expected)
})
})
})

suite('Block styles', () => {
var variations = ['>', '|', '>8', '|+1', '>-', '>+', '|-', '|+']
suite('Block styles', () => {
var variations = ['>', '|', '>8', '|+1', '>-', '>+', '|-', '|+']

test('are always strings', function () {
for (const variant of variations) {
assert.deepEqual(determineScalarType(safeLoad(variant + "\n 123")), ScalarType.string);
}
test('are always strings', function () {
for (const variant of variations) {
assert.deepEqual(determineScalarType(safeLoad(variant + "\n 123")), ScalarType.string);
}
})
})
})
})
Expand Down Expand Up @@ -162,4 +220,4 @@ suite('parseYamlFloat', () => {

assert(error, "should have thrown")
})
})
})