【问题标题】:Describing a transform between two sets of data... a "meta-transform"?描述两组数据之间的转换......“元转换”?
【发布时间】:2019-10-07 22:38:54
【问题描述】:

我正在尝试针对我脑海中酝酿的一个想法研究不同的方法,但我不确定如何搜索我正在尝试做的事情。

我有一系列的功能。这些都转换相同的 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
    }
  ]
}

我想把它转换成一个帖子对象,比如......

{
  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,因为我希望能够描述转换而不是自己进行转换。

我可以用谷歌搜索一下,可以提供有关此类编程的更多信息吗?

我不是在寻找可以做到这一点的代码。我可以阅读更多有关该主题的材料,以便我自己做。

谢谢

【问题讨论】:

  • 我很想看看是否存在这样的映射语言/模式,尽管似乎为了使语言足够复杂以能够处理这些映射,它可能不是特别比在 javascript 中手动做同样的事情要简单
  • @OliverRadini 哦,我毫不怀疑它会很复杂。但希望复杂性可以存在于单个函数/对象/系统中,然后我可以传递一个“相对”简单的 meta-transform 对象,该对象将允许复杂性进行转换。如果是这样,那将允许我相对轻松地向新类型添加新的转换。 ...希望。
  • 有趣的问题。我自己的一些谷歌搜索把我带到了这个答案:stackoverflow.com/a/17413374/628699。该库上的应用程序演示看起来可能就是您所追求的
  • 优秀,开始的地方。谢谢 :D 我会回来告诉你进展如何 :D

标签: javascript metaprogramming transformation


【解决方案1】:

听起来您正在描述模式转译器,它读取模式并从中构建抽象语法树。然后,您可以读取该树以构建您喜欢的任何方式的新模式,从而允许您描述来自同一 AST 的不同形状。

这个 repo 专业地解释了如何使用 JavaScript 构建编译器。使用模式(因为它是 JSON)会容易得多,因为您所要做的就是解析 JSON 并迭代对象,而不是读取文件中的每个字符。

https://github.com/jamiebuilds/the-super-tiny-compiler

请记住,我们的目标是构建可以生成干净独立的 AST 的东西,其他东西可以使用它。祝你好运?

【讨论】:

    【解决方案2】:

    一种更实用的方法,使用对象作为模式,使用函数获取映射属性。

    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; }

    【讨论】:

    • 哦,也很棒!我喜欢使用这样的模式来映射属性的想法。谢谢!
    猜你喜欢
    • 2017-11-30
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-20
    • 1970-01-01
    • 1970-01-01
    • 2020-06-06
    相关资源
    最近更新 更多