jsonnet-modifiers - A new Jsonnet library for conveniently modifying nested values

465 views
Skip to first unread message

Stanisław Barzowski

unread,
Feb 28, 2019, 3:18:22 PM2/28/19
to Jsonnet
Inspired by some questions on Stack Overflow, I was thinking about a better way of changing deep nested structures.

For example take a look at these cases:

I wrote a tiny library which allows to do this sort of thing more easily. It's also pretty extensible - it's basically a pattern of doing deep modifications and a few convenience functions.

It could perhaps be a good fit for stdlib one day, but I want to collect some feedback and ideas for improving the API before proposing including it.

Please let me know what you think. Here's the link: https://github.com/sbarzowski/jsonnet-modifiers

Brett Viren

unread,
Feb 28, 2019, 5:04:45 PM2/28/19
to Stanisław Barzowski, Jsonnet
Stanisław Barzowski <stanislaw...@gmail.com> writes:

> Please let me know what you think. Here's the link: https://github.com/sbarzowski/jsonnet-modifiers

Just last week I was wishing for something exactly like this. Thanks!

-Brett.
signature.asc

Dan Compton

unread,
Feb 28, 2019, 10:55:18 PM2/28/19
to Brett Viren, Stanisław Barzowski, Jsonnet
This is an extremely useful contribution and should be included in the standard library.  One example I can provide where
it's useful is for the modification of attributes of an ECS Task Definition JSON (e.g. the envoy container) in the snippet below.


      "containerDefinitions": [
      {
         "dockerLabels": {
            "SERVICE_80_NAME": "someService-api-http",
            "SERVICE_80_TAGS": "service=someService-api-http"
         },
         "environment": [
            {
               "name": "DEST_DB",
               "value": "db.service.consul:27000"
            }
         ],
         "essential": true,
         "image": "someRepo/someServiced:0.4.2",
         "links": [
            "envoy:db.service.consul"
         ],
         "memory": 218,
         "mountPoints": [ ],
         "name": "someService",
         "portMappings": [
            {
               "containerPort": 80,
               "hostPort": 0,
               "protocol": "tcp"
            }
         ]
      },
      {
         "dockerLabels": {
            "SERVICE_9901_NAME": "envoy",
            "SERVICE_9901_TAGS": "service=someService-api-http"
         },
         "environment": [
                 ...
         ],
         "essential": true,
         "image": "someRepo/mongoproxy-envoy:versio",
         "memory": 100,
         "name": "envoy",
         "portMappings": [
            {
               "containerPort": 9901,
               "hostPort": 0,
               "protocol": "tcp"
            }
         ],
         "volumesFrom": [ ]
      }
   ],
   "family": "someService-someENV",
   "placementConstraints": [ ],
   "volumes": [ ]
}

Related: It would also be useful to have functions to test existence of deeply nested fields as well as retrieve deeply nested values including those nested in objects in arrays nested within objects -- for example, if I wanted to obtain the value of the
envoy container or to determine if the envoy container exists given the json above.   The following does not cover the array case, but does not require nested std.objectHas statements. (please let me know if there's a better way to do this in jsonet -- it's possible
via the jsonPath query $.containerDefinitions[?(@.name=='envoy']  if we had something similar then perhaps we could perform the query, patch the object, and subsequently patch the original object)

  // Returns value at path or default, if path does not
  // exist in the specified obj.
  objectValueAtPath(obj, path, default=null)::
    assert std.isObject(obj) == true;
    assert std.isArray(path) == true;
    if std.length(path) <= 0 then
      default
    else if std.length(path) == 1 then
      if std.objectHas(obj, path[0]) then
        obj[path[0]]
      else
        default
    else
      if !std.objectHas(obj, path[0]) then
        default
      else
        zstd.objectValueAtPath(obj[path[0]], path[1:], default) tailstrict,


--
You received this message because you are subscribed to the Google Groups "Jsonnet" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jsonnet+u...@googlegroups.com.
To post to this group, send email to jso...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jsonnet/867edj1sed.fsf%40hierocles.phy.bnl.gov.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages