为什么我需要在pkl中多次说“new”(特别是为了获取json数组)?

问题描述 投票:0回答:1

这个PKL代码:

class Check_run_delete_olds {
  hidden days: Int
  hidden shortname: String
  name = "\(shortname) Runs OK And At Least Once In The Past \(days) Day(s)"
  filters: Any
}

imapserver = "imap.gmail.com"
login = "[email protected]"
password = "nope"
handlers = new Dynamic {
  new Check_run_delete_olds {
    shortname = "foo"
    days = 3
    filters = new Dynamic {
      new { Match = new { From = " <[email protected]>$" } }
      new { Match = new { Subject = "^Cron " } }
      new { Match = new { Subject = "puppet agent" } }
    }
  }
}

正是我想要的,即产生以下输出:

$ ./pkl eval -f json test.pkl
{
  "imapserver": "imap.gmail.com",
  "login": "[email protected]",
  "password": "nope",
  "handlers": [
    {
      "name": "foo Runs OK And At Least Once In The Past 3 Day(s)",
      "filters": [
        {
          "Match": {
            "From": " <[email protected]>$"
          }
        },
        {
          "Match": {
            "Subject": "^Cron "
          }
        },
        {
          "Match": {
            "Subject": "puppet agent"
          }
        }
      ]
    }
  ]
}

但是哇,我好像经常说“新”。 特别是,似乎我必须到处说“new”才能获得 json 对象数组。

是否有一些更干净/更惯用的方法来获得相同的输出?

pkl
1个回答
0
投票

这是减少了

new
数量的版本。

class Check_run_delete_olds {
  hidden days: Int
  hidden shortname: String
  name = "\(shortname) Runs OK And At Least Once In The Past \(days) Day(s)"
  filters: Any
}

imapserver = "imap.gmail.com"
login = "[email protected]"
password = "nope"
handlers {
  (Check_run_delete_olds) {
    shortname = "foo"
    days = 3
    filters = (Dynamic) {
      new { Match { From = " <[email protected]>$" } }
      new { Match { Subject = "^Cron " } }
      new { Match { Subject = "puppet agent" } }
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.