From eb2b506e3b618f1a9cff0b3266c185b48ff168c7 Mon Sep 17 00:00:00 2001 From: Mickael Remond Date: Fri, 7 Jun 2019 16:25:18 +0200 Subject: [PATCH] Add helpers to access full / bare jid as string --- jid.go | 11 +++++++++++ jid_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/jid.go b/jid.go index 05f7274..9d012d9 100644 --- a/jid.go +++ b/jid.go @@ -50,6 +50,17 @@ func NewJid(sjid string) (*Jid, error) { 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 { invalidRunes := []rune{'@', '/', '\'', '"', ':', '<', '>'} return strings.IndexFunc(username, isInvalid(invalidRunes)) < 0 diff --git a/jid_test.go b/jid_test.go index 64efdd5..502d45f 100644 --- a/jid_test.go +++ b/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) + } +}