aboutsummaryrefslogtreecommitdiff
path: root/server/store/string_slice.go
diff options
context:
space:
mode:
authorNiall Sheridan <nsheridan@gmail.com>2018-08-06 00:25:26 +0100
committerNiall Sheridan <nsheridan@gmail.com>2018-08-07 15:43:39 +0100
commitd0e5f62cf27d3e5c81385342c63d9f42c2eb7e2f (patch)
tree756c8c9e12fd57923ad571d05423598050570629 /server/store/string_slice.go
parent4f2385db4b3d4171fff841594f8c591703e84b0f (diff)
Move StringSlice into the store package
Diffstat (limited to 'server/store/string_slice.go')
-rw-r--r--server/store/string_slice.go37
1 files changed, 37 insertions, 0 deletions
diff --git a/server/store/string_slice.go b/server/store/string_slice.go
new file mode 100644
index 0000000..ac86360
--- /dev/null
+++ b/server/store/string_slice.go
@@ -0,0 +1,37 @@
+package store
+
+import (
+ "database/sql/driver"
+ "encoding/json"
+)
+
+// StringSlice is a []string which will be stored in a database as a JSON array.
+type StringSlice []string
+
+var _ driver.Valuer = (*StringSlice)(nil)
+
+// Value implements the driver.Valuer interface, marshalling the raw value to
+// a JSON array.
+func (s StringSlice) Value() (driver.Value, error) {
+ v, err := json.Marshal(s)
+ if err != nil {
+ return nil, err
+ }
+ return string(v), err
+}
+
+// Scan implements the sql.Scanner interface, unmarshalling the value coming
+// off the wire and storing the result in the StringSlice.
+func (s *StringSlice) Scan(value interface{}) error {
+ if value == nil {
+ s = &StringSlice{}
+ return nil
+ }
+ var err error
+ if v, err := driver.String.ConvertValue(value); err == nil {
+ if v, ok := v.([]byte); ok {
+ err = json.Unmarshal(v, s)
+ }
+ }
+ return err
+}