Skip to content

Commit

Permalink
back to the stdlib
Browse files Browse the repository at this point in the history
  • Loading branch information
asim committed Nov 20, 2020
1 parent 866dbbc commit 934cb4d
Show file tree
Hide file tree
Showing 17 changed files with 12 additions and 671 deletions.
13 changes: 2 additions & 11 deletions app/client/rpc/rpc_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
raw "github.com/asim/nitro/app/codec/bytes"
"github.com/asim/nitro/app/codec/json"
"github.com/asim/nitro/app/codec/jsonrpc"
"github.com/asim/nitro/app/codec/proto"
"github.com/asim/nitro/app/codec/protorpc"
"github.com/asim/nitro/app/errors"
"github.com/asim/nitro/app/network"
"github.com/asim/nitro/app/registry"
Expand Down Expand Up @@ -52,20 +50,15 @@ var (
DefaultContentType = "application/json"

DefaultCodecs = map[string]codec.NewCodec{
"application/protobuf": proto.NewCodec,
"application/json": json.NewCodec,
"application/json-rpc": jsonrpc.NewCodec,
"application/proto-rpc": protorpc.NewCodec,
"application/octet-stream": raw.NewCodec,
}

// TODO: remove legacy codec list
defaultCodecs = map[string]codec.NewCodec{
"application/json": jsonrpc.NewCodec,
"application/json-rpc": jsonrpc.NewCodec,
"application/protobuf": protorpc.NewCodec,
"application/proto-rpc": protorpc.NewCodec,
"application/octet-stream": protorpc.NewCodec,
"application/json": jsonrpc.NewCodec,
"application/json-rpc": jsonrpc.NewCodec,
}
)

Expand Down Expand Up @@ -139,8 +132,6 @@ func setupProtocol(msg *network.Message, node *registry.Node) codec.NewCodec {
switch msg.Header["Content-Type"] {
case "application/json":
msg.Header["Content-Type"] = "application/json-rpc"
case "application/protobuf":
msg.Header["Content-Type"] = "application/proto-rpc"
}

// now return codec
Expand Down
4 changes: 2 additions & 2 deletions app/client/rpc/rpc_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ func TestRequestOptions(t *testing.T) {
t.Fatalf("expected 'endpoint' got %s", r.ContentType())
}

r2 := newRequest("service", "endpoint", nil, "application/json", client.WithContentType("application/protobuf"))
if r2.ContentType() != "application/protobuf" {
r2 := newRequest("service", "endpoint", nil, "application/octet", client.WithContentType("application/octet"))
if r2.ContentType() != "application/octet" {
t.Fatalf("expected 'endpoint' got %s", r2.ContentType())
}
}
10 changes: 3 additions & 7 deletions app/codec/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"github.com/asim/nitro/app/codec/bytes"
"github.com/asim/nitro/app/codec/json"
"github.com/asim/nitro/app/codec/jsonrpc"
"github.com/asim/nitro/app/codec/proto"
"github.com/asim/nitro/app/codec/protorpc"
)

type testRWC struct{}
Expand All @@ -28,11 +26,9 @@ func (rwc *testRWC) Close() error {

func getCodecs(c io.ReadWriteCloser) map[string]codec.Codec {
return map[string]codec.Codec{
"bytes": bytes.NewCodec(c),
"json": json.NewCodec(c),
"jsonrpc": jsonrpc.NewCodec(c),
"proto": proto.NewCodec(c),
"protorpc": protorpc.NewCodec(c),
"bytes": bytes.NewCodec(c),
"json": json.NewCodec(c),
"jsonrpc": jsonrpc.NewCodec(c),
}
}

Expand Down
5 changes: 0 additions & 5 deletions app/codec/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"io"

"github.com/asim/nitro/app/codec"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
)

type Codec struct {
Expand All @@ -24,9 +22,6 @@ func (c *Codec) ReadBody(b interface{}) error {
if b == nil {
return nil
}
if pb, ok := b.(proto.Message); ok {
return jsonpb.UnmarshalNext(c.Decoder, pb)
}
return c.Decoder.Decode(b)
}

Expand Down
16 changes: 0 additions & 16 deletions app/codec/json/marshaler.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,21 @@
package json

import (
"bytes"
"encoding/json"

"github.com/asim/nitro/util/buf"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
)

var jsonpbMarshaler = &jsonpb.Marshaler{}

// create buffer pool with 16 instances each preallocated with 256 bytes
var bufferPool = buf.NewPool()

type Marshaler struct{}

func (j Marshaler) Marshal(v interface{}) ([]byte, error) {
if pb, ok := v.(proto.Message); ok {
buf := bufferPool.Get()
defer bufferPool.Put(buf)
if err := jsonpbMarshaler.Marshal(buf, pb); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
return json.Marshal(v)
}

func (j Marshaler) Unmarshal(d []byte, v interface{}) error {
if pb, ok := v.(proto.Message); ok {
return jsonpb.Unmarshal(bytes.NewReader(d), pb)
}
return json.Unmarshal(d, v)
}

Expand Down
47 changes: 0 additions & 47 deletions app/codec/proto/marshaler.go

This file was deleted.

37 changes: 0 additions & 37 deletions app/codec/proto/message.go

This file was deleted.

64 changes: 0 additions & 64 deletions app/codec/proto/proto.go

This file was deleted.

Loading

0 comments on commit 934cb4d

Please sign in to comment.