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 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') {

View file

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