Skip to content

Commit

Permalink
Got a bunch of field mappings done.
Browse files Browse the repository at this point in the history
  • Loading branch information
manchicken committed Jun 19, 2023
1 parent 126116d commit 1516f22
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 10 deletions.
136 changes: 130 additions & 6 deletions __tests__/lib/fieldMappings.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* @fileoverview This test covers all of the field mappings across versions.
**/

const { mappings, mapField } = require('../../lib/fieldMappings')

describe.each([
Expand Down Expand Up @@ -37,20 +41,140 @@ describe.each([
input: 'app-team-name',
value: { team: 'app-team-name' },
},
// STOPPED HERE
{
version: 'v2',
field: 'contacts',
input: `
name: Testy McTester
type: email
contact: testy@mctester.com
- name: Testy McTester
type: email
contact: testy@mctester.com
`,
value: {
contacts: [
{
name: 'Testy McTester',
type: 'email',
contact: 'testy@mctester.com',
},
],
},
},
{
version: 'v2.1',
field: 'contacts',
input: `
- name: Testy McTester
type: email
contact: testy@mctester.com
`,
value: {
contacts: [
{
name: 'Testy McTester',
type: 'email',
contact: 'testy@mctester.com',
},
],
},
},
{
version: 'v2',
field: 'tags',
input: `
- plain:nospaces
- compat1: space-in-value
- compat2 : space-in-both
`,
value: {
tags: [
'plain:nospaces',
'compat1:space-in-value',
'compat2:space-in-both',
],
},
},
{
version: 'v2.1',
field: 'tags',
input: `
- plain:nospaces
- compat1: space-in-value
- compat2 : space-in-both
`,
value: {
tags: [
'plain:nospaces',
'compat1:space-in-value',
'compat2:space-in-both',
],
},
},
{
version: 'v2',
field: 'links',
input: `
- name: 'first-link'
type: url
url: https://manchicken.io/testing
`,
value: {
links: [
{
name: 'first-link',
type: 'url',
url: 'https://manchicken.io/testing',
},
],
},
},
{
version: 'v2',
field: 'links',
input: `
- name: 'first-link'
type: url
url: https://manchicken.io/testing
provider: Website
`,
value: {
links: [
{
name: 'first-link',
type: 'url',
url: 'https://manchicken.io/testing',
},
],
},
},
{
version: 'v2.1',
field: 'links',
input: `
- name: 'first-link'
type: url
url: https://manchicken.io/testing
provider: Website
`,
value: { contacts: '' },
value: {
links: [
{
name: 'first-link',
type: 'url',
url: 'https://manchicken.io/testing',
provider: 'Website',
},
],
},
},
])('$field:$version', ({ version, field, input, value }) => {
test('mapping', () => {
console.debug({ result: mapField(field, version)(input), value })
console.debug(
JSON.stringify(
{ result: mapField(field, version)(input), value },
undefined,
2,
),
)
expect(mapField(field, version)(input)).toEqual(value)
})
})
32 changes: 28 additions & 4 deletions lib/fieldMappings.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const _ = require('lodash')
const {
expandObjectInputs,
forceArray,
Expand All @@ -10,7 +11,7 @@ const useSharedMappings = (versions, mapper) =>
const mapToUsing = (input, func) => (value) => func(input, value)

const passThru = (input, value) => ({ [input]: value })
const omit = (_input, _value) => ({})
const skipField = (_input, _value) => ({})
const simpleYamlParse = (input, str) => ({ [input]: expandObjectInputs(str) })

/**
Expand All @@ -37,9 +38,32 @@ const mappings = {
mapToUsing('contacts', simpleYamlParse),
),

tags: {},

links: {},
// This tags setup is a little hairy, but the biggest thing
// to keep in mind is that we want a list of strings, made up
// of colon-separated values. Mercifully, this is the same
// for both v2 and v2.1.
tags: useSharedMappings(['v2', 'v2.1'], (input) => ({
tags: forceArray(expandObjectInputs(input)).map((entry) =>
_.isPlainObject(entry)
? _.join(
_.head(_.toPairs(entry)).map((x) =>
typeof x === 'string' ? x.trim() : x,
),
':',
)
: entry,
),
})),

links: {
v2: (input) => ({
links: forceArray(expandObjectInputs(input)).map((x) =>
// v2 doesn't have a provider field
_.omit(x, ['provider']),
),
}),
'v2.1': (input) => ({ links: forceArray(expandObjectInputs(input)) }),
},

integrations: {},

Expand Down

0 comments on commit 1516f22

Please sign in to comment.