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

Content fields should be interface{}/[]interface{} #10

Open
sheenobu opened this issue Jul 28, 2019 · 2 comments
Open

Content fields should be interface{}/[]interface{} #10

sheenobu opened this issue Jul 28, 2019 · 2 comments

Comments

@sheenobu
Copy link
Owner

If content fields are interface{}, we can (ironically) use type safe structs rather than raw strings for Content fields

@sheenobu sheenobu changed the title Content fields should be interface{} Content fields should be interface{}/[]interface{} Jul 28, 2019
@sheenobu
Copy link
Owner Author

[]interface{} will NOT work... ,innerxml will be ignored the moment you start adding slices

This is the ideal:

var msg xco.Message
msg.Contents.Append(Body("My lord, dispatch; read o'er these articles."));
msg.Contents.Append(xep0184.RequestReceipt{});

=> 

<message>
  <body>My lord, dispatch; read o'er these articles.</body>
  <request xmlns='urn:xmpp:receipts'/>
</message>

@sheenobu
Copy link
Owner Author

oh it works!

https://play.golang.org/p/2afYrvYCf08

package main

import (
	"encoding/xml"
	"os"
)

type A struct {
	Contents interface{} `xml:",innerxml"`
}

type B struct {
	XMLName xml.Name `xml:"B"`
}

type C struct {
	XMLName xml.Name `xml:"C"`
}

func main() {
	var a A
	a.Contents = []interface{}{B{}, C{}}
	var enc = xml.NewEncoder(os.Stdout)
	enc.Encode(&a)
}

=>

<A><B></B><C></C></A>

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

No branches or pull requests

1 participant