register: drop verbose values export

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-04-20 12:39:21 +03:00
parent 852f19598d
commit 41837a67f8
4 changed files with 20 additions and 92 deletions

View File

@ -3,7 +3,6 @@ package register
import ( import (
"fmt" "fmt"
"reflect" "reflect"
"strings"
"unicode" "unicode"
"unicode/utf8" "unicode/utf8"
@ -11,12 +10,12 @@ import (
) )
// ExtractValue from reflect.Type from specified depth // 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 { if d == 3 {
return nil return ""
} }
if v == nil { if v == nil {
return nil return ""
} }
if v.Kind() == reflect.Ptr { if v.Kind() == reflect.Ptr {
@ -25,7 +24,7 @@ func ExtractValue(v reflect.Type, d int) *Value {
// slices and maps don't have a defined name // slices and maps don't have a defined name
if (v.Kind() == reflect.Slice || v.Kind() == reflect.Map) || len(v.Name()) == 0 { if (v.Kind() == reflect.Slice || v.Kind() == reflect.Map) || len(v.Name()) == 0 {
return nil return ""
} }
// get the rune character // get the rune character
@ -33,58 +32,10 @@ func ExtractValue(v reflect.Type, d int) *Value {
// crude check for is unexported field // crude check for is unexported field
if unicode.IsLower(a) { if unicode.IsLower(a) {
return nil return ""
} }
arg := &Value{ return v.Name()
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
} }
// ExtractEndpoint extract *Endpoint from reflect.Method // ExtractEndpoint extract *Endpoint from reflect.Method
@ -116,7 +67,7 @@ func ExtractEndpoint(method reflect.Method) *Endpoint {
request := ExtractValue(reqType, 0) request := ExtractValue(reqType, 0)
response := ExtractValue(rspType, 0) response := ExtractValue(rspType, 0)
if request == nil || response == nil { if request == "" || response == "" {
return nil return nil
} }
@ -135,7 +86,7 @@ func ExtractEndpoint(method reflect.Method) *Endpoint {
} }
// ExtractSubValue exctact *Value from reflect.Type // ExtractSubValue exctact *Value from reflect.Type
func ExtractSubValue(typ reflect.Type) *Value { func ExtractSubValue(typ reflect.Type) string {
var reqType reflect.Type var reqType reflect.Type
switch typ.NumIn() { switch typ.NumIn() {
case 1: case 1:
@ -145,7 +96,7 @@ func ExtractSubValue(typ reflect.Type) *Value {
case 3: case 3:
reqType = typ.In(2) reqType = typ.In(2)
default: default:
return nil return ""
} }
return ExtractValue(reqType, 0) return ExtractValue(reqType, 0)
} }

View File

@ -36,28 +36,21 @@ func TestExtractEndpoint(t *testing.T) {
t.Fatalf("Expected handler Test, got %s", endpoints[0].Name) 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") t.Fatal("Expected non nil Request")
} }
if endpoints[0].Response == nil { if endpoints[0].Response == "" {
t.Fatal("Expected non nil Request") t.Fatal("Expected non nil Request")
} }
if endpoints[0].Request.Name != "TestRequest" { if endpoints[0].Request != "TestRequest" {
t.Fatalf("Expected TestRequest got %s", endpoints[0].Request.Name) t.Fatalf("Expected TestRequest got %s", endpoints[0].Request)
} }
if endpoints[0].Response.Name != "TestResponse" { if endpoints[0].Response != "TestResponse" {
t.Fatalf("Expected TestResponse got %s", endpoints[0].Response.Name) t.Fatalf("Expected TestResponse got %s", endpoints[0].Response)
}
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)
} }
t.Logf("XXX %#+v\n", endpoints[0])
} }

View File

@ -490,15 +490,6 @@ func recordToService(r *record, domain string) *Service {
endpoints := make([]*Endpoint, len(r.Endpoints)) endpoints := make([]*Endpoint, len(r.Endpoints))
for i, e := range 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)) metadata := make(map[string]string, len(e.Metadata))
for k, v := range e.Metadata { for k, v := range e.Metadata {
metadata[k] = v metadata[k] = v
@ -506,8 +497,8 @@ func recordToService(r *record, domain string) *Service {
endpoints[i] = &Endpoint{ endpoints[i] = &Endpoint{
Name: e.Name, Name: e.Name,
Request: request, Request: e.Request,
Response: response, Response: e.Response,
Metadata: metadata, Metadata: metadata,
} }
} }

View File

@ -59,19 +59,12 @@ type Node struct {
// Endpoint holds endpoint register info // Endpoint holds endpoint register info
type Endpoint struct { type Endpoint struct {
Request *Value `json:"request"` Request string `json:"request"`
Response *Value `json:"response"` Response string `json:"response"`
Metadata metadata.Metadata `json:"metadata"` Metadata metadata.Metadata `json:"metadata"`
Name string `json:"name"` 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 // Option func signature
type Option func(*Options) type Option func(*Options)