diff --git a/src/pcbs.js b/src/pcbs.js index e49c86e..4eb9610 100644 --- a/src/pcbs.js +++ b/src/pcbs.js @@ -1,4 +1,6 @@ const m = require('makerjs') +const yaml = require('js-yaml') + const u = require('./utils') const a = require('./assert') const prep = require('./prepare') @@ -202,22 +204,24 @@ const footprint = exports._footprint = (points, net_indexer, component_indexer, // combine default value with potential user override let value = params[param_name] !== undefined ? params[param_name] : parsed_def.value const type = parsed_def.type - a.in(type, `${name}.params.${param_name}.type`, [ - 'string', 'number', 'boolean', 'array', 'object', 'net', 'anchor' - ]) // templating support, with conversion back to raw datatypes const converters = { string: v => v, number: v => a.sane(v, `${name}.params.${param_name}`, 'number')(units), - boolean: v => v === 'true' + boolean: v => v === 'true', + array: v => yaml.load(v), + object: v => yaml.load(v), + net: v => v, + anchor: v => yaml.load(v) } - if (a.type(value)() == 'string' && Object.keys(converters).includes(type)) { + a.in(type, `${name}.params.${param_name}.type`, Object.keys(converters)) + if (a.type(value)() == 'string') { value = u.template(value, point.meta) value = converters[type](value) } - // type-specific processing + // type-specific postprocessing if (['string', 'number', 'boolean', 'array', 'object'].includes(type)) { parsed_params[param_name] = value } else if (type == 'net') { diff --git a/src/utils.js b/src/utils.js index de9cb5f..34a852d 100644 --- a/src/utils.js +++ b/src/utils.js @@ -23,7 +23,7 @@ exports.template = (str, vals={}) => { let res = str let shift = 0 for (const match of str.matchAll(regex)) { - const replacement = deep(vals, match[1]) || '' + const replacement = (deep(vals, match[1]) || '') + '' res = res.substring(0, match.index + shift) + replacement + res.substring(match.index + shift + match[0].length) diff --git a/test/pcbs/mock_footprints.yaml b/test/pcbs/mock_footprints.yaml index b46d95f..14e9821 100644 --- a/test/pcbs/mock_footprints.yaml +++ b/test/pcbs/mock_footprints.yaml @@ -1,5 +1,7 @@ points.zones.matrix: mirror: 10 + key: + magic_value: 5 outlines: edge: - what: rectangle @@ -44,3 +46,10 @@ pcbs: params: start: {x: 5, y: 5} end: [[6, 6], [7, 7]] + arrobj_templated: + what: arrobj_test + where: + ref: matrix + params: + start: '{x: {{magic_value}}, y: {{magic_value}}}' + end: '[[6, 6], [7, {{magic_value}}]]' diff --git a/test/pcbs/mock_footprints___pcbs_main.kicad_pcb b/test/pcbs/mock_footprints___pcbs_main.kicad_pcb index 6cde5ae..463dd01 100644 --- a/test/pcbs/mock_footprints___pcbs_main.kicad_pcb +++ b/test/pcbs/mock_footprints___pcbs_main.kicad_pcb @@ -221,6 +221,19 @@ ) + + + (module arrobj_test (layer F.Cu) (tedit 5CF31DEF) + + (at 0 0 0) + + (fp_line (start 5 5) (end 6 6) (layer Dwgs.User) (width 0.05)) +(fp_line (start 5 5) (end 7 5) (layer Dwgs.User) (width 0.05)) + + + ) + + (gr_line (start -9.5 9.5) (end 9.5 9.5) (angle 90) (layer Edge.Cuts) (width 0.15)) (gr_line (start 9.5 9.5) (end 9.5 -9.5) (angle 90) (layer Edge.Cuts) (width 0.15)) (gr_line (start 9.5 -9.5) (end -9.5 -9.5) (angle 90) (layer Edge.Cuts) (width 0.15)) diff --git a/test/unit/utils.js b/test/unit/utils.js index e702791..7ac48b8 100644 --- a/test/unit/utils.js +++ b/test/unit/utils.js @@ -33,6 +33,7 @@ describe('Utils', function() { {longlonglong: 'long', short: 'shortshortshort'} ).should.equal('long_shortshortshort') u.template('{{a.b.c}}', {a: {b: {c: 'deep'}}}).should.equal('deep') + u.template('{x: {{number}}, y: {{number}}}', {number: 5}).should.equal('{x: 5, y: 5}') }) it('eq', function() {