前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >聊聊dubbo-go-proxy的jtypes

聊聊dubbo-go-proxy的jtypes

原创
作者头像
code4it
修改2021-02-11 18:19:07
4580
修改2021-02-11 18:19:07
举报
文章被收录于专栏:码匠的流水账码匠的流水账

本文主要研究一下dubbo-go-proxy的jtypes

JTypeMapper

dubbo-go-proxy/pkg/common/constant/jtypes.go

代码语言:javascript
复制
// Object represents the java.lang.Object type
type Object interface{}

// JTypeMapper maps the java basic types to golang types
var JTypeMapper = map[string]reflect.Type{
    "string":           reflect.TypeOf(""),
    "java.lang.String": reflect.TypeOf(""),
    "char":             reflect.TypeOf(""),
    "short":            reflect.TypeOf(int32(0)),
    "int":              reflect.TypeOf(int32(0)),
    "long":             reflect.TypeOf(int64(0)),
    "float":            reflect.TypeOf(float64(0)),
    "double":           reflect.TypeOf(float64(0)),
    "boolean":          reflect.TypeOf(true),
    "java.util.Date":   reflect.TypeOf(time.Time{}),
    "date":             reflect.TypeOf(time.Time{}),
    "object":           reflect.TypeOf([]Object{}).Elem(),
    "java.lang.Object": reflect.TypeOf([]Object{}).Elem(),
}

JTypeMapper定义了个map,key为java类型,value为golang的reflect.Type

mapTypes

dubbo-go-proxy/pkg/client/dubbo/mapper.go

代码语言:javascript
复制
func mapTypes(jType string, originVal interface{}) (interface{}, error) {
    targetType, ok := constant.JTypeMapper[jType]
    if !ok {
        return nil, errors.Errorf("Invalid parameter type: %s", jType)
    }
    switch targetType {
    case reflect.TypeOf(""):
        return cast.ToStringE(originVal)
    case reflect.TypeOf(int32(0)):
        return cast.ToInt32E(originVal)
    case reflect.TypeOf(int64(0)):
        return cast.ToInt64E(originVal)
    case reflect.TypeOf(float64(0)):
        return cast.ToFloat64E(originVal)
    case reflect.TypeOf(true):
        return cast.ToBoolE(originVal)
    case reflect.TypeOf(time.Time{}):
        return cast.ToBoolE(originVal)
    default:
        return originVal, nil
    }
}

mapTypes方法先根据jType从JTypeMapper取出对应的golang的reflect.Type,然后挨个根据这个类型使用cast类库进行转换,转成golang的interface{}

caste.go

github.com/spf13/cast@v1.3.1/caste.go

代码语言:javascript
复制
// ToStringE casts an interface to a string type.
func ToStringE(i interface{}) (string, error) {
    i = indirectToStringerOrError(i)

    switch s := i.(type) {
    case string:
        return s, nil
    case bool:
        return strconv.FormatBool(s), nil
    case float64:
        return strconv.FormatFloat(s, 'f', -1, 64), nil
    case float32:
        return strconv.FormatFloat(float64(s), 'f', -1, 32), nil
    case int:
        return strconv.Itoa(s), nil
    case int64:
        return strconv.FormatInt(s, 10), nil
    case int32:
        return strconv.Itoa(int(s)), nil
    case int16:
        return strconv.FormatInt(int64(s), 10), nil
    case int8:
        return strconv.FormatInt(int64(s), 10), nil
    case uint:
        return strconv.FormatUint(uint64(s), 10), nil
    case uint64:
        return strconv.FormatUint(uint64(s), 10), nil
    case uint32:
        return strconv.FormatUint(uint64(s), 10), nil
    case uint16:
        return strconv.FormatUint(uint64(s), 10), nil
    case uint8:
        return strconv.FormatUint(uint64(s), 10), nil
    case []byte:
        return string(s), nil
    case template.HTML:
        return string(s), nil
    case template.URL:
        return string(s), nil
    case template.JS:
        return string(s), nil
    case template.CSS:
        return string(s), nil
    case template.HTMLAttr:
        return string(s), nil
    case nil:
        return "", nil
    case fmt.Stringer:
        return s.String(), nil
    case error:
        return s.Error(), nil
    default:
        return "", fmt.Errorf("unable to cast %#v of type %T to string", i, i)
    }
}

github.com/spf13/cast提供了一系列的cast方法,用于将interface{}转换为指定的类型

小结

JTypeMapper定义了个map,key为java类型,value为golang的reflect.Type;mapTypes方法先根据jType从JTypeMapper取出对应的golang的reflect.Type,然后挨个根据这个类型使用cast类库进行转换,转成golang的interface{}。

doc

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • JTypeMapper
  • mapTypes
  • caste.go
  • 小结
  • doc
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com