Skip to content

Commit

Permalink
Add tag arg to doc.schema.createNode(), fixing !!pairs & !!omap creation
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed Dec 30, 2018
1 parent 9d1d5b3 commit bd2c190
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
26 changes: 25 additions & 1 deletion __tests__/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,18 @@ date (00:00:00Z): 2002-12-14\n`)
expect(doc.toJSON()).toMatchObject([{ a: 1 }, { b: 2 }, { a: 3 }])
expect(String(doc)).toBe(src)
})

test('stringify', () => {
const doc = new YAML.Document({ version: '1.1' })
doc.setSchema()
doc.contents = doc.schema.createNode(
[['a', 1], ['b', 2], ['a', 3]],
false,
'!!pairs'
)
expect(doc.contents.tag).toBe('tag:yaml.org,2002:pairs')
expect(String(doc)).toBe(`!!pairs\n- a: 1\n- b: 2\n- a: 3\n`)
})
})

describe('!!omap', () => {
Expand Down Expand Up @@ -421,13 +433,25 @@ date (00:00:00Z): 2002-12-14\n`)
])
})

test('stringify', () => {
test('stringify Map', () => {
const map = new Map([['a', 1], ['b', 2], ['c', 3]])
const str = YAML.stringify(map, { version: '1.1' })
expect(str).toBe(`!!omap\n- a: 1\n- b: 2\n- c: 3\n`)
const str2 = YAML.stringify(map)
expect(str2).toBe(`a: 1\nb: 2\nc: 3\n`)
})

test('stringify Array', () => {
const doc = new YAML.Document({ version: '1.1' })
doc.setSchema()
doc.contents = doc.schema.createNode(
[['a', 1], ['b', 2], ['a', 3]],
false,
'!!omap'
)
expect(doc.contents).toBeInstanceOf(YAMLOMap)
expect(String(doc)).toBe(`!!omap\n- a: 1\n- b: 2\n- a: 3\n`)
})
})

describe('!!set', () => {
Expand Down
9 changes: 8 additions & 1 deletion src/schema/_omap.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ import YAMLSeq from './Seq'
import { createPairs, parsePairs } from './_pairs'

export class YAMLOMap extends YAMLSeq {
static tag = 'tag:yaml.org,2002:omap'

constructor() {
super()
this.tag = YAMLOMap.tag
}

toJSON(_, opt) {
const map = new Map()
for (const pair of this.items) {
Expand Down Expand Up @@ -41,8 +48,8 @@ function parseOMap(doc, cst) {
}

function createOMap(schema, iterable, wrapScalars) {
const omap = new YAMLOMap()
const pairs = createPairs(schema, iterable, wrapScalars)
const omap = new YAMLOMap()
omap.items = pairs.items
return omap
}
Expand Down
20 changes: 14 additions & 6 deletions src/schema/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,19 @@ export default class Schema {
return props.join(' ')
}

createNode(item, wrapScalars, onTagObj) {
const tagObj = this.tags.find(
t => t.class && item instanceof t.class && !t.format
)
if (!tagObj) return createNode(item, wrapScalars)
createNode(item, wrapScalars, tag, onTagObj) {
let tagObj
if (tag) {
if (tag.startsWith('!!')) tag = Schema.defaultPrefix + tag.slice(2)
const match = this.tags.filter(t => t.tag === tag)
tagObj = match.find(t => !t.format) || match[0]
if (!tagObj) throw new Error(`Tag ${tag} not found`)
} else {
tagObj = this.tags.find(
t => t.class && item instanceof t.class && !t.format
)
if (!tagObj) return createNode(item, wrapScalars)
}
if (onTagObj) onTagObj(tagObj)
return tagObj.createNode
? tagObj.createNode(this, item, wrapScalars)
Expand All @@ -189,7 +197,7 @@ export default class Schema {
stringify(item, ctx, onComment, onChompKeep) {
let tagObj
if (!(item instanceof Node))
item = this.createNode(item, true, o => (tagObj = o))
item = this.createNode(item, true, null, o => (tagObj = o))
ctx.tags = this
if (item instanceof Pair) return item.toString(ctx, onComment, onChompKeep)
if (!tagObj) tagObj = this.getTagObject(item)
Expand Down

0 comments on commit bd2c190

Please sign in to comment.