Add helpers to access full / bare jid as string

This commit is contained in:
Mickael Remond 2019-06-07 16:25:18 +02:00
parent ae153e1ee5
commit eb2b506e3b
No known key found for this signature in database
GPG Key ID: E6F6045D79965AA3
2 changed files with 36 additions and 0 deletions

11
jid.go
View File

@ -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

View File

@ -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)
}
}