Skip to content

Commit 6885b6d

Browse files
committed
Refactoring
1 parent 5fa3962 commit 6885b6d

19 files changed

+44
-216
lines changed

.travis.yml

+16-9
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
1-
sudo: required
2-
dist: trusty
1+
sudo: false
32

43
language: go
54
go:
6-
- 1.6
7-
- 1.7
8-
- 1.8
5+
- 1.6.4
6+
- 1.7.6
7+
- 1.8.3
8+
- 1.9
9+
- tip
10+
11+
matrix:
12+
allow_failures:
13+
- go: tip
914

1015
before_install:
11-
- sudo add-apt-repository ppa:masterminds/glide -y
12-
- sudo apt-get update
13-
- sudo apt-get install glide -y
16+
- go get -u github.com/Masterminds/glide github.com/golang/lint/golint
1417

1518
install:
1619
- glide install
1720

21+
before_script:
22+
- go vet .
23+
- golint .
24+
1825
script:
19-
- go test -race -covermode=atomic -coverprofile=coverage.txt .
26+
- go test -covermode=atomic -coverprofile=coverage.txt .
2027

2128
after_success:
2229
- bash <(curl -s https://codecov.io/bash)

README.md

+8-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
- Simple, Poetic, Pithy.
1313
- No `reflect` package.
1414
- But `reflect` package is used only when invoke the debug handler.
15-
- Support both packages `context` and `golang.org/x/net/context`.
1615
- Support GAE/Go Standard Environment.
1716
- Compliance with [JSON-RPC 2.0](http://www.jsonrpc.org/specification).
1817

@@ -25,30 +24,28 @@ $ go get -u github.com/osamingo/jsonrpc
2524
## Usage
2625

2726
```go
28-
package main
27+
package jsonrpc
2928

3029
import (
31-
"context"
3230
"encoding/json"
3331
"log"
3432
"net/http"
3533

3634
"github.com/osamingo/jsonrpc"
35+
"golang.org/x/net/context"
3736
)
3837

3938
type (
40-
EchoHandler struct {}
41-
EchoParams struct {
39+
EchoHandler struct{}
40+
EchoParams struct {
4241
Name string `json:"name"`
4342
}
4443
EchoResult struct {
4544
Message string `json:"message"`
4645
}
4746
)
4847

49-
var _ (jsonrpc.Handler) = (*EchoHandler)(nil)
50-
51-
func (h *EchoHandler)ServeJSONRPC(c context.Context, params *json.RawMessage) (interface{}, *jsonrpc.Error) {
48+
func (h *EchoHandler) ServeJSONRPC(c context.Context, params *json.RawMessage) (interface{}, *jsonrpc.Error) {
5249

5350
var p EchoParams
5451
if err := jsonrpc.Unmarshal(params, &p); err != nil {
@@ -65,7 +62,9 @@ func init() {
6562
}
6663

6764
func main() {
68-
http.HandleFunc("/jrpc", jsonrpc.HandlerFunc)
65+
http.HandleFunc("/jrpc", func(w http.ResponseWriter, r *http.Request) {
66+
jsonrpc.HandlerFunc(r.Context(), w, r)
67+
})
6968
http.HandleFunc("/jrpc/debug", jsonrpc.DebugHandlerFunc)
7069
if err := http.ListenAndServe(":8080", nil); err != nil {
7170
log.Fatalln(err)

context.go

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// +build !go1.7
2-
31
package jsonrpc
42

53
import (

context17.go

-20
This file was deleted.

context17_test.go

-23
This file was deleted.

context_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// +build !go1.7
2-
31
package jsonrpc
42

53
import (

debug_test.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// +build go1.7
2-
31
package jsonrpc
42

53
import (
@@ -26,7 +24,7 @@ func TestDebugHandler(t *testing.T) {
2624
require.NoError(t, RegisterMethod("Debug.Sample", SampleHandler(), struct {
2725
Name string `json:"name"`
2826
}{}, struct {
29-
Message string `json:"message,omitrmpty"`
27+
Message string `json:"message,omitempty"`
3028
}{}))
3129

3230
rec = httptest.NewRecorder()

error.go

-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ const (
1515
ErrorCodeInternal ErrorCode = -32603
1616
)
1717

18-
var _ error = (*Error)(nil)
19-
2018
type (
2119
// A ErrorCode by JSON-RPC 2.0.
2220
ErrorCode int

error_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ import (
77
"github.com/stretchr/testify/require"
88
)
99

10+
func TestError(t *testing.T) {
11+
var err interface{} = &Error{}
12+
_, ok := err.(error)
13+
require.True(t, ok)
14+
}
15+
1016
func TestError_Error(t *testing.T) {
1117

1218
err := &Error{

example_test.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
// +build go1.7
1+
//+build !go1.6
22

33
package jsonrpc
44

55
import (
66
"bytes"
7-
"context"
87
"encoding/json"
98
"io"
109
"log"
1110
"net/http"
1211
"net/http/httptest"
1312
"os"
13+
14+
"golang.org/x/net/context"
1415
)
1516

1617
type (
@@ -43,13 +44,15 @@ func ExampleEchoHandler_ServeJSONRPC() {
4344
log.Fatalln(err)
4445
}
4546

46-
http.HandleFunc("/v17/jrpc", HandlerFunc)
47-
http.HandleFunc("/v17/jrpc/debug", DebugHandlerFunc)
47+
http.HandleFunc("/jrpc", func(w http.ResponseWriter, r *http.Request) {
48+
HandlerFunc(r.Context(), w, r)
49+
})
50+
http.HandleFunc("/jrpc/debug", DebugHandlerFunc)
4851

4952
srv := httptest.NewServer(http.DefaultServeMux)
5053
defer srv.Close()
5154

52-
resp, err := http.Post(srv.URL+"/v17/jrpc", "application/json", bytes.NewBufferString(`{
55+
resp, err := http.Post(srv.URL+"/jrpc", "application/json", bytes.NewBufferString(`{
5356
"jsonrpc": "2.0",
5457
"method": "Main.Echo",
5558
"params": {

glide.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

glide.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
package: .
1+
package: github.com/osamingo/jsonrpc
22
import:
3+
- package: github.com/alecthomas/jsonschema
34
- package: golang.org/x/net
45
subpackages:
56
- context
6-
- package: github.com/alecthomas/jsonschema
77
testImport:
88
- package: github.com/stretchr/testify
99
subpackages:

handler.go

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// +build !go1.7
2-
31
package jsonrpc
42

53
import (

handler17.go

-13
This file was deleted.

handler_func.go

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// +build !go1.7
2-
31
package jsonrpc
42

53
import (

handler_func17.go

-47
This file was deleted.

handler_func17_test.go

-68
This file was deleted.

handler_func_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// +build !go1.7
2-
31
package jsonrpc
42

53
import (

0 commit comments

Comments
 (0)