From 82303d72ec2b45bdc69db8ea9104c1d9ad6f99ac Mon Sep 17 00:00:00 2001 From: filipecosta90 Date: Mon, 20 Jul 2020 12:15:20 +0100 Subject: [PATCH] [add] Added go fmt check to circleci. Applied go fmt ./... to the entire project. --- .circleci/config.yml | 3 ++- Makefile | 11 ++++++++++ .../redisearch_temporary_index.go | 2 +- redisearch/aggregate_test.go | 14 +++++------- redisearch/autocomplete.go | 2 +- redisearch/autocomplete_test.go | 10 ++++----- redisearch/client.go | 4 ++-- redisearch/client_test.go | 4 ++-- redisearch/document.go | 18 +++++++-------- redisearch/document_test.go | 22 +++++++++---------- redisearch/pool_test.go | 14 ++++++------ redisearch/reducer.go | 2 +- redisearch/schema.go | 2 +- redisearch/schema_test.go | 2 +- 14 files changed, 59 insertions(+), 51 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index a76a359..9ab1cc7 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -17,7 +17,7 @@ jobs: - run: name: Generate a root CA and a server certificate using redis helpers command: | - git clone git://github.com/antirez/redis.git --branch 6.0.1 + git clone git://github.com/antirez/redis.git --branch 6.0.5 cd redis ./utils/gen-test-certs.sh cd .. @@ -52,6 +52,7 @@ jobs: steps: - checkout - run: make get + - run: make checkfmt - run: make coverage - run: bash <(curl -s https://codecov.io/bash) -t ${CODECOV_TOKEN} diff --git a/Makefile b/Makefile index c6fec56..a886a7d 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,7 @@ GOCLEAN=$(GOCMD) clean GOTEST=$(GOCMD) test GOGET=$(GOCMD) get GOMOD=$(GOCMD) mod +GOFMT=$(GOCMD) fmt .PHONY: all test coverage all: test coverage examples @@ -18,6 +19,15 @@ TLS_KEY ?= redis.key TLS_CACERT ?= ca.crt REDISEARCH_TEST_HOST ?= 127.0.0.1:6379 +checkfmt: + @echo 'Checking gofmt';\ + bash -c "diff -u <(echo -n) <(gofmt -d .)";\ + EXIT_CODE=$$?;\ + if [ "$$EXIT_CODE" -ne 0 ]; then \ + echo '$@: Go files must be formatted with gofmt'; \ + fi && \ + exit $$EXIT_CODE + examples: get @echo " " @echo "Building the examples..." @@ -31,6 +41,7 @@ examples: get --host $(REDISEARCH_TEST_HOST) test: get + $(GOFMT) ./... $(GOTEST) -race -covermode=atomic ./... coverage: get test diff --git a/examples/redisearch_temporary_index/redisearch_temporary_index.go b/examples/redisearch_temporary_index/redisearch_temporary_index.go index c979c6f..99800f7 100644 --- a/examples/redisearch_temporary_index/redisearch_temporary_index.go +++ b/examples/redisearch_temporary_index/redisearch_temporary_index.go @@ -51,7 +51,7 @@ func main() { fmt.Println(docs[0].Id, docs[0].Properties["title"], total, err) // Output: doc1 Hello world 1 - time.Sleep(15*time.Second) + time.Sleep(15 * time.Second) // Searching with limit and sorting _, err = c.Info() fmt.Println(err) diff --git a/redisearch/aggregate_test.go b/redisearch/aggregate_test.go index 278257d..f2c2d31 100644 --- a/redisearch/aggregate_test.go +++ b/redisearch/aggregate_test.go @@ -16,7 +16,6 @@ import ( "testing" ) - // Game struct which contains a Asin, a Description, a Title, a Price, and a list of categories // a type and a list of social links @@ -30,7 +29,6 @@ type Game struct { Categories []string `json:"categories"` } - func init() { /* load test data */ value, exists := os.LookupEnv("REDISEARCH_RDB_LOADED") @@ -317,9 +315,9 @@ func TestCursor_Serialize(t *testing.T) { fields fields want redis.Args }{ - {"TestCursor_Serialize_1", fields{1, 0, 0,}, redis.Args{"WITHCURSOR"}}, - {"TestCursor_Serialize_2_MAXIDLE", fields{1, 0, 30000,}, redis.Args{"WITHCURSOR", "MAXIDLE", 30000}}, - {"TestCursor_Serialize_3_COUNT_MAXIDLE", fields{1, 10, 30000,}, redis.Args{"WITHCURSOR", "COUNT", 10, "MAXIDLE", 30000}}, + {"TestCursor_Serialize_1", fields{1, 0, 0}, redis.Args{"WITHCURSOR"}}, + {"TestCursor_Serialize_2_MAXIDLE", fields{1, 0, 30000}, redis.Args{"WITHCURSOR", "MAXIDLE", 30000}}, + {"TestCursor_Serialize_3_COUNT_MAXIDLE", fields{1, 10, 30000}, redis.Args{"WITHCURSOR", "COUNT", 10, "MAXIDLE", 30000}}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -352,7 +350,7 @@ func TestGroupBy_AddFields(t *testing.T) { }{ {"TestGroupBy_AddFields_1", fields{[]string{}, nil, nil}, - args{"a",}, + args{"a"}, &GroupBy{[]string{"a"}, nil, nil}, }, } @@ -651,8 +649,8 @@ func TestProcessAggResponse(t *testing.T) { args args want [][]string }{ - {"empty-reply", args{[]interface{}{}}, [][]string{},}, - {"1-element-reply", args{[]interface{}{[]interface{}{"userFullName", "berge, julius", "count", "2783"}}}, [][]string{{"userFullName", "berge, julius", "count", "2783"}},}, + {"empty-reply", args{[]interface{}{}}, [][]string{}}, + {"1-element-reply", args{[]interface{}{[]interface{}{"userFullName", "berge, julius", "count", "2783"}}}, [][]string{{"userFullName", "berge, julius", "count", "2783"}}}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/redisearch/autocomplete.go b/redisearch/autocomplete.go index 13e6df6..1c020ff 100644 --- a/redisearch/autocomplete.go +++ b/redisearch/autocomplete.go @@ -1,8 +1,8 @@ package redisearch import ( - "strconv" "github.com/gomodule/redigo/redis" + "strconv" ) // Autocompleter implements a redisearch auto-completer API diff --git a/redisearch/autocomplete_test.go b/redisearch/autocomplete_test.go index 8ce8cba..5de6a0c 100644 --- a/redisearch/autocomplete_test.go +++ b/redisearch/autocomplete_test.go @@ -34,11 +34,11 @@ func TestAutocompleter_Serialize(t *testing.T) { want redis.Args want1 int }{ - {"default options", fields{"key1"}, args{"ab", DefaultSuggestOptions,}, redis.Args{"key1", "ab", "MAX", 5}, 1}, - {"FUZZY", fields{"key1"}, args{"ab", fuzzy,}, redis.Args{"key1", "ab", "MAX", 5, "FUZZY"}, 1}, - {"WITHSCORES", fields{"key1"}, args{"ab", withscores,}, redis.Args{"key1", "ab", "MAX", 5, "WITHSCORES"}, 2}, - {"WITHPAYLOADS", fields{"key1"}, args{"ab", withpayloads,}, redis.Args{"key1", "ab", "MAX", 5, "WITHPAYLOADS"}, 2}, - {"all", fields{"key1"}, args{"ab", all,}, redis.Args{"key1", "ab", "MAX", 5, "FUZZY", "WITHSCORES", "WITHPAYLOADS"}, 3}, + {"default options", fields{"key1"}, args{"ab", DefaultSuggestOptions}, redis.Args{"key1", "ab", "MAX", 5}, 1}, + {"FUZZY", fields{"key1"}, args{"ab", fuzzy}, redis.Args{"key1", "ab", "MAX", 5, "FUZZY"}, 1}, + {"WITHSCORES", fields{"key1"}, args{"ab", withscores}, redis.Args{"key1", "ab", "MAX", 5, "WITHSCORES"}, 2}, + {"WITHPAYLOADS", fields{"key1"}, args{"ab", withpayloads}, redis.Args{"key1", "ab", "MAX", 5, "WITHPAYLOADS"}, 2}, + {"all", fields{"key1"}, args{"ab", all}, redis.Args{"key1", "ab", "MAX", 5, "FUZZY", "WITHSCORES", "WITHPAYLOADS"}, 3}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/redisearch/client.go b/redisearch/client.go index 29411e7..8cce654 100644 --- a/redisearch/client.go +++ b/redisearch/client.go @@ -2,11 +2,11 @@ package redisearch import ( "errors" + "github.com/gomodule/redigo/redis" "log" "reflect" "strconv" "strings" - "github.com/gomodule/redigo/redis" ) // Client is an interface to redisearch's redis commands @@ -65,7 +65,7 @@ func (i *Client) CreateIndex(s *Schema) (err error) { func (i *Client) AddField(f Field) error { args := redis.Args{i.name} args = append(args, "SCHEMA", "ADD") - args,err := serializeField(f,args) + args, err := serializeField(f, args) if err != nil { return err } diff --git a/redisearch/client_test.go b/redisearch/client_test.go index 5bd68c7..5160439 100644 --- a/redisearch/client_test.go +++ b/redisearch/client_test.go @@ -541,7 +541,7 @@ func TestClient_AddField(t *testing.T) { assert.Nil(t, err) err = c.AddField(NewNumericField("age")) assert.Nil(t, err) - err = c.Index(NewDocument("doc-n1",1.0).Set("age",15 )) + err = c.Index(NewDocument("doc-n1", 1.0).Set("age", 15)) assert.Nil(t, err) } @@ -572,4 +572,4 @@ func TestClient_CreateIndex(t *testing.T) { } }) } -} \ No newline at end of file +} diff --git a/redisearch/document.go b/redisearch/document.go index 87ddee0..e39b9bb 100644 --- a/redisearch/document.go +++ b/redisearch/document.go @@ -20,21 +20,20 @@ type Document struct { Properties map[string]interface{} } - // IndexingOptions represent the options for indexing a single document type IndexingOptions struct { // If set, we use a stemmer for the supplied language during indexing. If set to "", we Default to English. - Language string + Language string // If set to true, we will not save the actual document in the database and only index it. - NoSave bool + NoSave bool // If set, we will do an UPSERT style insertion - and delete an older version of the document if it exists. - Replace bool + Replace bool // (only applicable with Replace): If set, you do not have to specify all fields for reindexing. - Partial bool + Partial bool // Applicable only in conjunction with Replace and optionally Partial // Update the document only if a boolean expression applies to the document before the update @@ -79,9 +78,9 @@ func (d Document) Set(name string, value interface{}) Document { // to signify an actual backslash, so the actual text in redis-cli for example, will be entered as `hello\\-world`. // Underscores (`_`) are not used as separators in either document or query. // So the text `hello_world` will remain as is after tokenization. -func EscapeTextFileString(value string) (string) { +func EscapeTextFileString(value string) string { for _, char := range field_tokenization { - value = strings.Replace(value, string(char), ("\\"+string(char)), -1 ) + value = strings.Replace(value, string(char), ("\\" + string(char)), -1) } return value } @@ -111,9 +110,8 @@ func loadDocument(arr []interface{}, idIdx, scoreIdx, payloadIdx, fieldsIdx int) return doc, nil } - // SetPayload Sets the document payload -func (d *Document) loadFields(lst []interface{}) *Document{ +func (d *Document) loadFields(lst []interface{}) *Document { for i := 0; i < len(lst); i += 2 { var prop string switch lst[i].(type) { @@ -130,7 +128,7 @@ func (d *Document) loadFields(lst []interface{}) *Document{ default: val = v } - *d = d.Set(prop,val) + *d = d.Set(prop, val) } return d } diff --git a/redisearch/document_test.go b/redisearch/document_test.go index 614a20a..96cce0d 100644 --- a/redisearch/document_test.go +++ b/redisearch/document_test.go @@ -15,10 +15,10 @@ func TestEscapeTextFileString(t *testing.T) { want string }{ { - "url", args{"https://en.wikipedia.org/wiki",}, "https\\://en\\.wikipedia\\.org/wiki", + "url", args{"https://en.wikipedia.org/wiki"}, "https\\://en\\.wikipedia\\.org/wiki", }, { - "hello_world", args{"hello_world",}, "hello_world", + "hello_world", args{"hello_world"}, "hello_world", }, } for _, tt := range tests { @@ -43,13 +43,13 @@ func TestDocument_EstimateSize(t *testing.T) { wantSz int }{ { - "only-id", fields{"doc1", 1.0, []byte{}, map[string]interface{}{},}, len("doc1"), + "only-id", fields{"doc1", 1.0, []byte{}, map[string]interface{}{}}, len("doc1"), }, { - "id-payload", fields{"doc1", 1.0, []byte("payload"), map[string]interface{}{},}, len("doc1") + len([]byte("payload")), + "id-payload", fields{"doc1", 1.0, []byte("payload"), map[string]interface{}{}}, len("doc1") + len([]byte("payload")), }, { - "id-payload-fields", fields{"doc1", 1.0, []byte("payload"), map[string]interface{}{"text1": []byte("text1")},}, len("doc1") + len([]byte("payload")) + 2*len([]byte("text1")), + "id-payload-fields", fields{"doc1", 1.0, []byte("payload"), map[string]interface{}{"text1": []byte("text1")}}, len("doc1") + len([]byte("payload")) + 2*len([]byte("text1")), }, } for _, tt := range tests { @@ -78,14 +78,14 @@ func TestDocument_SetPayload(t *testing.T) { payload []byte } tests := []struct { - name string - fields fields - args args + name string + fields fields + args args wantPayload []byte }{ - {"empty-payload", fields{"doc1", 1.0, []byte{}, map[string]interface{}{},}, args{[]byte{}}, []byte{}}, - {"simple-set", fields{"doc1", 1.0, []byte{}, map[string]interface{}{},}, args{[]byte("payload")},[]byte("payload")}, - {"set-with-previous-payload", fields{"doc1", 1.0, []byte("previous_payload"), map[string]interface{}{},}, args{[]byte("payload")},[]byte("payload")}, + {"empty-payload", fields{"doc1", 1.0, []byte{}, map[string]interface{}{}}, args{[]byte{}}, []byte{}}, + {"simple-set", fields{"doc1", 1.0, []byte{}, map[string]interface{}{}}, args{[]byte("payload")}, []byte("payload")}, + {"set-with-previous-payload", fields{"doc1", 1.0, []byte("previous_payload"), map[string]interface{}{}}, args{[]byte("payload")}, []byte("payload")}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/redisearch/pool_test.go b/redisearch/pool_test.go index 71a1796..1bbc468 100644 --- a/redisearch/pool_test.go +++ b/redisearch/pool_test.go @@ -15,8 +15,8 @@ func TestNewMultiHostPool(t *testing.T) { name string args args }{ - {"multi-host single address", args{[]string{host,},},}, - {"multi-host several addresses", args{[]string{host, host,},},}, + {"multi-host single address", args{[]string{host}}}, + {"multi-host several addresses", args{[]string{host, host}}}, } if password == "" { for _, tt := range tests { @@ -37,14 +37,14 @@ func TestMultiHostPool_Close(t *testing.T) { host, password := getTestConnectionDetails() // Test a simple flow if password == "" { - oneMulti := NewMultiHostPool([]string{host,}) + oneMulti := NewMultiHostPool([]string{host}) conn := oneMulti.Get() assert.NotNil(t, conn) err := oneMulti.Close() assert.Nil(t, err) err = oneMulti.Close() assert.NotNil(t, conn) - severalMulti := NewMultiHostPool([]string{host, host,}) + severalMulti := NewMultiHostPool([]string{host, host}) connMulti := severalMulti.Get() assert.NotNil(t, connMulti) err = severalMulti.Close() @@ -70,9 +70,9 @@ func TestMultiHostPool_Close(t *testing.T) { fields fields wantErr bool }{ - {"empty", fields{map[string]*redis.Pool{}, []string{}}, false,}, - {"normal", fields{map[string]*redis.Pool{"hostpool1": pool1}, []string{"hostpool1"}}, false,}, - {"pool3-already-close", fields{map[string]*redis.Pool{"hostpool2": pool2, "hostpool3": pool3, "hostpool4": pool4,}, []string{"hostpool2", "hostpool3", "hostpool3"}}, false,}, + {"empty", fields{map[string]*redis.Pool{}, []string{}}, false}, + {"normal", fields{map[string]*redis.Pool{"hostpool1": pool1}, []string{"hostpool1"}}, false}, + {"pool3-already-close", fields{map[string]*redis.Pool{"hostpool2": pool2, "hostpool3": pool3, "hostpool4": pool4}, []string{"hostpool2", "hostpool3", "hostpool3"}}, false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/redisearch/reducer.go b/redisearch/reducer.go index 9bfc368..26edf31 100644 --- a/redisearch/reducer.go +++ b/redisearch/reducer.go @@ -61,7 +61,7 @@ func NewReducer(name GroupByReducers, args []string) *Reducer { } // NewReducer creates a new Reducer object -func NewReducerAlias(name GroupByReducers, args []string, alias string ) *Reducer { +func NewReducerAlias(name GroupByReducers, args []string, alias string) *Reducer { return &Reducer{ Name: name, Alias: alias, diff --git a/redisearch/schema.go b/redisearch/schema.go index bc28055..c17e6ad 100644 --- a/redisearch/schema.go +++ b/redisearch/schema.go @@ -246,7 +246,7 @@ func SerializeSchema(s *Schema, args redis.Args) (argsOut redis.Args, err error) argsOut = append(argsOut, "NOOFFSETS") } if s.Options.Temporary { - argsOut = append(argsOut, "TEMPORARY",s.Options.TemporaryPeriod) + argsOut = append(argsOut, "TEMPORARY", s.Options.TemporaryPeriod) } if s.Options.NoFieldFlags { argsOut = append(argsOut, "NOFIELDS") diff --git a/redisearch/schema_test.go b/redisearch/schema_test.go index abf40eb..f81eb48 100644 --- a/redisearch/schema_test.go +++ b/redisearch/schema_test.go @@ -46,7 +46,7 @@ func TestSerializeSchema(t *testing.T) { {"default-args", args{NewSchema(DefaultOptions), redis.Args{}}, redis.Args{"SCHEMA"}, false}, {"default-args-with-different-constructor", args{NewSchema(*NewOptions()), redis.Args{}}, redis.Args{"SCHEMA"}, false}, - {"temporary", args{NewSchema(*NewOptions().SetTemporaryPeriod(60)), redis.Args{}}, redis.Args{"TEMPORARY",60,"SCHEMA"}, false}, + {"temporary", args{NewSchema(*NewOptions().SetTemporaryPeriod(60)), redis.Args{}}, redis.Args{"TEMPORARY", 60, "SCHEMA"}, false}, {"no-frequencies", args{NewSchema(Options{NoFrequencies: true}), redis.Args{}}, redis.Args{"NOFREQS", "SCHEMA"}, false}, {"no-fields", args{NewSchema(Options{NoFieldFlags: true}), redis.Args{}}, redis.Args{"NOFIELDS", "SCHEMA"}, false}, {"custom-stopwords", args{NewSchema(Options{Stopwords: []string{"custom"}}), redis.Args{}}, redis.Args{"STOPWORDS", 1, "custom", "SCHEMA"}, false},