Skip to content

Commit c94affa

Browse files
committed
stanza: improve docs to mention stream based API
1 parent b289c41 commit c94affa

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

stanza/doc.go

+22-5
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@
1111
// Presence is a publish-subscribe mechanism and is used to broadcast
1212
// availability on the network (sometimes called "status" in chat, eg. online,
1313
// offline, or away).
14-
// IQ (Info-Query) is a request response mechanism for data that requires a
14+
// IQ (Info/Query) is a request response mechanism for data that requires a
1515
// response (eg. fetching an avatar or a list of client features).
1616
//
17-
// Stanzas created using the structs in this package are not guaranteed to be
18-
// valid or enforce specific stanza semantics.
17+
// There are two APIs for creating stanzas in this package, a token based XML
18+
// stream API where the final stanza can be read from an xml.TokenReader, and a
19+
// struct based API that relies on embedding structs in this package into the
20+
// users own types.
21+
// Stanzas created using either API are not guaranteed to be valid or enforce
22+
// specific stanza semantics.
1923
// For instance, using this package you could create an IQ without a unique ID,
2024
// which is illegal in XMPP.
2125
// Packages that require correct stanza semantics, such as the `mellium.im/xmpp`
@@ -26,8 +30,8 @@
2630
//
2731
// The stanza types in this package aren't very useful by themselves. To
2832
// transmit meaningful data our stanzas must contain a payload.
29-
// To add a payload we use composition to create a new struct that contains the
30-
// payload as additional fields.
33+
// To add a payload with the struct based API we use composition to create a new
34+
// struct that contains the payload as additional fields.
3135
// For example, XEP-0199: XMPP Ping defines an IQ stanza with a payload named
3236
// "ping" qualified by the "urn:xmpp:ping" namespace.
3337
// To implement this in our own code we might create a Ping struct similar to
@@ -40,6 +44,19 @@
4044
// Ping struct{} `xml:"urn:xmpp:ping ping"`
4145
// }
4246
//
47+
//
4348
// For details on marshaling and the use of the xml tag, refer to the
4449
// encoding/xml package.
50+
//
51+
// We could also create a similar stanza with the token stream API:
52+
//
53+
// // PingIQ returns an xml.TokenReader that outputs a new IQ stanza with a
54+
// // ping payload.
55+
// func PingIQ(to jid.JID) xml.TokenReader {
56+
// start := xml.StartElement{Name: xml.Name{Space: "urn:xmpp:ping", Local: "ping"}}
57+
// return stanza.WrapIQ(
58+
// &stanza.IQ{To: to, Type: stanza.GetIQ},
59+
// xmlstream.Wrap(nil, start)
60+
// )
61+
// }
4562
package stanza // import "mellium.im/xmpp/stanza"

stanza/example_pingstruct_test.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"log"
1010
"os"
1111

12+
"mellium.im/xmpp/jid"
1213
"mellium.im/xmpp/stanza"
1314
)
1415

@@ -23,14 +24,18 @@ func Example_struct() {
2324
e := xml.NewEncoder(os.Stdout)
2425
e.Indent("", "\t")
2526

27+
j := jid.MustParse("[email protected]/siJo4eeT")
2628
err := e.Encode(PingIQ{
27-
IQ: stanza.IQ{Type: stanza.GetIQ},
29+
IQ: stanza.IQ{
30+
Type: stanza.GetIQ,
31+
To: j,
32+
},
2833
})
2934
if err != nil {
3035
log.Fatal(err)
3136
}
3237
// Output:
33-
// <iq id="" to="" from="" type="get">
38+
// <iq id="" to="[email protected]/siJo4eeT" from="" type="get">
3439
// <ping xmlns="urn:xmpp:ping"></ping>
3540
// </iq>
3641
}

0 commit comments

Comments
 (0)