Skip to content

Commit

Permalink
Update support Go version
Browse files Browse the repository at this point in the history
  • Loading branch information
osamingo committed Oct 9, 2022
1 parent c93ecf4 commit 368308e
Show file tree
Hide file tree
Showing 20 changed files with 285 additions and 174 deletions.
34 changes: 13 additions & 21 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,24 @@ on:
jobs:
lint:
name: Lint
runs-on: ubuntu-latest
runs-on: ubuntu-18.04
steps:
- name: Check out code
uses: actions/checkout@v2
- name: Download golangci-lint command
run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.21.0
- name: Lint Go Code
run: PATH=$PATH:$(go env GOPATH)/bin golangci-lint run ./...
- uses: actions/checkout@v3
- uses: golangci/golangci-lint-action@v3
with:
version: v1.49.0
test:
name: Test
runs-on: ubuntu-latest
runs-on: ubuntu-18.04
strategy:
matrix:
go: [ '1.15.x', '1.16.x' ]
go: [ '1.18.x', '1.19.x' ]
steps:
- name: Set up Go
uses: actions/setup-go@v2
- uses: actions/checkout@v3
- uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go }}
- name: Check out code
uses: actions/checkout@v2
- name: Test Go Code
run: PATH=$PATH:$(go env GOPATH)/bin go test -covermode=atomic -coverprofile=coverage.txt ./...
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: coverage.txt
yml: codecov.yml
- run: go test -race -covermode=atomic -coverprofile=coverage.txt ./...
env:
KENALL_AUTHORIZATION_TOKEN: ${{ secrets.KENALL_AUTHORIZATION_TOKEN }}
- uses: codecov/codecov-action@v2
9 changes: 8 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@ linters-settings:
linters:
enable-all: true
disable:
- whitespace
- varnamelen
- wsl
- exhaustruct
- exhaustivestruct

issues:
exclude-rules:
- path: _test\.go
text: "does not use range value in test Run"
linters:
- paralleltest
- path: _test\.go
linters:
- lll
- funlen
- dupword
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
## Install

```
$ go get -u github.com/osamingo/jsonrpc/v2
$ go get github.com/osamingo/jsonrpc/v2@latest
```

## Usage
Expand Down Expand Up @@ -49,7 +49,7 @@ type (
}
)

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

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

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

var p PositionalParams
if err := jsonrpc.Unmarshal(params, &p); err != nil {
Expand Down Expand Up @@ -110,8 +110,8 @@ type (
HandleParamsResulter interface {
jsonrpc.Handler
Name() string
Params() interface{}
Result() interface{}
Params() any
Result() any
}
Servicer interface {
MethodName(HandleParamsResulter) string
Expand Down
12 changes: 9 additions & 3 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ type (

// RequestID takes request id from context.
func RequestID(c context.Context) *json.RawMessage {
return c.Value(requestIDKey{}).(*json.RawMessage)
v, _ := c.Value(requestIDKey{}).(*json.RawMessage)

return v
}

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

// GetMetadata takes jsonrpc metadata from context.
func GetMetadata(c context.Context) Metadata {
return c.Value(metadataIDKey{}).(Metadata)
v, _ := c.Value(metadataIDKey{}).(Metadata)

return v
}

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

// MethodName takes method name from context.
func MethodName(c context.Context) string {
return c.Value(methodNameKey{}).(string)
v, _ := c.Value(methodNameKey{}).(string)

return v
}

// WithMethodName adds method name to context.
Expand Down
25 changes: 16 additions & 9 deletions context_test.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,48 @@
package jsonrpc
package jsonrpc_test

import (
"context"
"testing"

"github.com/goccy/go-json"
"github.com/osamingo/jsonrpc/v2"
"github.com/stretchr/testify/require"
)

func TestRequestID(t *testing.T) {
t.Parallel()

c := context.Background()
id := json.RawMessage("1")
c = WithRequestID(c, &id)
c = jsonrpc.WithRequestID(c, &id)
var pick *json.RawMessage
require.NotPanics(t, func() {
pick = RequestID(c)
pick = jsonrpc.RequestID(c)
})
require.Equal(t, &id, pick)
}

func TestMetadata(t *testing.T) {
t.Parallel()

c := context.Background()
md := Metadata{Params: Metadata{}}
c = WithMetadata(c, md)
var pick Metadata
md := jsonrpc.Metadata{Params: jsonrpc.Metadata{}}
c = jsonrpc.WithMetadata(c, md)
var pick jsonrpc.Metadata
require.NotPanics(t, func() {
pick = GetMetadata(c)
pick = jsonrpc.GetMetadata(c)
})
require.Equal(t, md, pick)
}

func TestMethodName(t *testing.T) {
t.Parallel()

c := context.Background()
c = WithMethodName(c, t.Name())
c = jsonrpc.WithMethodName(c, t.Name())
var pick string
require.NotPanics(t, func() {
pick = MethodName(c)
pick = jsonrpc.MethodName(c)
})
require.Equal(t, t.Name(), pick)
}
9 changes: 6 additions & 3 deletions debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"net/http"
"reflect"

"github.com/alecthomas/jsonschema"
"github.com/goccy/go-json"
"github.com/invopop/jsonschema"
)

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

// ServeDebug views registered method list.
func (mr *MethodRepository) ServeDebug(w http.ResponseWriter, r *http.Request) { // nolint: unparam
func (mr *MethodRepository) ServeDebug(w http.ResponseWriter, r *http.Request) {
ms := mr.Methods()
if len(ms) == 0 {
w.WriteHeader(http.StatusNotFound)

return
}
l := make([]*MethodReference, 0, len(ms))
for k, md := range ms {
l = append(l, makeMethodReference(k, md))
}
w.Header().Set(contentTypeKey, contentTypeValue)
if err := json.NewEncoder(w).Encode(l); err != nil {
if err := json.NewEncoder(w).EncodeContext(r.Context(), l); err != nil {
w.WriteHeader(http.StatusInternalServerError)

return
}
}
Expand All @@ -49,5 +51,6 @@ func makeMethodReference(k string, md Metadata) *MethodReference {
if md.Result != nil {
mr.Result = jsonschema.Reflect(md.Result)
}

return mr
}
12 changes: 8 additions & 4 deletions debug_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package jsonrpc
package jsonrpc_test

import (
"context"
"net/http"
"net/http/httptest"
"testing"

"github.com/osamingo/jsonrpc/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestDebugHandler(t *testing.T) {
mr := NewMethodRepository()
t.Parallel()

mr := jsonrpc.NewMethodRepository()

rec := httptest.NewRecorder()
r, err := http.NewRequest("", "", nil)
r, err := http.NewRequestWithContext(context.Background(), "", "", nil)
require.NoError(t, err)

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

rec = httptest.NewRecorder()
r, err = http.NewRequest("", "", nil)
r, err = http.NewRequestWithContext(context.Background(), "", "", nil)
require.NoError(t, err)

mr.ServeDebug(rec, r)
Expand Down
6 changes: 3 additions & 3 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ type (

// An Error is a wrapper for a JSON interface value.
Error struct {
Code ErrorCode `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data,omitempty"`
Code ErrorCode `json:"code"`
Message string `json:"message"`
Data any `json:"data,omitempty"`
}
)

Expand Down
43 changes: 29 additions & 14 deletions error_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
package jsonrpc
package jsonrpc_test

import (
"testing"

"github.com/osamingo/jsonrpc/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestError(t *testing.T) {
var err interface{} = &Error{}
t.Parallel()

var err any = &jsonrpc.Error{}
_, ok := err.(error)
require.True(t, ok)
}

func TestError_Error(t *testing.T) {
err := &Error{
Code: ErrorCode(100),
t.Parallel()

err := &jsonrpc.Error{
Code: jsonrpc.ErrorCode(100),
Message: "test",
Data: map[string]string{
"test": "test",
Expand All @@ -26,26 +31,36 @@ func TestError_Error(t *testing.T) {
}

func TestErrParse(t *testing.T) {
err := ErrParse()
require.Equal(t, ErrorCodeParse, err.Code)
t.Parallel()

err := jsonrpc.ErrParse()
require.Equal(t, jsonrpc.ErrorCodeParse, err.Code)
}

func TestErrInvalidRequest(t *testing.T) {
err := ErrInvalidRequest()
require.Equal(t, ErrorCodeInvalidRequest, err.Code)
t.Parallel()

err := jsonrpc.ErrInvalidRequest()
require.Equal(t, jsonrpc.ErrorCodeInvalidRequest, err.Code)
}

func TestErrMethodNotFound(t *testing.T) {
err := ErrMethodNotFound()
require.Equal(t, ErrorCodeMethodNotFound, err.Code)
t.Parallel()

err := jsonrpc.ErrMethodNotFound()
require.Equal(t, jsonrpc.ErrorCodeMethodNotFound, err.Code)
}

func TestErrInvalidParams(t *testing.T) {
err := ErrInvalidParams()
require.Equal(t, ErrorCodeInvalidParams, err.Code)
t.Parallel()

err := jsonrpc.ErrInvalidParams()
require.Equal(t, jsonrpc.ErrorCodeInvalidParams, err.Code)
}

func TestErrInternal(t *testing.T) {
err := ErrInternal()
require.Equal(t, ErrorCodeInternal, err.Code)
t.Parallel()

err := jsonrpc.ErrInternal()
require.Equal(t, jsonrpc.ErrorCodeInternal, err.Code)
}
Loading

0 comments on commit 368308e

Please sign in to comment.