Created
August 1, 2014 22:49
-
-
Save srfrog/e3ecf0fe10d5a7a11997 to your computer and use it in GitHub Desktop.
StringSlice is used to scan Postgresql's arrays into Go's string arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// StringSlice is used to scan Postgresql's arrays into Go's string arrays | |
type StringSlice []string | |
func (s *StringSlice) Scan(src interface{}) error { | |
b, ok := src.([]byte) | |
if !ok { | |
return error(errors.New("Scan src was not []bytes")) | |
} | |
// removes all quotes and nesting | |
// XXX: this assumes "{} are not part of the values | |
cleanUp := func(r rune) rune { | |
if r == '"' || r == '{' || r == '}' { | |
return -1 | |
} | |
return r | |
} | |
str := strings.Map(cleanUp, string(b)) | |
strArr := strings.Split(str, ",") | |
(*s) = StringSlice(strArr) | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment