Skip to content

Commit

Permalink
fix linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Landgraf committed Dec 14, 2020
1 parent caeac41 commit 8dd0b4c
Show file tree
Hide file tree
Showing 19 changed files with 85 additions and 54 deletions.
3 changes: 3 additions & 0 deletions backend/queue/rmq.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ func NewQueue(name string, healthyLimit int) (rmq.Queue, error) {
return nil, err
}
queue, err := rmqConnection.OpenQueue(name)
if err != nil {
return nil, err
}
if _, ok := queueHealthLimits.Load(name); ok {
return queue, nil
}
Expand Down
6 changes: 4 additions & 2 deletions backend/queue/rmq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ func TestIntegrationHealthCheck(t *testing.T) {
ctx := log.WithContext(context.Background())
q1, err := queue.NewQueue("integrationTestTasks", 1)
assert.NoError(t, err)
q1.Publish("nothing here")
err = q1.Publish("nothing here")
assert.NoError(t, err)

check := &queue.HealthCheck{IgnoreInterval: true}
res := check.HealthCheck(ctx)
if res.State != "OK" {
t.Errorf("Expected health check to be OK for a non-full queue")
}

q1.Publish("nothing here either")
err = q1.Publish("nothing here either")
assert.NoError(t, err)

res = check.HealthCheck(ctx)
if res.State == "OK" {
Expand Down
5 changes: 4 additions & 1 deletion http/jsonapi/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ func TestMarshalErrorsWritesTheExpectedPayload(t *testing.T) {
var writer io.Writer = buffer

_ = MarshalErrors(writer, testRow.In)
json.Unmarshal(buffer.Bytes(), &output)
err := json.Unmarshal(buffer.Bytes(), &output)
if err != nil {
t.Fatal(err)
}

if !reflect.DeepEqual(output, testRow.Out) {
t.Fatalf("Expected: \n%#v \nto equal: \n%#v", output, testRow.Out)
Expand Down
2 changes: 0 additions & 2 deletions http/jsonapi/generator/generate_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"github.com/getkin/kin-openapi/openapi3"
)

const pkgRuntime = "github.com/pace/bricks/http/jsonapi/runtime"

func (g *Generator) addGoDoc(typeName, description string) {
if description != "" {
g.goSource.Comment(fmt.Sprintf("%s %s", typeName, description))
Expand Down
5 changes: 4 additions & 1 deletion http/jsonapi/generator/generate_security.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ func (g *Generator) buildSecurityConfigs(schema *openapi3.Swagger) error {
if e, ok := value.Value.Extensions["openIdConnectUrl"]; ok {
var url string
if data, ok := e.(json.RawMessage); ok {
json.Unmarshal(data, &url)
err := json.Unmarshal(data, &url)
if err != nil {
return err
}
instanceVal[jen.Id("OpenIdConnectURL")] = jen.Lit(url)
}
}
Expand Down
8 changes: 4 additions & 4 deletions http/jsonapi/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,8 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node)

buf := bytes.NewBuffer(nil)

json.NewEncoder(buf).Encode(data.Relationships[args[1]])
json.NewDecoder(buf).Decode(relationship)
err = json.NewEncoder(buf).Encode(data.Relationships[args[1]]) // nolint: errcheck
json.NewDecoder(buf).Decode(relationship) // nolint: errcheck

data := relationship.Data
models := reflect.New(fieldValue.Type()).Elem()
Expand All @@ -302,10 +302,10 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node)

buf := bytes.NewBuffer(nil)

json.NewEncoder(buf).Encode(
json.NewEncoder(buf).Encode( // nolint: errcheck
data.Relationships[args[1]],
)
json.NewDecoder(buf).Decode(relationship)
json.NewDecoder(buf).Decode(relationship) // nolint: errcheck

/*
http://jsonapi.org/format/#document-resource-object-relationships
Expand Down
40 changes: 29 additions & 11 deletions http/jsonapi/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"errors"
"fmt"
"io"
"log"
"reflect"
"sort"
"strings"
Expand Down Expand Up @@ -202,7 +203,6 @@ func TestUnmarshalToStructWithPointerAttr_BadType_Struct(t *testing.T) {

func TestUnmarshalToStructWithPointerAttr_BadType_IntSlice(t *testing.T) {
out := new(WithPointer)
type FooStruct struct{ A, B int }
in := map[string]json.RawMessage{
"name": json.RawMessage(`[4, 5]`), // This is the wrong type.
}
Expand Down Expand Up @@ -361,7 +361,10 @@ func TestUnmarshalParsesISO8601(t *testing.T) {
}

in := bytes.NewBuffer(nil)
json.NewEncoder(in).Encode(payload)
err := json.NewEncoder(in).Encode(payload)
if err != nil {
log.Fatal(err)
}

out := new(Timestamp)

Expand All @@ -387,7 +390,10 @@ func TestUnmarshalParsesISO8601TimePointer(t *testing.T) {
}

in := bytes.NewBuffer(nil)
json.NewEncoder(in).Encode(payload)
err := json.NewEncoder(in).Encode(payload)
if err != nil {
t.Fatal(err)
}

out := new(Timestamp)

Expand All @@ -413,7 +419,10 @@ func TestUnmarshalInvalidISO8601(t *testing.T) {
}

in := bytes.NewBuffer(nil)
json.NewEncoder(in).Encode(payload)
err := json.NewEncoder(in).Encode(payload)
if err != nil {
t.Fatal(err)
}

out := new(Timestamp)

Expand Down Expand Up @@ -969,7 +978,7 @@ func samplePayload() io.Reader {
}

out := bytes.NewBuffer(nil)
json.NewEncoder(out).Encode(payload)
json.NewEncoder(out).Encode(payload) // nolint: errcheck

return out
}
Expand All @@ -987,7 +996,7 @@ func samplePayloadWithID() io.Reader {
}

out := bytes.NewBuffer(nil)
json.NewEncoder(out).Encode(payload)
json.NewEncoder(out).Encode(payload) // nolint: errcheck

return out
}
Expand All @@ -1002,7 +1011,7 @@ func samplePayloadWithBadTypes(m map[string]json.RawMessage) io.Reader {
}

out := bytes.NewBuffer(nil)
json.NewEncoder(out).Encode(payload)
json.NewEncoder(out).Encode(payload) // nolint: errcheck

return out
}
Expand All @@ -1017,7 +1026,7 @@ func sampleWithPointerPayload(m map[string]json.RawMessage) io.Reader {
}

out := bytes.NewBuffer(nil)
json.NewEncoder(out).Encode(payload)
json.NewEncoder(out).Encode(payload) // nolint: errcheck

return out
}
Expand Down Expand Up @@ -1094,17 +1103,26 @@ func samplePayloadWithSideloaded() io.Reader {
testModel := testModel()

out := bytes.NewBuffer(nil)
MarshalPayload(out, testModel)
err := MarshalPayload(out, testModel)
if err != nil {
panic(err)
}

return out
}

func sampleSerializedEmbeddedTestModel() *Blog {
out := bytes.NewBuffer(nil)
MarshalOnePayloadEmbedded(out, testModel())
err := MarshalOnePayloadEmbedded(out, testModel())
if err != nil {
panic(err)
}

blog := new(Blog)
UnmarshalPayload(out, blog)
err = UnmarshalPayload(out, blog)
if err != nil {
panic(err)
}

return blog
}
Expand Down
12 changes: 9 additions & 3 deletions http/jsonapi/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ func TestMarshalPayload(t *testing.T) {

// One
out1 := bytes.NewBuffer(nil)
MarshalPayload(out1, book)
err := MarshalPayload(out1, book)
if err != nil {
t.Fatal(err)
}

if strings.Contains(out1.String(), `"9.9999999999999999999"`) {
t.Fatalf("decimals should be encoded as number, got: %q", out1.String())
Expand All @@ -39,11 +42,14 @@ func TestMarshalPayload(t *testing.T) {
if _, ok := jsonData["data"].(map[string]interface{}); !ok {
t.Fatalf("data key did not contain an Hash/Dict/Map")
}
fmt.Println(string(out1.Bytes()))
fmt.Println(out1.String())

// Many
out2 := bytes.NewBuffer(nil)
MarshalPayload(out2, books)
err = MarshalPayload(out2, books)
if err != nil {
t.Fatal(err)
}

if err := json.Unmarshal(out2.Bytes(), &jsonData); err != nil {
t.Fatal(err)
Expand Down
8 changes: 4 additions & 4 deletions http/jsonapi/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ func (r *Runtime) UnmarshalPayload(reader io.Reader, model interface{}) error {
}

// UnmarshalManyPayload has docs in request.go for UnmarshalManyPayload.
func (r *Runtime) UnmarshalManyPayload(reader io.Reader, kind reflect.Type) (elems []interface{}, err error) {
r.instrumentCall(UnmarshalStart, UnmarshalStop, func() error {
elems, err = UnmarshalManyPayload(reader, kind)
func (r *Runtime) UnmarshalManyPayload(reader io.Reader, kind reflect.Type) (elements []interface{}, err error) {
err2 := r.instrumentCall(UnmarshalStart, UnmarshalStop, func() error {
elements, err = UnmarshalManyPayload(reader, kind)
return err
})

return
return elements, err2
}

// MarshalPayload has docs in response.go for MarshalPayload.
Expand Down
8 changes: 0 additions & 8 deletions http/jsonapi/runtime/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,6 @@ func WriteError(w http.ResponseWriter, code int, err error) {
log.Logger().Info().Str("req_id", reqID).
Err(err).Msg("Unable to send error response to the client")
}

// log all errors send to the client
for _, ei := range errList.List {
ev := log.Logger().Info().Str("req_id", reqID)
if source := ei.Source; source != nil {
ev = ev.Fields(*source)
}
}
}

// Error objects MUST be returned as an array keyed by errors in the top level of a JSON API document.
Expand Down
7 changes: 6 additions & 1 deletion http/jsonapi/runtime/standart_params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func TestIntegrationFilterParameter(t *testing.T) {
// Paging
r = httptest.NewRequest("GET", "http://abc.de/whatEver?page[number]=3&page[size]=2", nil)
urlParams, err = runtime.ReadURLQueryParameters(r, mapper, &testValueSanitizer{})
assert.NoError(t, err)
var modelsPaging []TestModel
q = db.Model(&modelsPaging)
q = urlParams.AddToQuery(q)
Expand All @@ -77,6 +78,7 @@ func TestIntegrationFilterParameter(t *testing.T) {
// Sorting
r = httptest.NewRequest("GET", "http://abc.de/whatEver?sort=-test", nil)
urlParams, err = runtime.ReadURLQueryParameters(r, mapper, &testValueSanitizer{})
assert.NoError(t, err)
var modelsSort []TestModel
q = db.Model(&modelsSort)
q = urlParams.AddToQuery(q)
Expand All @@ -90,16 +92,19 @@ func TestIntegrationFilterParameter(t *testing.T) {
// Combine all
r = httptest.NewRequest("GET", "http://abc.de/whatEver?sort=-test&filter[test]=a,b&page[number]=0&page[size]=1", nil)
urlParams, err = runtime.ReadURLQueryParameters(r, mapper, &testValueSanitizer{})
assert.NoError(t, err)
var modelsCombined []TestModel
q = db.Model(&modelsCombined)
q = urlParams.AddToQuery(q)
err = q.Select()
assert.NoError(t, err)
a.Equal(1, len(modelsCombined))
a.Equal("b", modelsCombined[0].FilterName)
// Tear Down
db.DropTable(&TestModel{}, &orm.DropTableOptions{
err = db.DropTable(&TestModel{}, &orm.DropTableOptions{
IfExists: true,
})
assert.NoError(t, err)
}

func setupDatabase(a *assert.Assertions) *pg.DB {
Expand Down
4 changes: 2 additions & 2 deletions http/longpoll/longpoll_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestLongPollUntilBounds(t *testing.T) {
ok, err := Until(context.Background(), -1, func(ctx context.Context) (bool, error) {
budget, ok := ctx.Deadline()
assert.True(t, ok)
assert.Equal(t, time.Millisecond*999, budget.Sub(time.Now()).Truncate(time.Millisecond))
assert.Equal(t, time.Millisecond*999, budget.Sub(time.Now()).Truncate(time.Millisecond)) // nolint: gosimple
called++
return true, nil
})
Expand All @@ -26,7 +26,7 @@ func TestLongPollUntilBounds(t *testing.T) {
ok, err = Until(context.Background(), time.Hour, func(ctx context.Context) (bool, error) {
budget, ok := ctx.Deadline()
assert.True(t, ok)
assert.Equal(t, time.Second*59, budget.Sub(time.Now()).Truncate(time.Second))
assert.Equal(t, time.Second*59, budget.Sub(time.Now()).Truncate(time.Second)) // nolint: gosimple
called++
return true, nil
})
Expand Down
4 changes: 2 additions & 2 deletions http/middleware/external_dependency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func Test_ExternalDependency_Middleare(t *testing.T) {
w.WriteHeader(http.StatusOK)
}))
h.ServeHTTP(rec, req)
assert.Nil(t, rec.HeaderMap[ExternalDependencyHeaderName])
assert.Nil(t, rec.Result().Header[ExternalDependencyHeaderName])
})
t.Run("one dependency set", func(t *testing.T) {
rec := httptest.NewRecorder()
Expand All @@ -34,7 +34,7 @@ func Test_ExternalDependency_Middleare(t *testing.T) {
w.Write(nil) // nolint: errcheck
}))
h.ServeHTTP(rec, req)
assert.Equal(t, rec.HeaderMap[ExternalDependencyHeaderName][0], "test:1000")
assert.Equal(t, rec.Result().Header[ExternalDependencyHeaderName][0], "test:1000")
})
}

Expand Down
4 changes: 2 additions & 2 deletions http/transport/chainable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ func TestRoundTripperRace(t *testing.T) {

go func() {
for i := 0; i < 10; i++ {
client.Get(server.URL + "/test001")
client.Get(server.URL + "/test001") // nolint: errcheck
}
}()

for i := 0; i < 10; i++ {
client.Get(server.URL + "/test002")
client.Get(server.URL + "/test002") // nolint: errcheck
}
}

Expand Down
2 changes: 1 addition & 1 deletion http/transport/locale_round_tripper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestLocaleRoundTrip(t *testing.T) {
r, err := http.NewRequest("GET", "http://example.com/test", nil)
require.NoError(t, err)

lrt.RoundTrip(r.WithContext(locale.WithLocale(context.Background(), l)))
lrt.RoundTrip(r.WithContext(locale.WithLocale(context.Background(), l))) // nolint: errcheck

lctx, ok := locale.FromCtx(mock.r.Context())
require.True(t, ok)
Expand Down
3 changes: 2 additions & 1 deletion maintenance/errors/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ func TestHide(t *testing.T) {
canceledContext, cancel := context.WithCancel(backgroundContext)
cancel()

exceededContext, _ := context.WithTimeout(backgroundContext, time.Millisecond)
exceededContext, cancel2 := context.WithTimeout(backgroundContext, time.Millisecond)
defer cancel2()
time.Sleep(2 * time.Millisecond)

type args struct {
Expand Down
Loading

0 comments on commit 8dd0b4c

Please sign in to comment.