forked from sqlc-dev/sqlc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
multierr: Move dinosql.ParserErr to a new package (sqlc-dev#496)
- Loading branch information
1 parent
5d28068
commit 4b7238e
Showing
6 changed files
with
101 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package multierr | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/kyleconroy/sqlc/internal/pg" | ||
"github.com/kyleconroy/sqlc/internal/source" | ||
) | ||
|
||
type FileError struct { | ||
Filename string | ||
Line int | ||
Column int | ||
Err error | ||
} | ||
|
||
func (e *FileError) Unwrap() error { | ||
return e.Err | ||
} | ||
|
||
type Error struct { | ||
errs []*FileError | ||
} | ||
|
||
func (e *Error) Add(filename, in string, loc int, err error) { | ||
line := 1 | ||
column := 1 | ||
if lerr, ok := err.(pg.Error); ok { | ||
if lerr.Location != 0 { | ||
loc = lerr.Location | ||
} | ||
} | ||
if in != "" && loc != 0 { | ||
line, column = source.LineNumber(in, loc) | ||
} | ||
e.errs = append(e.errs, &FileError{filename, line, column, err}) | ||
} | ||
|
||
func (e *Error) Errs() []*FileError { | ||
return e.errs | ||
} | ||
|
||
func (e *Error) Error() string { | ||
return fmt.Sprintf("multiple errors: %d errors", len(e.errs)) | ||
} | ||
|
||
func New() *Error { | ||
return &Error{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package source | ||
|
||
import "unicode" | ||
|
||
func LineNumber(source string, head int) (int, int) { | ||
// Calculate the true line and column number for a query, ignoring spaces | ||
var comment bool | ||
var loc, line, col int | ||
for i, char := range source { | ||
loc += 1 | ||
col += 1 | ||
// TODO: Check bounds | ||
if char == '-' && source[i+1] == '-' { | ||
comment = true | ||
} | ||
if char == '\n' { | ||
comment = false | ||
line += 1 | ||
col = 0 | ||
} | ||
if loc <= head { | ||
continue | ||
} | ||
if unicode.IsSpace(char) { | ||
continue | ||
} | ||
if comment { | ||
continue | ||
} | ||
break | ||
} | ||
return line + 1, col | ||
} |