JID Full() vs String() and docs.

This commit is contained in:
Matt Goodall 2012-07-06 10:59:13 +01:00
parent b908366544
commit 6da9c080d4
1 changed files with 21 additions and 1 deletions

View File

@ -5,12 +5,23 @@ import (
"strings"
)
/*
Jabber Identifier - uniquely identifies an individual entity in a XMPP/Jabber
network.
*/
type JID struct {
// Local component e.g. the alice of alice@example.com/foo.
Local string
// Domain component, e.g. the example.com of alice@example.com/foo for a
// client or the whole JID of a component.
Domain string
// Resource component, e.g. the foo of alice@example.com/foo.
Resource string
}
// Return the "bare" JID, i.e. no resource component.
func (jid JID) Bare() string {
if jid.Local == "" {
return jid.Domain
@ -18,13 +29,20 @@ func (jid JID) Bare() string {
return fmt.Sprintf("%s@%s", jid.Local, jid.Domain)
}
func (jid JID) String() string {
// Return the full JID as a string.
func (jid JID) Full() string {
if jid.Resource == "" {
return jid.Bare()
}
return fmt.Sprintf("%s@%s/%s", jid.Local, jid.Domain, jid.Resource)
}
// Return full JID as a string.
func (jid JID) String() string {
return jid.Full()
}
// Parse a string into a JID structure.
func ParseJID(s string) (jid JID, err error) {
if parts := strings.SplitN(s, "/", 2); len(parts) == 1 {
@ -43,3 +61,5 @@ func ParseJID(s string) (jid JID, err error) {
return
}
// BUG(matt): ParseJID should fail for incorrectly formatted JIDs.