Std function for uuid generation

885 views
Skip to first unread message

jesse zhao

unread,
Aug 24, 2018, 2:06:53 AM8/24/18
to Jsonnet
I can't find one in std library. Any other solution?

Dave Cunningham

unread,
Sep 10, 2018, 6:54:52 AM9/10/18
to jesse zhao, Jsonnet
Such a function would be inherently non-hermetic.  The usual advice here is to put some placeholder in the config that is expanded to the UUID by whomever consumes the config. E.g. return something that embeds a unique string identifying the UUID within the context of the config, but not unique across time / re-runs of the config.  That way the consumer can generate the right number of UUIDs for the config.  Also if you need to reconcile the original config with the deployed artifact containing the UUIDs generated when it was deployed, you can do that too.

You can also parameterize the jsonnet run with the UUIDs, but that means you need to know how many there are ahead of time, so it's a bit less general.



On Fri, Aug 24, 2018 at 2:06 AM, jesse zhao <chat2...@gmail.com> wrote:
I can't find one in std library. Any other solution?

--
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+unsubscribe@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/1f3c864d-fff6-41b6-8596-d13212305015%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

jesse zhao

unread,
Sep 18, 2018, 7:06:17 PM9/18/18
to Jsonnet
Initially I used the 1st approach you suggested, it worked fine.

then I came across a different solution per std.native() based on this post

main.py

import uuid
import json
import _jsonnet
import sys

def make_uuid():
 
return str(uuid.uuid4())

native_callbacks
= {
 
'make_uuid': ((), make_uuid),
}

json_str
= _jsonnet.evaluate_file(
 
'test.jsonnet',
 native_callbacks
=native_callbacks,
)
sys
.stdout.write(json_str)

test.jsonnet

local make_uuid() =
 std
.native("make_uuid")();

{
 response
: make_uuid(),
}


On Monday, September 10, 2018 at 3:54:52 AM UTC-7, Dave Cunningham wrote:
Such a function would be inherently non-hermetic.  The usual advice here is to put some placeholder in the config that is expanded to the UUID by whomever consumes the config. E.g. return something that embeds a unique string identifying the UUID within the context of the config, but not unique across time / re-runs of the config.  That way the consumer can generate the right number of UUIDs for the config.  Also if you need to reconcile the original config with the deployed artifact containing the UUIDs generated when it was deployed, you can do that too.

You can also parameterize the jsonnet run with the UUIDs, but that means you need to know how many there are ahead of time, so it's a bit less general.


On Fri, Aug 24, 2018 at 2:06 AM, jesse zhao <chat2...@gmail.com> wrote:
I can't find one in std library. Any other solution?

--
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.
Reply all
Reply to author
Forward
0 new messages