-
Notifications
You must be signed in to change notification settings - Fork 14
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
Highlight field errors in generation when they happen #58
Conversation
💔 Tests Failed
Expand to view the summary
Build stats
Test stats 🧪
Test errorsExpand to view the tests failures
|
@@ -40,12 +40,12 @@ func NewGeneratorWithTextTemplate(tpl []byte, cfg Config, fields Fields) (*Gener | |||
templateFns["generate"] = func(field string) interface{} { | |||
bindF, ok := fieldMap[field] | |||
if !ok { | |||
return "" | |||
return "<missing field>" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking about this, I'd rather send a null
, so if the value is assigned to a variable that's expected to be of another type rather than a string, and further functions are applied the template will most likely fail to render
the best would be if we could avoid any kind of failure in the generate
body
value, err := bindF(state, nil)
can be indeed refactored without returning any error (see here
we cannot do anything about bindF, ok := fieldMap[field]
, but maybe let's fail the whole command, since having in a template generate "fieldNotInFieldsYaml"
is not a transient error but a bug in the template
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather send a null
Returning nil
does not halt execution, just renders an empty template.
but maybe let's fail the whole command,
I tried panicking from generate
function, but template.Execute
recover from panics (see here, go1.19).
I've not been able to have the panic triggered in generate
to escape this recovery, due to this I've found no way to properly halt execution.
value, err := bindF(state, nil)
I will remove the commit from this PR, we will address this in a future update.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried panicking from
generate
function, buttemplate.Execute
recover from panics (see here, go1.19).I've not been able to have the panic triggered in
generate
to escape this recovery, due to this I've found no way to properly halt execution.
we can have an error channel and close it on !ok
, then in the emit()
we add a select with <-errorChan
after parsing the template, that returns an error, and a default that return nil
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can have an error channel and close it on
!ok
, then in theemit()
we add a select with<-errorChan
after parsing the template, that returns an error, and a default thatreturn nil
please, @endorama , check my proposal: 2e1bc5e#diff-0fc87380e8715fdb1a33c600e6513325ba82801e849851c5856064b7beac59b8R120
In case `generate()` is being used in the template on a missing field the current behaviour of returning an empty string hides the problem to the template developer. As it is not expected for the template to try generating a missing field, this commit returns a known string highlighting the problem.
In case there in an error in the bind function for generating a value for a field, instead of returning an empty string, return a fixed known string that highlight the issue.
This reverts commit ef2fd14.
69c05e6
to
3aa3d48
Compare
Closing this as the requested improvements are part of #51 |
This PR aims at helping template developers in 2 situations:
generate
on a missing fieldIn case
generate()
is being used in the template on a missing fieldthe current behaviour of returning an empty string hides the problem to
the template developer.
As it is not expected for the template to try generating a missing
field, this commit returns a known string highlighting the problem.
In case there in an error in the bind function for generating a value
for a field, instead of returning an empty string, return a fixed known
string that highlight the issue.