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

Pull in sprig functions? #283

Closed
docwhat opened this issue Apr 12, 2018 · 12 comments
Closed

Pull in sprig functions? #283

docwhat opened this issue Apr 12, 2018 · 12 comments

Comments

@docwhat
Copy link

docwhat commented Apr 12, 2018

Sprig looks like it has a lot of really cool functions. What about stealing using their library of functions in gomplate?

@hairyhenderson
Copy link
Owner

@docwhat weird coincidence - I was just thinking about that yesterday 😂...

There's definitely some overlap, there's lots of things in Sprig that aren't yet in gomplate. Unfortunately the functions themselves aren't exported, so really we'd just be talking about copying the same functions into gomplate.

An option would be to use the FuncMap that Sprig exports, and simply add that into the chain, but I'm not a big fan of that since it "pollutes" the root namespace.

Basically, I think the right approach would be to be more methodical and bring in the specific functions that are missing, within the appropriate namespace.

Or if the Sprig maintainers could be convinced to export all of the functions 🤔...

@docwhat
Copy link
Author

docwhat commented Apr 12, 2018

Can't you just walk FuncMap or TxtFuncMap and copy what you want into a new map with whatever name you want? Everything is exported (indirectly) via the Map.

I didn't know if you knew that .Funcs(someFuncMap) can be called multiple times. I just discovered that.

PS: temple got Sprig functions before you. 😝

@hairyhenderson
Copy link
Owner

PS: temple got Sprig functions before you. 😝

😂

@hairyhenderson
Copy link
Owner

Can't you just walk FuncMap or TxtFuncMap and copy what you want into a new map with whatever name you want? Everything is exported (indirectly) via the Map.

That's a really good point - hadn't considered that! I'll take a look to see how elegant (or inelegant) this can be.

I didn't know if you knew that .Funcs(someFuncMap) can be called multiple times. I just discovered that.

Yup!

@hairyhenderson
Copy link
Owner

Can't you just walk FuncMap or TxtFuncMap and copy what you want into a new map with whatever name you want? Everything is exported (indirectly) via the Map.

... I'll take a look to see how elegant (or inelegant) this can be.

So... Pretty inelegant, it turns out. With the exception of the (mostly legacy) functions in the root namespace, the only entries in gomplate's main FuncMap are namespaces like strings. There is no entry for strings.Trim for example, because function names can't include a ..

If I wanted to add the abbrev function to gomplate's strings namespace as strings.Abbrev, I'd have to do something like this:

// Abbrev -
func (f *StringFuncs) Abbrev(width int, s string) (string, error) {
	a := sprig.GenericFuncMap()["abbrev"]
	switch v := a.(type) {
	case func(int, string) string:
		return v(width, s), nil
	default:
		panic("wat")
	}
}

TBH, this is more trouble than it's worth - especially (in this case) as there's actually an exported util.Abbrev function from https://github.com/Masterminds/goutils...

So I think instead of trying to shoehorn sprig into gomplate, I'll see about adding in equivalent functionality that makes sense.

Thanks for logging the issue though. This is a good source of ideas for new functions!

@djgilcrease
Copy link
Contributor

would not be overly difficult to import the sprig functions as sprig_* not properly namespaced, but not as bad as how they are doing it by default

package funcs

import (
	"github.com/Masterminds/sprig"
	"fmt"
)

func AddSprigFuncs(f map[string]interface{}) {
	for fn, fc := range sprig.TxtFuncMap() {
		f[fmt.Sprintf("sprig_%s", fn)] = fc
	}

}

@hairyhenderson
Copy link
Owner

@djgilcrease true, but IMO it's a bit messy, and doesn't really fit in with the "feel" of the current gomplate functions.

Are there any particular functions that you're missing?

@djgilcrease
Copy link
Contributor

No, I was just commenting on the open task if someone absolutely needed it. I like your method of namespacing the template funcs, might be interesting to split it out into it's own project

@hairyhenderson
Copy link
Owner

Ok, I'm going to close this issue out then. If there's any specific new functions that anyone needs - please open a new issue 🙂

@hickey
Copy link

hickey commented Aug 1, 2022

Not looking to reopen this issue, but the big Sprig function that would be really nice to have access to is default. This would allow default values to be specified or input values to be overridden by other input values.

I am sure that there are probably a couple more that might be useful, but that is the biggie that I see.

@hairyhenderson
Copy link
Owner

@hickey have you seen https://docs.gomplate.ca/functions/conv/#conv-default?

(also please open a new issue if you're looking for new functions, or start a discussion)

@hickey
Copy link

hickey commented Sep 14, 2022

Ah, no I have not seen that. I would not have thought that the function would have been classified as a conversion method. Thanks for the pointer.

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

No branches or pull requests

4 participants