首页 > 解决方案 > 如何使用 GO 自动生成 JavaScript 代码

问题描述

我需要根据输入的规则集在 GO 中自动生成 JS 代码。例如,假设我在 GO 中有一个应用程序,它接收包含一组规则的 Json:

{
  "enabled": true,
  "rulesLimit": 3,
  "Rules": [
    {
      "name": "Not Dave or Ted",
      "enabled": true,
      "id": 1,
      "trigger": {
        "operator": "AND",
        "conditions": [
          {
            "operator": "NOT EQUAL",
            "value": "Dave"
          },
          {
            "operator": "NOT EQUAL",
            "value": "Ted"
          }
        ]
      }
    },
    {
      "name": "Only from home",
      "enabled": true,
      "id": 2,
      "trigger": {
        "conditions": [
          {
            "operator": "EQUAL",
            "urlValue": "Home"
          }
        ]
      }
    }
]
}

该应用程序需要自动生成一个包含请求规则的函数的 JS 代码:

function applyRules(){     
    if  (person[name] === "Dave" || person[name] === "Ted") {         
        return     
    }          
    if  (person[address] !== "Home") {         
        return     
    } 
} 
applyRules();

我目前正在研究什么是处理它的最佳方法。起初我考虑使用Go Template将模板与占位符一起使用,但是当我开始实现它时,复杂性有点高。有没有我可以研究的其他方法、工具或方法可以满足我的需要?谢谢

标签: javascriptgo

解决方案


推荐阅读