timoni icon indicating copy to clipboard operation
timoni copied to clipboard

Parse arbitrary JSON or YAML documents as values

Open errordeveloper opened this issue 2 years ago • 7 comments

This would be a follow-up feature to #9. It would be useful to be able to read a JSON or YAML document and pick some paths from it.

I was trying to do this:

package main

import ( 
    "encoding/json"
    "tool/file"
)

imagesFile: file.Read & {
    filename: "images.json"
    contents: string
}

images: json.Unmarshal(imagesFile.contents)

values: {
    imageDigest: "\(images[0].digest)"
}

But it turns out tool/file only works in conjunction with tool/cli, i.e. via cue cmd, as it compromises hermeticity.

So if there could be a way to achieve this in Timoni, it'd be great. It may require creating a custom Go extension to CUE, which would be possible. I do wonder how the hermeticity problem can be addressed.

Is the way JSON and YAML values work as of #51?

I suppose the way to spin this is that results will be reproducible as long as inputs don't change and that CUE prevents users from compromising hermiticity by not allowing reading files and generating either time-variant or random values from their code.

/cc @squaremo

errordeveloper avatar Apr 24 '23 21:04 errordeveloper

We could extend the Runtime API with support for querying JSON and YAML files.

Something like:

runtime: {
	apiVersion: "v1alpha1"
	name:       "production"
	values: [
		{
			query: "file:///absolute/path/to/data.json"
			for: {
				"id":   "obj.user.id"
				"user": "obj.user.info.name"
			}
		},
		{
			query: "https://example.com/data.yaml"
			for: {
				"id":   "obj.user.id"
				"user": "obj.user.info.name"
			}
		},
	]
}

stefanprodan avatar Sep 22 '23 15:09 stefanprodan

I guess I'm running into a similar need...I'm experimenting with converting a large kustomize project, and one of things we use heavily are configmap generators, which create a configmap from the contents of a file, relative to the location of the kustomization file. It looks like in cue, I would need to embed the data in these files inside a cue file wrapper.

That's a little awkward for a couple reasons: 1. embedding big xml or text documents inside cue is kind of ugly, and 2. my editors don't know to apply appropriate syntax highlighting and editing tools to embedded data.

I'm sort of surprised cue doesn't support this out of the box. I get the argument for being hermetic, but it seems to be that locating resource files at relative positions to the cue files wouldn't really violate that rule...I mean, the cue tool has to resolve the locations of other .cue files within the a package...

ansel1 avatar Apr 02 '24 21:04 ansel1

That's a little awkward for a couple reasons: 1. embedding big xml or text documents inside cue is kind of ugly, and 2. my editors don't know to apply appropriate syntax highlighting and editing tools to embedded data.

@ansel1 I think what you listed are very cosmetic reason, the biggest reason why embedding sucks is because it's hard to update, if it's a separate file you can copy it verbatim and validate easily, which is much harder once it's embedded in another file. In some cases quoting or some other kind of reformatting may need be applied as well.

errordeveloper avatar Apr 02 '24 21:04 errordeveloper

Found a discussion along similar lines in cue's forums. https://github.com/cue-lang/cue/discussions/1582. Basically suggesting something like go's embed feature.

ansel1 avatar Apr 03 '24 01:04 ansel1

Basically suggesting something like go's embed feature.

That does broadly make sense, however in Go there is a compilation step, but CUE doesn't have it, albeit Timoni could potentially do it, but that would be rather akin to runtime API, which is what Stefan is suggesting and it makes a lot of sense to me.

errordeveloper avatar Apr 03 '24 09:04 errordeveloper

Wouldn't cue export be the equivalent?

ansel1 avatar Apr 03 '24 15:04 ansel1