Skip to content

Commit

Permalink
Use any instead of interface{} (#984)
Browse files Browse the repository at this point in the history
* ignore vendor

* fix codegen

* `interface{}` -> `any`

* remove contributors list changes (must be fixed manually)

* lint rules to ensure any
  • Loading branch information
candiduslynx authored May 5, 2023
1 parent 1812cf4 commit eec85ec
Show file tree
Hide file tree
Showing 134 changed files with 1,499 additions and 720 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ _testmain.go
coverage.txt
.idea/**
dev/*
.run/**
.run/**

vendor
71 changes: 71 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
run:
tests: false
skip-dirs:
- benchmark
- tests
- internal/cmd

linters-settings:
gocritic:
disabled-checks:
- singleCaseSwitch
- commentFormatting

decorder:
dec-order:
- type
- const
- var
- func
disable-dec-order-check: false

revive:
enable-all-rules: true
rules:
- name: cyclomatic
disabled: true
- name: argument-limit
disabled: true
- name: function-length
disabled: true
- name: function-result-limit
disabled: true
- name: line-length-limit
disabled: true
- name: file-header
disabled: true
- name: cognitive-complexity
disabled: true
- name: banned-characters
disabled: true
- name: max-public-structs
disabled: true
- name: add-constant
disabled: true
- name: unhandled-error
disabled: true
- name: deep-exit
disabled: true
- name: nested-structs
disabled: true

gofmt:
rewrite-rules:
- pattern: 'interface{}'
replacement: 'any'
- pattern: 'a[b:len(a)]'
replacement: 'a[b:]'

linters:
disable-all: true
enable:
- asciicheck
- bodyclose
- depguard
- gocritic
- gofmt
- govet
- ineffassign
- importas
- misspell
- staticcheck
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ test:

lint:
golangci-lint run || :
gocritic check -disable=singleCaseSwitch ./... || :

contributors:
@git log --pretty="%an <%ae>%n%cn <%ce>" | sort -u -t '<' -k 2,2 | LC_ALL=C sort | \
Expand All @@ -27,7 +26,7 @@ staticcheck:
staticcheck ./...

codegen: contributors
@cd lib/column && go run codegen/main.go
@go run lib/column/codegen/main.go
@go-licenser -licensor "ClickHouse, Inc."

.PHONY: contributors
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Support for the ClickHouse protocol advanced features using `Context`:
return d.DialContext(ctx, "tcp", addr)
},
Debug: true,
Debugf: func(format string, v ...interface{}) {
Debugf: func(format string, v ...any) {
fmt.Printf(format, v)
},
Settings: clickhouse.Settings{
Expand Down
22 changes: 11 additions & 11 deletions bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
)

func Named(name string, value interface{}) driver.NamedValue {
func Named(name string, value any) driver.NamedValue {
return driver.NamedValue{
Name: name,
Value: value,
Expand All @@ -45,10 +45,10 @@ const (
)

type GroupSet struct {
Value []interface{}
Value []any
}

type ArraySet []interface{}
type ArraySet []any

func DateNamed(name string, value time.Time, scale TimeUnit) driver.NamedDateValue {
return driver.NamedDateValue{
Expand All @@ -61,7 +61,7 @@ func DateNamed(name string, value time.Time, scale TimeUnit) driver.NamedDateVal
var bindNumericRe = regexp.MustCompile(`\$[0-9]+`)
var bindPositionalRe = regexp.MustCompile(`[^\\][?]`)

func bind(tz *time.Location, query string, args ...interface{}) (string, error) {
func bind(tz *time.Location, query string, args ...any) (string, error) {
if len(args) == 0 {
return query, nil
}
Expand Down Expand Up @@ -90,7 +90,7 @@ func bind(tz *time.Location, query string, args ...interface{}) (string, error)
return bindPositional(tz, query, args...)
}

func checkAllNamedArguments(args ...interface{}) (bool, error) {
func checkAllNamedArguments(args ...any) (bool, error) {
var (
haveNamed bool
haveAnonymous bool
Expand All @@ -111,7 +111,7 @@ func checkAllNamedArguments(args ...interface{}) (bool, error) {

var bindPositionCharRe = regexp.MustCompile(`[?]`)

func bindPositional(tz *time.Location, query string, args ...interface{}) (_ string, err error) {
func bindPositional(tz *time.Location, query string, args ...any) (_ string, err error) {
var (
unbind = make(map[int]struct{})
params = make([]string, len(args))
Expand Down Expand Up @@ -146,7 +146,7 @@ func bindPositional(tz *time.Location, query string, args ...interface{}) (_ str
return strings.ReplaceAll(query, "\\?", "?"), nil
}

func bindNumeric(tz *time.Location, query string, args ...interface{}) (_ string, err error) {
func bindNumeric(tz *time.Location, query string, args ...any) (_ string, err error) {
var (
unbind = make(map[string]struct{})
params = make(map[string]string)
Expand Down Expand Up @@ -178,7 +178,7 @@ func bindNumeric(tz *time.Location, query string, args ...interface{}) (_ string

var bindNamedRe = regexp.MustCompile(`@[a-zA-Z0-9\_]+`)

func bindNamed(tz *time.Location, query string, args ...interface{}) (_ string, err error) {
func bindNamed(tz *time.Location, query string, args ...any) (_ string, err error) {
var (
unbind = make(map[string]struct{})
params = make(map[string]string)
Expand Down Expand Up @@ -243,7 +243,7 @@ func formatTime(tz *time.Location, scale TimeUnit, value time.Time) (string, err
return fmt.Sprintf("toDateTime64('%s', %d, '%s')", value.Format(fmt.Sprintf("2006-01-02 15:04:05.%0*d", int(scale*3), 0)), int(scale*3), value.Location().String()), nil
}

func format(tz *time.Location, scale TimeUnit, v interface{}) (string, error) {
func format(tz *time.Location, scale TimeUnit, v any) (string, error) {
quote := func(v string) string {
return "'" + strings.NewReplacer(`\`, `\\`, `'`, `\'`).Replace(v) + "'"
}
Expand Down Expand Up @@ -328,8 +328,8 @@ func join[E any](tz *time.Location, scale TimeUnit, values []E) (string, error)
return strings.Join(items, ", "), nil
}

func rebind(in []std_driver.NamedValue) []interface{} {
args := make([]interface{}, 0, len(in))
func rebind(in []std_driver.NamedValue) []any {
args := make([]any, 0, len(in))
for _, v := range in {
switch {
case len(v.Name) != 0:
Expand Down
58 changes: 29 additions & 29 deletions bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ func TestBindNumeric(t *testing.T) {
)
`, 1, 2, "I'm a string param", nil)
var nilPtr *bool = nil
var nilPtrPtr **interface{} = nil
valuedPtr := &([]interface{}{123}[0])
nilValuePtr := &([]interface{}{nil}[0])
var nilPtrPtr **any = nil
valuedPtr := &([]any{123}[0])
nilValuePtr := &([]any{nil}[0])
_, err = bind(time.Local, `
SELECT * FROM t WHERE col = $5
AND col2 = $2
Expand All @@ -50,27 +50,27 @@ func TestBindNumeric(t *testing.T) {
if assert.NoError(t, err) {
assets := []struct {
query string
params []interface{}
params []any
expected string
}{
{
query: "SELECT $1",
params: []interface{}{1},
params: []any{1},
expected: "SELECT 1",
},
{
query: "SELECT $2 $1 $3",
params: []interface{}{1, 2, 3},
params: []any{1, 2, 3},
expected: "SELECT 2 1 3",
},
{
query: "SELECT $2 $1 $3",
params: []interface{}{"a", "b", "c"},
params: []any{"a", "b", "c"},
expected: "SELECT 'b' 'a' 'c'",
},
{
query: "SELECT $2 $1",
params: []interface{}{true, false},
params: []any{true, false},
expected: "SELECT 0 1",
},
}
Expand Down Expand Up @@ -99,9 +99,9 @@ func TestBindNamed(t *testing.T) {
Named("col4", nil),
)
var nilPtr *bool = nil
var nilPtrPtr **interface{} = nil
valuedPtr := &([]interface{}{123}[0])
nilValuePtr := &([]interface{}{nil}[0])
var nilPtrPtr **any = nil
valuedPtr := &([]any{123}[0])
nilValuePtr := &([]any{nil}[0])
_, err = bind(time.Local, `
SELECT * FROM t WHERE col = @col1
AND col2 = @col2
Expand All @@ -119,19 +119,19 @@ func TestBindNamed(t *testing.T) {
if assert.NoError(t, err) {
assets := []struct {
query string
params []interface{}
params []any
expected string
}{
{
query: "SELECT @col1",
params: []interface{}{
params: []any{
Named("col1", 1),
},
expected: "SELECT 1",
},
{
query: "SELECT @col2 @col1 @col3",
params: []interface{}{
params: []any{
Named("col1", 1),
Named("col2", 2),
Named("col3", 3),
Expand All @@ -140,7 +140,7 @@ func TestBindNamed(t *testing.T) {
},
{
query: "SELECT @col2 @col1 @col3",
params: []interface{}{
params: []any{
Named("col1", "a"),
Named("col2", "b"),
Named("col3", "c"),
Expand All @@ -149,7 +149,7 @@ func TestBindNamed(t *testing.T) {
},
{
query: "SELECT @col2 @col1",
params: []interface{}{
params: []any{
Named("col1", true),
Named("col2", false),
},
Expand All @@ -176,37 +176,37 @@ func TestBindPositional(t *testing.T) {
if assert.NoError(t, err) {
assets := []struct {
query string
params []interface{}
params []any
expected string
}{
{
query: "SELECT ?",
params: []interface{}{1},
params: []any{1},
expected: "SELECT 1",
},
{
query: "SELECT ? ? ?",
params: []interface{}{1, 2, 3},
params: []any{1, 2, 3},
expected: "SELECT 1 2 3",
},
{
query: "SELECT ? ? ?",
params: []interface{}{"a", "b", "c"},
params: []any{"a", "b", "c"},
expected: "SELECT 'a' 'b' 'c'",
},
{
query: "SELECT ? ? '\\?'",
params: []interface{}{"a", "b"},
params: []any{"a", "b"},
expected: "SELECT 'a' 'b' '?'",
},
{
query: "SELECT x where col = 'blah\\?' AND col2 = ?",
params: []interface{}{"a"},
params: []any{"a"},
expected: "SELECT x where col = 'blah?' AND col2 = 'a'",
},
{
query: "SELECT ? ?",
params: []interface{}{true, false},
params: []any{true, false},
expected: "SELECT 1 0",
},
}
Expand All @@ -229,9 +229,9 @@ func TestBindPositional(t *testing.T) {
assert.Error(t, err)

var nilPtr *bool = nil
var nilPtrPtr **interface{} = nil
valuedPtr := &([]interface{}{123}[0])
nilValuePtr := &([]interface{}{nil}[0])
var nilPtrPtr **any = nil
valuedPtr := &([]any{123}[0])
nilValuePtr := &([]any{nil}[0])

_, err = bind(time.Local, `
SELECT * FROM t WHERE col = ?
Expand Down Expand Up @@ -307,13 +307,13 @@ func TestStringBasedType(t *testing.T) {
}

func TestFormatGroup(t *testing.T) {
groupSet := GroupSet{Value: []interface{}{"A", 1}}
groupSet := GroupSet{Value: []any{"A", 1}}
val, _ := format(time.UTC, Seconds, groupSet)
assert.Equal(t, "('A', 1)", val)
{
tuples := []GroupSet{
{Value: []interface{}{"A", 1}},
{Value: []interface{}{"B", 2}},
{Value: []any{"A", 1}},
{Value: []any{"B", 2}},
}
val, _ = format(time.UTC, Seconds, tuples)
assert.Equal(t, "('A', 1), ('B', 2)", val)
Expand Down
6 changes: 3 additions & 3 deletions clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (ch *clickhouse) ServerVersion() (*driver.ServerVersion, error) {
return &conn.server, nil
}

func (ch *clickhouse) Query(ctx context.Context, query string, args ...interface{}) (rows driver.Rows, err error) {
func (ch *clickhouse) Query(ctx context.Context, query string, args ...any) (rows driver.Rows, err error) {
conn, err := ch.acquire(ctx)
if err != nil {
return nil, err
Expand All @@ -122,7 +122,7 @@ func (ch *clickhouse) Query(ctx context.Context, query string, args ...interface
return conn.query(ctx, ch.release, query, args...)
}

func (ch *clickhouse) QueryRow(ctx context.Context, query string, args ...interface{}) (rows driver.Row) {
func (ch *clickhouse) QueryRow(ctx context.Context, query string, args ...any) (rows driver.Row) {
conn, err := ch.acquire(ctx)
if err != nil {
return &row{
Expand All @@ -133,7 +133,7 @@ func (ch *clickhouse) QueryRow(ctx context.Context, query string, args ...interf
return conn.queryRow(ctx, ch.release, query, args...)
}

func (ch *clickhouse) Exec(ctx context.Context, query string, args ...interface{}) error {
func (ch *clickhouse) Exec(ctx context.Context, query string, args ...any) error {
conn, err := ch.acquire(ctx)
if err != nil {
return err
Expand Down
Loading

0 comments on commit eec85ec

Please sign in to comment.