1
0

Templating bugfix (#86)

This commit is contained in:
Bán Dénes 2023-03-17 10:29:09 +01:00
parent f0d22328cd
commit 9ad080d24c
5 changed files with 34 additions and 7 deletions

View File

@ -1,4 +1,6 @@
const m = require('makerjs') const m = require('makerjs')
const yaml = require('js-yaml')
const u = require('./utils') const u = require('./utils')
const a = require('./assert') const a = require('./assert')
const prep = require('./prepare') const prep = require('./prepare')
@ -202,22 +204,24 @@ const footprint = exports._footprint = (points, net_indexer, component_indexer,
// combine default value with potential user override // combine default value with potential user override
let value = params[param_name] !== undefined ? params[param_name] : parsed_def.value let value = params[param_name] !== undefined ? params[param_name] : parsed_def.value
const type = parsed_def.type 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 // templating support, with conversion back to raw datatypes
const converters = { const converters = {
string: v => v, string: v => v,
number: v => a.sane(v, `${name}.params.${param_name}`, 'number')(units), 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 = u.template(value, point.meta)
value = converters[type](value) value = converters[type](value)
} }
// type-specific processing // type-specific postprocessing
if (['string', 'number', 'boolean', 'array', 'object'].includes(type)) { if (['string', 'number', 'boolean', 'array', 'object'].includes(type)) {
parsed_params[param_name] = value parsed_params[param_name] = value
} else if (type == 'net') { } else if (type == 'net') {

View File

@ -23,7 +23,7 @@ exports.template = (str, vals={}) => {
let res = str let res = str
let shift = 0 let shift = 0
for (const match of str.matchAll(regex)) { 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) res = res.substring(0, match.index + shift)
+ replacement + replacement
+ res.substring(match.index + shift + match[0].length) + res.substring(match.index + shift + match[0].length)

View File

@ -1,5 +1,7 @@
points.zones.matrix: points.zones.matrix:
mirror: 10 mirror: 10
key:
magic_value: 5
outlines: outlines:
edge: edge:
- what: rectangle - what: rectangle
@ -44,3 +46,10 @@ pcbs:
params: params:
start: {x: 5, y: 5} start: {x: 5, y: 5}
end: [[6, 6], [7, 7]] 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}}]]'

View File

@ -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)) (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))

View File

@ -33,6 +33,7 @@ describe('Utils', function() {
{longlonglong: 'long', short: 'shortshortshort'} {longlonglong: 'long', short: 'shortshortshort'}
).should.equal('long_shortshortshort') ).should.equal('long_shortshortshort')
u.template('{{a.b.c}}', {a: {b: {c: 'deep'}}}).should.equal('deep') 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() { it('eq', function() {