oapi-codegen icon indicating copy to clipboard operation
oapi-codegen copied to clipboard

Add Sprig templates for code generation

Open deefdragon opened this issue 1 year ago • 3 comments

I'm planning on taking on #1521 to make it a bit easier to generate your own templates.

To make writing templates easier in general, I'd also like to import Sprig and add it to the functions used for the templates. I've been using them in my templates for some time via a fork. Many of the added functions are VERY useful for navigating the OAPI structs and the extensions Ive added.

Wanted to discuss before I PR if there are any questions or potential issues.

deefdragon avatar Apr 11 '24 03:04 deefdragon

Thanks for the interest! I think right now, I'd prefer to avoid adding the dependency (especially as it'll pull a number of dependencies) and since the work off the back of #1142 I'd love to keep us as lean as possible.

What sort of template functions are you looking for?

It is possible to modify the existing template functions to add the helpers we need?

Or to call oapi-codegen as a library (but needs better documenting in #1487) ?

jamietanna avatar Apr 11 '24 09:04 jamietanna

The most common funcs I've pulled in are dig, haskey, list,and get functions. Specifically I use those to work with the extensions that I have added to my openapi yaml. (fail is useful for validation reasons as well, tho much less necessary as compared to the other 4.)

If I were to generalize, I think the datastructure funcs are the ones that would provide the most given the purpose of oapi-codegen. Most of them are implementable without imports, tho the merge ones are debatable.

deefdragon avatar Apr 17 '24 03:04 deefdragon

Ive gone through the list of functions that may be useful from sprig for dict/list manipulation.

  • list, append, prepend, first, rest, last, initial, reverse, uniq, without, has, slice, concat, chunk
  • dict, get, set, unset, hasKey, pluck, keys, values, pick, omit, dig

Most of the Must variants etc. are not that useful in this lib I think, so those can be excluded. I also excluded the merge funcs as those pulled in an external library, and I dont think people are going to require merging maps.

I have not gone through and looked at the string manipulation functions yet, but I can almost guarantee some of those would be useful. Still, Id rather start out "small" if its believed worth it to any of these at all.

deefdragon avatar Apr 18 '24 02:04 deefdragon