From 2fba1544a74c13fa25eadf2be95fed10e03c6be2 Mon Sep 17 00:00:00 2001 From: Gorbunov Kirill Andreevich Date: Tue, 27 Feb 2024 23:09:28 +0300 Subject: [PATCH 1/3] #97 add As for all interface. --- micro.go | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++ micro_test.go | 24 +++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 micro.go create mode 100644 micro_test.go diff --git a/micro.go b/micro.go new file mode 100644 index 00000000..20295601 --- /dev/null +++ b/micro.go @@ -0,0 +1,94 @@ +package micro + +import ( + "reflect" + + "go.unistack.org/micro/v3/broker" + "go.unistack.org/micro/v3/client" + "go.unistack.org/micro/v3/codec" + "go.unistack.org/micro/v3/flow" + "go.unistack.org/micro/v3/fsm" + "go.unistack.org/micro/v3/logger" + "go.unistack.org/micro/v3/meter" + "go.unistack.org/micro/v3/register" + "go.unistack.org/micro/v3/resolver" + "go.unistack.org/micro/v3/router" + "go.unistack.org/micro/v3/selector" + "go.unistack.org/micro/v3/server" + "go.unistack.org/micro/v3/store" + "go.unistack.org/micro/v3/sync" + "go.unistack.org/micro/v3/tracer" +) + +func As(b any, target any) bool { + if b == nil { + return false + } + if target == nil { + return false + } + val := reflect.ValueOf(target) + typ := val.Type() + if typ.Kind() != reflect.Ptr || val.IsNil() { + return false + } + targetType := typ.Elem() + if targetType.Kind() != reflect.Interface { + switch { + case targetType.Implements(brokerType): + break + case targetType.Implements(loggerType): + break + case targetType.Implements(clientType): + break + case targetType.Implements(serverType): + break + case targetType.Implements(codecType): + break + case targetType.Implements(flowType): + break + case targetType.Implements(fsmType): + break + case targetType.Implements(meterType): + break + case targetType.Implements(registerType): + break + case targetType.Implements(resolverType): + break + case targetType.Implements(selectorType): + break + case targetType.Implements(storeType): + break + case targetType.Implements(syncType): + break + case targetType.Implements(serviceType): + break + case targetType.Implements(routerType): + break + default: + return false + } + } + if reflect.TypeOf(b).AssignableTo(targetType) { + val.Elem().Set(reflect.ValueOf(b)) + return true + } + return false +} + +var brokerType = reflect.TypeOf((*broker.Broker)(nil)).Elem() +var loggerType = reflect.TypeOf((*logger.Logger)(nil)).Elem() +var clientType = reflect.TypeOf((*client.Client)(nil)).Elem() +var serverType = reflect.TypeOf((*server.Server)(nil)).Elem() +var codecType = reflect.TypeOf((*codec.Codec)(nil)).Elem() +var flowType = reflect.TypeOf((*flow.Flow)(nil)).Elem() +var fsmType = reflect.TypeOf((*fsm.FSM)(nil)).Elem() +var meterType = reflect.TypeOf((*meter.Meter)(nil)).Elem() +var registerType = reflect.TypeOf((*register.Register)(nil)).Elem() +var resolverType = reflect.TypeOf((*resolver.Resolver)(nil)).Elem() +var routerType = reflect.TypeOf((*router.Router)(nil)).Elem() +var selectorType = reflect.TypeOf((*selector.Selector)(nil)).Elem() +var storeType = reflect.TypeOf((*store.Store)(nil)).Elem() +var syncType = reflect.TypeOf((*sync.Sync)(nil)).Elem() +var tracerType = reflect.TypeOf((*tracer.Tracer)(nil)).Elem() +var serviceType = reflect.TypeOf((*Service)(nil)).Elem() diff --git a/micro_test.go b/micro_test.go new file mode 100644 index 00000000..cb90e977 --- /dev/null +++ b/micro_test.go @@ -0,0 +1,24 @@ +package micro + +import "testing" + +func TestAs(t *testing.T) { + type args struct { + b any + target any + } + tests := []struct { + name string + args args + want bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := As(tt.args.b, tt.args.target); got != tt.want { + t.Errorf("As() = %v, want %v", got, tt.want) + } + }) + } +} -- 2.45.2 From d07ac6730069fb44018177d9e6572213d0e61a49 Mon Sep 17 00:00:00 2001 From: Gorbunov Kirill Andreevich Date: Tue, 27 Feb 2024 23:11:53 +0300 Subject: [PATCH 2/3] #97 add As for all interface. --- micro_test.go | 103 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 91 insertions(+), 12 deletions(-) diff --git a/micro_test.go b/micro_test.go index cb90e977..b9387c97 100644 --- a/micro_test.go +++ b/micro_test.go @@ -1,24 +1,103 @@ package micro -import "testing" +import ( + "context" + "fmt" + "reflect" + "testing" + + "go.unistack.org/micro/v3/broker" + "go.unistack.org/micro/v3/fsm" + "go.unistack.org/micro/v3/options" +) func TestAs(t *testing.T) { - type args struct { + var b *bro + broTarget := &bro{name: "kafka"} + fsmTarget := &fsmT{name: "fsm"} + + testCases := []struct { b any target any - } - tests := []struct { - name string - args args - want bool + match bool + want any }{ - // TODO: Add test cases. + { + broTarget, + &b, + true, + broTarget, + }, + { + nil, + &b, + false, + nil, + }, + { + fsmTarget, + &b, + false, + nil, + }, } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := As(tt.args.b, tt.args.target); got != tt.want { - t.Errorf("As() = %v, want %v", got, tt.want) + for i, tc := range testCases { + name := fmt.Sprintf("%d:As(Errorf(..., %v), %v)", i, tc.b, tc.target) + // Clear the target pointer, in case it was set in a previous test. + rtarget := reflect.ValueOf(tc.target) + rtarget.Elem().Set(reflect.Zero(reflect.TypeOf(tc.target).Elem())) + t.Run(name, func(t *testing.T) { + match := As(tc.b, tc.target) + if match != tc.match { + t.Fatalf("match: got %v; want %v", match, tc.match) + } + if !match { + return + } + if got := rtarget.Elem().Interface(); got != tc.want { + t.Fatalf("got %#v, want %#v", got, tc.want) } }) } } + +type bro struct { + name string +} + +func (p *bro) Name() string { return p.name } +func (p *bro) Init(opts ...options.Option) error { return nil } + +// Options returns broker options +func (p *bro) Options() broker.Options { return broker.Options{} } + +// Address return configured address +func (p *bro) Address() string { return "" } + +// Connect connects to broker +func (p *bro) Connect(ctx context.Context) error { return nil } + +// Disconnect disconnect from broker +func (p *bro) Disconnect(ctx context.Context) error { return nil } + +// Publish message, msg can be single broker.Message or []broker.Message +func (p *bro) Publish(ctx context.Context, msg interface{}, opts ...options.Option) error { return nil } + +// Subscribe subscribes to topic message via handler +func (p *bro) Subscribe(ctx context.Context, topic string, handler interface{}, opts ...options.Option) (broker.Subscriber, error) { + return nil, nil +} + +// String type of broker +func (p *bro) String() string { return p.name } + +type fsmT struct { + name string +} + +func (f *fsmT) Start(ctx context.Context, a interface{}, o ...Option) (interface{}, error) { + return nil, nil +} +func (f *fsmT) Current() string { return f.name } +func (f *fsmT) Reset() {} +func (f *fsmT) State(s string, sf fsm.StateFunc) {} -- 2.45.2 From 781cada47b26483aee7e33145ee699d028a7cba8 Mon Sep 17 00:00:00 2001 From: Gorbunov Kirill Andreevich Date: Tue, 27 Feb 2024 23:24:51 +0300 Subject: [PATCH 3/3] #97 add As for all interface. --- micro_test.go | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/micro_test.go b/micro_test.go index b9387c97..d2a20121 100644 --- a/micro_test.go +++ b/micro_test.go @@ -8,7 +8,6 @@ import ( "go.unistack.org/micro/v3/broker" "go.unistack.org/micro/v3/fsm" - "go.unistack.org/micro/v3/options" ) func TestAs(t *testing.T) { @@ -65,8 +64,9 @@ type bro struct { name string } -func (p *bro) Name() string { return p.name } -func (p *bro) Init(opts ...options.Option) error { return nil } +func (p *bro) Name() string { return p.name } + +func (p *bro) Init(opts ...broker.Option) error { return nil } // Options returns broker options func (p *bro) Options() broker.Options { return broker.Options{} } @@ -81,10 +81,22 @@ func (p *bro) Connect(ctx context.Context) error { return nil } func (p *bro) Disconnect(ctx context.Context) error { return nil } // Publish message, msg can be single broker.Message or []broker.Message -func (p *bro) Publish(ctx context.Context, msg interface{}, opts ...options.Option) error { return nil } +func (p *bro) Publish(ctx context.Context, topic string, msg *broker.Message, opts ...broker.PublishOption) error { + return nil +} + +// BatchPublish messages to broker with multiple topics +func (p *bro) BatchPublish(ctx context.Context, msgs []*broker.Message, opts ...broker.PublishOption) error { + return nil +} + +// BatchSubscribe subscribes to topic messages via handler +func (p *bro) BatchSubscribe(ctx context.Context, topic string, h broker.BatchHandler, opts ...broker.SubscribeOption) (broker.Subscriber, error) { + return nil, nil +} // Subscribe subscribes to topic message via handler -func (p *bro) Subscribe(ctx context.Context, topic string, handler interface{}, opts ...options.Option) (broker.Subscriber, error) { +func (p *bro) Subscribe(ctx context.Context, topic string, handler broker.Handler, opts ...broker.SubscribeOption) (broker.Subscriber, error) { return nil, nil } -- 2.45.2