Skip to content

Commit

Permalink
minor error message fix, minor internal tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
mitranim committed Nov 28, 2023
1 parent 70d1e01 commit 6eaf4a6
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 66 deletions.
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ func Example_composition() {

## Changelog

### v0.7.4

Minor fix for reporting types in error messages. Some internal tweaks.

### v0.7.3

Added `UpsertConflictVoid`, `UpsertConflict`.
Expand Down
4 changes: 2 additions & 2 deletions sqlb_dict.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ shortcut.
type List []any

// Implement part of the `ArgDict` interface.
func (self List) IsEmpty() bool { return self.Len() == 0 }
func (self List) IsEmpty() bool { return self.Len() <= 0 }

// Implement part of the `ArgDict` interface.
func (self List) Len() int { return len(self) }
Expand Down Expand Up @@ -43,7 +43,7 @@ the `DictQ` shortcut.
type Dict map[string]any

// Implement part of the `ArgDict` interface.
func (self Dict) IsEmpty() bool { return self.Len() == 0 }
func (self Dict) IsEmpty() bool { return self.Len() <= 0 }

// Implement part of the `ArgDict` interface.
func (self Dict) Len() int { return len(self) }
Expand Down
18 changes: 9 additions & 9 deletions sqlb_expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (self Identifier) AppendExpr(text []byte, args []any) ([]byte, []any) {
// Implement the `AppenderTo` interface, sometimes allowing more efficient text
// encoding.
func (self Identifier) AppendTo(text []byte) []byte {
if len(self) == 0 {
if len(self) <= 0 {
return text
}
for ind, val := range self {
Expand Down Expand Up @@ -114,7 +114,7 @@ func (self Path) AppendExpr(text []byte, args []any) ([]byte, []any) {
// Implement the `AppenderTo` interface, sometimes allowing more efficient text
// encoding.
func (self Path) AppendTo(text []byte) []byte {
if len(self) == 0 {
if len(self) <= 0 {
return text
}

Expand Down Expand Up @@ -165,7 +165,7 @@ func (self PseudoPath) AppendExpr(text []byte, args []any) ([]byte, []any) {
// Implement the `AppenderTo` interface, sometimes allowing more efficient text
// encoding.
func (self PseudoPath) AppendTo(text []byte) []byte {
if len(self) == 0 {
if len(self) <= 0 {
return text
}

Expand Down Expand Up @@ -215,7 +215,7 @@ func (self AliasedPath) AppendExpr(text []byte, args []any) ([]byte, []any) {
// Implement the `AppenderTo` interface, sometimes allowing more efficient text
// encoding.
func (self AliasedPath) AppendTo(text []byte) []byte {
if len(self) == 0 {
if len(self) <= 0 {
return text
}

Expand Down Expand Up @@ -259,7 +259,7 @@ func (self Table) AppendExpr(text []byte, args []any) ([]byte, []any) {
// Implement the `AppenderTo` interface, sometimes allowing more efficient text
// encoding.
func (self Table) AppendTo(text []byte) []byte {
if len(self) == 0 {
if len(self) <= 0 {
return text
}
text = appendMaybeSpaced(text, `table`)
Expand Down Expand Up @@ -536,7 +536,7 @@ func (self *Seq) appendEmpty(bui *Bui) {
func (self Seq) appendSlice(bui *Bui, src any) {
val := valueOf(src)

if val.Len() == 0 {
if val.Len() <= 0 {
self.appendEmpty(bui)
return
}
Expand Down Expand Up @@ -625,7 +625,7 @@ type Ands []any

// Implement the `Expr` interface, making this a sub-expression.
func (self Ands) AppendExpr(text []byte, args []any) ([]byte, []any) {
if len(self) == 0 {
if len(self) <= 0 {
return And{}.AppendExpr(text, args)
}
return And{[]any(self)}.AppendExpr(text, args)
Expand All @@ -643,7 +643,7 @@ type Ors []any

// Implement the `Expr` interface, making this a sub-expression.
func (self Ors) AppendExpr(text []byte, args []any) ([]byte, []any) {
if len(self) == 0 {
if len(self) <= 0 {
return Or{}.AppendExpr(text, args)
}
return Or{[]any(self)}.AppendExpr(text, args)
Expand Down Expand Up @@ -885,7 +885,7 @@ type StructsInsert[A any] []A

// Implement the `Expr` interface, making this a sub-expression.
func (self StructsInsert[A]) AppendExpr(text []byte, args []any) ([]byte, []any) {
if len(self) == 0 {
if len(self) <= 0 {
return text, args
}

Expand Down
2 changes: 1 addition & 1 deletion sqlb_jel.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ falling back on "true" if empty.
func (self Jel) AppendExpr(text []byte, args []any) ([]byte, []any) {
bui := Bui{text, args}

if len(self.Text) == 0 {
if len(self.Text) <= 0 {
bui.Str(`true`)
} else {
self.decode(&bui, stringToBytesUnsafe(self.Text))
Expand Down
10 changes: 5 additions & 5 deletions sqlb_misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ func TypeCols(typ r.Type) string {

/*
Returns the output of `ColsDeep` for the given type, but takes `reflect.Type` as
input, rather than a type-carrying `any`. Used internally by
`ColsDeep`. The result is cached and reused. Subsequent calls for the same type
are nearly free.
input, rather than a type-carrying `any`. Used internally by `ColsDeep`. The
result is cached and reused. Subsequent calls for the same type are nearly
free.
*/
func TypeColsDeep(typ r.Type) string {
return colsDeepCache.Get(typeElem(typ))
Expand All @@ -54,15 +54,15 @@ func Preparse(val string) Prep { return prepCache.Get(val) }

// Shortcut for `StrQ{text, List(args)}`.
func ListQ(text string, args ...any) StrQ {
if len(args) == 0 {
if len(args) <= 0 {
return StrQ{text, nil}
}
return StrQ{text, List(args)}
}

// Shortcut for `StrQ{text, Dict(args)}`.
func DictQ(text string, args map[string]any) StrQ {
if len(args) == 0 {
if len(args) <= 0 {
return StrQ{text, nil}
}
return StrQ{text, Dict(args)}
Expand Down
4 changes: 2 additions & 2 deletions sqlb_ord.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (self Ords) RowNumberOver() RowNumberOver {
}

// Returns true if there are no non-nil items.
func (self Ords) IsEmpty() bool { return self.Len() == 0 }
func (self Ords) IsEmpty() bool { return self.Len() <= 0 }

// Returns the amount of non-nil items.
func (self Ords) Len() (count int) {
Expand Down Expand Up @@ -205,7 +205,7 @@ func (self Ord) AppendTo(text []byte) []byte {
func (self Ord) String() string { return AppenderString(&self) }

// True if the path is empty.
func (self Ord) IsEmpty() bool { return len(self.Path) == 0 }
func (self Ord) IsEmpty() bool { return len(self.Path) <= 0 }

// Same as `Ord{Path: path, Dir: DirAsc}` but more syntactically convenient
// and uses less memory.
Expand Down
76 changes: 38 additions & 38 deletions sqlb_tokenizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (self *Tokenizer) nextToken() Token {
if self.maybeNamedParam(); self.cursor > mid {
return self.choose(start, mid, TokenTypeNamedParam)
}
self.char()
self.skipChar()
}

if self.cursor > start {
Expand Down Expand Up @@ -128,7 +128,7 @@ func (self *Tokenizer) setNext(val Token) {

func (self *Tokenizer) maybeWhitespace() {
for self.more() && charsetWhitespace.has(self.headByte()) {
self.scan(1)
self.skipBytes(1)
}
}

Expand All @@ -145,10 +145,10 @@ func (self *Tokenizer) maybeQuotedGrave() {
}

func (self *Tokenizer) maybeCommentLine() {
if !self.scannedString(commentLinePrefix) {
if !self.skippedString(commentLinePrefix) {
return
}
for self.more() && !self.scannedNewline() && self.scannedChar() {
for self.more() && !self.skippedNewline() && self.skippedChar() {
}
}

Expand All @@ -163,85 +163,85 @@ func (self *Tokenizer) maybeDoubleColon() {

func (self *Tokenizer) maybeOrdinalParam() {
start := self.cursor
if !self.scannedByte(ordinalParamPrefix) {
if !self.skippedByte(ordinalParamPrefix) {
return
}
if !self.scannedDigits() {
if !self.skippedDigits() {
self.cursor = start
}
}

func (self *Tokenizer) maybeNamedParam() {
start := self.cursor
if !self.scannedByte(namedParamPrefix) {
if !self.skippedByte(namedParamPrefix) {
return
}
if !self.scannedIdent() {
if !self.skippedIdent() {
self.cursor = start
}
}

func (self *Tokenizer) maybeString(val string) {
_ = self.scannedString(val)
_ = self.skippedString(val)
}

func (self *Tokenizer) scannedNewline() bool {
func (self *Tokenizer) skippedNewline() bool {
start := self.cursor
self.maybeNewline()
return self.cursor > start
}

func (self *Tokenizer) maybeNewline() {
self.scan(leadingNewlineSize(self.rest()))
self.skipBytes(leadingNewlineSize(self.rest()))
}

func (self *Tokenizer) scannedChar() bool {
func (self *Tokenizer) skippedChar() bool {
start := self.cursor
self.char()
self.skipChar()
return self.cursor > start
}

func (self *Tokenizer) char() {
func (self *Tokenizer) skipChar() {
_, size := utf8.DecodeRuneInString(self.rest())
self.scan(size)
self.skipBytes(size)
}

func (self *Tokenizer) scannedDigits() bool {
func (self *Tokenizer) skippedDigits() bool {
start := self.cursor
self.maybeDigits()
self.maybeSkipDigits()
return self.cursor > start
}

func (self *Tokenizer) maybeDigits() {
func (self *Tokenizer) maybeSkipDigits() {
for self.more() && charsetDigitDec.has(self.headByte()) {
self.scan(1)
self.skipBytes(1)
}
}

func (self *Tokenizer) scannedIdent() bool {
func (self *Tokenizer) skippedIdent() bool {
start := self.cursor
self.maybeIdent()
return self.cursor > start
}

func (self *Tokenizer) maybeIdent() {
if !self.scannedByteIn(charsetIdentStart) {
if !self.skippedByteFromCharset(charsetIdentStart) {
return
}
for self.more() && self.scannedByteIn(charsetIdent) {
for self.more() && self.skippedByteFromCharset(charsetIdent) {
}
}

func (self *Tokenizer) maybeStringBetween(prefix, suffix string) {
if !self.scannedString(prefix) {
if !self.skippedString(prefix) {
return
}

for self.more() {
if self.scannedString(suffix) {
if self.skippedString(suffix) {
return
}
self.char()
self.skipChar()
}

panic(ErrUnexpectedEOF{Err{
Expand All @@ -251,15 +251,15 @@ func (self *Tokenizer) maybeStringBetween(prefix, suffix string) {
}

func (self *Tokenizer) maybeStringBetweenBytes(prefix, suffix byte) {
if !self.scannedByte(prefix) {
if !self.skippedByte(prefix) {
return
}

for self.more() {
if self.scannedByte(suffix) {
if self.skippedByte(suffix) {
return
}
self.char()
self.skipChar()
}

panic(ErrUnexpectedEOF{Err{
Expand All @@ -268,7 +268,7 @@ func (self *Tokenizer) maybeStringBetweenBytes(prefix, suffix byte) {
}})
}

func (self *Tokenizer) scan(val int) {
func (self *Tokenizer) skipBytes(val int) {
self.cursor += val
}

Expand All @@ -288,33 +288,30 @@ func (self *Tokenizer) headByte() byte {
return self.Source[self.cursor]
}

func (self *Tokenizer) scannedByte(val byte) bool {
func (self *Tokenizer) skippedByte(val byte) bool {
if self.headByte() == val {
self.scan(1)
self.skipBytes(1)
return true
}
return false
}

func (self *Tokenizer) scannedByteIn(val *charset) bool {
func (self *Tokenizer) skippedByteFromCharset(val *charset) bool {
if val.has(self.headByte()) {
self.scan(1)
self.skipBytes(1)
return true
}
return false
}

func (self *Tokenizer) scannedString(val string) bool {
func (self *Tokenizer) skippedString(val string) bool {
if strings.HasPrefix(self.rest(), val) {
self.scan(len(val))
self.skipBytes(len(val))
return true
}
return false
}

// Part of `Token`.
type TokenType byte

const (
TokenTypeInvalid TokenType = iota
TokenTypeText
Expand All @@ -329,6 +326,9 @@ const (
TokenTypeNamedParam
)

// Part of `Token`.
type TokenType byte

// Represents an arbitrary chunk of SQL text parsed by `Tokenizer`.
type Token struct {
Text string
Expand Down
Loading

0 comments on commit 6eaf4a6

Please sign in to comment.