Skip to content

Commit 368308e

Browse files
committed
Update support Go version
1 parent c93ecf4 commit 368308e

20 files changed

+285
-174
lines changed

.github/workflows/actions.yml

+13-21
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,24 @@ on:
1111
jobs:
1212
lint:
1313
name: Lint
14-
runs-on: ubuntu-latest
14+
runs-on: ubuntu-18.04
1515
steps:
16-
- name: Check out code
17-
uses: actions/checkout@v2
18-
- name: Download golangci-lint command
19-
run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.21.0
20-
- name: Lint Go Code
21-
run: PATH=$PATH:$(go env GOPATH)/bin golangci-lint run ./...
16+
- uses: actions/checkout@v3
17+
- uses: golangci/golangci-lint-action@v3
18+
with:
19+
version: v1.49.0
2220
test:
2321
name: Test
24-
runs-on: ubuntu-latest
22+
runs-on: ubuntu-18.04
2523
strategy:
2624
matrix:
27-
go: [ '1.15.x', '1.16.x' ]
25+
go: [ '1.18.x', '1.19.x' ]
2826
steps:
29-
- name: Set up Go
30-
uses: actions/setup-go@v2
27+
- uses: actions/checkout@v3
28+
- uses: actions/setup-go@v2
3129
with:
3230
go-version: ${{ matrix.go }}
33-
- name: Check out code
34-
uses: actions/checkout@v2
35-
- name: Test Go Code
36-
run: PATH=$PATH:$(go env GOPATH)/bin go test -covermode=atomic -coverprofile=coverage.txt ./...
37-
- name: Upload coverage to Codecov
38-
uses: codecov/codecov-action@v1
39-
with:
40-
token: ${{ secrets.CODECOV_TOKEN }}
41-
file: coverage.txt
42-
yml: codecov.yml
31+
- run: go test -race -covermode=atomic -coverprofile=coverage.txt ./...
32+
env:
33+
KENALL_AUTHORIZATION_TOKEN: ${{ secrets.KENALL_AUTHORIZATION_TOKEN }}
34+
- uses: codecov/codecov-action@v2

.golangci.yml

+8-1
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,19 @@ linters-settings:
88
linters:
99
enable-all: true
1010
disable:
11-
- whitespace
11+
- varnamelen
1212
- wsl
13+
- exhaustruct
14+
- exhaustivestruct
1315

1416
issues:
1517
exclude-rules:
18+
- path: _test\.go
19+
text: "does not use range value in test Run"
20+
linters:
21+
- paralleltest
1622
- path: _test\.go
1723
linters:
1824
- lll
1925
- funlen
26+
- dupword

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
## Install
1717

1818
```
19-
$ go get -u github.com/osamingo/jsonrpc/v2
19+
$ go get github.com/osamingo/jsonrpc/v2@latest
2020
```
2121

2222
## Usage
@@ -49,7 +49,7 @@ type (
4949
}
5050
)
5151

52-
func (h EchoHandler) ServeJSONRPC(c context.Context, params *json.RawMessage) (interface{}, *jsonrpc.Error) {
52+
func (h EchoHandler) ServeJSONRPC(c context.Context, params *json.RawMessage) (any, *jsonrpc.Error) {
5353

5454
var p EchoParams
5555
if err := jsonrpc.Unmarshal(params, &p); err != nil {
@@ -61,7 +61,7 @@ func (h EchoHandler) ServeJSONRPC(c context.Context, params *json.RawMessage) (i
6161
}, nil
6262
}
6363

64-
func (h PositionalHandler) ServeJSONRPC(c context.Context, params *json.RawMessage) (interface{}, **jsonrpc.Error) {
64+
func (h PositionalHandler) ServeJSONRPC(c context.Context, params *json.RawMessage) (any, **jsonrpc.Error) {
6565

6666
var p PositionalParams
6767
if err := jsonrpc.Unmarshal(params, &p); err != nil {
@@ -110,8 +110,8 @@ type (
110110
HandleParamsResulter interface {
111111
jsonrpc.Handler
112112
Name() string
113-
Params() interface{}
114-
Result() interface{}
113+
Params() any
114+
Result() any
115115
}
116116
Servicer interface {
117117
MethodName(HandleParamsResulter) string

context.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ type (
1414

1515
// RequestID takes request id from context.
1616
func RequestID(c context.Context) *json.RawMessage {
17-
return c.Value(requestIDKey{}).(*json.RawMessage)
17+
v, _ := c.Value(requestIDKey{}).(*json.RawMessage)
18+
19+
return v
1820
}
1921

2022
// WithRequestID adds request id to context.
@@ -24,7 +26,9 @@ func WithRequestID(c context.Context, id *json.RawMessage) context.Context {
2426

2527
// GetMetadata takes jsonrpc metadata from context.
2628
func GetMetadata(c context.Context) Metadata {
27-
return c.Value(metadataIDKey{}).(Metadata)
29+
v, _ := c.Value(metadataIDKey{}).(Metadata)
30+
31+
return v
2832
}
2933

3034
// WithMetadata adds jsonrpc metadata to context.
@@ -34,7 +38,9 @@ func WithMetadata(c context.Context, md Metadata) context.Context {
3438

3539
// MethodName takes method name from context.
3640
func MethodName(c context.Context) string {
37-
return c.Value(methodNameKey{}).(string)
41+
v, _ := c.Value(methodNameKey{}).(string)
42+
43+
return v
3844
}
3945

4046
// WithMethodName adds method name to context.

context_test.go

+16-9
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,48 @@
1-
package jsonrpc
1+
package jsonrpc_test
22

33
import (
44
"context"
55
"testing"
66

77
"github.com/goccy/go-json"
8+
"github.com/osamingo/jsonrpc/v2"
89
"github.com/stretchr/testify/require"
910
)
1011

1112
func TestRequestID(t *testing.T) {
13+
t.Parallel()
14+
1215
c := context.Background()
1316
id := json.RawMessage("1")
14-
c = WithRequestID(c, &id)
17+
c = jsonrpc.WithRequestID(c, &id)
1518
var pick *json.RawMessage
1619
require.NotPanics(t, func() {
17-
pick = RequestID(c)
20+
pick = jsonrpc.RequestID(c)
1821
})
1922
require.Equal(t, &id, pick)
2023
}
2124

2225
func TestMetadata(t *testing.T) {
26+
t.Parallel()
27+
2328
c := context.Background()
24-
md := Metadata{Params: Metadata{}}
25-
c = WithMetadata(c, md)
26-
var pick Metadata
29+
md := jsonrpc.Metadata{Params: jsonrpc.Metadata{}}
30+
c = jsonrpc.WithMetadata(c, md)
31+
var pick jsonrpc.Metadata
2732
require.NotPanics(t, func() {
28-
pick = GetMetadata(c)
33+
pick = jsonrpc.GetMetadata(c)
2934
})
3035
require.Equal(t, md, pick)
3136
}
3237

3338
func TestMethodName(t *testing.T) {
39+
t.Parallel()
40+
3441
c := context.Background()
35-
c = WithMethodName(c, t.Name())
42+
c = jsonrpc.WithMethodName(c, t.Name())
3643
var pick string
3744
require.NotPanics(t, func() {
38-
pick = MethodName(c)
45+
pick = jsonrpc.MethodName(c)
3946
})
4047
require.Equal(t, t.Name(), pick)
4148
}

debug.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"net/http"
55
"reflect"
66

7-
"github.com/alecthomas/jsonschema"
87
"github.com/goccy/go-json"
8+
"github.com/invopop/jsonschema"
99
)
1010

1111
// A MethodReference is a reference of JSON-RPC method.
@@ -17,19 +17,21 @@ type MethodReference struct {
1717
}
1818

1919
// ServeDebug views registered method list.
20-
func (mr *MethodRepository) ServeDebug(w http.ResponseWriter, r *http.Request) { // nolint: unparam
20+
func (mr *MethodRepository) ServeDebug(w http.ResponseWriter, r *http.Request) {
2121
ms := mr.Methods()
2222
if len(ms) == 0 {
2323
w.WriteHeader(http.StatusNotFound)
24+
2425
return
2526
}
2627
l := make([]*MethodReference, 0, len(ms))
2728
for k, md := range ms {
2829
l = append(l, makeMethodReference(k, md))
2930
}
3031
w.Header().Set(contentTypeKey, contentTypeValue)
31-
if err := json.NewEncoder(w).Encode(l); err != nil {
32+
if err := json.NewEncoder(w).EncodeContext(r.Context(), l); err != nil {
3233
w.WriteHeader(http.StatusInternalServerError)
34+
3335
return
3436
}
3537
}
@@ -49,5 +51,6 @@ func makeMethodReference(k string, md Metadata) *MethodReference {
4951
if md.Result != nil {
5052
mr.Result = jsonschema.Reflect(md.Result)
5153
}
54+
5255
return mr
5356
}

debug_test.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
1-
package jsonrpc
1+
package jsonrpc_test
22

33
import (
4+
"context"
45
"net/http"
56
"net/http/httptest"
67
"testing"
78

9+
"github.com/osamingo/jsonrpc/v2"
810
"github.com/stretchr/testify/assert"
911
"github.com/stretchr/testify/require"
1012
)
1113

1214
func TestDebugHandler(t *testing.T) {
13-
mr := NewMethodRepository()
15+
t.Parallel()
16+
17+
mr := jsonrpc.NewMethodRepository()
1418

1519
rec := httptest.NewRecorder()
16-
r, err := http.NewRequest("", "", nil)
20+
r, err := http.NewRequestWithContext(context.Background(), "", "", nil)
1721
require.NoError(t, err)
1822

1923
mr.ServeDebug(rec, r)
@@ -27,7 +31,7 @@ func TestDebugHandler(t *testing.T) {
2731
}{}))
2832

2933
rec = httptest.NewRecorder()
30-
r, err = http.NewRequest("", "", nil)
34+
r, err = http.NewRequestWithContext(context.Background(), "", "", nil)
3135
require.NoError(t, err)
3236

3337
mr.ServeDebug(rec, r)

error.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ type (
2121

2222
// An Error is a wrapper for a JSON interface value.
2323
Error struct {
24-
Code ErrorCode `json:"code"`
25-
Message string `json:"message"`
26-
Data interface{} `json:"data,omitempty"`
24+
Code ErrorCode `json:"code"`
25+
Message string `json:"message"`
26+
Data any `json:"data,omitempty"`
2727
}
2828
)
2929

error_test.go

+29-14
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
1-
package jsonrpc
1+
package jsonrpc_test
22

33
import (
44
"testing"
55

6+
"github.com/osamingo/jsonrpc/v2"
67
"github.com/stretchr/testify/assert"
78
"github.com/stretchr/testify/require"
89
)
910

1011
func TestError(t *testing.T) {
11-
var err interface{} = &Error{}
12+
t.Parallel()
13+
14+
var err any = &jsonrpc.Error{}
1215
_, ok := err.(error)
1316
require.True(t, ok)
1417
}
1518

1619
func TestError_Error(t *testing.T) {
17-
err := &Error{
18-
Code: ErrorCode(100),
20+
t.Parallel()
21+
22+
err := &jsonrpc.Error{
23+
Code: jsonrpc.ErrorCode(100),
1924
Message: "test",
2025
Data: map[string]string{
2126
"test": "test",
@@ -26,26 +31,36 @@ func TestError_Error(t *testing.T) {
2631
}
2732

2833
func TestErrParse(t *testing.T) {
29-
err := ErrParse()
30-
require.Equal(t, ErrorCodeParse, err.Code)
34+
t.Parallel()
35+
36+
err := jsonrpc.ErrParse()
37+
require.Equal(t, jsonrpc.ErrorCodeParse, err.Code)
3138
}
3239

3340
func TestErrInvalidRequest(t *testing.T) {
34-
err := ErrInvalidRequest()
35-
require.Equal(t, ErrorCodeInvalidRequest, err.Code)
41+
t.Parallel()
42+
43+
err := jsonrpc.ErrInvalidRequest()
44+
require.Equal(t, jsonrpc.ErrorCodeInvalidRequest, err.Code)
3645
}
3746

3847
func TestErrMethodNotFound(t *testing.T) {
39-
err := ErrMethodNotFound()
40-
require.Equal(t, ErrorCodeMethodNotFound, err.Code)
48+
t.Parallel()
49+
50+
err := jsonrpc.ErrMethodNotFound()
51+
require.Equal(t, jsonrpc.ErrorCodeMethodNotFound, err.Code)
4152
}
4253

4354
func TestErrInvalidParams(t *testing.T) {
44-
err := ErrInvalidParams()
45-
require.Equal(t, ErrorCodeInvalidParams, err.Code)
55+
t.Parallel()
56+
57+
err := jsonrpc.ErrInvalidParams()
58+
require.Equal(t, jsonrpc.ErrorCodeInvalidParams, err.Code)
4659
}
4760

4861
func TestErrInternal(t *testing.T) {
49-
err := ErrInternal()
50-
require.Equal(t, ErrorCodeInternal, err.Code)
62+
t.Parallel()
63+
64+
err := jsonrpc.ErrInternal()
65+
require.Equal(t, jsonrpc.ErrorCodeInternal, err.Code)
5166
}

0 commit comments

Comments
 (0)