From 1bb385f0447cb7be86a4535cd89c03779905586c Mon Sep 17 00:00:00 2001 From: Piotr Piotrowski Date: Thu, 22 Jun 2023 10:52:40 +0200 Subject: [PATCH] Revert "[IMPROVED] Replace empty interface with any" (#1330) This reverts commit af3706ecfd61b9adec6a504abc954c2f15a7dc8a. --- context.go | 2 +- enc.go | 14 +++++++------- encoders/builtin/default_enc.go | 4 ++-- encoders/builtin/gob_enc.go | 4 ++-- encoders/builtin/json_enc.go | 4 ++-- encoders/protobuf/protobuf_enc.go | 6 +++--- encoders/protobuf/testdata/pbtest.pb.go | 4 ++-- jetstream/api.go | 2 +- js.go | 2 +- micro/request.go | 4 ++-- micro/service.go | 2 +- micro/test/service_test.go | 18 +++++++++--------- nats_test.go | 2 +- netchan.go | 8 ++++---- test/helper_test.go | 6 +++--- test/js_test.go | 2 +- test/json_test.go | 2 +- test/protobuf_test.go | 2 +- 18 files changed, 44 insertions(+), 44 deletions(-) diff --git a/context.go b/context.go index c4ef4be17..8c3c45478 100644 --- a/context.go +++ b/context.go @@ -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 any, vPtr any) error { +func (c *EncodedConn) RequestWithContext(ctx context.Context, subject string, v interface{}, vPtr interface{}) error { if ctx == nil { return ErrInvalidContext } diff --git a/enc.go b/enc.go index a1c54f246..450fa76c8 100644 --- a/enc.go +++ b/enc.go @@ -26,8 +26,8 @@ import ( // Encoder interface is for all register encoders type Encoder interface { - Encode(subject string, v any) ([]byte, error) - Decode(subject string, data []byte, vPtr any) error + Encode(subject string, v interface{}) ([]byte, error) + Decode(subject string, data []byte, vPtr interface{}) error } var encMap map[string]Encoder @@ -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 any) error { +func (c *EncodedConn) Publish(subject string, v interface{}) error { b, err := c.Enc.Encode(subject, v) if err != nil { return err @@ -99,7 +99,7 @@ func (c *EncodedConn) Publish(subject string, v any) 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 any) error { +func (c *EncodedConn) PublishRequest(subject, reply string, v interface{}) error { b, err := c.Enc.Encode(subject, v) if err != nil { return err @@ -110,7 +110,7 @@ func (c *EncodedConn) PublishRequest(subject, reply string, v any) 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 any, vPtr any, timeout time.Duration) error { +func (c *EncodedConn) Request(subject string, v interface{}, vPtr interface{}, timeout time.Duration) error { b, err := c.Enc.Encode(subject, v) if err != nil { return err @@ -129,7 +129,7 @@ func (c *EncodedConn) Request(subject string, v any, vPtr any, timeout time.Dura } // Handler is a specific callback used for Subscribe. It is generalized to -// an any, but we will discover its format and arguments at runtime +// an interface{}, 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. // @@ -150,7 +150,7 @@ func (c *EncodedConn) Request(subject string, v any, vPtr any, timeout time.Dura // 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 any +type Handler interface{} // Dissect the cb Handler's signature func argInfo(cb Handler) (reflect.Type, int) { diff --git a/encoders/builtin/default_enc.go b/encoders/builtin/default_enc.go index 65c2d68bb..46d918eea 100644 --- a/encoders/builtin/default_enc.go +++ b/encoders/builtin/default_enc.go @@ -35,7 +35,7 @@ var falseB = []byte("false") var nilB = []byte("") // Encode -func (je *DefaultEncoder) Encode(subject string, v any) ([]byte, error) { +func (je *DefaultEncoder) Encode(subject string, v interface{}) ([]byte, error) { switch arg := v.(type) { case string: bytes := *(*[]byte)(unsafe.Pointer(&arg)) @@ -58,7 +58,7 @@ func (je *DefaultEncoder) Encode(subject string, v any) ([]byte, error) { } // Decode -func (je *DefaultEncoder) Decode(subject string, data []byte, vPtr any) error { +func (je *DefaultEncoder) Decode(subject string, data []byte, vPtr interface{}) error { // Figure out what it's pointing to... sData := *(*string)(unsafe.Pointer(&data)) switch arg := vPtr.(type) { diff --git a/encoders/builtin/gob_enc.go b/encoders/builtin/gob_enc.go index 4e7cecba2..632bcbd39 100644 --- a/encoders/builtin/gob_enc.go +++ b/encoders/builtin/gob_enc.go @@ -28,7 +28,7 @@ type GobEncoder struct { // FIXME(dlc) - This could probably be more efficient. // Encode -func (ge *GobEncoder) Encode(subject string, v any) ([]byte, error) { +func (ge *GobEncoder) Encode(subject string, v interface{}) ([]byte, error) { b := new(bytes.Buffer) enc := gob.NewEncoder(b) if err := enc.Encode(v); err != nil { @@ -38,7 +38,7 @@ func (ge *GobEncoder) Encode(subject string, v any) ([]byte, error) { } // Decode -func (ge *GobEncoder) Decode(subject string, data []byte, vPtr any) (err error) { +func (ge *GobEncoder) Decode(subject string, data []byte, vPtr interface{}) (err error) { dec := gob.NewDecoder(bytes.NewBuffer(data)) err = dec.Decode(vPtr) return diff --git a/encoders/builtin/json_enc.go b/encoders/builtin/json_enc.go index 9b6ffc017..c9670f313 100644 --- a/encoders/builtin/json_enc.go +++ b/encoders/builtin/json_enc.go @@ -26,7 +26,7 @@ type JsonEncoder struct { } // Encode -func (je *JsonEncoder) Encode(subject string, v any) ([]byte, error) { +func (je *JsonEncoder) Encode(subject string, v interface{}) ([]byte, error) { b, err := json.Marshal(v) if err != nil { return nil, err @@ -35,7 +35,7 @@ func (je *JsonEncoder) Encode(subject string, v any) ([]byte, error) { } // Decode -func (je *JsonEncoder) Decode(subject string, data []byte, vPtr any) (err error) { +func (je *JsonEncoder) Decode(subject string, data []byte, vPtr interface{}) (err error) { switch arg := vPtr.(type) { case *string: // If they want a string and it is a JSON string, strip quotes diff --git a/encoders/protobuf/protobuf_enc.go b/encoders/protobuf/protobuf_enc.go index 9cb53c0a3..8f0785c2e 100644 --- a/encoders/protobuf/protobuf_enc.go +++ b/encoders/protobuf/protobuf_enc.go @@ -43,7 +43,7 @@ var ( ) // Encode -func (pb *ProtobufEncoder) Encode(subject string, v any) ([]byte, error) { +func (pb *ProtobufEncoder) Encode(subject string, v interface{}) ([]byte, error) { if v == nil { return nil, nil } @@ -60,8 +60,8 @@ func (pb *ProtobufEncoder) Encode(subject string, v any) ([]byte, error) { } // Decode -func (pb *ProtobufEncoder) Decode(subject string, data []byte, vPtr any) error { - if _, ok := vPtr.(*any); ok { +func (pb *ProtobufEncoder) Decode(subject string, data []byte, vPtr interface{}) error { + if _, ok := vPtr.(*interface{}); ok { return nil } i, found := vPtr.(proto.Message) diff --git a/encoders/protobuf/testdata/pbtest.pb.go b/encoders/protobuf/testdata/pbtest.pb.go index 4f0522402..2d7ee6366 100644 --- a/encoders/protobuf/testdata/pbtest.pb.go +++ b/encoders/protobuf/testdata/pbtest.pb.go @@ -130,7 +130,7 @@ func file_pbtest_proto_rawDescGZIP() []byte { } var file_pbtest_proto_msgTypes = make([]protoimpl.MessageInfo, 2) -var file_pbtest_proto_goTypes = []any{ +var file_pbtest_proto_goTypes = []interface{}{ (*Person)(nil), // 0: testdata.Person nil, // 1: testdata.Person.ChildrenEntry } @@ -150,7 +150,7 @@ func file_pbtest_proto_init() { return } if !protoimpl.UnsafeEnabled { - file_pbtest_proto_msgTypes[0].Exporter = func(v any, i int) any { + file_pbtest_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*Person); i { case 0: return &v.state diff --git a/jetstream/api.go b/jetstream/api.go index 0402587d9..4fef8773d 100644 --- a/jetstream/api.go +++ b/jetstream/api.go @@ -103,7 +103,7 @@ const ( apiMsgDeleteT = "STREAM.MSG.DELETE.%s" ) -func (js *jetStream) apiRequestJSON(ctx context.Context, subject string, resp any, data ...[]byte) (*jetStreamMsg, error) { +func (js *jetStream) apiRequestJSON(ctx context.Context, subject string, resp interface{}, data ...[]byte) (*jetStreamMsg, error) { jsMsg, err := js.apiRequest(ctx, subject, data...) if err != nil { return nil, err diff --git a/js.go b/js.go index 9ecf1f53d..76a55d468 100644 --- a/js.go +++ b/js.go @@ -1352,7 +1352,7 @@ func processConsInfo(info *ConsumerInfo, userCfg *ConsumerConfig, isPullMode boo } func checkConfig(s, u *ConsumerConfig) error { - makeErr := func(fieldName string, usrVal, srvVal any) error { + makeErr := func(fieldName string, usrVal, srvVal interface{}) error { return fmt.Errorf("configuration requests %s to be %v, but consumer's value is %v", fieldName, usrVal, srvVal) } diff --git a/micro/request.go b/micro/request.go index eb4c0b88d..ce2498543 100644 --- a/micro/request.go +++ b/micro/request.go @@ -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(any, ...RespondOpt) error + RespondJSON(interface{}, ...RespondOpt) error // Error prepares and publishes error response from a handler. // A response error should be set containing an error code and description. @@ -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 any, opts ...RespondOpt) error { +func (r *request) RespondJSON(response interface{}, opts ...RespondOpt) error { resp, err := json.Marshal(response) if err != nil { return ErrMarshalResponse diff --git a/micro/service.go b/micro/service.go index cee4ef3af..6c01570d8 100644 --- a/micro/service.go +++ b/micro/service.go @@ -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) any + StatsHandler func(*Endpoint) interface{} // ServiceIdentity contains fields helping to identity a service instance. ServiceIdentity struct { diff --git a/micro/test/service_test.go b/micro/test/service_test.go index 5386ae214..6129671ff 100644 --- a/micro/test/service_test.go +++ b/micro/test/service_test.go @@ -795,7 +795,7 @@ func TestMonitoringHandlers(t *testing.T) { name string subject string withError bool - expectedResponse any + expectedResponse interface{} }{ { name: "PING all", @@ -934,7 +934,7 @@ func TestMonitoringHandlers(t *testing.T) { t.Fatalf("Unexpected error: %v", err) } - respMap := make(map[string]any) + respMap := make(map[string]interface{}) if err := json.Unmarshal(resp.Data, &respMap); err != nil { t.Fatalf("Unexpected error: %v", err) } @@ -942,7 +942,7 @@ func TestMonitoringHandlers(t *testing.T) { if err != nil { t.Fatalf("Unexpected error: %v", err) } - expectedRespMap := make(map[string]any) + expectedRespMap := make(map[string]interface{}) if err := json.Unmarshal(expectedResponseJSON, &expectedRespMap); err != nil { t.Fatalf("Unexpected error: %v", err) } @@ -1018,7 +1018,7 @@ func TestServiceStats(t *testing.T) { tests := []struct { name string config micro.Config - expectedStats map[string]any + expectedStats map[string]interface{} }{ { name: "stats handler", @@ -1032,13 +1032,13 @@ func TestServiceStats(t *testing.T) { config: micro.Config{ Name: "test_service", Version: "0.1.0", - StatsHandler: func(e *micro.Endpoint) any { - return map[string]any{ + StatsHandler: func(e *micro.Endpoint) interface{} { + return map[string]interface{}{ "key": "val", } }, }, - expectedStats: map[string]any{ + expectedStats: map[string]interface{}{ "key": "val", }, }, @@ -1140,7 +1140,7 @@ func TestServiceStats(t *testing.T) { } if test.expectedStats != nil { - var data map[string]any + var data map[string]interface{} if err := json.Unmarshal(stats.Endpoints[0].Data, &data); err != nil { t.Fatalf("Unexpected error: %v", err) } @@ -1160,7 +1160,7 @@ func TestRequestRespond(t *testing.T) { tests := []struct { name string - respondData any + respondData interface{} respondHeaders micro.Headers errDescription string errCode string diff --git a/nats_test.go b/nats_test.go index 5cee524ed..d697055c7 100644 --- a/nats_test.go +++ b/nats_test.go @@ -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 ...any) { +func stackFatalf(t *testing.T, f string, args ...interface{}) { lines := make([]string, 0, 32) msg := fmt.Sprintf(f, args...) lines = append(lines, msg) diff --git a/netchan.go b/netchan.go index 060721eb4..a1af9e06e 100644 --- a/netchan.go +++ b/netchan.go @@ -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 any) error { +func (c *EncodedConn) BindSendChan(subject string, channel interface{}) error { chVal := reflect.ValueOf(channel) if chVal.Kind() != reflect.Chan { return ErrChanArg @@ -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 any) (*Subscription, error) { +func (c *EncodedConn) BindRecvChan(subject string, channel interface{}) (*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 any) (*Subscription, error) { +func (c *EncodedConn) BindRecvQueueChan(subject, queue string, channel interface{}) (*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 any) (*Subscription, error) { +func (c *EncodedConn) bindRecvChan(subject, queue string, channel interface{}) (*Subscription, error) { chVal := reflect.ValueOf(channel) if chVal.Kind() != reflect.Chan { return nil, ErrChanArg diff --git a/test/helper_test.go b/test/helper_test.go index 632af70e4..992e6d7ef 100644 --- a/test/helper_test.go +++ b/test/helper_test.go @@ -30,8 +30,8 @@ import ( // So that we can pass tests and benchmarks... type tLogger interface { - Fatalf(format string, args ...any) - Errorf(format string, args ...any) + Fatalf(format string, args ...interface{}) + Errorf(format string, args ...interface{}) } // TestLogger @@ -52,7 +52,7 @@ func WaitTime(ch chan bool, timeout time.Duration) error { return errors.New("timeout") } -func stackFatalf(t tLogger, f string, args ...any) { +func stackFatalf(t tLogger, f string, args ...interface{}) { lines := make([]string, 0, 32) msg := fmt.Sprintf(f, args...) lines = append(lines, msg) diff --git a/test/js_test.go b/test/js_test.go index da89222f6..3ce30beb3 100644 --- a/test/js_test.go +++ b/test/js_test.go @@ -6490,7 +6490,7 @@ func testJetStream_ClusterMultipleFetchPullSubscribe(t *testing.T, subject strin gotNoMessages bool count = 0 ) - queues.Range(func(k, v any) bool { + queues.Range(func(k, v interface{}) bool { msgs := v.([]*nats.Msg) count += len(msgs) diff --git a/test/json_test.go b/test/json_test.go index ca9e1daa5..c90718430 100644 --- a/test/json_test.go +++ b/test/json_test.go @@ -126,7 +126,7 @@ func TestEncBuiltinJsonMarshalNull(t *testing.T) { var testValue *TestType - ec.Subscribe("json_null", func(i any) { + ec.Subscribe("json_null", func(i interface{}) { if i != nil { t.Fatalf("Received test of '%v', wanted 'nil'\n", i) } diff --git a/test/protobuf_test.go b/test/protobuf_test.go index 501648341..963e741e2 100644 --- a/test/protobuf_test.go +++ b/test/protobuf_test.go @@ -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, _ any) { + ec.Subscribe("nil_test", func(_, reply string, _ interface{}) { ec.Publish(reply, testPerson) })