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

fix(yaml): add conflict handling #266

Merged
merged 1 commit into from
Apr 21, 2023
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
18 changes: 9 additions & 9 deletions libraries/yaml/source/extensions/$import.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
'use strict'

const { resolve, dirname } = require('node:path')
const { traverse, merge } = require('@toa.io/generic')
const { traverse, add } = require('@toa.io/generic')
const { file } = require('@toa.io/filesystem')

/** @type {toa.yaml.extension} */
const $import = (object, path, yaml) => {
return traverse(object, (node) => {
if ('$import' in node) extend(node, path, yaml)
})
}

/**
* @param {{$import: string}} node
* @param {string} [path]
Expand All @@ -17,14 +24,7 @@ function extend (node, path, yaml) {

delete node.$import

return objects.reduce((node, object) => merge(node, object), node)
}

/** @type {toa.yaml.extension} */
const $import = (object, path, yaml) => {
return traverse(object, (node) => {
if ('$import' in node) extend(node, path, yaml)
})
return objects.reduce((node, object) => add(node, object), node)
}

module.exports = $import
4 changes: 4 additions & 0 deletions libraries/yaml/test/examples/imports/conflicts/a.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$import: ./b.yaml
foo: 1
obj:
bar: 1
5 changes: 5 additions & 0 deletions libraries/yaml/test/examples/imports/conflicts/b.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
foo: 2
baz: 2
obj:
bar: 2
baz: 2
60 changes: 34 additions & 26 deletions libraries/yaml/test/import.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,51 @@
const { resolve } = require('node:path')
const { load } = require('../source')

describe('basic', () => {
it('should import files', async () => {
const path = resolve(__dirname, './examples/imports/basic/c.yaml')
const object = await load(path)
it('should import files', async () => {
const path = resolve(__dirname, './examples/imports/basic/c.yaml')
const object = await load(path)

expect(object).toStrictEqual({
expect(object).toStrictEqual({
foo: {
bar: 1,
baz: 2
},
qux: 'hello'
})
})

it('should import recursively', async () => {
const path = resolve(__dirname, './examples/imports/nested/c.yaml')
const object = await load(path)

expect(object).toStrictEqual({
result: {
foo: {
bar: 1,
baz: 2
},
qux: 'hello'
})
}
})
})

describe('nested', () => {
it('should import files', async () => {
const path = resolve(__dirname, './examples/imports/nested/c.yaml')
const object = await load(path)

expect(object).toStrictEqual({
result: {
foo: {
bar: 1,
baz: 2
},
qux: 'hello'
}
})
it('should not overwrite existing values', async () => {
const path = resolve(__dirname, './examples/imports/conflicts/a.yaml')
const object = await load(path)

expect(object).toStrictEqual({
foo: 1,
baz: 2,
obj: {
bar: 1,
baz: 2
}
})
})

describe('multidoc', () => {
it('should import files', async () => {
const path = resolve(__dirname, './examples/imports/multidoc/a.yaml')
const object = await load.all(path)
it('should import within multidoc files', async () => {
const path = resolve(__dirname, './examples/imports/multidoc/a.yaml')
const object = await load.all(path)

expect(object).toStrictEqual([{ foo: { bar: 1 } }, { qux: 'hello' }])
})
expect(object).toStrictEqual([{ foo: { bar: 1 } }, { qux: 'hello' }])
})