3e885308a0
Fixes #2988. Brings 'golangci-lint run ./...' to zero issues (was ~373): - errcheck: explicitly ignore fire-and-forget calls with '_ =' (and a small errcheck.exclude-functions list for response writes — json Encoder.Encode, http ResponseWriter.Write, fmt.Fprint*); genuine cases handled. - unused: remove dead code (unexported decls and dead test helpers) and the imports they orphaned. - staticcheck: ST1005 error strings, ST1016 receiver names, S1000/S1017/S1019/ S1023 simplifications, SA4004/SA4006/SA4010 dead code, SA1021 net.IP.Equal, SA6002 (store *[]byte in sync.Pool). - govet: fix a context leak (lostcancel) in internal/util/mdns and move t.Fatal/Fatalf out of goroutines (testinggoroutine) in tests. - ineffassign, unconvert: mechanical fixes. CI: the Lint workflow now runs a blocking full-tree 'golangci-lint run' on pushes and PRs (dropped only-new-issues now that the tree is clean). Verified: go build, go vet, test compilation, and unit tests for the behaviourally-touched packages all pass. Claude-Session: https://claude.ai/code/session_01CmdEY7pYmV5zzwCjNJ4ykL Co-authored-by: Claude <noreply@anthropic.com>
89 lines
1.6 KiB
Go
89 lines
1.6 KiB
Go
// Package jsonrpc provides a json-rpc 1.0 codec
|
|
package jsonrpc
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
|
|
"go-micro.dev/v6/codec"
|
|
)
|
|
|
|
type jsonCodec struct {
|
|
rwc io.ReadWriteCloser
|
|
buf *bytes.Buffer
|
|
c *clientCodec
|
|
s *serverCodec
|
|
mt codec.MessageType
|
|
}
|
|
|
|
func (j *jsonCodec) Close() error {
|
|
j.buf.Reset()
|
|
return j.rwc.Close()
|
|
}
|
|
|
|
func (j *jsonCodec) String() string {
|
|
return "json-rpc"
|
|
}
|
|
|
|
func (j *jsonCodec) Write(m *codec.Message, b interface{}) error {
|
|
switch m.Type {
|
|
case codec.Request:
|
|
return j.c.Write(m, b)
|
|
case codec.Response, codec.Error:
|
|
return j.s.Write(m, b)
|
|
case codec.Event:
|
|
data, err := json.Marshal(b)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = j.rwc.Write(data)
|
|
return err
|
|
default:
|
|
return fmt.Errorf("unrecognized message type: %v", m.Type)
|
|
}
|
|
}
|
|
|
|
func (j *jsonCodec) ReadHeader(m *codec.Message, mt codec.MessageType) error {
|
|
j.buf.Reset()
|
|
j.mt = mt
|
|
|
|
switch mt {
|
|
case codec.Request:
|
|
return j.s.ReadHeader(m)
|
|
case codec.Response:
|
|
return j.c.ReadHeader(m)
|
|
case codec.Event:
|
|
_, err := io.Copy(j.buf, j.rwc)
|
|
return err
|
|
default:
|
|
return fmt.Errorf("unrecognized message type: %v", mt)
|
|
}
|
|
}
|
|
|
|
func (j *jsonCodec) ReadBody(b interface{}) error {
|
|
switch j.mt {
|
|
case codec.Request:
|
|
return j.s.ReadBody(b)
|
|
case codec.Response:
|
|
return j.c.ReadBody(b)
|
|
case codec.Event:
|
|
if b != nil {
|
|
return json.Unmarshal(j.buf.Bytes(), b)
|
|
}
|
|
default:
|
|
return fmt.Errorf("unrecognized message type: %v", j.mt)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func NewCodec(rwc io.ReadWriteCloser) codec.Codec {
|
|
return &jsonCodec{
|
|
buf: bytes.NewBuffer(nil),
|
|
rwc: rwc,
|
|
c: newClientCodec(rwc),
|
|
s: newServerCodec(rwc),
|
|
}
|
|
}
|