Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there an equivalent to sprig's set? #1783

Open
thesuperzapper opened this issue Jul 6, 2023 · 9 comments
Open

Is there an equivalent to sprig's set? #1783

thesuperzapper opened this issue Jul 6, 2023 · 9 comments
Assignees
Milestone

Comments

@thesuperzapper
Copy link

I am needing to update the contents of an existing dict, but I see that set does not exist.

In sprig, the following can be used to update $myDict:

$_ := set $myDict "my-key" "my-value"

Is there an equivalent that I am missing in gomplate?

@hairyhenderson
Copy link
Owner

There is not currently an equivalent. I considered asking a set-like function years ago but I seem to recall some sort of technical blocker - I don't recall exactly what. Either way, this is the first time (to my recollection) that someone's requested it.

It's not too complex, and I'm not opposed to it. One factor with sprig's implementation is that it modifies the input; I think I would rather leave the input unmodified, and instead return a copy, so that further uses of the input map are left intact (you can always re-assign the input variable if you do want to overwrite it)

FWIW, the coll.JQ function, while somewhat inscrutable, can already do this:

$ gomplate -i '{{ $m := dict "foo" "bar" }}{{ $m = jq `. += { baz: "qux" }` $m }}m is {{ $m }}'
m is map[baz:qux foo:bar]

LMK what you think!

@hairyhenderson
Copy link
Owner

Oh! I think I remember now why I didn't add a set function - this can be accomplished (with slightly more complexity) with coll.Merge.

So your example can be done like this:

{{ $myDict = merge $myDict (dict "my-key" "my-value") }}

A set function would likely get implemented like this:

func (f *CollFuncs) Set(in map[string]interface{}, key string, val interface{}) (map[string]interface{}, error) {
	return f.Merge(in, map[string]interface{}{key: val})
}

@thesuperzapper
Copy link
Author

@hairyhenderson my problem with the merge approach is actually the fact that it copies the map, which results in massive inefficiency when doing things like iteratively building up a map in a loop (you don't want to know the horrors of what I am doing with gomplate).

Here is sprig's implementation for set and unset.

@thesuperzapper
Copy link
Author

While we are here, if we are allowing maps to be mutable, I would appreciate a deepCopy function!

Here is sprigs implementation for deepCopy.

@thesuperzapper
Copy link
Author

And one more thing! A true get function that does not result in a reflected value like index does would fix some problems.

Here is sprigs implementation of get.

@hairyhenderson
Copy link
Owner

Ah - thanks @thesuperzapper. Updating does indeed make sense for your use-case (if I didn't want to know the horrors, why'd you link it? I couldn't resist looking! 🤣)

Is unset necessary?

I'll see about getting these all added before v4.

@hairyhenderson hairyhenderson added this to the v4.0.0 milestone Jul 8, 2023
@hairyhenderson hairyhenderson self-assigned this Jul 8, 2023
@thesuperzapper
Copy link
Author

@hairyhenderson If we are implementing set, we may as well do unset.

In any case, I'm going to be releasing the first version of deployKF today after a very long time (I almost don't want to think) and I would like to thank you for that great foundation that gomplate provides (Even if I have had to fork it to change a few things).

If you ever need a machine learning platform on Kubernetes, you know what to use!

https://www.deploykf.org/

@thesuperzapper
Copy link
Author

@hairyhenderson was there any movement on getting set into gomplate v4?

@hairyhenderson
Copy link
Owner

not yet - I've made some progress on v4 in general (there's a prerelease now!), but this is still outstanding

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants