Skip to content

Commit

Permalink
Merge branch 'master' into config
Browse files Browse the repository at this point in the history
  • Loading branch information
Reasno authored Jul 29, 2021
2 parents 4a91837 + b6f2d0a commit 42644dc
Show file tree
Hide file tree
Showing 73 changed files with 440 additions and 328 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,14 @@ jobs:
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: coverage.txt
lint:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.41
args: --disable errcheck --timeout 5m0s
only-new-issues: true
4 changes: 4 additions & 0 deletions c.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ package core

import (
"context"
"errors"
"fmt"
"reflect"
"regexp"

"github.com/DoNewsCode/core/codec/yaml"
"github.com/DoNewsCode/core/config"
Expand Down Expand Up @@ -412,6 +414,8 @@ func (c *C) AddModuleFunc(constructor interface{}) {
func (c *C) Invoke(function interface{}) {
err := c.di.Invoke(function)
if err != nil {
re := regexp.MustCompile(` missing dependencies for function "reflect"\.makeFuncStub \(.+?\):`)
err = errors.New(re.ReplaceAllString(err.Error(), ""))
panic(err)
}
}
Expand Down
25 changes: 25 additions & 0 deletions c_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,31 @@ func TestC_Provide(t *testing.T) {
})
}

type a struct{}
type b struct{}

func mockConstructor(b b) (a, func(), error) {
return a{}, func() {}, nil
}

func TestNew_missingDependencyErrorMessage(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Log(r)
if strings.Contains(r.(error).Error(), "\"reflect\".makeFuncStub") {
t.Error("should not contain reflection stub")
}
return
}
t.Error("test should panic")
}()
c := New()
c.Provide(di.Deps{mockConstructor})
c.Invoke(func(a a) error {
return nil
})
}

func put(cfg clientv3.Config, key, val string) error {
client, err := clientv3.New(cfg)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions clihttp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (c *Client) logRequest(req *http.Request, span opentracing.Span) {
span.LogKV("error", errors.Wrap(err, "cannot get request body"))
return
}
length, _ := strconv.Atoi(req.Header.Get(http.CanonicalHeaderKey("Content-Length")))
length, _ := strconv.Atoi(req.Header.Get("Content-Length"))
if length > c.requestLogThreshold {
ext.Error.Set(span, true)
span.LogKV("request", "elided: Content-Length too large")
Expand All @@ -128,7 +128,7 @@ func (c *Client) logResponse(response *http.Response, span opentracing.Span) {
if response.Body == nil {
return
}
length, _ := strconv.Atoi(response.Header.Get(http.CanonicalHeaderKey("Content-Length")))
length, _ := strconv.Atoi(response.Header.Get("Content-Length"))
if length > c.responseLogThreshold {
span.LogKV("response", "elided: Content-Length too large")
return
Expand Down
9 changes: 6 additions & 3 deletions clihttp/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ func TestClient_Do(t *testing.T) {
t.Parallel()
tracer := mocktracer.New()
client := NewClient(tracer, c.Option...)
_, _ = client.Do(c.request)
resp, _ := client.Do(c.request)
defer resp.Body.Close()
assert.NotEmpty(t, tracer.FinishedSpans())
})
}
Expand All @@ -59,7 +60,8 @@ func TestClient_race(t *testing.T) {
t.Run("", func(t *testing.T) {
t.Parallel()
r, _ := http.NewRequest("GET", "https://example.com/", nil)
_, _ = client.Do(r)
resp, _ := client.Do(r)
defer resp.Body.Close()
})
}
}
Expand All @@ -73,7 +75,8 @@ func TestClient_context(t *testing.T) {
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "https://example.com/", nil)

client := NewClient(tracer)
_, _ = client.Do(req)
resp, _ := client.Do(req)
defer resp.Body.Close()
assert.Len(t, tracer.FinishedSpans(), 2)
assert.Equal(t, "bar", tracer.FinishedSpans()[1].BaggageItem("foo"))
}
3 changes: 2 additions & 1 deletion clihttp/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ import (
func Example() {
client := NewClient(opentracing.GlobalTracer())
req, _ := http.NewRequest("GET", "https://example.com/", nil)
_, _ = client.Do(req)
resp, _ := client.Do(req)
resp.Body.Close()
}
21 changes: 9 additions & 12 deletions codec/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,6 @@ import (
"google.golang.org/protobuf/proto"
)

var (
// marshalOptions is a configurable JSON format marshaller.
marshalOptions = protojson.MarshalOptions{
EmitUnpopulated: true,
}
// unmarshalOptions is a configurable JSON format parser.
unmarshalOptions = protojson.UnmarshalOptions{
DiscardUnknown: true,
}
)

// Codec is a Codec implementation with json.
type Codec struct {
prefix string
Expand All @@ -37,7 +26,15 @@ type Option func(*Codec)

// NewCodec creates a new json codec
func NewCodec(opts ...Option) Codec {
var codec Codec
var (
codec Codec
marshalOptions = protojson.MarshalOptions{
EmitUnpopulated: true,
}
unmarshalOptions = protojson.UnmarshalOptions{
DiscardUnknown: true,
}
)
codec.marshalOptions = marshalOptions
codec.unmarshalOptions = unmarshalOptions
for _, f := range opts {
Expand Down
3 changes: 2 additions & 1 deletion config/app_name_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package config

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewAppNameFromConf(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,11 @@ func (m MapAdapter) Route(s string) contract.ConfigAccessor {
v = m[s]
}

switch v.(type) {
switch x := v.(type) {
case map[string]interface{}:
return MapAdapter(v.(map[string]interface{}))
return MapAdapter(x)
case MapAdapter:
return v.(MapAdapter)
return x
default:
panic(fmt.Sprintf("value at path %s is not a valid Router", s))
}
Expand Down
1 change: 1 addition & 0 deletions config/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config_test
import (
"flag"
"fmt"

"github.com/DoNewsCode/core/config"
"github.com/knadh/koanf/parsers/json"
"github.com/knadh/koanf/providers/basicflag"
Expand Down
2 changes: 1 addition & 1 deletion config/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestDuration_MarshalJSON(t *testing.T) {
data, err := json.Marshal(c.value)
assert.NoError(t, err)
assert.Equal(t, c.expectedJSON, string(data))
data, err = yaml.Marshal(c.value)
data, _ = yaml.Marshal(c.value)
assert.Equal(t, c.expectedYaml, string(data))
})
}
Expand Down
3 changes: 2 additions & 1 deletion cronopts/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package cronopts

import (
"bytes"
"testing"

"github.com/go-kit/kit/log"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"testing"
)

func TestCronLogAdapter_Info(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion di/module_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package di

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestModule(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion dtx/sagas/config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package sagas

import (
"time"

"github.com/DoNewsCode/core/config"
"github.com/DoNewsCode/core/di"
"time"
)

type configOut struct {
Expand Down
2 changes: 1 addition & 1 deletion dtx/sagas/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func Example() {
Cost: resp.(OrderResponse).Cost,
})
if err != nil {
tx.Rollback(ctx)
_ = tx.Rollback(ctx)
}
tx.Commit(ctx)
fmt.Println(resp.(PaymentResponse).Success)
Expand Down
3 changes: 2 additions & 1 deletion dtx/sagas/mysqlstore/config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package mysqlstore

import (
"time"

"github.com/DoNewsCode/core/config"
"github.com/DoNewsCode/core/di"
"time"
)

type configOut struct {
Expand Down
1 change: 1 addition & 0 deletions dtx/sagas/mysqlstore/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mysqlstore

import (
"context"

"github.com/DoNewsCode/core/contract"
"github.com/DoNewsCode/core/di"
"github.com/DoNewsCode/core/dtx/sagas"
Expand Down
8 changes: 4 additions & 4 deletions dtx/sagas/mysqlstore/mysql_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func TestMySQLStore(t *testing.T) {
StepError: nil,
})
assert.NoError(t, err)
err = store.Ack(ctx, "3", errors.New("foo error"))
_ = store.Ack(ctx, "3", errors.New("foo error"))
err = store.Log(ctx, sagas.Log{
ID: "4",
StartedAt: time.Now(),
Expand Down Expand Up @@ -181,7 +181,7 @@ func TestMySQLStore(t *testing.T) {
StepError: nil,
})
assert.NoError(t, err)
err = store.Ack(ctx, "4", nil)
_ = store.Ack(ctx, "4", nil)
logs, err := store.UnacknowledgedSteps(ctx, "foo")
assert.NoError(t, err)
assert.Len(t, logs, 0)
Expand Down Expand Up @@ -212,7 +212,7 @@ func TestMySQLStore(t *testing.T) {
StepError: nil,
})
assert.NoError(t, err)
err = store.Ack(ctx, "3", errors.New("foo error"))
_ = store.Ack(ctx, "3", errors.New("foo error"))
logs, err := store.UncommittedSagas(ctx)
assert.NoError(t, err)
assert.Len(t, logs, 1)
Expand Down Expand Up @@ -255,7 +255,7 @@ func TestMySQLStore(t *testing.T) {
StepError: nil,
})
assert.NoError(t, err)
err = store.Ack(ctx, "9", errors.New("bar"))
_ = store.Ack(ctx, "9", errors.New("bar"))
logs, err := store.UnacknowledgedSteps(ctx, "multiple steps")
assert.NoError(t, err)
assert.Len(t, logs, 2)
Expand Down
3 changes: 2 additions & 1 deletion events.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package core

import (
"google.golang.org/grpc"
"net"
"net/http"

"google.golang.org/grpc"
)

type event string
Expand Down
2 changes: 1 addition & 1 deletion events/dispatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type MockListener struct {
}

func (T MockListener) Listen() (topic interface{}) {
return MockEvent{}
return T.topic
}

func (T MockListener) Process(ctx context.Context, event interface{}) error {
Expand Down
1 change: 1 addition & 0 deletions events/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package events

import (
"context"

"github.com/DoNewsCode/core/contract"
)

Expand Down
7 changes: 6 additions & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,18 @@ func Example_minimal() {
})
}))
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go c.Serve(ctx)

// Giver server sometime to be ready.
time.Sleep(time.Second)

// Let's try if the server works.
resp, _ := http.Get("http://localhost:8080/")
resp, err := http.Get("http://localhost:8080/")
if err != nil {
return
}
defer resp.Body.Close()
bytes, _ := ioutil.ReadAll(resp.Body)
cancel()

Expand Down
2 changes: 1 addition & 1 deletion key/key_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func With(k contract.Keyer, parts ...string) manager {
// SpreadInterface likes Spread, but returns a slice of interface{}
func SpreadInterface(k contract.Keyer) []interface{} {
var spreader = k.Spread()
var out = make([]interface{}, len(spreader), len(spreader))
var out = make([]interface{}, len(spreader))
for i := range k.Spread() {
out[i] = interface{}(spreader[i])
}
Expand Down
2 changes: 1 addition & 1 deletion leader/election.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (e *Election) Campaign(ctx context.Context) error {

// Resign gives up the leadership.
func (e *Election) Resign(ctx context.Context) error {
if e.status.isLeader.Load() != true {
if !e.status.isLeader.Load() {
return ErrNotALeader
}
// trigger events
Expand Down
3 changes: 2 additions & 1 deletion observability/metrics.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package observability

import (
"github.com/DoNewsCode/core/otkafka"
"sync"

"github.com/DoNewsCode/core/otkafka"

"github.com/DoNewsCode/core/otgorm"
"github.com/DoNewsCode/core/otredis"
"github.com/go-kit/kit/metrics"
Expand Down
Loading

0 comments on commit 42644dc

Please sign in to comment.