vendor dependencies with dep

This commit is contained in:
dhax 2017-09-25 20:20:52 +02:00
parent 93d8310491
commit 1384296a47
2712 changed files with 965742 additions and 0 deletions

View file

@ -0,0 +1,80 @@
package parser
import (
"bytes"
"fmt"
)
type ArrayParser struct {
*Parser
stickyErr error
}
func NewArrayParser(b []byte) *ArrayParser {
var err error
if len(b) < 2 || b[0] != '{' || b[len(b)-1] != '}' {
err = fmt.Errorf("pg: can't parse array: %s", string(b))
} else {
b = b[1 : len(b)-1]
}
return &ArrayParser{
Parser: New(b),
stickyErr: err,
}
}
func (p *ArrayParser) NextElem() ([]byte, error) {
if p.stickyErr != nil {
return nil, p.stickyErr
}
switch c := p.Peek(); c {
case '"':
p.Advance()
b := p.readSubstring()
p.Skip(',')
return b, nil
case '{':
b := p.readElem()
if b != nil {
b = append(b, '}')
}
p.Skip(',')
return b, nil
default:
b, _ := p.ReadSep(',')
if bytes.Equal(b, pgNull) {
b = nil
}
return b, nil
}
}
func (p *ArrayParser) readElem() []byte {
var b []byte
for p.Valid() {
c := p.Read()
switch c {
case '"':
b = append(b, '"')
for {
bb, ok := p.ReadSep('"')
b = append(b, bb...)
stop := len(b) > 0 && b[len(b)-1] != '\\'
if ok {
b = append(b, '"')
}
if stop {
break
}
}
case '}':
return b
default:
b = append(b, c)
}
}
return b
}

View file

@ -0,0 +1,55 @@
package parser_test
import (
"testing"
"github.com/go-pg/pg/internal/parser"
)
var arrayTests = []struct {
s string
els []string
}{
{`{"\\"}`, []string{`\`}},
{`{"''"}`, []string{`'`}},
{`{{"''\"{}"}}`, []string{`{"''\"{}"}`}},
{`{"''\"{}"}`, []string{`'"{}`}},
{"{1,2}", []string{"1", "2"}},
{"{1,NULL}", []string{"1", ""}},
{`{"1","2"}`, []string{"1", "2"}},
{`{"{1}","{2}"}`, []string{"{1}", "{2}"}},
{"{{1,2},{3}}", []string{"{1,2}", "{3}"}},
}
func TestArrayParser(t *testing.T) {
for testi, test := range arrayTests {
p := parser.NewArrayParser([]byte(test.s))
var got []string
for p.Valid() {
b, err := p.NextElem()
if err != nil {
t.Fatal(err)
}
got = append(got, string(b))
}
if len(got) != len(test.els) {
t.Fatalf(
"#%d got %d elements, wanted %d (got=%#v wanted=%#v)",
testi, len(got), len(test.els), got, test.els,
)
}
for i, el := range got {
if el != test.els[i] {
t.Fatalf(
"#%d el #%d does not match: %q != %q (got=%#v wanted=%#v)",
testi, i, el, test.els[i], got, test.els,
)
}
}
}
}

View file

@ -0,0 +1,40 @@
package parser
import "fmt"
type HstoreParser struct {
*Parser
}
func NewHstoreParser(b []byte) *HstoreParser {
return &HstoreParser{
Parser: New(b),
}
}
func (p *HstoreParser) NextKey() ([]byte, error) {
if p.Skip(',') {
p.Skip(' ')
}
if !p.Skip('"') {
return nil, fmt.Errorf("pg: can't parse hstore key: %q", p.Bytes())
}
key := p.readSubstring()
if !(p.Skip('=') && p.Skip('>')) {
return nil, fmt.Errorf("pg: can't parse hstore key: %q", p.Bytes())
}
return key, nil
}
func (p *HstoreParser) NextValue() ([]byte, error) {
if !p.Skip('"') {
return nil, fmt.Errorf("pg: can't parse hstore value: %q", p.Bytes())
}
value := p.readSubstring()
p.SkipBytes([]byte(", "))
return value, nil
}

View file

@ -0,0 +1,57 @@
package parser_test
import (
"testing"
"github.com/go-pg/pg/internal/parser"
)
var hstoreTests = []struct {
s string
m map[string]string
}{
{`""=>""`, map[string]string{"": ""}},
{`"k''k"=>"k''k"`, map[string]string{"k'k": "k'k"}},
{`"k\"k"=>"k\"k"`, map[string]string{`k"k`: `k"k`}},
{`"k\k"=>"k\k"`, map[string]string{`k\k`: `k\k`}},
{`"foo"=>"bar"`, map[string]string{"foo": "bar"}},
{`"foo"=>"bar","k"=>"v"`, map[string]string{"foo": "bar", "k": "v"}},
}
func TestHstoreParser(t *testing.T) {
for testi, test := range hstoreTests {
p := parser.NewHstoreParser([]byte(test.s))
got := make(map[string]string)
for p.Valid() {
key, err := p.NextKey()
if err != nil {
t.Fatal(err)
}
value, err := p.NextValue()
if err != nil {
t.Fatal(err)
}
got[string(key)] = string(value)
}
if len(got) != len(test.m) {
t.Fatalf(
"#%d got %d elements, wanted %d (got=%#v wanted=%#v)",
testi, len(got), len(test.m), got, test.m,
)
}
for k, v := range got {
if v != test.m[k] {
t.Fatalf(
"#%d el %q does not match: %q != %q (got=%#v wanted=%#v)",
testi, k, v, test.m[k], got, test.m,
)
}
}
}
}

153
vendor/github.com/go-pg/pg/internal/parser/parser.go generated vendored Normal file
View file

@ -0,0 +1,153 @@
package parser
import (
"bytes"
"strconv"
"github.com/go-pg/pg/internal"
)
type Parser struct {
b []byte
}
func New(b []byte) *Parser {
return &Parser{
b: b,
}
}
func NewString(s string) *Parser {
return New(internal.StringToBytes(s))
}
func (p *Parser) Bytes() []byte {
return p.b
}
func (p *Parser) Valid() bool {
return len(p.b) > 0
}
func (p *Parser) Read() byte {
if p.Valid() {
c := p.b[0]
p.Skip(c)
return c
}
return 0
}
func (p *Parser) Peek() byte {
if p.Valid() {
return p.b[0]
}
return 0
}
func (p *Parser) Advance() {
p.b = p.b[1:]
}
func (p *Parser) Skip(c byte) bool {
if p.Peek() == c {
p.Advance()
return true
}
return false
}
func (p *Parser) SkipBytes(b []byte) bool {
if len(b) > len(p.b) {
return false
}
if !bytes.Equal(p.b[:len(b)], b) {
return false
}
p.b = p.b[len(b):]
return true
}
func (p *Parser) ReadSep(c byte) ([]byte, bool) {
ind := bytes.IndexByte(p.b, c)
if ind == -1 {
b := p.b
p.b = p.b[len(p.b):]
return b, false
}
b := p.b[:ind]
p.b = p.b[ind+1:]
return b, true
}
func (p *Parser) ReadIdentifier() (s string, numeric bool) {
end := len(p.b)
numeric = true
for i, ch := range p.b {
if isNum(ch) {
continue
}
if isAlpha(ch) || ch == '_' {
numeric = false
continue
}
end = i
break
}
if end <= 0 {
return "", false
}
b := p.b[:end]
p.b = p.b[end:]
return internal.BytesToString(b), numeric
}
func (p *Parser) ReadNumber() int {
end := len(p.b)
for i, ch := range p.b {
if !isNum(ch) {
end = i
break
}
}
if end <= 0 {
return 0
}
n, _ := strconv.Atoi(string(p.b[:end]))
p.b = p.b[end:]
return n
}
func (p *Parser) readSubstring() []byte {
var b []byte
for p.Valid() {
c := p.Read()
switch c {
case '\\':
switch p.Peek() {
case '\\':
b = append(b, '\\')
p.Advance()
case '"':
b = append(b, '"')
p.Advance()
default:
b = append(b, c)
}
case '\'':
switch p.Peek() {
case '\'':
b = append(b, '\'')
p.Skip(c)
default:
b = append(b, c)
}
case '"':
return b
default:
b = append(b, c)
}
}
return b
}

15
vendor/github.com/go-pg/pg/internal/parser/util.go generated vendored Normal file
View file

@ -0,0 +1,15 @@
package parser
var pgNull = []byte("NULL")
func isNum(c byte) bool {
return c >= '0' && c <= '9'
}
func isAlpha(c byte) bool {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
}
func isAlnum(c byte) bool {
return isAlpha(c) || isNum(c)
}