Add helpers to access full / bare jid as string
This commit is contained in:
parent
ae153e1ee5
commit
eb2b506e3b
11
jid.go
11
jid.go
|
@ -50,6 +50,17 @@ func NewJid(sjid string) (*Jid, error) {
|
||||||
return jid, nil
|
return jid, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (j *Jid) Full() string {
|
||||||
|
return j.Node + "@" + j.Domain + "/" + j.Resource
|
||||||
|
}
|
||||||
|
|
||||||
|
func (j *Jid) Bare() string {
|
||||||
|
return j.Node + "@" + j.Domain
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============================================================================
|
||||||
|
// Helpers, for parsing / validation
|
||||||
|
|
||||||
func isUsernameValid(username string) bool {
|
func isUsernameValid(username string) bool {
|
||||||
invalidRunes := []rune{'@', '/', '\'', '"', ':', '<', '>'}
|
invalidRunes := []rune{'@', '/', '\'', '"', ':', '<', '>'}
|
||||||
return strings.IndexFunc(username, isInvalid(invalidRunes)) < 0
|
return strings.IndexFunc(username, isInvalid(invalidRunes)) < 0
|
||||||
|
|
25
jid_test.go
25
jid_test.go
|
@ -59,3 +59,28 @@ func TestIncorrectJids(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFull(t *testing.T) {
|
||||||
|
jid := "test@domain.com/my resource"
|
||||||
|
parsedJid, err := NewJid(jid)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("could not parse jid: %v", err)
|
||||||
|
}
|
||||||
|
fullJid := parsedJid.Full()
|
||||||
|
if fullJid != jid {
|
||||||
|
t.Errorf("incorrect full jid: %s", fullJid)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBare(t *testing.T) {
|
||||||
|
jid := "test@domain.com"
|
||||||
|
fullJid := jid + "/my resource"
|
||||||
|
parsedJid, err := NewJid(fullJid)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("could not parse jid: %v", err)
|
||||||
|
}
|
||||||
|
bareJid := parsedJid.Bare()
|
||||||
|
if bareJid != jid {
|
||||||
|
t.Errorf("incorrect bare jid: %s", bareJid)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue