Updated for weekly.2012-01-15.
authorChris Jones <christian.jones@sri.com>
Thu, 19 Jan 2012 13:41:42 -0600
changeset 75 03a923eb5c01
parent 74 e619e18dcec3
child 76 caa722ab8a0f
Updated for weekly.2012-01-15.
roster.go
roster_test.go
stream.go
structs.go
structs_test.go
--- a/roster.go	Thu Jan 19 12:25:36 2012 -0600
+++ b/roster.go	Thu Jan 19 13:41:42 2012 -0600
@@ -18,16 +18,16 @@
 // Roster query/result
 type RosterQuery struct {
 	XMLName xml.Name `xml:"jabber:iq:roster query"`
-	Item    []RosterItem
+	Item    []RosterItem `xml:"item"`
 }
 
 // See RFC 3921, Section 7.1.
 type RosterItem struct {
 	XMLName      xml.Name `xml:"item"`
-	Jid          string   `xml:"attr"`
-	Subscription string   `xml:"attr"`
-	Name         string   `xml:"attr"`
-	Group        []string
+	Jid          string   `xml:"jid,attr"`
+	Subscription string   `xml:"subscription,attr"`
+	Name         string   `xml:"name,attr"`
+	Group        []string `xml:"group"`
 }
 
 type rosterClient struct {
--- a/roster_test.go	Thu Jan 19 12:25:36 2012 -0600
+++ b/roster_test.go	Thu Jan 19 13:41:42 2012 -0600
@@ -25,9 +25,13 @@
 		NsRoster + `"><item jid="a@b.c"/></query></iq>`
 	r := strings.NewReader(str)
 	var st Stanza = &Iq{}
-	xml.Unmarshal(r, st)
+	err := xml.Unmarshal(r, st)
+	if err != nil {
+		t.Fatalf("Unmarshal: %v", err)
+	}
+	assertEquals(t, "from", st.GetFrom())
 	m := map[string]func(*xml.Name) interface{}{NsRoster: newRosterQuery}
-	err := parseExtended(st, m)
+	err = parseExtended(st, m)
 	if err != nil {
 		t.Fatalf("parseExtended: %v", err)
 	}
--- a/stream.go	Thu Jan 19 12:25:36 2012 -0600
+++ b/stream.go	Thu Jan 19 13:41:42 2012 -0600
@@ -663,9 +663,9 @@
 			return false
 		}
 		jid := new(JID)
-		if !jid.Set(*jidStr) {
+		if err := jid.Set(*jidStr); err != nil {
 			if Log != nil {
-				Log.Err("Can't parse JID " + *jidStr)
+				Log.Err(err.Error())
 			}
 			return false
 		}
--- a/structs.go	Thu Jan 19 12:25:36 2012 -0600
+++ b/structs.go	Thu Jan 19 13:41:42 2012 -0600
@@ -31,11 +31,11 @@
 
 // XMPP's <stream:stream> XML element
 type stream struct {
-	To      string `xml:"attr"`
-	From    string `xml:"attr"`
-	Id      string `xml:"attr"`
-	Lang    string `xml:"attr"`
-	Version string `xml:"attr"`
+	To      string `xml:"to,attr"`
+	From    string `xml:"from,attr"`
+	Id      string `xml:"id,attr"`
+	Lang    string `xml:"lang,attr"`
+	Version string `xml:"version,attr"`
 }
 
 var _ xml.Marshaler = &stream{}
@@ -43,41 +43,41 @@
 
 // <stream:error>
 type streamError struct {
-	Any  Generic
-	Text *errText
+	Any  Generic `xml:",any"`
+	Text *errText `xml:"text"`
 }
 
 var _ xml.Marshaler = &streamError{}
 
 type errText struct {
-	Lang string `xml:"attr"`
-	Text string `xml:"chardata"`
+	Lang string `xml:"lang,attr"`
+	Text string `xml:",chardata"`
 }
 
 var _ xml.Marshaler = &errText{}
 
 type Features struct {
-	Starttls   *starttls
-	Mechanisms mechs
-	Bind       *bindIq
-	Session    *Generic
-	Any        *Generic
+	Starttls   *starttls `xml:"starttls"`
+	Mechanisms mechs `xml:"mechanisms"`
+	Bind       *bindIq `xml:"bind"`
+	Session    *Generic `xml:"session"`
+	Any        *Generic `xml:",any"`
 }
 
 type starttls struct {
 	XMLName  xml.Name
-	Required *string
+	Required *string `xml:"required"`
 }
 
 type mechs struct {
-	Mechanism []string
+	Mechanism []string `xml:"mechanism"`
 }
 
 type auth struct {
 	XMLName   xml.Name
-	Chardata  string `xml:"chardata"`
-	Mechanism string `xml:"attr"`
-	Any       *Generic
+	Chardata  string `xml:",chardata"`
+	Mechanism string `xml:"mechanism,attr"`
+	Any       *Generic `xml:",any"`
 }
 
 // One of the three core XMPP stanza types: iq, message, presence. See
@@ -106,16 +106,16 @@
 
 // message stanza
 type Message struct {
-	To       string `xml:"attr"`
-	From     string `xml:"attr"`
-	Id       string `xml:"attr"`
-	Type     string `xml:"attr"`
-	Lang     string `xml:"attr"`
-	Innerxml string `xml:"innerxml"`
-	Error    *Error
-	Subject  *Generic
-	Body     *Generic
-	Thread   *Generic
+	To       string `xml:"to,attr"`
+	From     string `xml:"from,attr"`
+	Id       string `xml:"id,attr"`
+	Type     string `xml:"type,attr"`
+	Lang     string `xml:"lang,attr"`
+	Innerxml string `xml:",innerxml"`
+	Error    *Error `xml:"error"`
+	Subject  *Generic `xml:"subject"`
+	Body     *Generic `xml:"body"`
+	Thread   *Generic `xml:"thread"`
 	Nested   []interface{}
 }
 
@@ -124,16 +124,16 @@
 
 // presence stanza
 type Presence struct {
-	To       string `xml:"attr"`
-	From     string `xml:"attr"`
-	Id       string `xml:"attr"`
-	Type     string `xml:"attr"`
-	Lang     string `xml:"attr"`
-	Innerxml string `xml:"innerxml"`
-	Error    *Error
-	Show     *Generic
-	Status   *Generic
-	Priority *Generic
+	To       string `xml:"to,attr"`
+	From     string `xml:"from,attr"`
+	Id       string `xml:"id,attr"`
+	Type     string `xml:"type,attr"`
+	Lang     string `xml:"lang,attr"`
+	Innerxml string `xml:",innerxml"`
+	Error    *Error `xml:"error"`
+	Show     *Generic `xml:"show"`
+	Status   *Generic `xml:"status"`
+	Priority *Generic `xml:"priority"`
 	Nested   []interface{}
 }
 
@@ -142,13 +142,13 @@
 
 // iq stanza
 type Iq struct {
-	To       string `xml:"attr"`
-	From     string `xml:"attr"`
-	Id       string `xml:"attr"`
-	Type     string `xml:"attr"`
-	Lang     string `xml:"attr"`
-	Innerxml string `xml:"innerxml"`
-	Error    *Error
+	To       string `xml:"to,attr"`
+	From     string `xml:"from,attr"`
+	Id       string `xml:"id,attr"`
+	Type     string `xml:"type,attr"`
+	Lang     string `xml:"lang,attr"`
+	Innerxml string `xml:",innerxml"`
+	Error    *Error `xml:"error"`
 	Nested   []interface{}
 }
 
@@ -159,9 +159,9 @@
 type Error struct {
 	XMLName xml.Name `xml:"error"`
 	// The error type attribute.
-	Type string `xml:"attr"`
+	Type string `xml:"type,attr"`
 	// Any nested element, if present.
-	Any *Generic
+	Any *Generic `xml:",any"`
 }
 
 var _ error = &Error{}
@@ -176,8 +176,8 @@
 // Holds an XML element not described by the more specific types.
 type Generic struct {
 	XMLName  xml.Name
-	Any      *Generic
-	Chardata string `xml:"chardata"`
+	Any      *Generic `xml:",any"`
+	Chardata string `xml:",chardata"`
 }
 
 var _ fmt.Stringer = &Generic{}
@@ -195,16 +195,16 @@
 
 // Set implements flag.Value. It returns true if it successfully
 // parses the string.
-func (jid *JID) Set(val string) bool {
+func (jid *JID) Set(val string) error {
 	r := regexp.MustCompile("^(([^@/]+)@)?([^@/]+)(/([^@/]+))?$")
 	parts := r.FindStringSubmatch(val)
 	if parts == nil {
-		return false
+		return errors.New("Can't parse as JID: " + val)
 	}
 	jid.Node = parts[2]
 	jid.Domain = parts[3]
 	jid.Resource = parts[5]
-	return true
+	return nil
 }
 
 func (s *stream) MarshalXML() ([]byte, error) {
--- a/structs_test.go	Thu Jan 19 12:25:36 2012 -0600
+++ b/structs_test.go	Thu Jan 19 13:41:42 2012 -0600
@@ -20,8 +20,8 @@
 func TestJid(t *testing.T) {
 	str := "user@domain/res"
 	jid := &JID{}
-	if !jid.Set(str) {
-		t.Errorf("Set(%s) failed\n", str)
+	if err := jid.Set(str); err != nil {
+		t.Errorf("Set(%s) failed: %s", str, err)
 	}
 	assertEquals(t, "user", jid.Node)
 	assertEquals(t, "domain", jid.Domain)
@@ -29,8 +29,8 @@
 	assertEquals(t, str, jid.String())
 
 	str = "domain.tld"
-	if !jid.Set(str) {
-		t.Errorf("Set(%s) failed\n", str)
+	if err := jid.Set(str); err != nil {
+		t.Errorf("Set(%s) failed: %s", str, err)
 	}
 	if jid.Node != "" {
 		t.Errorf("Node: %v\n", jid.Node)