Skip to content

Commit

Permalink
[IMPROVED] Replace empty interface with any
Browse files Browse the repository at this point in the history
Signed-off-by: Piotr Piotrowski <[email protected]>
  • Loading branch information
piotrpio committed Jun 14, 2023
1 parent ad14f4d commit af3706e
Show file tree
Hide file tree
Showing 18 changed files with 44 additions and 44 deletions.
2 changes: 1 addition & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func (nc *Conn) FlushWithContext(ctx context.Context) error {
// RequestWithContext will create an Inbox and perform a Request
// using the provided cancellation context with the Inbox reply
// for the data v. A response will be decoded into the vPtr last parameter.
func (c *EncodedConn) RequestWithContext(ctx context.Context, subject string, v interface{}, vPtr interface{}) error {
func (c *EncodedConn) RequestWithContext(ctx context.Context, subject string, v any, vPtr any) error {
if ctx == nil {
return ErrInvalidContext
}
Expand Down
14 changes: 7 additions & 7 deletions enc.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (

// Encoder interface is for all register encoders
type Encoder interface {
Encode(subject string, v interface{}) ([]byte, error)
Decode(subject string, data []byte, vPtr interface{}) error
Encode(subject string, v any) ([]byte, error)
Decode(subject string, data []byte, vPtr any) error
}

var encMap map[string]Encoder
Expand Down Expand Up @@ -88,7 +88,7 @@ func EncoderForType(encType string) Encoder {

// Publish publishes the data argument to the given subject. The data argument
// will be encoded using the associated encoder.
func (c *EncodedConn) Publish(subject string, v interface{}) error {
func (c *EncodedConn) Publish(subject string, v any) error {
b, err := c.Enc.Encode(subject, v)
if err != nil {
return err
Expand All @@ -99,7 +99,7 @@ func (c *EncodedConn) Publish(subject string, v interface{}) error {
// PublishRequest will perform a Publish() expecting a response on the
// reply subject. Use Request() for automatically waiting for a response
// inline.
func (c *EncodedConn) PublishRequest(subject, reply string, v interface{}) error {
func (c *EncodedConn) PublishRequest(subject, reply string, v any) error {
b, err := c.Enc.Encode(subject, v)
if err != nil {
return err
Expand All @@ -110,7 +110,7 @@ func (c *EncodedConn) PublishRequest(subject, reply string, v interface{}) error
// Request will create an Inbox and perform a Request() call
// with the Inbox reply for the data v. A response will be
// decoded into the vPtr Response.
func (c *EncodedConn) Request(subject string, v interface{}, vPtr interface{}, timeout time.Duration) error {
func (c *EncodedConn) Request(subject string, v any, vPtr any, timeout time.Duration) error {
b, err := c.Enc.Encode(subject, v)
if err != nil {
return err
Expand All @@ -129,7 +129,7 @@ func (c *EncodedConn) Request(subject string, v interface{}, vPtr interface{}, t
}

// Handler is a specific callback used for Subscribe. It is generalized to
// an interface{}, but we will discover its format and arguments at runtime
// an any, but we will discover its format and arguments at runtime
// and perform the correct callback, including demarshaling encoded data
// back into the appropriate struct based on the signature of the Handler.
//
Expand All @@ -150,7 +150,7 @@ func (c *EncodedConn) Request(subject string, v interface{}, vPtr interface{}, t
// and demarshal it into the given struct, e.g. person.
// There are also variants where the callback wants either the subject, or the
// subject and the reply subject.
type Handler interface{}
type Handler any

// Dissect the cb Handler's signature
func argInfo(cb Handler) (reflect.Type, int) {
Expand Down
4 changes: 2 additions & 2 deletions encoders/builtin/default_enc.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var falseB = []byte("false")
var nilB = []byte("")

// Encode
func (je *DefaultEncoder) Encode(subject string, v interface{}) ([]byte, error) {
func (je *DefaultEncoder) Encode(subject string, v any) ([]byte, error) {
switch arg := v.(type) {
case string:
bytes := *(*[]byte)(unsafe.Pointer(&arg))
Expand All @@ -58,7 +58,7 @@ func (je *DefaultEncoder) Encode(subject string, v interface{}) ([]byte, error)
}

// Decode
func (je *DefaultEncoder) Decode(subject string, data []byte, vPtr interface{}) error {
func (je *DefaultEncoder) Decode(subject string, data []byte, vPtr any) error {
// Figure out what it's pointing to...
sData := *(*string)(unsafe.Pointer(&data))
switch arg := vPtr.(type) {
Expand Down
4 changes: 2 additions & 2 deletions encoders/builtin/gob_enc.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type GobEncoder struct {
// FIXME(dlc) - This could probably be more efficient.

// Encode
func (ge *GobEncoder) Encode(subject string, v interface{}) ([]byte, error) {
func (ge *GobEncoder) Encode(subject string, v any) ([]byte, error) {
b := new(bytes.Buffer)
enc := gob.NewEncoder(b)
if err := enc.Encode(v); err != nil {
Expand All @@ -38,7 +38,7 @@ func (ge *GobEncoder) Encode(subject string, v interface{}) ([]byte, error) {
}

// Decode
func (ge *GobEncoder) Decode(subject string, data []byte, vPtr interface{}) (err error) {
func (ge *GobEncoder) Decode(subject string, data []byte, vPtr any) (err error) {
dec := gob.NewDecoder(bytes.NewBuffer(data))
err = dec.Decode(vPtr)
return
Expand Down
4 changes: 2 additions & 2 deletions encoders/builtin/json_enc.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type JsonEncoder struct {
}

// Encode
func (je *JsonEncoder) Encode(subject string, v interface{}) ([]byte, error) {
func (je *JsonEncoder) Encode(subject string, v any) ([]byte, error) {
b, err := json.Marshal(v)
if err != nil {
return nil, err
Expand All @@ -35,7 +35,7 @@ func (je *JsonEncoder) Encode(subject string, v interface{}) ([]byte, error) {
}

// Decode
func (je *JsonEncoder) Decode(subject string, data []byte, vPtr interface{}) (err error) {
func (je *JsonEncoder) Decode(subject string, data []byte, vPtr any) (err error) {
switch arg := vPtr.(type) {
case *string:
// If they want a string and it is a JSON string, strip quotes
Expand Down
6 changes: 3 additions & 3 deletions encoders/protobuf/protobuf_enc.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var (
)

// Encode
func (pb *ProtobufEncoder) Encode(subject string, v interface{}) ([]byte, error) {
func (pb *ProtobufEncoder) Encode(subject string, v any) ([]byte, error) {
if v == nil {
return nil, nil
}
Expand All @@ -60,8 +60,8 @@ func (pb *ProtobufEncoder) Encode(subject string, v interface{}) ([]byte, error)
}

// Decode
func (pb *ProtobufEncoder) Decode(subject string, data []byte, vPtr interface{}) error {
if _, ok := vPtr.(*interface{}); ok {
func (pb *ProtobufEncoder) Decode(subject string, data []byte, vPtr any) error {
if _, ok := vPtr.(*any); ok {
return nil
}
i, found := vPtr.(proto.Message)
Expand Down
4 changes: 2 additions & 2 deletions encoders/protobuf/testdata/pbtest.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion jetstream/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ const (
apiMsgDeleteT = "STREAM.MSG.DELETE.%s"
)

func (js *jetStream) apiRequestJSON(ctx context.Context, subject string, resp interface{}, data ...[]byte) (*jetStreamMsg, error) {
func (js *jetStream) apiRequestJSON(ctx context.Context, subject string, resp any, data ...[]byte) (*jetStreamMsg, error) {
jsMsg, err := js.apiRequest(ctx, subject, data...)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion js.go
Original file line number Diff line number Diff line change
Expand Up @@ -1352,7 +1352,7 @@ func processConsInfo(info *ConsumerInfo, userCfg *ConsumerConfig, isPullMode boo
}

func checkConfig(s, u *ConsumerConfig) error {
makeErr := func(fieldName string, usrVal, srvVal interface{}) error {
makeErr := func(fieldName string, usrVal, srvVal any) error {
return fmt.Errorf("configuration requests %s to be %v, but consumer's value is %v", fieldName, usrVal, srvVal)
}

Expand Down
4 changes: 2 additions & 2 deletions micro/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type (

// RespondJSON marshals the given response value and responds to the request.
// Additional headers can be passed using [WithHeaders] option.
RespondJSON(interface{}, ...RespondOpt) error
RespondJSON(any, ...RespondOpt) error

// Error prepares and publishes error response from a handler.
// A response error should be set containing an error code and description.
Expand Down Expand Up @@ -111,7 +111,7 @@ func (r *request) Respond(response []byte, opts ...RespondOpt) error {

// RespondJSON marshals the given response value and responds to the request.
// Additional headers can be passed using [WithHeaders] option.
func (r *request) RespondJSON(response interface{}, opts ...RespondOpt) error {
func (r *request) RespondJSON(response any, opts ...RespondOpt) error {
resp, err := json.Marshal(response)
if err != nil {
return ErrMarshalResponse
Expand Down
2 changes: 1 addition & 1 deletion micro/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ type (

// StatsHandler is a function used to configure a custom STATS endpoint.
// It should return a value which can be serialized to JSON.
StatsHandler func(*Endpoint) interface{}
StatsHandler func(*Endpoint) any

// ServiceIdentity contains fields helping to identity a service instance.
ServiceIdentity struct {
Expand Down
18 changes: 9 additions & 9 deletions micro/test/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ func TestMonitoringHandlers(t *testing.T) {
name string
subject string
withError bool
expectedResponse interface{}
expectedResponse any
}{
{
name: "PING all",
Expand Down Expand Up @@ -934,15 +934,15 @@ func TestMonitoringHandlers(t *testing.T) {
t.Fatalf("Unexpected error: %v", err)
}

respMap := make(map[string]interface{})
respMap := make(map[string]any)
if err := json.Unmarshal(resp.Data, &respMap); err != nil {
t.Fatalf("Unexpected error: %v", err)
}
expectedResponseJSON, err := json.Marshal(test.expectedResponse)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
expectedRespMap := make(map[string]interface{})
expectedRespMap := make(map[string]any)
if err := json.Unmarshal(expectedResponseJSON, &expectedRespMap); err != nil {
t.Fatalf("Unexpected error: %v", err)
}
Expand Down Expand Up @@ -1018,7 +1018,7 @@ func TestServiceStats(t *testing.T) {
tests := []struct {
name string
config micro.Config
expectedStats map[string]interface{}
expectedStats map[string]any
}{
{
name: "stats handler",
Expand All @@ -1032,13 +1032,13 @@ func TestServiceStats(t *testing.T) {
config: micro.Config{
Name: "test_service",
Version: "0.1.0",
StatsHandler: func(e *micro.Endpoint) interface{} {
return map[string]interface{}{
StatsHandler: func(e *micro.Endpoint) any {
return map[string]any{
"key": "val",
}
},
},
expectedStats: map[string]interface{}{
expectedStats: map[string]any{
"key": "val",
},
},
Expand Down Expand Up @@ -1140,7 +1140,7 @@ func TestServiceStats(t *testing.T) {
}

if test.expectedStats != nil {
var data map[string]interface{}
var data map[string]any
if err := json.Unmarshal(stats.Endpoints[0].Data, &data); err != nil {
t.Fatalf("Unexpected error: %v", err)
}
Expand All @@ -1160,7 +1160,7 @@ func TestRequestRespond(t *testing.T) {

tests := []struct {
name string
respondData interface{}
respondData any
respondHeaders micro.Headers
errDescription string
errCode string
Expand Down
2 changes: 1 addition & 1 deletion nats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func WaitTime(ch chan bool, timeout time.Duration) error {
return errors.New("timeout")
}

func stackFatalf(t *testing.T, f string, args ...interface{}) {
func stackFatalf(t *testing.T, f string, args ...any) {
lines := make([]string, 0, 32)
msg := fmt.Sprintf(f, args...)
lines = append(lines, msg)
Expand Down
8 changes: 4 additions & 4 deletions netchan.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
// Data will be encoded and decoded via the EncodedConn and its associated encoders.

// BindSendChan binds a channel for send operations to NATS.
func (c *EncodedConn) BindSendChan(subject string, channel interface{}) error {
func (c *EncodedConn) BindSendChan(subject string, channel any) error {
chVal := reflect.ValueOf(channel)
if chVal.Kind() != reflect.Chan {
return ErrChanArg
Expand Down Expand Up @@ -61,17 +61,17 @@ func chPublish(c *EncodedConn, chVal reflect.Value, subject string) {
}

// BindRecvChan binds a channel for receive operations from NATS.
func (c *EncodedConn) BindRecvChan(subject string, channel interface{}) (*Subscription, error) {
func (c *EncodedConn) BindRecvChan(subject string, channel any) (*Subscription, error) {
return c.bindRecvChan(subject, _EMPTY_, channel)
}

// BindRecvQueueChan binds a channel for queue-based receive operations from NATS.
func (c *EncodedConn) BindRecvQueueChan(subject, queue string, channel interface{}) (*Subscription, error) {
func (c *EncodedConn) BindRecvQueueChan(subject, queue string, channel any) (*Subscription, error) {
return c.bindRecvChan(subject, queue, channel)
}

// Internal function to bind receive operations for a channel.
func (c *EncodedConn) bindRecvChan(subject, queue string, channel interface{}) (*Subscription, error) {
func (c *EncodedConn) bindRecvChan(subject, queue string, channel any) (*Subscription, error) {
chVal := reflect.ValueOf(channel)
if chVal.Kind() != reflect.Chan {
return nil, ErrChanArg
Expand Down
6 changes: 3 additions & 3 deletions test/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import (

// So that we can pass tests and benchmarks...
type tLogger interface {
Fatalf(format string, args ...interface{})
Errorf(format string, args ...interface{})
Fatalf(format string, args ...any)
Errorf(format string, args ...any)
}

// TestLogger
Expand All @@ -52,7 +52,7 @@ func WaitTime(ch chan bool, timeout time.Duration) error {
return errors.New("timeout")
}

func stackFatalf(t tLogger, f string, args ...interface{}) {
func stackFatalf(t tLogger, f string, args ...any) {
lines := make([]string, 0, 32)
msg := fmt.Sprintf(f, args...)
lines = append(lines, msg)
Expand Down
2 changes: 1 addition & 1 deletion test/js_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6490,7 +6490,7 @@ func testJetStream_ClusterMultipleFetchPullSubscribe(t *testing.T, subject strin
gotNoMessages bool
count = 0
)
queues.Range(func(k, v interface{}) bool {
queues.Range(func(k, v any) bool {
msgs := v.([]*nats.Msg)
count += len(msgs)

Expand Down
2 changes: 1 addition & 1 deletion test/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func TestEncBuiltinJsonMarshalNull(t *testing.T) {

var testValue *TestType

ec.Subscribe("json_null", func(i interface{}) {
ec.Subscribe("json_null", func(i any) {
if i != nil {
t.Fatalf("Received test of '%v', wanted 'nil'\n", i)
}
Expand Down
2 changes: 1 addition & 1 deletion test/protobuf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestEncProtoNilRequest(t *testing.T) {
testPerson := &pb.Person{Name: "Anatolii", Age: 25, Address: "Ukraine, Nikolaev"}

//Subscribe with empty interface shouldn't failed on empty message
ec.Subscribe("nil_test", func(_, reply string, _ interface{}) {
ec.Subscribe("nil_test", func(_, reply string, _ any) {
ec.Publish(reply, testPerson)
})

Expand Down

0 comments on commit af3706e

Please sign in to comment.