Skip to content

Commit 3e86864

Browse files
authored
[v3] remove using global map for default codecs (#224)
* remove using global map for default codecs * fix tests
1 parent a68d3b2 commit 3e86864

File tree

5 files changed

+6
-21
lines changed

5 files changed

+6
-21
lines changed

client/noop.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ import (
1717
"go.unistack.org/micro/v3/tracer"
1818
)
1919

20-
// DefaultCodecs will be used to encode/decode data
21-
var DefaultCodecs = map[string]codec.Codec{
22-
"application/octet-stream": codec.NewCodec(),
23-
}
24-
2520
type noopClient struct {
2621
funcPublish FuncPublish
2722
funcBatchPublish FuncBatchPublish
@@ -178,9 +173,6 @@ func (n *noopClient) newCodec(contentType string) (codec.Codec, error) {
178173
if cf, ok := n.opts.Codecs[contentType]; ok {
179174
return cf, nil
180175
}
181-
if cf, ok := DefaultCodecs[contentType]; ok {
182-
return cf, nil
183-
}
184176
return nil, codec.ErrUnknownContentType
185177
}
186178

client/noop_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package client
33
import (
44
"context"
55
"testing"
6+
7+
"go.unistack.org/micro/v3/codec"
68
)
79

810
type testHook struct {
@@ -19,7 +21,7 @@ func (t *testHook) Publish(fn FuncPublish) FuncPublish {
1921
func TestNoopHook(t *testing.T) {
2022
h := &testHook{}
2123

22-
c := NewClient(Hooks(HookPublish(h.Publish)))
24+
c := NewClient(Codec("application/octet-stream", codec.NewCodec()), Hooks(HookPublish(h.Publish)))
2325

2426
if err := c.Init(); err != nil {
2527
t.Fatal(err)

client/options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func NewOptions(opts ...Option) Options {
198198
options := Options{
199199
Context: context.Background(),
200200
ContentType: DefaultContentType,
201-
Codecs: DefaultCodecs,
201+
Codecs: make(map[string]codec.Codec),
202202
CallOptions: CallOptions{
203203
Context: context.Background(),
204204
Backoff: DefaultBackoff,

logger/slog/slog_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func TestTime(t *testing.T) {
6262
WithHandlerFunc(slog.NewTextHandler),
6363
logger.WithAddStacktrace(true),
6464
logger.WithTimeFunc(func() time.Time {
65-
return time.Unix(0, 0)
65+
return time.Unix(0, 0).UTC()
6666
}),
6767
)
6868
if err := l.Init(logger.WithFields("key1", "val1")); err != nil {
@@ -71,8 +71,7 @@ func TestTime(t *testing.T) {
7171

7272
l.Error(ctx, "msg1", errors.New("err"))
7373

74-
if !bytes.Contains(buf.Bytes(), []byte(`timestamp=1970-01-01T03:00:00.000000000+03:00`)) &&
75-
!bytes.Contains(buf.Bytes(), []byte(`timestamp=1970-01-01T00:00:00.000000000Z`)) {
74+
if !bytes.Contains(buf.Bytes(), []byte(`timestamp=1970-01-01T00:00:00.000000000Z`)) {
7675
t.Fatalf("logger error not works, buf contains: %s", buf.Bytes())
7776
}
7877
}

server/noop.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ import (
2121
"go.unistack.org/micro/v3/util/rand"
2222
)
2323

24-
// DefaultCodecs will be used to encode/decode
25-
var DefaultCodecs = map[string]codec.Codec{
26-
"application/octet-stream": codec.NewCodec(),
27-
}
28-
2924
const (
3025
defaultContentType = "application/json"
3126
)
@@ -93,9 +88,6 @@ func (n *noopServer) newCodec(contentType string) (codec.Codec, error) {
9388
if cf, ok := n.opts.Codecs[contentType]; ok {
9489
return cf, nil
9590
}
96-
if cf, ok := DefaultCodecs[contentType]; ok {
97-
return cf, nil
98-
}
9991
return nil, codec.ErrUnknownContentType
10092
}
10193

0 commit comments

Comments
 (0)