summaryrefslogtreecommitdiff
path: root/vendor/github.com/uptrace/bun/internal
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/uptrace/bun/internal')
-rw-r--r--vendor/github.com/uptrace/bun/internal/flag.go16
-rw-r--r--vendor/github.com/uptrace/bun/internal/hex.go43
-rw-r--r--vendor/github.com/uptrace/bun/internal/logger.go27
-rw-r--r--vendor/github.com/uptrace/bun/internal/map_key.go67
-rw-r--r--vendor/github.com/uptrace/bun/internal/parser/parser.go141
-rw-r--r--vendor/github.com/uptrace/bun/internal/safe.go11
-rw-r--r--vendor/github.com/uptrace/bun/internal/tagparser/parser.go147
-rw-r--r--vendor/github.com/uptrace/bun/internal/time.go41
-rw-r--r--vendor/github.com/uptrace/bun/internal/underscore.go67
-rw-r--r--vendor/github.com/uptrace/bun/internal/unsafe.go20
-rw-r--r--vendor/github.com/uptrace/bun/internal/util.go57
11 files changed, 637 insertions, 0 deletions
diff --git a/vendor/github.com/uptrace/bun/internal/flag.go b/vendor/github.com/uptrace/bun/internal/flag.go
new file mode 100644
index 000000000..b42f59df7
--- /dev/null
+++ b/vendor/github.com/uptrace/bun/internal/flag.go
@@ -0,0 +1,16 @@
+package internal
+
+type Flag uint64
+
+func (flag Flag) Has(other Flag) bool {
+ return flag&other == other
+}
+
+func (flag Flag) Set(other Flag) Flag {
+ return flag | other
+}
+
+func (flag Flag) Remove(other Flag) Flag {
+ flag &= ^other
+ return flag
+}
diff --git a/vendor/github.com/uptrace/bun/internal/hex.go b/vendor/github.com/uptrace/bun/internal/hex.go
new file mode 100644
index 000000000..6fae2bb78
--- /dev/null
+++ b/vendor/github.com/uptrace/bun/internal/hex.go
@@ -0,0 +1,43 @@
+package internal
+
+import (
+ fasthex "github.com/tmthrgd/go-hex"
+)
+
+type HexEncoder struct {
+ b []byte
+ written bool
+}
+
+func NewHexEncoder(b []byte) *HexEncoder {
+ return &HexEncoder{
+ b: b,
+ }
+}
+
+func (enc *HexEncoder) Bytes() []byte {
+ return enc.b
+}
+
+func (enc *HexEncoder) Write(b []byte) (int, error) {
+ if !enc.written {
+ enc.b = append(enc.b, '\'')
+ enc.b = append(enc.b, `\x`...)
+ enc.written = true
+ }
+
+ i := len(enc.b)
+ enc.b = append(enc.b, make([]byte, fasthex.EncodedLen(len(b)))...)
+ fasthex.Encode(enc.b[i:], b)
+
+ return len(b), nil
+}
+
+func (enc *HexEncoder) Close() error {
+ if enc.written {
+ enc.b = append(enc.b, '\'')
+ } else {
+ enc.b = append(enc.b, "NULL"...)
+ }
+ return nil
+}
diff --git a/vendor/github.com/uptrace/bun/internal/logger.go b/vendor/github.com/uptrace/bun/internal/logger.go
new file mode 100644
index 000000000..2e22a0893
--- /dev/null
+++ b/vendor/github.com/uptrace/bun/internal/logger.go
@@ -0,0 +1,27 @@
+package internal
+
+import (
+ "fmt"
+ "log"
+ "os"
+)
+
+var Warn = log.New(os.Stderr, "WARN: bun: ", log.LstdFlags)
+
+var Deprecated = log.New(os.Stderr, "DEPRECATED: bun: ", log.LstdFlags)
+
+type Logging interface {
+ Printf(format string, v ...interface{})
+}
+
+type logger struct {
+ log *log.Logger
+}
+
+func (l *logger) Printf(format string, v ...interface{}) {
+ _ = l.log.Output(2, fmt.Sprintf(format, v...))
+}
+
+var Logger Logging = &logger{
+ log: log.New(os.Stderr, "bun: ", log.LstdFlags|log.Lshortfile),
+}
diff --git a/vendor/github.com/uptrace/bun/internal/map_key.go b/vendor/github.com/uptrace/bun/internal/map_key.go
new file mode 100644
index 000000000..bb5fcca8c
--- /dev/null
+++ b/vendor/github.com/uptrace/bun/internal/map_key.go
@@ -0,0 +1,67 @@
+package internal
+
+import "reflect"
+
+var ifaceType = reflect.TypeOf((*interface{})(nil)).Elem()
+
+type MapKey struct {
+ iface interface{}
+}
+
+func NewMapKey(is []interface{}) MapKey {
+ return MapKey{
+ iface: newMapKey(is),
+ }
+}
+
+func newMapKey(is []interface{}) interface{} {
+ switch len(is) {
+ case 1:
+ ptr := new([1]interface{})
+ copy((*ptr)[:], is)
+ return *ptr
+ case 2:
+ ptr := new([2]interface{})
+ copy((*ptr)[:], is)
+ return *ptr
+ case 3:
+ ptr := new([3]interface{})
+ copy((*ptr)[:], is)
+ return *ptr
+ case 4:
+ ptr := new([4]interface{})
+ copy((*ptr)[:], is)
+ return *ptr
+ case 5:
+ ptr := new([5]interface{})
+ copy((*ptr)[:], is)
+ return *ptr
+ case 6:
+ ptr := new([6]interface{})
+ copy((*ptr)[:], is)
+ return *ptr
+ case 7:
+ ptr := new([7]interface{})
+ copy((*ptr)[:], is)
+ return *ptr
+ case 8:
+ ptr := new([8]interface{})
+ copy((*ptr)[:], is)
+ return *ptr
+ case 9:
+ ptr := new([9]interface{})
+ copy((*ptr)[:], is)
+ return *ptr
+ case 10:
+ ptr := new([10]interface{})
+ copy((*ptr)[:], is)
+ return *ptr
+ default:
+ }
+
+ at := reflect.New(reflect.ArrayOf(len(is), ifaceType)).Elem()
+ for i, v := range is {
+ *(at.Index(i).Addr().Interface().(*interface{})) = v
+ }
+ return at.Interface()
+}
diff --git a/vendor/github.com/uptrace/bun/internal/parser/parser.go b/vendor/github.com/uptrace/bun/internal/parser/parser.go
new file mode 100644
index 000000000..cdfc0be16
--- /dev/null
+++ b/vendor/github.com/uptrace/bun/internal/parser/parser.go
@@ -0,0 +1,141 @@
+package parser
+
+import (
+ "bytes"
+ "strconv"
+
+ "github.com/uptrace/bun/internal"
+)
+
+type Parser struct {
+ b []byte
+ i int
+}
+
+func New(b []byte) *Parser {
+ return &Parser{
+ b: b,
+ }
+}
+
+func NewString(s string) *Parser {
+ return New(internal.Bytes(s))
+}
+
+func (p *Parser) Valid() bool {
+ return p.i < len(p.b)
+}
+
+func (p *Parser) Bytes() []byte {
+ return p.b[p.i:]
+}
+
+func (p *Parser) Read() byte {
+ if p.Valid() {
+ c := p.b[p.i]
+ p.Advance()
+ return c
+ }
+ return 0
+}
+
+func (p *Parser) Peek() byte {
+ if p.Valid() {
+ return p.b[p.i]
+ }
+ return 0
+}
+
+func (p *Parser) Advance() {
+ p.i++
+}
+
+func (p *Parser) Skip(skip byte) bool {
+ if p.Peek() == skip {
+ p.Advance()
+ return true
+ }
+ return false
+}
+
+func (p *Parser) SkipBytes(skip []byte) bool {
+ if len(skip) > len(p.b[p.i:]) {
+ return false
+ }
+ if !bytes.Equal(p.b[p.i:p.i+len(skip)], skip) {
+ return false
+ }
+ p.i += len(skip)
+ return true
+}
+
+func (p *Parser) ReadSep(sep byte) ([]byte, bool) {
+ ind := bytes.IndexByte(p.b[p.i:], sep)
+ if ind == -1 {
+ b := p.b[p.i:]
+ p.i = len(p.b)
+ return b, false
+ }
+
+ b := p.b[p.i : p.i+ind]
+ p.i += ind + 1
+ return b, true
+}
+
+func (p *Parser) ReadIdentifier() (string, bool) {
+ if p.i < len(p.b) && p.b[p.i] == '(' {
+ s := p.i + 1
+ if ind := bytes.IndexByte(p.b[s:], ')'); ind != -1 {
+ b := p.b[s : s+ind]
+ p.i = s + ind + 1
+ return internal.String(b), false
+ }
+ }
+
+ ind := len(p.b) - p.i
+ var alpha bool
+ for i, c := range p.b[p.i:] {
+ if isNum(c) {
+ continue
+ }
+ if isAlpha(c) || (i > 0 && alpha && c == '_') {
+ alpha = true
+ continue
+ }
+ ind = i
+ break
+ }
+ if ind == 0 {
+ return "", false
+ }
+ b := p.b[p.i : p.i+ind]
+ p.i += ind
+ return internal.String(b), !alpha
+}
+
+func (p *Parser) ReadNumber() int {
+ ind := len(p.b) - p.i
+ for i, c := range p.b[p.i:] {
+ if !isNum(c) {
+ ind = i
+ break
+ }
+ }
+ if ind == 0 {
+ return 0
+ }
+ n, err := strconv.Atoi(string(p.b[p.i : p.i+ind]))
+ if err != nil {
+ panic(err)
+ }
+ p.i += ind
+ return n
+}
+
+func isNum(c byte) bool {
+ return c >= '0' && c <= '9'
+}
+
+func isAlpha(c byte) bool {
+ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
+}
diff --git a/vendor/github.com/uptrace/bun/internal/safe.go b/vendor/github.com/uptrace/bun/internal/safe.go
new file mode 100644
index 000000000..862ff0eb3
--- /dev/null
+++ b/vendor/github.com/uptrace/bun/internal/safe.go
@@ -0,0 +1,11 @@
+// +build appengine
+
+package internal
+
+func String(b []byte) string {
+ return string(b)
+}
+
+func Bytes(s string) []byte {
+ return []byte(s)
+}
diff --git a/vendor/github.com/uptrace/bun/internal/tagparser/parser.go b/vendor/github.com/uptrace/bun/internal/tagparser/parser.go
new file mode 100644
index 000000000..8ef89248c
--- /dev/null
+++ b/vendor/github.com/uptrace/bun/internal/tagparser/parser.go
@@ -0,0 +1,147 @@
+package tagparser
+
+import (
+ "strings"
+)
+
+type Tag struct {
+ Name string
+ Options map[string]string
+}
+
+func (t Tag) HasOption(name string) bool {
+ _, ok := t.Options[name]
+ return ok
+}
+
+func Parse(s string) Tag {
+ p := parser{
+ s: s,
+ }
+ p.parse()
+ return p.tag
+}
+
+type parser struct {
+ s string
+ i int
+
+ tag Tag
+ seenName bool // for empty names
+}
+
+func (p *parser) setName(name string) {
+ if p.seenName {
+ p.addOption(name, "")
+ } else {
+ p.seenName = true
+ p.tag.Name = name
+ }
+}
+
+func (p *parser) addOption(key, value string) {
+ p.seenName = true
+ if key == "" {
+ return
+ }
+ if p.tag.Options == nil {
+ p.tag.Options = make(map[string]string)
+ }
+ p.tag.Options[key] = value
+}
+
+func (p *parser) parse() {
+ for p.valid() {
+ p.parseKeyValue()
+ if p.peek() == ',' {
+ p.i++
+ }
+ }
+}
+
+func (p *parser) parseKeyValue() {
+ start := p.i
+
+ for p.valid() {
+ switch c := p.read(); c {
+ case ',':
+ key := p.s[start : p.i-1]
+ p.setName(key)
+ return
+ case ':':
+ key := p.s[start : p.i-1]
+ value := p.parseValue()
+ p.addOption(key, value)
+ return
+ case '"':
+ key := p.parseQuotedValue()
+ p.setName(key)
+ return
+ }
+ }
+
+ key := p.s[start:p.i]
+ p.setName(key)
+}
+
+func (p *parser) parseValue() string {
+ start := p.i
+
+ for p.valid() {
+ switch c := p.read(); c {
+ case '"':
+ return p.parseQuotedValue()
+ case ',':
+ return p.s[start : p.i-1]
+ }
+ }
+
+ if p.i == start {
+ return ""
+ }
+ return p.s[start:p.i]
+}
+
+func (p *parser) parseQuotedValue() string {
+ if i := strings.IndexByte(p.s[p.i:], '"'); i >= 0 && p.s[p.i+i-1] != '\\' {
+ s := p.s[p.i : p.i+i]
+ p.i += i + 1
+ return s
+ }
+
+ b := make([]byte, 0, 16)
+
+ for p.valid() {
+ switch c := p.read(); c {
+ case '\\':
+ b = append(b, p.read())
+ case '"':
+ return string(b)
+ default:
+ b = append(b, c)
+ }
+ }
+
+ return ""
+}
+
+func (p *parser) valid() bool {
+ return p.i < len(p.s)
+}
+
+func (p *parser) read() byte {
+ if !p.valid() {
+ return 0
+ }
+ c := p.s[p.i]
+ p.i++
+ return c
+}
+
+func (p *parser) peek() byte {
+ if !p.valid() {
+ return 0
+ }
+ c := p.s[p.i]
+ return c
+}
diff --git a/vendor/github.com/uptrace/bun/internal/time.go b/vendor/github.com/uptrace/bun/internal/time.go
new file mode 100644
index 000000000..e4e0804b0
--- /dev/null
+++ b/vendor/github.com/uptrace/bun/internal/time.go
@@ -0,0 +1,41 @@
+package internal
+
+import (
+ "fmt"
+ "time"
+)
+
+const (
+ dateFormat = "2006-01-02"
+ timeFormat = "15:04:05.999999999"
+ timestampFormat = "2006-01-02 15:04:05.999999999"
+ timestamptzFormat = "2006-01-02 15:04:05.999999999-07:00:00"
+ timestamptzFormat2 = "2006-01-02 15:04:05.999999999-07:00"
+ timestamptzFormat3 = "2006-01-02 15:04:05.999999999-07"
+)
+
+func ParseTime(s string) (time.Time, error) {
+ switch l := len(s); {
+ case l < len("15:04:05"):
+ return time.Time{}, fmt.Errorf("bun: can't parse time=%q", s)
+ case l <= len(timeFormat):
+ if s[2] == ':' {
+ return time.ParseInLocation(timeFormat, s, time.UTC)
+ }
+ return time.ParseInLocation(dateFormat, s, time.UTC)
+ default:
+ if s[10] == 'T' {
+ return time.Parse(time.RFC3339Nano, s)
+ }
+ if c := s[l-9]; c == '+' || c == '-' {
+ return time.Parse(timestamptzFormat, s)
+ }
+ if c := s[l-6]; c == '+' || c == '-' {
+ return time.Parse(timestamptzFormat2, s)
+ }
+ if c := s[l-3]; c == '+' || c == '-' {
+ return time.Parse(timestamptzFormat3, s)
+ }
+ return time.ParseInLocation(timestampFormat, s, time.UTC)
+ }
+}
diff --git a/vendor/github.com/uptrace/bun/internal/underscore.go b/vendor/github.com/uptrace/bun/internal/underscore.go
new file mode 100644
index 000000000..9de52fb7b
--- /dev/null
+++ b/vendor/github.com/uptrace/bun/internal/underscore.go
@@ -0,0 +1,67 @@
+package internal
+
+func IsUpper(c byte) bool {
+ return c >= 'A' && c <= 'Z'
+}
+
+func IsLower(c byte) bool {
+ return c >= 'a' && c <= 'z'
+}
+
+func ToUpper(c byte) byte {
+ return c - 32
+}
+
+func ToLower(c byte) byte {
+ return c + 32
+}
+
+// Underscore converts "CamelCasedString" to "camel_cased_string".
+func Underscore(s string) string {
+ r := make([]byte, 0, len(s)+5)
+ for i := 0; i < len(s); i++ {
+ c := s[i]
+ if IsUpper(c) {
+ if i > 0 && i+1 < len(s) && (IsLower(s[i-1]) || IsLower(s[i+1])) {
+ r = append(r, '_', ToLower(c))
+ } else {
+ r = append(r, ToLower(c))
+ }
+ } else {
+ r = append(r, c)
+ }
+ }
+ return string(r)
+}
+
+func CamelCased(s string) string {
+ r := make([]byte, 0, len(s))
+ upperNext := true
+ for i := 0; i < len(s); i++ {
+ c := s[i]
+ if c == '_' {
+ upperNext = true
+ continue
+ }
+ if upperNext {
+ if IsLower(c) {
+ c = ToUpper(c)
+ }
+ upperNext = false
+ }
+ r = append(r, c)
+ }
+ return string(r)
+}
+
+func ToExported(s string) string {
+ if len(s) == 0 {
+ return s
+ }
+ if c := s[0]; IsLower(c) {
+ b := []byte(s)
+ b[0] = ToUpper(c)
+ return string(b)
+ }
+ return s
+}
diff --git a/vendor/github.com/uptrace/bun/internal/unsafe.go b/vendor/github.com/uptrace/bun/internal/unsafe.go
new file mode 100644
index 000000000..4bc79701f
--- /dev/null
+++ b/vendor/github.com/uptrace/bun/internal/unsafe.go
@@ -0,0 +1,20 @@
+// +build !appengine
+
+package internal
+
+import "unsafe"
+
+// String converts byte slice to string.
+func String(b []byte) string {
+ return *(*string)(unsafe.Pointer(&b))
+}
+
+// Bytes converts string to byte slice.
+func Bytes(s string) []byte {
+ return *(*[]byte)(unsafe.Pointer(
+ &struct {
+ string
+ Cap int
+ }{s, len(s)},
+ ))
+}
diff --git a/vendor/github.com/uptrace/bun/internal/util.go b/vendor/github.com/uptrace/bun/internal/util.go
new file mode 100644
index 000000000..c831dc659
--- /dev/null
+++ b/vendor/github.com/uptrace/bun/internal/util.go
@@ -0,0 +1,57 @@
+package internal
+
+import (
+ "reflect"
+)
+
+func MakeSliceNextElemFunc(v reflect.Value) func() reflect.Value {
+ if v.Kind() == reflect.Array {
+ var pos int
+ return func() reflect.Value {
+ v := v.Index(pos)
+ pos++
+ return v
+ }
+ }
+
+ elemType := v.Type().Elem()
+
+ if elemType.Kind() == reflect.Ptr {
+ elemType = elemType.Elem()
+ return func() reflect.Value {
+ if v.Len() < v.Cap() {
+ v.Set(v.Slice(0, v.Len()+1))
+ elem := v.Index(v.Len() - 1)
+ if elem.IsNil() {
+ elem.Set(reflect.New(elemType))
+ }
+ return elem.Elem()
+ }
+
+ elem := reflect.New(elemType)
+ v.Set(reflect.Append(v, elem))
+ return elem.Elem()
+ }
+ }
+
+ zero := reflect.Zero(elemType)
+ return func() reflect.Value {
+ if v.Len() < v.Cap() {
+ v.Set(v.Slice(0, v.Len()+1))
+ return v.Index(v.Len() - 1)
+ }
+
+ v.Set(reflect.Append(v, zero))
+ return v.Index(v.Len() - 1)
+ }
+}
+
+func Unwrap(err error) error {
+ u, ok := err.(interface {
+ Unwrap() error
+ })
+ if !ok {
+ return nil
+ }
+ return u.Unwrap()
+}