-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: text/template: add reverse Execute method (parse text into struct) #52473
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
Comments
That is very different from how text/template operates today. This might be better implemented as a completely different package, perhaps using the text/template/parse package. |
The more I think about it, the more I agree. There are several features that seem pretty complicated to me at a glance that I think would be necessary to making this useful outside of extremely specific situations. I added a couple implementation problems to the bottom of the proposal. To expand on the "fuzziness" issue: if I have the following text and I want to parse the two values on the right side:
That's pretty easy. But how can a solution be implemented that allows the lines to appear in an arbitrary order, like reversed for example:
Or what about with another thing in between them:
Or a simpler problem, what if there is just an extra newline in the first example:
Lots of situations to consider that could hinder the usefulness of this feature if these things aren't accounted for. |
This also seems practically impossible to implement in general. If a template has logic like range loops or conditionals, how is the "reverse execution" meant to handle those? |
As @mvdan mentioned, reversing a template is impossible in general. For example, given the template For trivial cases like your first example, named captured groups seem like a good approach. For example, With regards to the line order insensitivity issue you mentioned, for something like that probably you should just go line by line through the input and parse as you go. Seems like it would be a combination |
This proposal has been added to the active column of the proposals project |
Closing as infeasible. |
This proposal has been added to the active column of the proposals project |
Seems almost accomplished by third-party packages such as https://github.com/natekfl/untemplate and https://github.com/laktek/extract-values |
text/template currently allows a developer to pass in a struct to print the struct's fields in an arbitrary format. Here is the example from the pkg.go.dev page that prints "17 items are made of wool"
I propose that a method be added that allows the reverse of this operation: parsing data from a body of text into the given struct, similar to unmarshalling JSON.
The following example parses the
Count
andMaterial
fields out of thetextToParse
string and stores the values insweaters
. It would then print the data to stdout using the same template that was used to parse the data.(
ReverseExecute
is a bad name for this method and should be changed.)Compared to alternative approaches, such as parsing values out of a string using regex, this has the benefit of using one thing (a template) to perform both the parsing and writing operations. Passing a struct into this method also removes much of the manual work and wheel re-invention a developer would need to go through to parse arbitrary text into a struct, with regards to type checks and casting.
Potential issues for discussion:
The text was updated successfully, but these errors were encountered: