2015-06-03 02:25:37 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2015-06-12 20:52:27 +02:00
|
|
|
"fmt"
|
2015-06-03 02:25:37 +02:00
|
|
|
"reflect"
|
2016-07-30 20:01:42 +02:00
|
|
|
"strings"
|
2015-06-03 02:25:37 +02:00
|
|
|
|
2024-06-04 22:40:43 +02:00
|
|
|
"go-micro.dev/v5/registry"
|
2015-06-03 02:25:37 +02:00
|
|
|
)
|
|
|
|
|
2015-12-25 03:06:51 +02:00
|
|
|
func extractValue(v reflect.Type, d int) *registry.Value {
|
|
|
|
if d == 3 {
|
|
|
|
return nil
|
|
|
|
}
|
2015-06-03 02:25:37 +02:00
|
|
|
if v == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.Kind() == reflect.Ptr {
|
|
|
|
v = v.Elem()
|
|
|
|
}
|
|
|
|
|
|
|
|
arg := ®istry.Value{
|
|
|
|
Name: v.Name(),
|
|
|
|
Type: v.Name(),
|
|
|
|
}
|
|
|
|
|
2015-12-07 18:12:46 +02:00
|
|
|
switch v.Kind() {
|
|
|
|
case reflect.Struct:
|
2015-06-03 02:25:37 +02:00
|
|
|
for i := 0; i < v.NumField(); i++ {
|
2016-07-30 20:01:42 +02:00
|
|
|
f := v.Field(i)
|
2021-12-07 11:30:48 +02:00
|
|
|
if f.PkgPath != "" {
|
2021-11-18 11:07:00 +02:00
|
|
|
continue
|
|
|
|
}
|
2016-07-30 20:01:42 +02:00
|
|
|
val := extractValue(f.Type, d+1)
|
2015-12-25 03:06:51 +02:00
|
|
|
if val == nil {
|
|
|
|
continue
|
|
|
|
}
|
2016-07-30 20:01:42 +02:00
|
|
|
|
|
|
|
// if we can find a json tag use it
|
|
|
|
if tags := f.Tag.Get("json"); len(tags) > 0 {
|
|
|
|
parts := strings.Split(tags, ",")
|
2019-03-19 02:21:25 +02:00
|
|
|
if parts[0] == "-" || parts[0] == "omitempty" {
|
|
|
|
continue
|
|
|
|
}
|
2016-07-30 20:01:42 +02:00
|
|
|
val.Name = parts[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
// if there's no name default it
|
|
|
|
if len(val.Name) == 0 {
|
|
|
|
val.Name = v.Field(i).Name
|
|
|
|
}
|
|
|
|
|
2019-03-19 02:21:25 +02:00
|
|
|
// still no name then continue
|
|
|
|
if len(val.Name) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-06-03 02:25:37 +02:00
|
|
|
arg.Values = append(arg.Values, val)
|
|
|
|
}
|
2015-12-07 18:12:46 +02:00
|
|
|
case reflect.Slice:
|
|
|
|
p := v.Elem()
|
|
|
|
if p.Kind() == reflect.Ptr {
|
|
|
|
p = p.Elem()
|
|
|
|
}
|
|
|
|
arg.Type = "[]" + p.Name()
|
2015-06-03 02:25:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return arg
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractEndpoint(method reflect.Method) *registry.Endpoint {
|
|
|
|
if method.PkgPath != "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var rspType, reqType reflect.Type
|
2015-06-12 20:52:27 +02:00
|
|
|
var stream bool
|
2015-06-03 02:25:37 +02:00
|
|
|
mt := method.Type
|
|
|
|
|
|
|
|
switch mt.NumIn() {
|
|
|
|
case 3:
|
|
|
|
reqType = mt.In(1)
|
|
|
|
rspType = mt.In(2)
|
|
|
|
case 4:
|
|
|
|
reqType = mt.In(2)
|
|
|
|
rspType = mt.In(3)
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-26 16:52:08 +02:00
|
|
|
// are we dealing with a stream?
|
|
|
|
switch rspType.Kind() {
|
|
|
|
case reflect.Func, reflect.Interface:
|
2015-06-12 20:52:27 +02:00
|
|
|
stream = true
|
|
|
|
}
|
2015-06-03 02:25:37 +02:00
|
|
|
|
2015-12-25 03:06:51 +02:00
|
|
|
request := extractValue(reqType, 0)
|
|
|
|
response := extractValue(rspType, 0)
|
2015-06-03 02:25:37 +02:00
|
|
|
|
2019-11-09 12:32:52 +02:00
|
|
|
ep := ®istry.Endpoint{
|
2015-06-03 02:25:37 +02:00
|
|
|
Name: method.Name,
|
|
|
|
Request: request,
|
|
|
|
Response: response,
|
2019-11-11 19:37:48 +02:00
|
|
|
Metadata: make(map[string]string),
|
2019-11-09 12:32:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// set endpoint metadata for stream
|
|
|
|
if stream {
|
|
|
|
ep.Metadata = map[string]string{
|
2015-06-12 20:52:27 +02:00
|
|
|
"stream": fmt.Sprintf("%v", stream),
|
2019-11-09 12:32:52 +02:00
|
|
|
}
|
2015-06-12 20:52:27 +02:00
|
|
|
}
|
2019-11-09 12:32:52 +02:00
|
|
|
|
|
|
|
return ep
|
2015-06-12 20:52:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func extractSubValue(typ reflect.Type) *registry.Value {
|
|
|
|
var reqType reflect.Type
|
|
|
|
switch typ.NumIn() {
|
|
|
|
case 1:
|
|
|
|
reqType = typ.In(0)
|
|
|
|
case 2:
|
|
|
|
reqType = typ.In(1)
|
2015-12-03 02:49:58 +02:00
|
|
|
case 3:
|
|
|
|
reqType = typ.In(2)
|
2015-06-12 20:52:27 +02:00
|
|
|
default:
|
|
|
|
return nil
|
2015-06-03 02:25:37 +02:00
|
|
|
}
|
2015-12-25 03:06:51 +02:00
|
|
|
return extractValue(reqType, 0)
|
2015-06-03 02:25:37 +02:00
|
|
|
}
|