kitex-contrib / codec-dubbo

支持 kitex <-> dubbo 互通的 dubbo 协议编解码器。
Apache License 2.0
16 stars 14 forks source link

[P2] The support of Extension type Enumeration #88

Open s5364733 opened 6 months ago

s5364733 commented 6 months ago

It seems that enumeration is not supported now, but Hessian-dubbo already has support for int enumeration.

// POJO interface
// !!! Pls attention that Every field name should be upper case.
// Otherwise the app may panic.
type POJO interface {
    JavaClassName() string // got a go struct's Java Class package name which should be a POJO class.
}

// POJOEnum enum for POJO
type POJOEnum interface {
    POJO
    String() string
    EnumValue(string) JavaEnum
}

// JavaEnum type
type JavaEnum int32

// JavaEnumClass struct
type JavaEnumClass struct {
    name string
}

The writing method when using the hessian2 protocol conversion type normally is as follows:

type CompanyEnum hessian.JavaEnum

const (
    Kigger     CompanyEnum = 5
    Zharhg CompanyEnum = 1
)

var companyName = map[CompanyEnum]string{
    Kigger:     "Kigger",
    Zharhg : "Zharhg ",
}

var companyValue = map[string]CompanyEnum{
    "Kigger":     Kigger,
    "Zharhg ": Zharhg ,
}

func (g CompanyEnum) JavaClassName() string {
    return "xxxxxx"
}

func (g CompanyEnum) String() string {
    s, ok := companyName[g]
    if ok {
        return s
    }

    return strconv.Itoa(int(g))
}

func (g CompanyEnum) EnumValue(s string) hessian.JavaEnum {
    v, ok := companyValue[s]
    if ok {
        return hessian.JavaEnum(v)
    }

    return hessian.InvalidJavaEnum
}

Because Codec-dubbo calls hessian-dubbo for serialization at the bottom level, the purpose can be achieved as long as the type can be guaranteed to be serialized by hessian-dubbo. For specific real-time solutions, you can refer to the implementation of extended thrift.

具体实现参考: dubbo-go-hessian2 IDL-REF

Lvnszn commented 6 months ago

嗯,#64 跟这个应该是一个事情。尽量基于 thrift 的 enum 去实现。

DMwangnima commented 6 months ago

需要参照 thrift 的 enum,让它的生成内容向 dubbo-go-hessian2 已经支持的范围靠拢。并且我们最好不要修改 thriftgo,应该可以参照 https://github.com/cloudwego/kitex/pull/1076, 拿到 thriftgo 解析好的 AST 后,再生成 hessian2 独有的代码

s5364733 commented 6 months ago

梳理一下流程

1. 定义thrift 原语法如下:


enum Direction {
    NORTH,
    EAST,
    SOUTH,
    WEST
}

struct Location {
    1: Direction direction,
    2: i32 distance
}

**2. 使用kitex cmd thrfit go 插件解析到thrift ast

  1. 通过AST 拿到ENUM 相关参数
  2. 通过参数在 register_tpl.go 中加入hessian-go-dubbo支持的枚举语法模板
  3. 生成兼容代码,**

    生成兼容代码之后 因为Codec-Dubbo使用hessian2编码枚举,所以编码层应该不需要改动

Lvnszn commented 6 months ago

有几个问题还得确认

  1. thrift 原本生成的 enum 跟你想要的可能不是一个东西, enum 在 hessian 那边需要设定类型,实现接口和进行注册。这块应该需要设计一下怎么适配
  2. 然后适配 enum 的代码也需要设计一下, dubbo-codec 和 kitex 这两块怎么相互引用。