google / jsonnet

Jsonnet - The data templating language
http://jsonnet.org
Apache License 2.0
6.92k stars 437 forks source link

Added support for field exemptions for std.prune #1167

Open raynix opened 1 month ago

raynix commented 1 month ago

This is to add support for fields which will be exempted and will not be pruned in an object. For example

{
  prune(a, ex=[])::
    local isContent(b) =
      if b == null then
        false
      else if std.isArray(b) then
        std.length(b) > 0
      else if std.isObject(b) then
        std.length(b) > 0
      else
        true;
    if std.isArray(a) then
      [$.prune(x, ex) for x in a if isContent($.prune(x, ex))]
    else if std.isObject(a) then {
      [x]: $.prune(a[x], ex)
      for x in std.objectFields(a)
      if std.member(ex, x) || isContent($.prune(a[x], ex))
    } else
      a,
  containers: {
    local container = {
      image: 'nginx:latest',
      env: [],
      args: [],
      volumeMounts: [
        {
          name: 'test',
          emptyDir: {},
          rubbish: {}
        }
      ],
      resources: {
        requests: {
          cpu: '0.5',
          rubbish: []
        },
        rubbish: [],
      }
    },
    test: $.prune(container, ['emptyDir']),
  },
}

the emptyDir will be preserved as a preferred result.

{
  "containers": {
    "test": {
      "image": "nginx:latest",
      "resources": {
        "requests": {
          "cpu": "0.5"
        }
      },
      "volumeMounts": [
        {
          "emptyDir": {},
          "name": "test"
        }
      ]
    }
  }
}

Fully compatible with existing usage without the exemption list.

google-cla[bot] commented 1 month ago

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.