Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update linter #189

Merged
merged 3 commits into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# golangci-lint configuration file
# see: https://github.com/golangci/golangci/wiki/Configuration

run:
timeout: 5m
skip-dirs:
- bin

linters-settings:

govet:
check-shadowing: true

golint:
min-confidence: 0

goimports:
local-prefixes: github.com/goccy/go-zetasqlite

gocyclo:
min-complexity: 10

maligned:
suggest-new: true

depguard:
list-type: blacklist
include-go-root: false


misspell:
locale: US

lll:
line-length: 120

nakedret:
max-func-lines: 0

gocritic:
disabled-checks:
- whyNoLint
- wrapperFunc
- ifElseChain
- unnamedResult
- paramTypeCombine
- hugeParam
- singleCaseSwitch
- octalLiteral

enabled-tags:
- performance
- style
- experimental
gci:
sections:
- "standard"
- "default"
- "prefix(github.com/goccy/go-zetasqlite)"
- "blank"
- "dot"

linters:
enable:
- bodyclose
- rowserrcheck
- gosec
- unconvert
- asciicheck
- gofmt
- goimports
- misspell
- unparam
- dogsled
- nakedret
- gocritic
- godox
- whitespace
- goprintffuncname
- gomodguard
- godot
- nolintlint
- asasalint
- bidichk
- durationcheck
- importas
- tagliatelle
- tenv
- gci
disable:
- stylecheck
- maligned
- prealloc
- gochecknoglobals
- wsl
- testpackage
- gocognit
- depguard

# Configuration of issue rules
issues:
# Excluding configuration per-path, per-linter, per-text and per-source
exclude-rules:
# Exclude lll issues for long lines with go:generate
- linters:
- lll
source: "^//go:generate "

# Exclude shadow checking on the variable named err and ctx
- text: "shadow: declaration of \"(err|ctx)\""
linters:
- govet

# Exclude godox check for TODOs, FIXMEs, and BUGs
- text: "Line contains TODO/BUG/FIXME:"
linters:
- godox

# Exclude some linters from running on tests files
- path: suite_test\.go
linters:
- typecheck
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,8 @@ cover-html: cover
lint: lint/install
$(GOBIN)/golangci-lint run --timeout 30m

lint/fix: lint/install
$(GOBIN)/golangci-lint run --fix --timeout 30m

lint/install: | $(GOBIN)
GOBIN=$(GOBIN) go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.52.2
GOBIN=$(GOBIN) go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.56.2
3 changes: 2 additions & 1 deletion driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
"fmt"
"sync"

internal "github.com/goccy/go-zetasqlite/internal"
"github.com/mattn/go-sqlite3"

internal "github.com/goccy/go-zetasqlite/internal"
)

var (
Expand Down
15 changes: 14 additions & 1 deletion driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"database/sql"
"testing"

zetasqlite "github.com/goccy/go-zetasqlite"
"github.com/google/go-cmp/cmp"

zetasqlite "github.com/goccy/go-zetasqlite"
)

func TestDriver(t *testing.T) {
Expand Down Expand Up @@ -110,6 +111,9 @@ CREATE TABLE IF NOT EXISTS Singers (
if err != nil {
t.Fatal(err)
}
if err := rows.Err(); err != nil {
t.Fatal(err)
}
resultCatalog, err := zetasqlite.ChangedCatalogFromResult(result)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -150,6 +154,9 @@ CREATE TABLE IF NOT EXISTS Singers (
if err != nil {
t.Fatal(err)
}
if err := rows.Err(); err != nil {
t.Fatal(err)
}
resultCatalog, err := zetasqlite.ChangedCatalogFromResult(result)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -202,6 +209,9 @@ CREATE TABLE IF NOT EXISTS Singers (
if err != nil {
t.Fatal(err)
}
if err := rows.Err(); err != nil {
t.Fatal(err)
}
if rows.Next() {
t.Fatal("found unexpected row; expected no rows")
}
Expand Down Expand Up @@ -236,6 +246,9 @@ CREATE TABLE IF NOT EXISTS Singers (
if err != nil {
t.Fatal(err)
}
if err := rows.Err(); err != nil {
t.Fatal(err)
}
if !rows.Next() {
t.Fatal("expected no rows; expected one row")
}
Expand Down
13 changes: 7 additions & 6 deletions exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
"testing"
"time"

zetasqlite "github.com/goccy/go-zetasqlite"
"github.com/google/go-cmp/cmp"

zetasqlite "github.com/goccy/go-zetasqlite"
)

func TestExec(t *testing.T) {
Expand Down Expand Up @@ -532,7 +533,7 @@ WITH Input AS (
defer rows.Close()

type queryRow struct {
JsonRow string
JSONRow string
Sum float64
}
results := []*queryRow{}
Expand All @@ -544,15 +545,15 @@ WITH Input AS (
if err := rows.Scan(&jsonRow, &sum); err != nil {
t.Fatal(err)
}
results = append(results, &queryRow{JsonRow: jsonRow, Sum: sum})
results = append(results, &queryRow{JSONRow: jsonRow, Sum: sum})
}
if rows.Err() != nil {
t.Fatal(rows.Err())
}
if diff := cmp.Diff(results, []*queryRow{
{JsonRow: `{"s":{"foo":1,"bar":2,"baz":{"x":"foo","foo":3.14}},"foo":10}`, Sum: 14.14},
{JsonRow: `{"s":null,"foo":4}`, Sum: 4},
{JsonRow: `{"s":{"foo":null,"bar":2,"baz":{"x":"fizz","foo":1.59}},"foo":null}`, Sum: 1.59},
{JSONRow: `{"s":{"foo":1,"bar":2,"baz":{"x":"foo","foo":3.14}},"foo":10}`, Sum: 14.14},
{JSONRow: `{"s":null,"foo":4}`, Sum: 4},
{JSONRow: `{"s":{"foo":null,"bar":2,"baz":{"x":"fizz","foo":1.59}},"foo":null}`, Sum: 1.59},
}); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
Expand Down
15 changes: 8 additions & 7 deletions internal/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func (a *Analyzer) newStmtAction(ctx context.Context, query string, args []drive
return nil, fmt.Errorf("unsupported stmt %s", node.DebugString())
}

func (a *Analyzer) newCreateTableStmtAction(ctx context.Context, query string, args []driver.NamedValue, node *ast.CreateTableStmtNode) (*CreateTableStmtAction, error) {
func (a *Analyzer) newCreateTableStmtAction(_ context.Context, query string, args []driver.NamedValue, node *ast.CreateTableStmtNode) (*CreateTableStmtAction, error) {
spec := newTableSpec(a.namePath, node)
params := getParamsFromNode(node)
queryArgs, err := getArgsFromParams(args, params)
Expand Down Expand Up @@ -319,7 +319,7 @@ func (a *Analyzer) newCreateTableAsSelectStmtAction(ctx context.Context, _ strin
}, nil
}

func (a *Analyzer) newCreateFunctionStmtAction(ctx context.Context, query string, args []driver.NamedValue, node *ast.CreateFunctionStmtNode) (*CreateFunctionStmtAction, error) {
func (a *Analyzer) newCreateFunctionStmtAction(ctx context.Context, query string, _ []driver.NamedValue, node *ast.CreateFunctionStmtNode) (*CreateFunctionStmtAction, error) {
var spec *FunctionSpec
if a.resultTypeIsTemplatedType(node.Signature()) {
realStmts, err := a.inferTemplatedTypeByRealType(query, node)
Expand All @@ -345,7 +345,7 @@ func (a *Analyzer) newCreateFunctionStmtAction(ctx context.Context, query string
}, nil
}

func (a *Analyzer) newCreateViewStmtAction(ctx context.Context, _ string, args []driver.NamedValue, node *ast.CreateViewStmtNode) (*CreateViewStmtAction, error) {
func (a *Analyzer) newCreateViewStmtAction(ctx context.Context, _ string, _ []driver.NamedValue, node *ast.CreateViewStmtNode) (*CreateViewStmtAction, error) {
query, err := newNode(node.Query()).FormatSQL(ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -529,12 +529,13 @@ func (a *Analyzer) newCommitStmtAction(ctx context.Context, query string, args [
return &CommitStmtAction{}, nil
}

func (a *Analyzer) newTruncateStmtAction(ctx context.Context, query string, args []driver.NamedValue, node *ast.TruncateStmtNode) (*TruncateStmtAction, error) {
//nolint:unparam
func (a *Analyzer) newTruncateStmtAction(_ context.Context, _ string, _ []driver.NamedValue, node *ast.TruncateStmtNode) (*TruncateStmtAction, error) {
table := node.TableScan().Table().Name()
return &TruncateStmtAction{query: fmt.Sprintf("DELETE FROM `%s`", table)}, nil
}

func (a *Analyzer) newMergeStmtAction(ctx context.Context, query string, args []driver.NamedValue, node *ast.MergeStmtNode) (*MergeStmtAction, error) {
func (a *Analyzer) newMergeStmtAction(ctx context.Context, _ string, args []driver.NamedValue, node *ast.MergeStmtNode) (*MergeStmtAction, error) {
targetTable, err := newNode(node.TableScan()).FormatSQL(ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -577,8 +578,8 @@ func (a *Analyzer) newMergeStmtAction(ctx context.Context, query string, args []
sourceColumn = colB.Column()
targetColumn = colA.Column()
}
mergedTableSourceColumnName := fmt.Sprintf("`%s`", string(uniqueColumnName(ctx, sourceColumn)))
mergedTableTargetColumnName := fmt.Sprintf("`%s`", string(uniqueColumnName(ctx, targetColumn)))
mergedTableSourceColumnName := fmt.Sprintf("`%s`", uniqueColumnName(ctx, sourceColumn))
mergedTableTargetColumnName := fmt.Sprintf("`%s`", uniqueColumnName(ctx, targetColumn))
mergedTableOutputColumns := []string{
mergedTableTargetColumnName,
mergedTableSourceColumnName,
Expand Down
3 changes: 3 additions & 0 deletions internal/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ func (c *Catalog) Sync(ctx context.Context, conn *Conn) error {
if err != nil {
return fmt.Errorf("failed to query load catalog: %w", err)
}
if err := rows.Err(); err != nil {
return err
}
defer rows.Close()
for rows.Next() {
var (
Expand Down
2 changes: 1 addition & 1 deletion internal/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (c *Conn) addTable(spec *TableSpec) {
c.cc.Table.Added = append(c.cc.Table.Added, spec)
}

//nolint: unused
//nolint:unused
func (c *Conn) updateTable(spec *TableSpec) {
c.cc.Table.Updated = append(c.cc.Table.Updated, spec)
}
Expand Down
30 changes: 15 additions & 15 deletions internal/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func LiteralFromValue(v Value) (string, error) {
if err != nil {
return "", fmt.Errorf("failed to encode value: %w", err)
}
return fmt.Sprintf(`"%s"`, base64.StdEncoding.EncodeToString(b)), nil
return fmt.Sprintf("%q", base64.StdEncoding.EncodeToString(b)), nil
}

func LiteralFromZetaSQLValue(v types.Value) (string, error) {
Expand All @@ -156,13 +156,13 @@ func ValueFromZetaSQLValue(v types.Value) (Value, error) {
case types.ENUM:
return stringValueFromLiteral(v.SQLLiteral(0))
case types.BYTES:
return bytesValueFromLiteral(v.SQLLiteral(0))
return bytesValueFromLiteral(v.SQLLiteral(0)), nil
case types.DATE:
return dateValueFromLiteral(v.ToInt64())
return dateValueFromLiteral(v.ToInt64()), nil
case types.DATETIME:
return datetimeValueFromLiteral(v.ToPacked64DatetimeMicros())
return datetimeValueFromLiteral(v.ToPacked64DatetimeMicros()), nil
case types.TIME:
return timeValueFromLiteral(v.ToPacked64TimeMicros())
return timeValueFromLiteral(v.ToPacked64TimeMicros()), nil
case types.TIMESTAMP:
microsec := v.ToUnixMicros()
microSecondsInSecond := int64(time.Second) / int64(time.Microsecond)
Expand Down Expand Up @@ -215,18 +215,18 @@ func stringValueFromLiteral(lit string) (StringValue, error) {
return StringValue(v), nil
}

func bytesValueFromLiteral(lit string) (BytesValue, error) {
func bytesValueFromLiteral(lit string) BytesValue {
// use a workaround because ToBytes doesn't work with certain values.
unquoted, err := strconv.Unquote(lit[1:])
if err != nil {
return BytesValue(lit), nil
return BytesValue(lit)
}
return BytesValue(unquoted), nil
return BytesValue(unquoted)
}

func dateValueFromLiteral(days int64) (DateValue, error) {
func dateValueFromLiteral(days int64) DateValue {
t := time.Unix(int64(time.Duration(days)*24*time.Hour/time.Second), 0)
return DateValue(t), nil
return DateValue(t)
}

const (
Expand All @@ -245,7 +245,7 @@ const (
yearMask = 0x3FFF << yearShift
)

func datetimeValueFromLiteral(bit int64) (DatetimeValue, error) {
func datetimeValueFromLiteral(bit int64) DatetimeValue {
b := bit >> 20
year := (b & yearMask) >> yearShift
month := (b & monthMask) >> monthShift
Expand All @@ -263,17 +263,17 @@ func datetimeValueFromLiteral(bit int64) (DatetimeValue, error) {
int(sec),
int(microSec)*1000, time.UTC,
)
return DatetimeValue(t), nil
return DatetimeValue(t)
}

func timeValueFromLiteral(bit int64) (TimeValue, error) {
func timeValueFromLiteral(bit int64) TimeValue {
b := bit >> 20
hour := (b & hourMask) >> hourShift
min := (b & minMask) >> minShift
sec := (b & secMask) >> secShift
microSec := (bit & microSecMask) >> 0
t := time.Date(0, 0, 0, int(hour), int(min), int(sec), int(microSec)*1000, time.UTC)
return TimeValue(t), nil
return TimeValue(t)
}

func timestampValueFromLiteral(t time.Time) (TimestampValue, error) {
Expand All @@ -295,7 +295,7 @@ func numericValueFromLiteral(lit string) (*NumericValue, error) {
numericLit := matches[0][1]
r := new(big.Rat)
r.SetString(numericLit)
if strings.Contains("BIGNUMERIC", lit) {
if strings.Contains(lit, "BIGNUMERIC") {
return &NumericValue{Rat: r, isBigNumeric: true}, nil
}
return &NumericValue{Rat: r}, nil
Expand Down
Loading
Loading