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

Enable all sprig functions in templates #59

Merged
merged 1 commit into from
Feb 23, 2023

Conversation

endorama
Copy link
Member

Non hermetic functions are defined as "impure" functions. The list is
available here: https://github.com/Masterminds/sprig/blob/581758eb7d96ae4d113649668fa96acc74d46e7f/functions.go#L70

We enable them mainly due to date related functions that are very
beneficial to template developers.

@elasticmachine
Copy link

elasticmachine commented Feb 14, 2023

💚 Build Succeeded

the below badges are clickable and redirect to their specific view in the CI or DOCS
Pipeline View Test View Changes Artifacts preview preview

Expand to view the summary

Build stats

  • Start Time: 2023-02-16T10:19:50.293+0000

  • Duration: 3 min 48 sec

Test stats 🧪

Test Results
Failed 0
Passed 65
Skipped 0
Total 65

🤖 GitHub comments

Expand to view the GitHub comments

To re-run your PR in the CI, just comment with:

  • /test : Re-trigger the build.

@@ -31,7 +31,7 @@ func NewGeneratorWithTextTemplate(tpl []byte, cfg Config, fields Fields) (*Gener

state := NewGenState()

templateFns := sprig.HermeticTxtFuncMap()
templateFns := sprig.TxtFuncMap()

templateFns["timeDuration"] = func(duration int64) time.Duration {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd would be nice if we can add timeDuration directly to sprig in the future

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can work in submitting this function to the library, but is not clear to me which is the expected usage. sprig already provides dateModify function, how would this be different?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dateModify, mustDateModify
The dateModify takes a modification and a date and returns the timestamp.

it is unclear to me if it returns a time.Time, what's exactly a "timestamp"? :)
we can test anyway, if so, no need, since we can have

{{- $durationInSecond := generate "durationInSecond" }}
{{- $durationString := cat $durationInSecond "s" | nospace }}
{{- $modifiedTime := $timeField | dateModify $durationString }}
{{ $modifiedTime.Format "2006-01-02T15:04:05.999999Z07:00" }}

otherwise we need a function to generate a time.Duration from an int or string (I could not find any in sprig), since we don't have anything in the fields type that generates a time.Duration (and we probably cannot have, since the type comes from package spec)

@aspacca
Copy link
Contributor

aspacca commented Feb 15, 2023

I think you need to merge main on the branch @endorama

Non hermetic functions are defined as "impure" functions. The list is
available here: https://github.com/Masterminds/sprig/blob/581758eb7d96ae4d113649668fa96acc74d46e7f/functions.go#L70

We enable them mainly due to date related functions that are very
beneficial to template developers.
@endorama endorama force-pushed the sprig-enable-all-functions branch from f96857f to 17a3222 Compare February 16, 2023 10:19
@endorama endorama merged commit 5d1b9a5 into elastic:main Feb 23, 2023
@endorama endorama deleted the sprig-enable-all-functions branch February 23, 2023 11:01
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

Successfully merging this pull request may close these issues.

3 participants