diff options
author | 2021-08-25 15:34:33 +0200 | |
---|---|---|
committer | 2021-08-25 15:34:33 +0200 | |
commit | 2dc9fc1626507bb54417fc4a1920b847cafb27a2 (patch) | |
tree | 4ddeac479b923db38090aac8bd9209f3646851c1 /vendor/github.com/uptrace/bun/dialect/pgdialect/array.go | |
parent | Manually approves followers (#146) (diff) | |
download | gotosocial-2dc9fc1626507bb54417fc4a1920b847cafb27a2.tar.xz |
Pg to bun (#148)
* start moving to bun
* changing more stuff
* more
* and yet more
* tests passing
* seems stable now
* more big changes
* small fix
* little fixes
Diffstat (limited to 'vendor/github.com/uptrace/bun/dialect/pgdialect/array.go')
-rw-r--r-- | vendor/github.com/uptrace/bun/dialect/pgdialect/array.go | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/vendor/github.com/uptrace/bun/dialect/pgdialect/array.go b/vendor/github.com/uptrace/bun/dialect/pgdialect/array.go new file mode 100644 index 000000000..57f5a4384 --- /dev/null +++ b/vendor/github.com/uptrace/bun/dialect/pgdialect/array.go @@ -0,0 +1,65 @@ +package pgdialect + +import ( + "database/sql" + "fmt" + "reflect" + + "github.com/uptrace/bun/schema" +) + +type ArrayValue struct { + v reflect.Value + + append schema.AppenderFunc + scan schema.ScannerFunc +} + +// Array accepts a slice and returns a wrapper for working with PostgreSQL +// array data type. +// +// For struct fields you can use array tag: +// +// Emails []string `bun:",array"` +func Array(vi interface{}) *ArrayValue { + v := reflect.ValueOf(vi) + if !v.IsValid() { + panic(fmt.Errorf("bun: Array(nil)")) + } + + return &ArrayValue{ + v: v, + + append: arrayAppender(v.Type()), + scan: arrayScanner(v.Type()), + } +} + +var ( + _ schema.QueryAppender = (*ArrayValue)(nil) + _ sql.Scanner = (*ArrayValue)(nil) +) + +func (a *ArrayValue) AppendQuery(fmter schema.Formatter, b []byte) ([]byte, error) { + if a.append == nil { + panic(fmt.Errorf("bun: Array(unsupported %s)", a.v.Type())) + } + return a.append(fmter, b, a.v), nil +} + +func (a *ArrayValue) Scan(src interface{}) error { + if a.scan == nil { + return fmt.Errorf("bun: Array(unsupported %s)", a.v.Type()) + } + if a.v.Kind() != reflect.Ptr { + return fmt.Errorf("bun: Array(non-pointer %s)", a.v.Type()) + } + return a.scan(a.v, src) +} + +func (a *ArrayValue) Value() interface{} { + if a.v.IsValid() { + return a.v.Interface() + } + return nil +} |