Skip to content

Commit

Permalink
Stop using tag class for multiple purposes; add nodeClass
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed Dec 30, 2018
1 parent bd2c190 commit 3ae63bc
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/schema/_omap.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ function createOMap(schema, iterable, wrapScalars) {

export default {
class: Map,
nodeClass: YAMLOMap,
default: false,
tag: 'tag:yaml.org,2002:omap',
resolve: parseOMap,
Expand Down
1 change: 1 addition & 0 deletions src/schema/_set.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ function createSet(schema, iterable, wrapScalars) {

export default {
class: Set,
nodeClass: YAMLSet,
default: false,
tag: 'tag:yaml.org,2002:set',
resolve: parseSet,
Expand Down
8 changes: 4 additions & 4 deletions src/schema/failsafe.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import Map from './Map'
import Seq from './Seq'
import YAMLMap from './Map'
import YAMLSeq from './Seq'
import { str } from './_string'
import parseMap from './parseMap'
import parseSeq from './parseSeq'

export const map = {
class: Map,
nodeClass: YAMLMap,
default: true,
tag: 'tag:yaml.org,2002:map',
resolve: parseMap,
Expand All @@ -14,7 +14,7 @@ export const map = {
}

export const seq = {
class: Seq,
nodeClass: YAMLSeq,
default: true,
tag: 'tag:yaml.org,2002:seq',
resolve: parseSeq,
Expand Down
13 changes: 8 additions & 5 deletions src/schema/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ export default class Schema {
if (!tagObj) throw new Error('Tag not resolved for null value')
return tagObj
}
let obj = item
if (item.hasOwnProperty('value')) {
let tagObj, obj
if (item instanceof Scalar) {
switch (typeof item.value) {
case 'boolean':
obj = new Boolean()
Expand All @@ -148,10 +148,13 @@ export default class Schema {
default:
obj = item.value
}
const match = this.tags.filter(t => t.class && obj instanceof t.class)
tagObj =
match.find(t => t.format === item.format) || match.find(t => !t.format)
} else {
obj = item
tagObj = this.tags.find(t => t.nodeClass && obj instanceof t.nodeClass)
}
const match = this.tags.filter(t => t.class && obj instanceof t.class)
const tagObj =
match.find(t => t.format === item.format) || match.find(t => !t.format)
if (!tagObj) {
const name = obj && obj.constructor ? obj.constructor.name : typeof obj
throw new Error(`Tag not resolved for ${name} value`)
Expand Down

0 comments on commit 3ae63bc

Please sign in to comment.