From 51cc4c07b2a2b6345b1496baac865f5faf955e7d Mon Sep 17 00:00:00 2001 From: Niall Sheridan Date: Fri, 20 Jan 2017 00:52:56 +0000 Subject: Switch from database/sql to sqlx --- server/store/types/string_slice.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 server/store/types/string_slice.go (limited to 'server/store/types/string_slice.go') diff --git a/server/store/types/string_slice.go b/server/store/types/string_slice.go new file mode 100644 index 0000000..81b38c3 --- /dev/null +++ b/server/store/types/string_slice.go @@ -0,0 +1,37 @@ +package types + +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 +} -- cgit v1.2.3