diff --git a/.gitignore b/.gitignore index 0b9f9b8..3d443ac 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ .idea/ _vendor/ +vendor/ +coverage.out +Gopkg.lock diff --git a/.travis.yml b/.travis.yml index f0d2dc9..8e4ac05 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,29 +3,25 @@ notifications: email: false language: go go: - - 1.8 - - 1.9 - - "1.10" + - '1.9' + - '1.10' + - '1.11' services: - docker before_install: - travis_retry docker pull yandex/clickhouse-server - - docker run -d -p 127.0.0.1:8123:8123 --name dbr-clickhouse-server yandex/clickhouse-server + - make up_docker_server + - curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh install: - - travis_retry go get -u github.com/golang/lint/golint - - travis_retry go get github.com/mattn/goveralls - - travis_retry go get golang.org/x/tools/cmd/cover - - travis_retry go get github.com/stretchr/testify + - make init + - travis_retry go get -v github.com/mattn/goveralls/... before_script: - export TEST_CLICKHOUSE_DSN="http://localhost:8123/default" script: - - test -z "$(golint ./... | tee /dev/stderr)" - - go vet ./... - - test -z "$(gofmt -d -s . | tee /dev/stderr)" - - go test -v -covermode=count -coverprofile=coverage.out . + - make test - goveralls -coverprofile=coverage.out -service=travis-ci diff --git a/Gopkg.toml b/Gopkg.toml new file mode 100644 index 0000000..1e3d2c5 --- /dev/null +++ b/Gopkg.toml @@ -0,0 +1,14 @@ +# for tests +required = ["github.com/golang/lint/golint"] + +[[override]] + name = "golang.org/x/tools" + revision = "a019f6b7c5bfcffdf421924fc0ddb74b867a53f2" + +[[constraint]] + name = "github.com/stretchr/testify" + version = "1.2.2" + +[prune] + go-tests = true + unused-packages = true diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e6b3ab9 --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +SHELL := /bin/bash + +init: + dep ensure -v + go install ./vendor/... + +up_docker_server: stop_docker_server + docker run --rm=true -p 127.0.0.1:8123:8123 --name dbr-clickhouse-server -d yandex/clickhouse-server; + +stop_docker_server: + test -n "$$(docker ps --format {{.Names}} | grep dbr-clickhouse-server)" && docker stop dbr-clickhouse-server || true + +test: up_docker_server + test -z "$$(golint ./... | grep -v vendor | tee /dev/stderr)" + go vet -v ./... + test -z "$$(gofmt -d -s $$(find . -name \*.go -print | grep -v vendor) | tee /dev/stderr)" + go test -v -covermode=count -coverprofile=coverage.out . + $(MAKE) stop_docker_server \ No newline at end of file diff --git a/README.md b/README.md index dabe37e..c324ed3 100644 --- a/README.md +++ b/README.md @@ -86,7 +86,18 @@ func main() { if err != nil { log.Fatal(err) } - stmt, err := tx.Prepare("INSERT INTO example (country_code, os_id, browser_id, categories, action_day, action_time) VALUES (?, ?, ?, ?, ?, ?)") + stmt, err := tx.Prepare(` + INSERT INTO example ( + country_code, + os_id, + browser_id, + categories, + action_day, + action_time + ) VALUES ( + ?, ?, ?, ?, ?, ? + )`) + if err != nil { log.Fatal(err) } @@ -108,7 +119,17 @@ func main() { log.Fatal(err) } - rows, err := connect.Query("SELECT country_code, os_id, browser_id, categories, action_day, action_time FROM example") + rows, err := connect.Query(` + SELECT + country_code, + os_id, + browser_id, + categories, + action_day, + action_time + FROM + example`) + if err != nil { log.Fatal(err) } @@ -120,10 +141,19 @@ func main() { categories []int16 actionDay, actionTime time.Time ) - if err := rows.Scan(&country, &os, &browser, &categories, &actionDay, &actionTime); err != nil { + if err := rows.Scan( + &country, + &os, + &browser, + &categories, + &actionDay, + &actionTime, + ); err != nil { log.Fatal(err) } - log.Printf("country: %s, os: %d, browser: %d, categories: %v, action_day: %s, action_time: %s", country, os, browser, categories, actionDay, actionTime) + log.Printf("country: %s, os: %d, browser: %d, categories: %v, action_day: %s, action_time: %s", + country, os, browser, categories, actionDay, actionTime, + ) } } ``` @@ -161,7 +191,23 @@ func main() { } for _, item := range items { - log.Printf("country: %s, os: %d, browser: %d, categories: %v, action_time: %s", item.CountryCode, item.OsID, item.BrowserID, item.Categories, item.ActionTime) + log.Printf("country: %s, os: %d, browser: %d, categories: %v, action_time: %s", + item.CountryCode, item.OsID, item.BrowserID, item.Categories, item.ActionTime, + ) } } ``` + +## Go versions +Officially support last 3 golang releases + + +## Development +You can check the effect of changes on Travis CI or run tests locally: + +``` bash +make init # dep ensure and install +make test +``` + +_Remember that `make init` will add a few binaries used for testing (like `golint` and it's dependencies) into your GOPATH_ diff --git a/config.go b/config.go index ffc888e..28e5976 100644 --- a/config.go +++ b/config.go @@ -110,8 +110,7 @@ func ParseDSN(dsn string) (*Config, error) { } cfg := NewConfig() - cfg.Scheme = u.Scheme - cfg.Host = u.Host + cfg.Scheme, cfg.Host = u.Scheme, u.Host if len(u.Path) > 1 { // skip '/' cfg.Database = u.Path[1:] diff --git a/helpers.go b/helpers.go index d967fcc..c8b664c 100644 --- a/helpers.go +++ b/helpers.go @@ -42,7 +42,7 @@ func formatDate(value time.Time) string { func readResponse(response *http.Response) (result []byte, err error) { if response.ContentLength > 0 { - result = make([]byte, response.ContentLength) + result = make([]byte, 0, response.ContentLength) } buf := bytes.NewBuffer(result) defer response.Body.Close() @@ -52,7 +52,7 @@ func readResponse(response *http.Response) (result []byte, err error) { } func numOfColumns(data []byte) int { - cnt := 0 + var cnt int for _, ch := range data { switch ch { case '\t': @@ -66,8 +66,7 @@ func numOfColumns(data []byte) int { // splitTSV splits one row of tab separated values, returns begin of next row func splitTSV(data []byte, out []string) int { - i := 0 - k := 0 + var i, k int for j, ch := range data { switch ch { case '\t':