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

proposal: encoding/xml: Unmarshal comment with tag #39357

Closed
LinWanCen opened this issue Jun 2, 2020 · 6 comments
Closed

proposal: encoding/xml: Unmarshal comment with tag #39357

LinWanCen opened this issue Jun 2, 2020 · 6 comments

Comments

@LinWanCen
Copy link

What did you do?

<mapper namespace="TEST">

  <!-- Multi-table -->
  <select id="MULTI" parameterType="java.util.Map" resultType="Map">
    SELECT * FROM information_schema.COLUMNS, COLUMNS, ${schema}.TABLES
  </select>

  <!-- Join -->
  <select id="JOIN" parameterType="java.util.Map" resultType="Map">
    SELECT * FROM information_schema.COLUMNS JOIN TABLES
  </select>
<mapper namespace="TEST">
type (
	Mapper struct {
		XMLName   xml.Name
		Namespace string `xml:"namespace,attr"`
		Select    []sql  `xml:"select"`
		Insert    []sql  `xml:"insert"`
		Update    []sql  `xml:"update"`
		Delete    []sql  `xml:"delete"`
	}

	sql struct {
		Comment string `xml:",comment"`
		Id      string `xml:"id,attr"`
		SQL     string `xml:",innerxml"`
	}
)

func test(path string)  {
	model := Mapper{}
	xml.Unmarshal(util.ReadByte(path), &model)
}

What did you expect to see?

Mapper.Select[0].Comment == "Multi-table"
Mapper.Select[1].Comment == "Join"

if

	Mapper struct {
		XMLName   xml.Name
		Namespace string `xml:"namespace,attr"`
		Comment []string `xml:"select,comment"`
		Select    []sql  `xml:"select"`
		Insert    []sql  `xml:"insert"`
		Update    []sql  `xml:"update"`
		Delete    []sql  `xml:"delete"`
	}

then

Mapper.Comment[0] == "Multi-table"
Mapper.Comment[1] == "Join"

What did you see instead?

Mapper.Select[0].Comment == ""
Mapper.Select[1].Comment == ""

if

	Mapper struct {
		XMLName   xml.Name
		Namespace string `xml:"namespace,attr"`
		Comment string `xml:",comment"`
		Select    []sql  `xml:"select"`
		Insert    []sql  `xml:"insert"`
		Update    []sql  `xml:"update"`
		Delete    []sql  `xml:"delete"`
	}

then

Mapper.Comment  == "Multi-table Join"
@gopherbot gopherbot added this to the Proposal milestone Jun 2, 2020
@rsc rsc added this to Incoming in Proposals (old) Jun 10, 2020
@rsc
Copy link
Contributor

rsc commented Jul 14, 2021

I understand this request to be asking for a way to collect comment text in XML unmarshaling, same as the ability to collect inner CDATA text. That's interesting but I am not sure whether we should go down that road. If there is any data structure to unmarshal in the XML, it seems like it should be in non-comment form.

It would be especially problematic to have to attach comments to the XML elements that follow them, as in the example (<!-- Multi-table --> and <!-- Join --> are in the same overall XML element but are being attached to sibling elements instead of the parent!). I don't think this is tenable for encoding/xml to take on.

Note that using xml.Token it is possible to read the data stream and do whatever you like. If you know that there is a stream of comment/object/comment/object/... then you can use an xml.Decoder to use Decode.Token to get the comment, then Decoder.Decode to get the object, and repeat.

@rsc rsc changed the title Proposal: encoding/xml Unmarshal comment with tag proposal: encoding/xml: Unmarshal comment with tag Jul 14, 2021
@rsc rsc moved this from Incoming to Active in Proposals (old) Jul 14, 2021
@rsc
Copy link
Contributor

rsc commented Jul 14, 2021

This proposal has been added to the active column of the proposals project
and will now be reviewed at the weekly proposal review meetings.
— rsc for the proposal review group

@rsc
Copy link
Contributor

rsc commented Jul 21, 2021

Based on the discussion above, this proposal seems like a likely decline.
— rsc for the proposal review group

@rsc rsc moved this from Active to Likely Decline in Proposals (old) Jul 21, 2021
@LinWanCen
Copy link
Author

LinWanCen commented Jul 21, 2021

There may not be other languages ​​that support parsing comments. Golang is the only one I know, but connecting comments together is not very practical.

Developers can do it through complex coding, but this is a bit difficult for ordinary people.

I think this way of parsing is similar to parsing documentation comments on functions, which is very elegant and practical.

@LinWanCen
Copy link
Author

LinWanCen commented Jul 21, 2021

如果在 XML 中有任何要解组的数据结构,它似乎应该是非注释形式。

This is indeed the case. My demand actually comes from parsing the stock code written by others in a large project. They use this kind of annotation method to describe the service configuration. I need to find all the services and bring an annotation explanation. I started I didn't have time to study how to write it. In order to realize it quickly, regular expressions were used at the time, but it was quite complicated😄.
I very much hope that this problem can be solved simply and without bugs.

@rsc rsc moved this from Likely Decline to Declined in Proposals (old) Jul 28, 2021
@rsc
Copy link
Contributor

rsc commented Jul 28, 2021

No change in consensus, so declined.
— rsc for the proposal review group

@rsc rsc closed this as completed Jul 28, 2021
@golang golang locked and limited conversation to collaborators Jul 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
No open projects
Development

No branches or pull requests

3 participants