google / go-jsonnet

Apache License 2.0
1.61k stars 231 forks source link

std.prune prunes hidden fields #765

Open mmonaco opened 1 month ago

mmonaco commented 1 month ago

NOTE: This isn't go-specific.

std.prune() prunes hidden fields from objects. Is this intentional? I'm not familiar with any history here...

At the least we should document this, but it feels more like a bug. std.prune() removes whole objects that have std.length() == 0, where the length does not include hidden fields but that seems reasonable. But then, for the objects it does keep which have at least 1 non-hidden field, the hidden members are removed.

Currently in stdast.go:

                "\n",
                "  prune(a)::\n",
                "    local isContent(b) =\n",
                "      if b == null then\n",
                "        false\n",
                "      else if std.isArray(b) then\n",
                "        std.length(b) > 0\n",
                "      else if std.isObject(b) then\n",
                "        std.length(b) > 0\n",
                "      else\n",
                "        true;\n",
                "    if std.isArray(a) then\n",
                "      [std.prune(x) for x in a if isContent($.prune(x))]\n",
                "    else if std.isObject(a) then {\n",
                "      [x]: $.prune(a[x])\n",
                "      for x in std.objectFields(a)\n",
                "      if isContent(std.prune(a[x]))\n",
                "    } else\n",
                "      a,\n",
                "\n",

IMO that last bit should be for x in std.objectFieldsAll(a)\n.

Is a patch welcome? :)