Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: unistack-org/micro
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v3.3.15
Choose a base ref
...
head repository: unistack-org/micro
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v3.3.16
Choose a head ref
  • 2 commits
  • 6 files changed
  • 1 contributor

Commits on Apr 20, 2021

  1. register: drop verbose values export

    Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
    vtolstov committed Apr 20, 2021
    Copy the full SHA
    41837a6 View commit details
  2. server: drop Internal option

    Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
    vtolstov committed Apr 20, 2021

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    3a60103 View commit details
Showing with 23 additions and 122 deletions.
  1. +9 −58 register/extractor.go
  2. +7 −14 register/extractor_test.go
  3. +2 −11 register/memory.go
  4. +2 −9 register/register.go
  5. +3 −9 server/noop.go
  6. +0 −21 server/options.go
67 changes: 9 additions & 58 deletions register/extractor.go
Original file line number Diff line number Diff line change
@@ -3,20 +3,19 @@ package register
import (
"fmt"
"reflect"
"strings"
"unicode"
"unicode/utf8"

"github.com/unistack-org/micro/v3/metadata"
)

// ExtractValue from reflect.Type from specified depth
func ExtractValue(v reflect.Type, d int) *Value {
func ExtractValue(v reflect.Type, d int) string {
if d == 3 {
return nil
return ""
}
if v == nil {
return nil
return ""
}

if v.Kind() == reflect.Ptr {
@@ -25,66 +24,18 @@ func ExtractValue(v reflect.Type, d int) *Value {

// slices and maps don't have a defined name
if (v.Kind() == reflect.Slice || v.Kind() == reflect.Map) || len(v.Name()) == 0 {
return nil
return ""
}

// get the rune character
a, _ := utf8.DecodeRuneInString(string(v.Name()[0]))

// crude check for is unexported field
if unicode.IsLower(a) {
return nil
return ""
}

arg := &Value{
Name: v.Name(),
Type: v.Name(),
}

switch v.Kind() {
case reflect.Struct:
for i := 0; i < v.NumField(); i++ {
f := v.Field(i)
val := ExtractValue(f.Type, d+1)
if val == nil {
continue
}

// if we can find a json tag use it
if tags := f.Tag.Get("json"); len(tags) > 0 {
parts := strings.Split(tags, ",")
if parts[0] == "-" || parts[0] == "omitempty" {
continue
}
val.Name = parts[0]
}

// if there's no name default it
if len(val.Name) == 0 {
val.Name = v.Field(i).Name
}

arg.Values = append(arg.Values, val)
}
case reflect.Slice:
p := v.Elem()
if p.Kind() == reflect.Ptr {
p = p.Elem()
}
arg.Type = "[]" + p.Name()
case reflect.Map:
p := v.Elem()
if p.Kind() == reflect.Ptr {
p = p.Elem()
}
key := v.Key()
if key.Kind() == reflect.Ptr {
key = key.Elem()
}
arg.Type = fmt.Sprintf("map[%s]%s", key.Name(), p.Name())
}

return arg
return v.Name()
}

// ExtractEndpoint extract *Endpoint from reflect.Method
@@ -116,7 +67,7 @@ func ExtractEndpoint(method reflect.Method) *Endpoint {

request := ExtractValue(reqType, 0)
response := ExtractValue(rspType, 0)
if request == nil || response == nil {
if request == "" || response == "" {
return nil
}

@@ -135,7 +86,7 @@ func ExtractEndpoint(method reflect.Method) *Endpoint {
}

// ExtractSubValue exctact *Value from reflect.Type
func ExtractSubValue(typ reflect.Type) *Value {
func ExtractSubValue(typ reflect.Type) string {
var reqType reflect.Type
switch typ.NumIn() {
case 1:
@@ -145,7 +96,7 @@ func ExtractSubValue(typ reflect.Type) *Value {
case 3:
reqType = typ.In(2)
default:
return nil
return ""
}
return ExtractValue(reqType, 0)
}
21 changes: 7 additions & 14 deletions register/extractor_test.go
Original file line number Diff line number Diff line change
@@ -36,28 +36,21 @@ func TestExtractEndpoint(t *testing.T) {
t.Fatalf("Expected handler Test, got %s", endpoints[0].Name)
}

if endpoints[0].Request == nil {
if endpoints[0].Request == "" {
t.Fatal("Expected non nil Request")
}

if endpoints[0].Response == nil {
if endpoints[0].Response == "" {
t.Fatal("Expected non nil Request")
}

if endpoints[0].Request.Name != "TestRequest" {
t.Fatalf("Expected TestRequest got %s", endpoints[0].Request.Name)
if endpoints[0].Request != "TestRequest" {
t.Fatalf("Expected TestRequest got %s", endpoints[0].Request)
}

if endpoints[0].Response.Name != "TestResponse" {
t.Fatalf("Expected TestResponse got %s", endpoints[0].Response.Name)
}

if endpoints[0].Request.Type != "TestRequest" {
t.Fatalf("Expected TestRequest type got %s", endpoints[0].Request.Type)
}

if endpoints[0].Response.Type != "TestResponse" {
t.Fatalf("Expected TestResponse type got %s", endpoints[0].Response.Type)
if endpoints[0].Response != "TestResponse" {
t.Fatalf("Expected TestResponse got %s", endpoints[0].Response)
}

t.Logf("XXX %#+v\n", endpoints[0])
}
13 changes: 2 additions & 11 deletions register/memory.go
Original file line number Diff line number Diff line change
@@ -490,24 +490,15 @@ func recordToService(r *record, domain string) *Service {

endpoints := make([]*Endpoint, len(r.Endpoints))
for i, e := range r.Endpoints {
request := new(Value)
if e.Request != nil {
*request = *e.Request
}
response := new(Value)
if e.Response != nil {
*response = *e.Response
}

metadata := make(map[string]string, len(e.Metadata))
for k, v := range e.Metadata {
metadata[k] = v
}

endpoints[i] = &Endpoint{
Name: e.Name,
Request: request,
Response: response,
Request: e.Request,
Response: e.Response,
Metadata: metadata,
}
}
11 changes: 2 additions & 9 deletions register/register.go
Original file line number Diff line number Diff line change
@@ -59,19 +59,12 @@ type Node struct {

// Endpoint holds endpoint register info
type Endpoint struct {
Request *Value `json:"request"`
Response *Value `json:"response"`
Request string `json:"request"`
Response string `json:"response"`
Metadata metadata.Metadata `json:"metadata"`
Name string `json:"name"`
}

// Value holds additional kv stuff
type Value struct {
Name string `json:"name"`
Type string `json:"type"`
Values []*Value `json:"values"`
}

// Option func signature
type Option func(*Options)

12 changes: 3 additions & 9 deletions server/noop.go
Original file line number Diff line number Diff line change
@@ -160,21 +160,15 @@ func (n *noopServer) Register() error {
n.RLock()
// Maps are ordered randomly, sort the keys for consistency
var handlerList []string
for n, e := range n.handlers {
// Only advertise non internal handlers
if !e.Options().Internal {
handlerList = append(handlerList, n)
}
for n, _ := range n.handlers {
handlerList = append(handlerList, n)
}

sort.Strings(handlerList)

var subscriberList []*subscriber
for e := range n.subscribers {
// Only advertise non internal subscribers
if !e.Options().Internal {
subscriberList = append(subscriberList, e)
}
subscriberList = append(subscriberList, e)
}
sort.Slice(subscriberList, func(i, j int) bool {
return subscriberList[i].topic > subscriberList[j].topic
21 changes: 0 additions & 21 deletions server/options.go
Original file line number Diff line number Diff line change
@@ -325,8 +325,6 @@ type HandlerOptions struct {
Context context.Context
// Metadata for hondler
Metadata map[string]metadata.Metadata
// Internal flag limits exporting to other nodes via register
Internal bool
}

// NewHandlerOptions creates new HandlerOptions
@@ -354,8 +352,6 @@ type SubscriberOptions struct {
Queue string
// AutoAck flag for auto ack messages after processing
AutoAck bool
// Internal flag limit exporting info via register
Internal bool
// BodyOnly flag specifies that message without headers
BodyOnly bool
}
@@ -382,23 +378,6 @@ func EndpointMetadata(name string, md metadata.Metadata) HandlerOption {
}
}

// InternalHandler options specifies that a handler is not advertised
// to the discovery system. In the future this may also limit request
// to the internal network or authorised user.
func InternalHandler(b bool) HandlerOption {
return func(o *HandlerOptions) {
o.Internal = b
}
}

// InternalSubscriber options specifies that a subscriber is not advertised
// to the discovery system.
func InternalSubscriber(b bool) SubscriberOption {
return func(o *SubscriberOptions) {
o.Internal = b
}
}

// DisableAutoAck will disable auto acking of messages
// after they have been handled.
func DisableAutoAck() SubscriberOption {