aboutsummaryrefslogtreecommitdiff
path: root/contact.go
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2015-12-10 17:54:35 +0100
committerDimitri Sokolyuk <demon@dim13.org>2015-12-10 17:54:35 +0100
commit48e3361e2c7018acaafd6b90b85d042f8edc620b (patch)
tree85d48a9afd62b366779fdcf110467d0451e670f9 /contact.go
parent54975553c4b67d2b2d9d6edcbf18dbcbdc508fec (diff)
Play with interfaces
Diffstat (limited to 'contact.go')
-rw-r--r--contact.go44
1 files changed, 39 insertions, 5 deletions
diff --git a/contact.go b/contact.go
index ea0b780..dcd5826 100644
--- a/contact.go
+++ b/contact.go
@@ -1,13 +1,47 @@
package acme
-import "encoding"
+import (
+ "encoding"
+ "encoding/json"
+ "errors"
+ "strings"
+)
-type Mail string
-type Phone string
+var errInvalidContact = errors.New("Ivalid contact")
type Contact interface {
encoding.TextMarshaler
}
-func (m Mail) MarshalText() ([]byte, error) { return []byte("mailto:" + m), nil }
-func (ph Phone) MarshalText() ([]byte, error) { return []byte("tel:" + ph), nil }
+type Mail string
+
+func (m Mail) MarshalText() ([]byte, error) {
+ return []byte("mailto:" + m), nil
+}
+
+type Phone string
+
+func (ph Phone) MarshalText() ([]byte, error) {
+ return []byte("tel:" + ph), nil
+}
+
+type Contacts []Contact
+
+func (c *Contacts) UnmarshalJSON(b []byte) error {
+ var tmp []string
+ err := json.Unmarshal(b, &tmp)
+ if err != nil {
+ return err
+ }
+ for _, s := range tmp {
+ switch {
+ case strings.HasPrefix(s, "mailto:"):
+ *c = append(*c, Mail(s[7:]))
+ case strings.HasPrefix(s, "tel:"):
+ *c = append(*c, Phone(s[4:]))
+ default:
+ return errInvalidContact
+ }
+ }
+ return nil
+}