From 219713475846ec922a2149c58910ba3d65742370 Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Fri, 21 Sep 2018 10:45:25 -0700 Subject: [PATCH] Implementing #117 - Profile Custom Attribute Map --- model/profile.go | 17 +++++++++++++++++ model/profile_test.go | 6 ++++++ 2 files changed, 23 insertions(+) diff --git a/model/profile.go b/model/profile.go index 1456159..cc26996 100644 --- a/model/profile.go +++ b/model/profile.go @@ -30,6 +30,7 @@ type Profile struct { Ed25519PrivateKey ed25519.PrivateKey OnionPrivateKey *rsa.PrivateKey Groups map[string]*Group + Custom map[string]string lock sync.Mutex } @@ -52,6 +53,7 @@ func GenerateNewProfile(name string) *Profile { p.Contacts = make(map[string]*PublicProfile) p.Contacts[p.Onion] = &p.PublicProfile p.Groups = make(map[string]*Group) + p.Custom = make(map[string]string) return p } @@ -113,6 +115,21 @@ func (p *Profile) GetGroups() []string { return keys } +// SetCustomAttribute allows applications to store arbitrary configuration info at the profile level. +func (p *Profile) SetCustomAttribute(name string, value string) { + p.lock.Lock() + defer p.lock.Unlock() + p.Custom[name] = value +} + +// GetCustomAttribute returns the value of a value set with SetCustomAttribute. If no such value has been set exists is set to false. +func (p *Profile) GetCustomAttribute(name string) (value string, exists bool) { + p.lock.Lock() + defer p.lock.Unlock() + value, exists = p.Custom[name] + return +} + // GetContacts returns an unordered list of contact onions associated with this profile. func (p *Profile) GetContacts() []string { p.lock.Lock() diff --git a/model/profile_test.go b/model/profile_test.go index 62432aa..7b8ac87 100644 --- a/model/profile_test.go +++ b/model/profile_test.go @@ -26,6 +26,12 @@ func TestProfileIdentity(t *testing.T) { t.Errorf("alice should be only contact: %v", alice.GetContacts()) } + alice.SetCustomAttribute("test", "hello world") + value, _ := alice.GetCustomAttribute("test") + if value != "hello world" { + t.Errorf("value from custom attribute should have been 'hello world', instead was: %v", value) + } + t.Logf("%v", alice) }