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: Add RawMessage type #25683

Closed
OuSatoru opened this Issue Jun 1, 2018 · 6 comments

Comments

Projects
None yet
6 participants
@OuSatoru
Copy link

OuSatoru commented Jun 1, 2018

Currently encoding/xml does not have a RawMessage type like encoding/json, this could be used like

type Info struct {
	XMLName xml.Name `xml:"ROOT"`
	A       string
	B       xml.RawMessage
}

func main() {
	a := `<?xml version="1.0" encoding="utf-8"?>
	<ROOT>
	<A>int</A>
	<B>123</B>
	</ROOT>`
	b := `<?xml version="1.0" encoding="utf-8"?>
	<ROOT>
	<A>string</A>
	<B>abcd</B>
	</ROOT>`
	var info Info
	xml.Unmarshal([]byte(a), &info)
	switch info.A {
	case "int":
		var i int
		xml.Unmarshal(info.B, &i)
		fmt.Println(info, i)
	case "string":
		var s string
		xml.Unmarshal(info.B, &s)
		fmt.Println(info, s)
	}
}

B could be some other struct.

@mvdan

This comment has been minimized.

Copy link
Member

mvdan commented Jun 1, 2018

What exactly is this issue about? It seems to me like people could declare json.RawMessage themselves and use it directly, without it needing to be part of encoding/json. So the same should apply to encoding/xml - you should be able to declare your own and use it.

Are you suggesting that encoding/xml.RawMessage be added to the standard library? Or are you asking for help on implementing XMLRawMessage yourself?

@mvdan mvdan added the WaitingForInfo label Jun 1, 2018

@OuSatoru

This comment has been minimized.

Copy link
Author

OuSatoru commented Jun 1, 2018

@mvdan Exactly I'm suggesting someone to add encoding/xml.RawMessage, but if I want to use it now, can I implement a simple version?

@mvdan

This comment has been minimized.

Copy link
Member

mvdan commented Jun 1, 2018

If you want to propose the addition of encoding/xml.RawMessage, you should use a proposal. See https://github.com/golang/proposal.

but if I want to use it now, can I implement a simple version?

Yes, as said above, I don't see why you wouldn't be able to declare that type yourself and use it.

@OuSatoru OuSatoru changed the title RawMessage for encoding/xml? proposal: encoding/xml: Add RawMessage type Jun 1, 2018

@gopherbot gopherbot added this to the Proposal milestone Jun 1, 2018

@gopherbot gopherbot added the Proposal label Jun 1, 2018

@OuSatoru

This comment has been minimized.

Copy link
Author

OuSatoru commented Jun 1, 2018

@mvdan edit it as a proposal

@kardianos

This comment has been minimized.

@rsc

This comment has been minimized.

Copy link
Contributor

rsc commented Jun 4, 2018

Like @mvdan says, you can implement this easily yourself. It doesn't need to be in encoding/xml. Also xml is effectively frozen until we have some more time to think about systemic issues in the API. Since this functionality does not require a change (it can be done outside the package), that's the best place for it.

@rsc rsc closed this Jun 4, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.