From 3fc7db17c32c05d4ec26691e92e21ba702ca2dd1 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Wed, 18 Nov 2020 13:08:50 +0300 Subject: [PATCH] update metadata Signed-off-by: Vasiliy Tolstov --- go.mod | 2 +- go.sum | 2 ++ http.go | 8 +------- message.go | 9 ++++++--- subscriber.go | 42 +++++++++++++++++------------------------- 5 files changed, 27 insertions(+), 36 deletions(-) diff --git a/go.mod b/go.mod index 257129c..75cf91b 100644 --- a/go.mod +++ b/go.mod @@ -5,5 +5,5 @@ go 1.13 require ( github.com/unistack-org/micro-codec-jsonrpc v0.0.0-20201102222451-ff6a69988bcd github.com/unistack-org/micro-codec-protorpc v0.0.0-20201102222610-3a343898c077 - github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20201104214903-1fbf8b2e209e + github.com/unistack-org/micro/v3 v3.0.2-0.20201117210202-01e64cb0c0f3 ) diff --git a/go.sum b/go.sum index 03e5a40..d21906a 100644 --- a/go.sum +++ b/go.sum @@ -307,6 +307,8 @@ github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20200920135754-1cbd1d2bad83/go.m github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20200922103357-4c4fa00a5d94/go.mod h1:aL+8VhSXpx0SuEeXPOWUo5BgS7kyvWYobeXFay90UUM= github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20201104214903-1fbf8b2e209e h1:v27OUgoE2UOyCe6uLksdpG6oErx62nUXWIkTPxS7yIw= github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20201104214903-1fbf8b2e209e/go.mod h1:LFvCXGOgcLIj2k/8eL71TpIpcJBN2SXXAUx8U6dz9Rw= +github.com/unistack-org/micro/v3 v3.0.2-0.20201117210202-01e64cb0c0f3 h1:knz0mt3Ot/CIXp5dRyb5dAPYtKiWA9PDhazpXX5Iu3o= +github.com/unistack-org/micro/v3 v3.0.2-0.20201117210202-01e64cb0c0f3/go.mod h1:LYbzHigEudM10AbLZztVSX0Y4JWgj5nKIExil/99h6E= github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= github.com/vultr/govultr v0.1.4/go.mod h1:9H008Uxr/C4vFNGLqKx232C206GL0PBHzOP0809bGNA= github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU= diff --git a/http.go b/http.go index ea2567d..d082932 100644 --- a/http.go +++ b/http.go @@ -78,13 +78,7 @@ func (h *httpServer) Handle(handler server.Handler) error { } func (h *httpServer) NewHandler(handler interface{}, opts ...server.HandlerOption) server.Handler { - options := server.HandlerOptions{ - Metadata: make(map[string]map[string]string), - } - - for _, o := range opts { - o(&options) - } + options := server.NewHandlerOptions(opts...) var eps []*registry.Endpoint diff --git a/message.go b/message.go index bef9371..0da3184 100644 --- a/message.go +++ b/message.go @@ -1,12 +1,15 @@ package http -import "github.com/unistack-org/micro/v3/codec" +import ( + "github.com/unistack-org/micro/v3/codec" + "github.com/unistack-org/micro/v3/metadata" +) type httpMessage struct { topic string payload interface{} contentType string - header map[string]string + header metadata.Metadata body []byte codec codec.Reader } @@ -23,7 +26,7 @@ func (r *httpMessage) ContentType() string { return r.contentType } -func (r *httpMessage) Header() map[string]string { +func (r *httpMessage) Header() metadata.Metadata { return r.header } diff --git a/subscriber.go b/subscriber.go index 5aa3790..7abaf81 100644 --- a/subscriber.go +++ b/subscriber.go @@ -38,9 +38,6 @@ type httpSubscriber struct { func newSubscriber(topic string, sub interface{}, opts ...server.SubscriberOption) server.Subscriber { options := server.NewSubscriberOptions(opts...) - for _, o := range opts { - o(&options) - } var endpoints []*registry.Endpoint var handlers []*handler @@ -59,15 +56,14 @@ func newSubscriber(topic string, sub interface{}, opts ...server.SubscriberOptio } handlers = append(handlers, h) - - endpoints = append(endpoints, ®istry.Endpoint{ - Name: "Func", - Request: registry.ExtractSubValue(typ), - Metadata: map[string]string{ - "topic": topic, - "subscriber": "true", - }, - }) + ep := ®istry.Endpoint{ + Name: "Func", + Request: registry.ExtractSubValue(typ), + Metadata: metadata.New(2), + } + ep.Metadata.Set("topic", topic) + ep.Metadata.Set("subscriber", "true") + endpoints = append(endpoints, ep) } else { hdlr := reflect.ValueOf(sub) name := reflect.Indirect(hdlr).Type().Name() @@ -87,15 +83,14 @@ func newSubscriber(topic string, sub interface{}, opts ...server.SubscriberOptio } handlers = append(handlers, h) - - endpoints = append(endpoints, ®istry.Endpoint{ - Name: name + "." + method.Name, - Request: registry.ExtractSubValue(method.Type), - Metadata: map[string]string{ - "topic": topic, - "subscriber": "true", - }, - }) + ep := ®istry.Endpoint{ + Name: name + "." + method.Name, + Request: registry.ExtractSubValue(method.Type), + Metadata: metadata.New(2), + } + ep.Metadata.Set("topic", topic) + ep.Metadata.Set("subscriber", "true") + endpoints = append(endpoints, ep) } } @@ -119,10 +114,7 @@ func (s *httpServer) createSubHandler(sb *httpSubscriber, opts server.Options) b return err } - hdr := make(map[string]string) - for k, v := range msg.Header { - hdr[k] = v - } + hdr := metadata.Copy(msg.Header) delete(hdr, "Content-Type") ctx := metadata.NewContext(context.Background(), hdr)