描述两组数据之间的转换... "meta-transform"?
Describing a transform between two sets of data... a "meta-transform"?
我正在尝试研究不同的方法来实现我脑海中酝酿的想法,但我不太确定如何搜索我想做的事情。
我有一系列的功能。这些都转换相同的 JSON 格式(模式)(具有不同的数据)。进入不同的对象(不同的模式)。
例如,我可能 JSON 喜欢...
{
heading: "Fogmeister",
type: "person",
body_text: "iOS developer",
sections: [
{
heading: 'Describing a transform between two sets of data... a "meta-transform"?',
type: "post",
body_text: "I'm trying to investigate..."
},
{
// other post
}
]
}
我想将其转换为用户对象,例如...
{
name: "Fogmeister",
profile: "iOS developer",
posts: [
{ title: 'Describing a transform between two sets of data... a "meta-transform"?' },
{ title: 'Other title' }
]
}
但我可能有一些不同的 JSON 比如...
{
heading: 'Describing a transform between two sets of data... a "meta-transform"?',
type: "post",
body_text: "I'm trying to investigate...",
sections: [
{
heading: null,
type: "answer",
body_text: "What you're looking for is..."
},
{
// other answer
}
]
}
我想将其转换为 post 对象,例如...
{
title: 'Describing a transform between two sets of data... a "meta-transform"?',
body: "I'm trying to investigate...",
answers: [
{ body_text: "What you're looking for is..." },
{ body_text: 'Other answer' }
]
}
希望您可以从这个小示例中看出输入架构相同但输出架构可能有很大不同。
我目前有不同的功能来映射每个不同的类型。但我想看看我是否能想出一种方法,我可以 describe
输入和输出之间的映射,然后将其放入一个对象(或其他东西)中。
这样我就可以有一个函数使用这个 Mapping
对象来转换数据。
但是...我不知道这是否已经有了名字。这有点像 meta-transform
,因为我希望能够描述转换而不是自己进行转换。
我可以google 提供有关此类编程的更多信息吗?
我不是在寻找可以执行此操作的代码。更只是 material 我可以围绕这个主题阅读,所以我可以自己做。
谢谢
听起来你在描述一个模式转换器,它读取模式,并从中构建一个抽象语法树。然后你可以阅读那棵树来建立一个你喜欢的任何方式的新模式,允许你从同一个 AST 描述不同的形状。
这个 repo 专业地解释了如何使用 JavaScript 构建编译器。使用模式(因为它是 JSON)会容易得多,因为您所要做的就是解析 JSON 并迭代对象而不是读取文件中的每个字符。
https://github.com/jamiebuilds/the-super-tiny-compiler
请记住,目标是构建可以生成干净的独立 AST 的东西,其他东西可以使用它。祝你好运
一种更实用的方法,使用对象作为模式,使用函数获取映射属性。
function convert(pattern, object) {
return Object.assign({}, ...Object
.entries(object)
.map(([k, v]) => k in pattern
? typeof pattern[k] === 'object'
? { [pattern[k].name]: pattern[k].fn(v) }
: { [pattern[k]]: v }
: {}
)
);
}
var pattern1 = {
heading: 'name',
body_text: 'profile',
sections: { name: 'posts', fn: array => array.map(o => convert({ heading: 'title' }, o)) }
},
input1 = { heading: "Fogmeister", type: "person", body_text: "iOS developer", sections: [{ heading: 'Describing a transform between two sets of data... a "meta-transform"?', type: "post", body_text: "I'm trying to investigate..." }, {}] };
console.log(convert(pattern1, input1));
.as-console-wrapper { max-height: 100% !important; top: 0; }
我正在尝试研究不同的方法来实现我脑海中酝酿的想法,但我不太确定如何搜索我想做的事情。
我有一系列的功能。这些都转换相同的 JSON 格式(模式)(具有不同的数据)。进入不同的对象(不同的模式)。
例如,我可能 JSON 喜欢...
{
heading: "Fogmeister",
type: "person",
body_text: "iOS developer",
sections: [
{
heading: 'Describing a transform between two sets of data... a "meta-transform"?',
type: "post",
body_text: "I'm trying to investigate..."
},
{
// other post
}
]
}
我想将其转换为用户对象,例如...
{
name: "Fogmeister",
profile: "iOS developer",
posts: [
{ title: 'Describing a transform between two sets of data... a "meta-transform"?' },
{ title: 'Other title' }
]
}
但我可能有一些不同的 JSON 比如...
{
heading: 'Describing a transform between two sets of data... a "meta-transform"?',
type: "post",
body_text: "I'm trying to investigate...",
sections: [
{
heading: null,
type: "answer",
body_text: "What you're looking for is..."
},
{
// other answer
}
]
}
我想将其转换为 post 对象,例如...
{
title: 'Describing a transform between two sets of data... a "meta-transform"?',
body: "I'm trying to investigate...",
answers: [
{ body_text: "What you're looking for is..." },
{ body_text: 'Other answer' }
]
}
希望您可以从这个小示例中看出输入架构相同但输出架构可能有很大不同。
我目前有不同的功能来映射每个不同的类型。但我想看看我是否能想出一种方法,我可以 describe
输入和输出之间的映射,然后将其放入一个对象(或其他东西)中。
这样我就可以有一个函数使用这个 Mapping
对象来转换数据。
但是...我不知道这是否已经有了名字。这有点像 meta-transform
,因为我希望能够描述转换而不是自己进行转换。
我可以google 提供有关此类编程的更多信息吗?
我不是在寻找可以执行此操作的代码。更只是 material 我可以围绕这个主题阅读,所以我可以自己做。
谢谢
听起来你在描述一个模式转换器,它读取模式,并从中构建一个抽象语法树。然后你可以阅读那棵树来建立一个你喜欢的任何方式的新模式,允许你从同一个 AST 描述不同的形状。
这个 repo 专业地解释了如何使用 JavaScript 构建编译器。使用模式(因为它是 JSON)会容易得多,因为您所要做的就是解析 JSON 并迭代对象而不是读取文件中的每个字符。
https://github.com/jamiebuilds/the-super-tiny-compiler
请记住,目标是构建可以生成干净的独立 AST 的东西,其他东西可以使用它。祝你好运
一种更实用的方法,使用对象作为模式,使用函数获取映射属性。
function convert(pattern, object) {
return Object.assign({}, ...Object
.entries(object)
.map(([k, v]) => k in pattern
? typeof pattern[k] === 'object'
? { [pattern[k].name]: pattern[k].fn(v) }
: { [pattern[k]]: v }
: {}
)
);
}
var pattern1 = {
heading: 'name',
body_text: 'profile',
sections: { name: 'posts', fn: array => array.map(o => convert({ heading: 'title' }, o)) }
},
input1 = { heading: "Fogmeister", type: "person", body_text: "iOS developer", sections: [{ heading: 'Describing a transform between two sets of data... a "meta-transform"?', type: "post", body_text: "I'm trying to investigate..." }, {}] };
console.log(convert(pattern1, input1));
.as-console-wrapper { max-height: 100% !important; top: 0; }