3
util/reflect/generate.go
Normal file
3
util/reflect/generate.go
Normal file
@@ -0,0 +1,3 @@
|
||||
package reflect
|
||||
|
||||
//go:generate protoc -I./proto --go_out=paths=source_relative:./proto proto/test.proto
|
263
util/reflect/proto/test.pb.go
Normal file
263
util/reflect/proto/test.pb.go
Normal file
@@ -0,0 +1,263 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.26.0
|
||||
// protoc v3.15.6
|
||||
// source: test.proto
|
||||
|
||||
package pb
|
||||
|
||||
import (
|
||||
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
|
||||
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
|
||||
wrapperspb "google.golang.org/protobuf/types/known/wrapperspb"
|
||||
reflect "reflect"
|
||||
sync "sync"
|
||||
)
|
||||
|
||||
const (
|
||||
// Verify that this generated code is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
|
||||
// Verify that runtime/protoimpl is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
|
||||
)
|
||||
|
||||
type CallReq struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
|
||||
Req string `protobuf:"bytes,2,opt,name=req,proto3" json:"req,omitempty"`
|
||||
Arg1 string `protobuf:"bytes,3,opt,name=arg1,proto3" json:"arg1,omitempty"`
|
||||
Arg2 uint64 `protobuf:"varint,4,opt,name=arg2,proto3" json:"arg2,omitempty"`
|
||||
Nested *Nested `protobuf:"bytes,5,opt,name=nested,proto3" json:"nested,omitempty"`
|
||||
}
|
||||
|
||||
func (x *CallReq) Reset() {
|
||||
*x = CallReq{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_test_proto_msgTypes[0]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *CallReq) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*CallReq) ProtoMessage() {}
|
||||
|
||||
func (x *CallReq) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_test_proto_msgTypes[0]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use CallReq.ProtoReflect.Descriptor instead.
|
||||
func (*CallReq) Descriptor() ([]byte, []int) {
|
||||
return file_test_proto_rawDescGZIP(), []int{0}
|
||||
}
|
||||
|
||||
func (x *CallReq) GetName() string {
|
||||
if x != nil {
|
||||
return x.Name
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *CallReq) GetReq() string {
|
||||
if x != nil {
|
||||
return x.Req
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *CallReq) GetArg1() string {
|
||||
if x != nil {
|
||||
return x.Arg1
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *CallReq) GetArg2() uint64 {
|
||||
if x != nil {
|
||||
return x.Arg2
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (x *CallReq) GetNested() *Nested {
|
||||
if x != nil {
|
||||
return x.Nested
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type Nested struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
StringArgs []string `protobuf:"bytes,1,rep,name=string_args,json=stringArgs,proto3" json:"string_args,omitempty"`
|
||||
Uint64Args []*wrapperspb.UInt64Value `protobuf:"bytes,2,rep,name=uint64_args,json=uint64Args,proto3" json:"uint64_args,omitempty"`
|
||||
}
|
||||
|
||||
func (x *Nested) Reset() {
|
||||
*x = Nested{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_test_proto_msgTypes[1]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *Nested) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*Nested) ProtoMessage() {}
|
||||
|
||||
func (x *Nested) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_test_proto_msgTypes[1]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use Nested.ProtoReflect.Descriptor instead.
|
||||
func (*Nested) Descriptor() ([]byte, []int) {
|
||||
return file_test_proto_rawDescGZIP(), []int{1}
|
||||
}
|
||||
|
||||
func (x *Nested) GetStringArgs() []string {
|
||||
if x != nil {
|
||||
return x.StringArgs
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *Nested) GetUint64Args() []*wrapperspb.UInt64Value {
|
||||
if x != nil {
|
||||
return x.Uint64Args
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var File_test_proto protoreflect.FileDescriptor
|
||||
|
||||
var file_test_proto_rawDesc = []byte{
|
||||
0x0a, 0x0a, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x07, 0x72, 0x65,
|
||||
0x66, 0x6c, 0x65, 0x63, 0x74, 0x1a, 0x1e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72,
|
||||
0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x77, 0x72, 0x61, 0x70, 0x70, 0x65, 0x72, 0x73, 0x2e,
|
||||
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x80, 0x01, 0x0a, 0x07, 0x43, 0x61, 0x6c, 0x6c, 0x52, 0x65,
|
||||
0x71, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52,
|
||||
0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x72, 0x65, 0x71, 0x18, 0x02, 0x20, 0x01,
|
||||
0x28, 0x09, 0x52, 0x03, 0x72, 0x65, 0x71, 0x12, 0x12, 0x0a, 0x04, 0x61, 0x72, 0x67, 0x31, 0x18,
|
||||
0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x61, 0x72, 0x67, 0x31, 0x12, 0x12, 0x0a, 0x04, 0x61,
|
||||
0x72, 0x67, 0x32, 0x18, 0x04, 0x20, 0x01, 0x28, 0x04, 0x52, 0x04, 0x61, 0x72, 0x67, 0x32, 0x12,
|
||||
0x27, 0x0a, 0x06, 0x6e, 0x65, 0x73, 0x74, 0x65, 0x64, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0b, 0x32,
|
||||
0x0f, 0x2e, 0x72, 0x65, 0x66, 0x6c, 0x65, 0x63, 0x74, 0x2e, 0x4e, 0x65, 0x73, 0x74, 0x65, 0x64,
|
||||
0x52, 0x06, 0x6e, 0x65, 0x73, 0x74, 0x65, 0x64, 0x22, 0x68, 0x0a, 0x06, 0x4e, 0x65, 0x73, 0x74,
|
||||
0x65, 0x64, 0x12, 0x1f, 0x0a, 0x0b, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x5f, 0x61, 0x72, 0x67,
|
||||
0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x09, 0x52, 0x0a, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x41,
|
||||
0x72, 0x67, 0x73, 0x12, 0x3d, 0x0a, 0x0b, 0x75, 0x69, 0x6e, 0x74, 0x36, 0x34, 0x5f, 0x61, 0x72,
|
||||
0x67, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x1c, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c,
|
||||
0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x55, 0x49, 0x6e, 0x74, 0x36,
|
||||
0x34, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x52, 0x0a, 0x75, 0x69, 0x6e, 0x74, 0x36, 0x34, 0x41, 0x72,
|
||||
0x67, 0x73, 0x42, 0x3b, 0x5a, 0x39, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d,
|
||||
0x2f, 0x75, 0x6e, 0x69, 0x73, 0x74, 0x61, 0x63, 0x6b, 0x2d, 0x6f, 0x72, 0x67, 0x2f, 0x6d, 0x69,
|
||||
0x63, 0x72, 0x6f, 0x2d, 0x74, 0x65, 0x73, 0x74, 0x73, 0x2f, 0x75, 0x74, 0x69, 0x6c, 0x2f, 0x72,
|
||||
0x65, 0x66, 0x6c, 0x65, 0x63, 0x74, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x3b, 0x70, 0x62, 0x62,
|
||||
0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
}
|
||||
|
||||
var (
|
||||
file_test_proto_rawDescOnce sync.Once
|
||||
file_test_proto_rawDescData = file_test_proto_rawDesc
|
||||
)
|
||||
|
||||
func file_test_proto_rawDescGZIP() []byte {
|
||||
file_test_proto_rawDescOnce.Do(func() {
|
||||
file_test_proto_rawDescData = protoimpl.X.CompressGZIP(file_test_proto_rawDescData)
|
||||
})
|
||||
return file_test_proto_rawDescData
|
||||
}
|
||||
|
||||
var file_test_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
|
||||
var file_test_proto_goTypes = []interface{}{
|
||||
(*CallReq)(nil), // 0: reflect.CallReq
|
||||
(*Nested)(nil), // 1: reflect.Nested
|
||||
(*wrapperspb.UInt64Value)(nil), // 2: google.protobuf.UInt64Value
|
||||
}
|
||||
var file_test_proto_depIdxs = []int32{
|
||||
1, // 0: reflect.CallReq.nested:type_name -> reflect.Nested
|
||||
2, // 1: reflect.Nested.uint64_args:type_name -> google.protobuf.UInt64Value
|
||||
2, // [2:2] is the sub-list for method output_type
|
||||
2, // [2:2] is the sub-list for method input_type
|
||||
2, // [2:2] is the sub-list for extension type_name
|
||||
2, // [2:2] is the sub-list for extension extendee
|
||||
0, // [0:2] is the sub-list for field type_name
|
||||
}
|
||||
|
||||
func init() { file_test_proto_init() }
|
||||
func file_test_proto_init() {
|
||||
if File_test_proto != nil {
|
||||
return
|
||||
}
|
||||
if !protoimpl.UnsafeEnabled {
|
||||
file_test_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*CallReq); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_test_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*Nested); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
type x struct{}
|
||||
out := protoimpl.TypeBuilder{
|
||||
File: protoimpl.DescBuilder{
|
||||
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
||||
RawDescriptor: file_test_proto_rawDesc,
|
||||
NumEnums: 0,
|
||||
NumMessages: 2,
|
||||
NumExtensions: 0,
|
||||
NumServices: 0,
|
||||
},
|
||||
GoTypes: file_test_proto_goTypes,
|
||||
DependencyIndexes: file_test_proto_depIdxs,
|
||||
MessageInfos: file_test_proto_msgTypes,
|
||||
}.Build()
|
||||
File_test_proto = out.File
|
||||
file_test_proto_rawDesc = nil
|
||||
file_test_proto_goTypes = nil
|
||||
file_test_proto_depIdxs = nil
|
||||
}
|
19
util/reflect/proto/test.proto
Normal file
19
util/reflect/proto/test.proto
Normal file
@@ -0,0 +1,19 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package reflect;
|
||||
|
||||
option go_package = "github.com/unistack-org/micro-tests/util/reflect/proto;pb";
|
||||
import "google/protobuf/wrappers.proto";
|
||||
|
||||
message CallReq {
|
||||
string name = 1;
|
||||
string req = 2;
|
||||
string arg1 = 3;
|
||||
uint64 arg2 = 4;
|
||||
Nested nested = 5;
|
||||
};
|
||||
|
||||
message Nested {
|
||||
repeated string string_args = 1;
|
||||
repeated google.protobuf.UInt64Value uint64_args = 2;
|
||||
};
|
258
util/reflect/reflect.go
Normal file
258
util/reflect/reflect.go
Normal file
@@ -0,0 +1,258 @@
|
||||
package reflect
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInvalidStruct = errors.New("invalid struct specified")
|
||||
ErrInvalidValue = errors.New("invalid value specified")
|
||||
)
|
||||
|
||||
func MergeMap(dst interface{}, mp map[string]interface{}, tags []string) error {
|
||||
var err error
|
||||
var sval reflect.Value
|
||||
var fname string
|
||||
|
||||
dviface := reflect.ValueOf(dst)
|
||||
if dviface.Kind() == reflect.Ptr {
|
||||
dviface = dviface.Elem()
|
||||
}
|
||||
|
||||
if dviface.Kind() != reflect.Struct {
|
||||
return ErrInvalidStruct
|
||||
}
|
||||
|
||||
dtype := dviface.Type()
|
||||
for idx := 0; idx < dtype.NumField(); idx++ {
|
||||
dfld := dtype.Field(idx)
|
||||
dval := dviface.Field(idx)
|
||||
if !dval.CanSet() || len(dfld.PkgPath) != 0 || !dval.IsValid() {
|
||||
continue
|
||||
}
|
||||
|
||||
fname = ""
|
||||
for _, tname := range tags {
|
||||
tvalue, ok := dfld.Tag.Lookup(tname)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
tpart := strings.Split(tvalue, ",")
|
||||
switch tname {
|
||||
case "protobuf":
|
||||
fname = tpart[3][5:]
|
||||
default:
|
||||
fname = tpart[0]
|
||||
}
|
||||
|
||||
if fname != "" {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if fname == "" {
|
||||
fname = strings.ToLower(dfld.Name)
|
||||
}
|
||||
|
||||
val, ok := mp[fname]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
sval = reflect.ValueOf(val)
|
||||
|
||||
switch dval.Kind() {
|
||||
case reflect.Bool:
|
||||
err = mergeBool(dval, sval)
|
||||
case reflect.String:
|
||||
err = mergeString(dval, sval)
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
err = mergeInt(dval, sval)
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
err = mergeUint(dval, sval)
|
||||
case reflect.Float32, reflect.Float64:
|
||||
err = mergeFloat(dval, sval)
|
||||
case reflect.Struct:
|
||||
mp, ok := sval.Interface().(map[string]interface{})
|
||||
if !ok {
|
||||
return ErrInvalidValue
|
||||
}
|
||||
err = MergeMap(dval.Interface(), mp, tags)
|
||||
/*
|
||||
case reflect.Interface:
|
||||
err = d.decodeBasic(name, input, outVal)
|
||||
case reflect.Map:
|
||||
err = mergeMap(dval, sval)
|
||||
case reflect.Ptr:
|
||||
err = mergePtr(dval, sval)
|
||||
case reflect.Slice:
|
||||
err = mergeSlice(dval, sval)
|
||||
case reflect.Array:
|
||||
err = mergeArray(dval, sval)
|
||||
*/
|
||||
default:
|
||||
err = ErrInvalidValue
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
err = fmt.Errorf("err: %v key %v invalid val %v", err, fname, sval.Interface())
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func mergeBool(dval reflect.Value, sval reflect.Value) error {
|
||||
switch sval.Kind() {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
switch sval.Int() {
|
||||
case 1:
|
||||
dval.SetBool(true)
|
||||
case 0:
|
||||
dval.SetBool(false)
|
||||
default:
|
||||
return ErrInvalidValue
|
||||
}
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
switch sval.Uint() {
|
||||
case 1:
|
||||
dval.SetBool(true)
|
||||
case 0:
|
||||
dval.SetBool(false)
|
||||
default:
|
||||
return ErrInvalidValue
|
||||
}
|
||||
case reflect.Float32, reflect.Float64:
|
||||
switch sval.Float() {
|
||||
case 1:
|
||||
dval.SetBool(true)
|
||||
case 0:
|
||||
dval.SetBool(false)
|
||||
default:
|
||||
return ErrInvalidValue
|
||||
}
|
||||
case reflect.Bool:
|
||||
dval.SetBool(sval.Bool())
|
||||
case reflect.String:
|
||||
switch sval.String() {
|
||||
case "t", "T", "true", "TRUE", "True", "1", "yes":
|
||||
dval.SetBool(true)
|
||||
case "f", "F", "false", "FALSE", "False", "0", "no":
|
||||
dval.SetBool(false)
|
||||
default:
|
||||
return ErrInvalidValue
|
||||
}
|
||||
default:
|
||||
return ErrInvalidValue
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func mergeString(dval reflect.Value, sval reflect.Value) error {
|
||||
switch sval.Kind() {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
dval.SetString(strconv.FormatInt(sval.Int(), sval.Type().Bits()))
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
dval.SetString(strconv.FormatUint(sval.Uint(), sval.Type().Bits()))
|
||||
case reflect.Float32, reflect.Float64:
|
||||
dval.SetString(strconv.FormatFloat(sval.Float(), 'f', -1, sval.Type().Bits()))
|
||||
case reflect.Bool:
|
||||
switch sval.Bool() {
|
||||
case true:
|
||||
dval.SetString(strconv.FormatBool(true))
|
||||
case false:
|
||||
dval.SetString(strconv.FormatBool(false))
|
||||
}
|
||||
case reflect.String:
|
||||
dval.SetString(sval.String())
|
||||
default:
|
||||
return ErrInvalidValue
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func mergeInt(dval reflect.Value, sval reflect.Value) error {
|
||||
switch sval.Kind() {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
dval.SetInt(sval.Int())
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
dval.SetInt(int64(sval.Uint()))
|
||||
case reflect.Float32, reflect.Float64:
|
||||
dval.SetInt(int64(sval.Float()))
|
||||
case reflect.Bool:
|
||||
switch sval.Bool() {
|
||||
case true:
|
||||
dval.SetInt(1)
|
||||
case false:
|
||||
dval.SetInt(0)
|
||||
}
|
||||
case reflect.String:
|
||||
l, err := strconv.ParseInt(sval.String(), 0, dval.Type().Bits())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
dval.SetInt(l)
|
||||
default:
|
||||
return ErrInvalidValue
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func mergeUint(dval reflect.Value, sval reflect.Value) error {
|
||||
switch sval.Kind() {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
dval.SetUint(uint64(sval.Int()))
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
dval.SetUint(sval.Uint())
|
||||
case reflect.Float32, reflect.Float64:
|
||||
dval.SetUint(uint64(sval.Float()))
|
||||
case reflect.Bool:
|
||||
switch sval.Bool() {
|
||||
case true:
|
||||
dval.SetUint(1)
|
||||
case false:
|
||||
dval.SetUint(0)
|
||||
}
|
||||
case reflect.String:
|
||||
l, err := strconv.ParseUint(sval.String(), 0, dval.Type().Bits())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
dval.SetUint(l)
|
||||
default:
|
||||
return ErrInvalidValue
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func mergeFloat(dval reflect.Value, sval reflect.Value) error {
|
||||
switch sval.Kind() {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
dval.SetFloat(float64(sval.Int()))
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
dval.SetFloat(float64(sval.Uint()))
|
||||
case reflect.Float32, reflect.Float64:
|
||||
dval.SetFloat(sval.Float())
|
||||
case reflect.Bool:
|
||||
switch sval.Bool() {
|
||||
case true:
|
||||
dval.SetFloat(1)
|
||||
case false:
|
||||
dval.SetFloat(0)
|
||||
}
|
||||
case reflect.String:
|
||||
l, err := strconv.ParseFloat(sval.String(), dval.Type().Bits())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
dval.SetFloat(l)
|
||||
default:
|
||||
return ErrInvalidValue
|
||||
}
|
||||
return nil
|
||||
}
|
106
util/reflect/reflect_test.go
Normal file
106
util/reflect/reflect_test.go
Normal file
@@ -0,0 +1,106 @@
|
||||
package reflect
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
pb "github.com/unistack-org/micro-tests/util/reflect/proto"
|
||||
rutil "github.com/unistack-org/micro/v3/util/reflect"
|
||||
)
|
||||
|
||||
func TestMergeBool(t *testing.T) {
|
||||
type str struct {
|
||||
Bool bool `json:"bool"`
|
||||
}
|
||||
|
||||
mp := make(map[string]interface{})
|
||||
mp["bool"] = "true"
|
||||
s := &str{}
|
||||
|
||||
if err := MergeMap(s, mp, []string{"json"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !s.Bool {
|
||||
t.Fatalf("merge bool error: %#+v\n", s)
|
||||
}
|
||||
|
||||
mp["bool"] = "false"
|
||||
|
||||
if err := MergeMap(s, mp, []string{"json"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if s.Bool {
|
||||
t.Fatalf("merge bool error: %#+v\n", s)
|
||||
}
|
||||
|
||||
mp["bool"] = 1
|
||||
|
||||
if err := MergeMap(s, mp, []string{"json"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !s.Bool {
|
||||
t.Fatalf("merge bool error: %#+v\n", s)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestMergeString(t *testing.T) {
|
||||
type str struct {
|
||||
Bool string `json:"bool"`
|
||||
}
|
||||
|
||||
mp := make(map[string]interface{})
|
||||
mp["bool"] = true
|
||||
s := &str{}
|
||||
|
||||
if err := MergeMap(s, mp, []string{"json"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if s.Bool != "true" {
|
||||
t.Fatalf("merge bool error: %#+v\n", s)
|
||||
}
|
||||
|
||||
mp["bool"] = false
|
||||
|
||||
if err := MergeMap(s, mp, []string{"json"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if s.Bool != "false" {
|
||||
t.Fatalf("merge bool error: %#+v\n", s)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestMergeMap(t *testing.T) {
|
||||
dst := &pb.CallReq{
|
||||
Name: "name_old",
|
||||
Req: "req_old",
|
||||
}
|
||||
|
||||
mp := make(map[string]interface{})
|
||||
mp["name"] = "name_new"
|
||||
mp["req"] = "req_new"
|
||||
mp["arg2"] = 1
|
||||
mp["nested.string_args"] = []string{"args1", "args2"}
|
||||
mp["nested.uint64_args"] = []uint64{1, 2, 3}
|
||||
|
||||
mp = rutil.FlattenMap(mp)
|
||||
|
||||
if err := MergeMap(dst, mp, []string{"protobuf"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if dst.Name != "name_new" || dst.Req != "req_new" || dst.Arg2 != 1 {
|
||||
t.Fatalf("merge error: %v", dst)
|
||||
}
|
||||
|
||||
if dst.Nested == nil ||
|
||||
len(dst.Nested.StringArgs) != 2 || dst.Nested.StringArgs[0] != "args1" ||
|
||||
len(dst.Nested.Uint64Args) != 3 || dst.Nested.Uint64Args[2].Value != 3 {
|
||||
t.Fatalf("merge error: %v", dst.Nested)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user