pqarrays
pqarrays/array.go
Fix bug parsing empty arrays, make golint and go vet happy. Add comments to make golint happy. Also, because comments are a good thing to have. Turn += 1 and -= 1 into ++ and --, respectively, so golint will be happy. Fix an improperly formated errorf, where a rune was being treated as a string. Thanks, go vet! Fix whitespace parsing, returning the parse functions again instead of just skipping the one character. Now if we have more than one whitespace character in a row, they'll all be skipped. Add a parseStringOrNullOrEnd parse function that will be called after the tokenArrayStart character, to fix a bug where empty arrays were expecting a string or null and getting the array end character. This is only valid after tokenArrayStart, however; in other places where parseSeparatorOrDelim is used, it wouldn't be appropriate. Add a parser test for an empty array.
1.1 --- a/array.go Sun Apr 19 23:47:36 2015 -0400 1.2 +++ b/array.go Thu Feb 25 23:52:05 2016 -0800 1.3 @@ -8,11 +8,15 @@ 1.4 ) 1.5 1.6 var ( 1.7 + // ErrUnexpectedValueType is returned when the passed value is not a string or []byte. 1.8 ErrUnexpectedValueType = errors.New("expected value to be a string or []byte") 1.9 ) 1.10 1.11 +// StringArray represents a Postgres array as a []string. 1.12 type StringArray []string 1.13 1.14 +// Value implements the Valuer interface for StringArray, allowing it to be transparently 1.15 +// stored in Postgres databases using the database/sql package. 1.16 func (s StringArray) Value() (driver.Value, error) { 1.17 output := make([]string, 0, len(s)) 1.18 for _, item := range s { 1.19 @@ -23,6 +27,9 @@ 1.20 return []byte(`{` + strings.Join(output, ",") + `}`), nil 1.21 } 1.22 1.23 +// Scan implements the Scanner interface for StringArray, allowing it to be transparently 1.24 +// retrieved from Postgres databases using the database/sql package. It expects `value` to 1.25 +// be a string or []byte, and throws ErrUnexpectedValueType when any other type is encountered. 1.26 func (s *StringArray) Scan(value interface{}) error { 1.27 *s = (*s)[:0] 1.28 var input string