2021-01-25 19:14:45 +01:00
|
|
|
const { LITERAL_MARKER } = require("../helpers/constants")
|
|
|
|
|
|
|
|
const PostProcessorNames = {
|
|
|
|
CONVERT_LITERALS: "convert-literals",
|
|
|
|
}
|
|
|
|
|
2021-01-21 19:08:04 +01:00
|
|
|
/* eslint-disable no-unused-vars */
|
2021-01-21 14:48:23 +01:00
|
|
|
class Postprocessor {
|
|
|
|
constructor(name, fn) {
|
|
|
|
this.name = name
|
|
|
|
this.fn = fn
|
|
|
|
}
|
2021-01-25 19:14:45 +01:00
|
|
|
|
|
|
|
process(statement) {
|
|
|
|
return this.fn(statement)
|
|
|
|
}
|
2021-01-21 14:48:23 +01:00
|
|
|
}
|
2021-01-21 18:56:00 +01:00
|
|
|
|
2021-01-25 19:14:45 +01:00
|
|
|
module.exports.processors = [
|
2021-05-04 12:32:22 +02:00
|
|
|
new Postprocessor(PostProcessorNames.CONVERT_LITERALS, statement => {
|
2021-05-27 12:45:17 +02:00
|
|
|
if (typeof statement !== "string" || !statement.includes(LITERAL_MARKER)) {
|
2021-01-25 19:14:45 +01:00
|
|
|
return statement
|
|
|
|
}
|
2021-03-16 19:16:56 +01:00
|
|
|
const splitMarkerIndex = statement.indexOf("-")
|
|
|
|
const type = statement.substring(12, splitMarkerIndex)
|
|
|
|
const value = statement.substring(
|
|
|
|
splitMarkerIndex + 1,
|
|
|
|
statement.length - 2
|
|
|
|
)
|
2021-01-25 19:14:45 +01:00
|
|
|
switch (type) {
|
|
|
|
case "string":
|
|
|
|
return value
|
|
|
|
case "number":
|
|
|
|
return parseFloat(value)
|
|
|
|
case "boolean":
|
|
|
|
return value === "true"
|
|
|
|
case "object":
|
|
|
|
return JSON.parse(value)
|
|
|
|
}
|
|
|
|
return value
|
2021-01-26 13:43:26 +01:00
|
|
|
}),
|
2021-01-25 19:14:45 +01:00
|
|
|
]
|