From e00598e1f622409f9ff2ea1910d575f36586cfd0 Mon Sep 17 00:00:00 2001 From: leiliang <1354370192@qq.com> Date: Mon, 15 Apr 2024 14:47:36 +0800 Subject: [PATCH 1/9] feat(p2) Added hessian2 enumeration support for dubbo (#88) --- pkg/hessian2/enumeration.go | 26 ++++++++++++++++++++++++++ pkg/hessian2/hessian2.go | 7 ++++++- 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 pkg/hessian2/enumeration.go diff --git a/pkg/hessian2/enumeration.go b/pkg/hessian2/enumeration.go new file mode 100644 index 00000000..b56a6410 --- /dev/null +++ b/pkg/hessian2/enumeration.go @@ -0,0 +1,26 @@ +/* + * Copyright 2024 CloudWeGo Authors + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package hessian2 + +import ( + hessian "github.com/apache/dubbo-go-hessian2" +) + +type JavaEnum hessian.JavaEnum diff --git a/pkg/hessian2/hessian2.go b/pkg/hessian2/hessian2.go index c3b0d9c0..cce42460 100644 --- a/pkg/hessian2/hessian2.go +++ b/pkg/hessian2/hessian2.go @@ -43,6 +43,11 @@ type ( func Register(pojos []interface{}) { for _, i := range pojos { - hessian.RegisterPOJO(i.(hessian.POJO)) + pojo, ok := i.(hessian.POJOEnum) + if ok { + hessian.RegisterJavaEnum(pojo) + } else { + hessian.RegisterPOJO(pojo) + } } } From 31dcb6082589265558c3591b3c10508a467fd00e Mon Sep 17 00:00:00 2001 From: leiliang <1354370192@qq.com> Date: Mon, 15 Apr 2024 15:40:26 +0800 Subject: [PATCH 2/9] feat(p2) Added hessian2 enumeration review fix (#88) --- pkg/hessian2/{enumeration.go => enum/enum.go} | 2 +- pkg/hessian2/hessian2.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename pkg/hessian2/{enumeration.go => enum/enum.go} (98%) diff --git a/pkg/hessian2/enumeration.go b/pkg/hessian2/enum/enum.go similarity index 98% rename from pkg/hessian2/enumeration.go rename to pkg/hessian2/enum/enum.go index b56a6410..1ea54b09 100644 --- a/pkg/hessian2/enumeration.go +++ b/pkg/hessian2/enum/enum.go @@ -17,7 +17,7 @@ * limitations under the License. */ -package hessian2 +package enum import ( hessian "github.com/apache/dubbo-go-hessian2" diff --git a/pkg/hessian2/hessian2.go b/pkg/hessian2/hessian2.go index cce42460..8847dfb2 100644 --- a/pkg/hessian2/hessian2.go +++ b/pkg/hessian2/hessian2.go @@ -47,7 +47,7 @@ func Register(pojos []interface{}) { if ok { hessian.RegisterJavaEnum(pojo) } else { - hessian.RegisterPOJO(pojo) + hessian.RegisterPOJO(i.(hessian.POJO)) } } } From da58db75514c40da00c2f6265da1b4e3843a4059 Mon Sep 17 00:00:00 2001 From: leiliang <1354370192@qq.com> Date: Mon, 15 Apr 2024 15:56:44 +0800 Subject: [PATCH 3/9] feat(p2) added InvalidJavaEnum const (#88) --- pkg/hessian2/enum/enum.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/hessian2/enum/enum.go b/pkg/hessian2/enum/enum.go index 1ea54b09..aed8cb3a 100644 --- a/pkg/hessian2/enum/enum.go +++ b/pkg/hessian2/enum/enum.go @@ -23,4 +23,6 @@ import ( hessian "github.com/apache/dubbo-go-hessian2" ) +const InvalidJavaEnum JavaEnum = -1 + type JavaEnum hessian.JavaEnum From 0bdcfe71d877c37b3cbb600261ef4bfcdb111879 Mon Sep 17 00:00:00 2001 From: leiliang <1354370192@qq.com> Date: Tue, 16 Apr 2024 10:29:57 +0800 Subject: [PATCH 4/9] feat(p2) fixed liscensed and refer to hessian InvalidJavaEnum (#88) --- pkg/hessian2/enum/enum.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/hessian2/enum/enum.go b/pkg/hessian2/enum/enum.go index aed8cb3a..82a87377 100644 --- a/pkg/hessian2/enum/enum.go +++ b/pkg/hessian2/enum/enum.go @@ -1,5 +1,5 @@ /* - * Copyright 2024 CloudWeGo Authors + * Copyright 2023 CloudWeGo Authors * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with @@ -23,6 +23,6 @@ import ( hessian "github.com/apache/dubbo-go-hessian2" ) -const InvalidJavaEnum JavaEnum = -1 +const InvalidJavaEnum = hessian.InvalidJavaEnum type JavaEnum hessian.JavaEnum From 03f5dafefcf7a38a972de7da772e839dd7422877 Mon Sep 17 00:00:00 2001 From: leiliang <1354370192@qq.com> Date: Tue, 16 Apr 2024 10:36:27 +0800 Subject: [PATCH 5/9] feat(p2) fixed liscensed and refer to hessian InvalidJavaEnum (#88) --- pkg/hessian2/enum/enum.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/hessian2/enum/enum.go b/pkg/hessian2/enum/enum.go index 82a87377..d8cd96ff 100644 --- a/pkg/hessian2/enum/enum.go +++ b/pkg/hessian2/enum/enum.go @@ -23,6 +23,6 @@ import ( hessian "github.com/apache/dubbo-go-hessian2" ) -const InvalidJavaEnum = hessian.InvalidJavaEnum +const InvalidJavaEnum JavaEnum = JavaEnum(hessian.InvalidJavaEnum) type JavaEnum hessian.JavaEnum From bb7cbca7d131d2ef55b56838102723d2e6f80916 Mon Sep 17 00:00:00 2001 From: leiliang <1354370192@qq.com> Date: Wed, 17 Apr 2024 10:32:56 +0800 Subject: [PATCH 6/9] feat(p2) Define aliases for interface types(#88) --- pkg/hessian2/enum/enum.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/hessian2/enum/enum.go b/pkg/hessian2/enum/enum.go index d8cd96ff..dd4ce260 100644 --- a/pkg/hessian2/enum/enum.go +++ b/pkg/hessian2/enum/enum.go @@ -25,4 +25,6 @@ import ( const InvalidJavaEnum JavaEnum = JavaEnum(hessian.InvalidJavaEnum) -type JavaEnum hessian.JavaEnum +type JavaEnum = hessian.JavaEnum + +type _ = hessian.POJOEnum From d05a8164ad2db5e5e0acef76baa87d6f4438a0f6 Mon Sep 17 00:00:00 2001 From: leiliang <1354370192@qq.com> Date: Wed, 17 Apr 2024 10:52:07 +0800 Subject: [PATCH 7/9] feat(p2) Update license for 2024|2023 (#88) --- .licenserc.yaml | 2 +- pkg/hessian2/enum/enum.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.licenserc.yaml b/.licenserc.yaml index 35a53d9c..cf8d2ed8 100644 --- a/.licenserc.yaml +++ b/.licenserc.yaml @@ -38,7 +38,7 @@ header: # `header` section is configurations for source codes license header. limitations under the License. # `pattern` is optional regexp if all the file headers are the same as `license` or the license of `spdx-id` and `copyright-owner`. pattern: | - Copyright 2023 CloudWeGo Authors + Copyright 202[34] CloudWeGo Authors Licensed to the Apache Software Foundation under one or more contributor license agreements. See the NOTICE file distributed with diff --git a/pkg/hessian2/enum/enum.go b/pkg/hessian2/enum/enum.go index dd4ce260..220f3443 100644 --- a/pkg/hessian2/enum/enum.go +++ b/pkg/hessian2/enum/enum.go @@ -1,5 +1,5 @@ /* - * Copyright 2023 CloudWeGo Authors + * Copyright 2024 CloudWeGo Authors * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with From 6cad491ac87c9575e20fc15164c4d9ce68b67d44 Mon Sep 17 00:00:00 2001 From: leiliang <1354370192@qq.com> Date: Thu, 25 Apr 2024 14:26:12 +0800 Subject: [PATCH 8/9] feat(p2) Supplement DM's description of JavaClassName and the use of enumerations (#88) --- README.md | 55 ++++++++- README_ENG.md | 46 ++++++++ go.mod | 2 +- go.sum | 2 + .../kitex/samples/enumeration/KitexEnum.java | 38 +++++++ samples/helloworld/kitex/api.thrift | 12 ++ samples/helloworld/kitex/go.mod | 2 +- .../helloworld/kitex/kitex_gen/hello/api.go | 106 +++++++++++++++++- .../kitex_gen/hello/greetservice/client.go | 4 +- .../hello/greetservice/greetservice.go | 69 +++++++++++- .../kitex_gen/hello/greetservice/invoker.go | 2 +- .../kitex_gen/hello/greetservice/server.go | 7 +- .../kitex_gen/hello/hessian2-register-api.go | 84 +++++++++++++- .../helloworld/kitex/kitex_gen/hello/k-api.go | 69 +++++++++++- 14 files changed, 479 insertions(+), 19 deletions(-) create mode 100644 samples/helloworld/dubbo/src/main/java/org/cloudwego/kitex/samples/enumeration/KitexEnum.java diff --git a/README.md b/README.md index 216fb854..4fc80e4d 100644 --- a/README.md +++ b/README.md @@ -282,7 +282,7 @@ service EchoService { #### 其它类型(java.lang.Object, java.util.Date) -由于 **thrift** 类型的局限性,**kitex** 与 **dubbo-java** 映射时有一些不兼容的类型。 +由于 **thrift** 类型的局限性,**kitex** 与 **dubbo-java** 映射时有一些不兼容的类型。 DubboCodec 在 [codec-dubbo/java](https://github.com/kitex-contrib/codec-dubbo/tree/main/java) 包中提供了更多 **thrift** 不支持的 **java** 类型。 为了启用这些类型,你可以在 **Thrift IDL** 中使用 `include "java.thrift"` 导入它们,并且在使用 **kitex** 脚手架工具生成代码时添加 `-hessian2 java_extension` 参数来拉取该拓展包。 @@ -322,6 +322,53 @@ service EchoService { string EchoMethodD(1: bool req1, 2: i32 req2) (JavaMethodName="EchoMethod") } ``` +### 枚举支持 + +支持Java的枚举类型,需要用户在枚举上加上注解映射到具体的Java类型,您可以在客户端做基本的枚举配置已经对应服务端代码,如下 + +#### thrift 配置 +```thrift + +enum KitexEnum { + ONE, + TWO, + THREE, + FOUR, + FIVE, +}(JavaClassName="org.cloudwego.kitex.samples.enumeration.KitexEnum") + +service GreetEnumService { + KitexEnum GreetEnum(1: KitexEnum req) +} + +``` +#### Dubbo服务侧代码 +```java + +package org.cloudwego.kitex.samples.enumeration; + +import java.io.Serializable; + +public enum KitexEnum implements Serializable { + + ONE("1"),TWO("2"),THREE("3"),FOUR("4"),FIVE("5"); + + final String codeStr ; + + KitexEnum(String number) { + this.codeStr = number; + } + + // 枚举类型的 getter 方法 + public String getCode() { + return this.codeStr; + } +} + +``` +**重要提示** +1. 这里强制您配置JavaClassName来映射具体的Java类型,如果您没有配置可能会导致不可预知的错误 + ### 异常处理 @@ -402,7 +449,7 @@ exception CustomizedException { }(JavaClassName="org.cloudwego.kitex.samples.api.CustomizedException") ``` -和[其它类型](#其它类型javalangobject-javautildate)一样,需要在使用 **kitex** 脚手架工具生成代码时添加 `-hessian2 java_extension` 参数来拉取拓展包。 +和[其它类型](#其它类型javalangobject-javautildate)一样,需要在使用 **kitex** 脚手架工具生成代码时添加 `-hessian2 java_extension` 参数来拉取拓展包。 使用方法与[常见异常](#常见异常)一致。 @@ -412,7 +459,7 @@ exception CustomizedException { 用于该功能的配置分为以下两个层次: 1. [registry/options.go](https://github.com/kitex-contrib/codec-dubbo/tree/main/registries/zookeeper/registry/options.go) 与 [resolver/options.go](https://github.com/kitex-contrib/codec-dubbo/tree/main/registries/zookeeper/resolver/options.go) 中的WithXXX函数提供注册中心级别的配置,请使用这些函数生成```registry.Registry``` -和```discovery.Resolver```实例。 + 和```discovery.Resolver```实例。 2. 服务级别的配置由```client.WithTag```与```server.WithRegistryInfo```进行传递,/registries/common.go提供Tag Keys: | Tag Key | client侧作用 | server侧作用 | @@ -545,7 +592,7 @@ func main() { ## 性能测试 -### 测试环境 +### 测试环境 CPU: **Intel(R) Xeon(R) Gold 5118 CPU @ 2.30GHz** 内存: **192GB** diff --git a/README_ENG.md b/README_ENG.md index fed9b04d..f7770578 100644 --- a/README_ENG.md +++ b/README_ENG.md @@ -317,6 +317,52 @@ service EchoService { string EchoMethodD(1: bool req1, 2: i32 req2) (JavaMethodName="EchoMethod") } ``` +### Enumeration support + +To support Java enumeration types, users need to add annotations on the enumeration to map it to specific Java types. You can make basic enumeration configurations on the client and correspond to the server code, as follows +#### Thrift configuration +```thrift + +enum KitexEnum { + ONE, + TWO, + THREE, + FOUR, + FIVE, +}(JavaClassName="org.cloudwego.kitex.samples.enumeration.KitexEnum") + +service GreetEnumService { + KitexEnum GreetEnum(1: KitexEnum req) +} + +``` +#### Dubbo service side code +```java + +package org.cloudwego.kitex.samples.enumeration; + +import java.io.Serializable; + +public enum KitexEnum implements Serializable { + + ONE("1"),TWO("2"),THREE("3"),FOUR("4"),FIVE("5"); + + final String codeStr ; + + KitexEnum(String number) { + this.codeStr = number; + } + + // Getter methods of enumeration types + public String getCode() { + return this.codeStr; + } +} + +``` +**Important notes:** +1. This forces you to configure JavaClassName to map specific Java types. If you do not configure it, it may cause unpredictable errors. + ### Exception Handling diff --git a/go.mod b/go.mod index ba693ecf..a9a69dc1 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,6 @@ require ( github.com/apache/dubbo-go-hessian2 v1.12.4 github.com/cloudwego/kitex v0.8.0 github.com/cloudwego/thriftgo v0.3.3 + github.com/kitex-contrib/codec-dubbo v0.2.5 github.com/stretchr/testify v1.8.2 - golang.org/x/net v0.17.0 // indirect ) diff --git a/go.sum b/go.sum index 060ff29b..83e08797 100644 --- a/go.sum +++ b/go.sum @@ -319,6 +319,8 @@ github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvW github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/kitex-contrib/codec-dubbo v0.2.5 h1:hM6MzbWyVFiNjXzB42sN+BtUXrbPr/N0UBocLxU+rKQ= +github.com/kitex-contrib/codec-dubbo v0.2.5/go.mod h1:0pp4flNA+oPBDxAcOCxH7D+McLAjFrLzGXVABdw22xE= github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= github.com/klauspost/cpuid/v2 v2.1.0/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY= github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk= diff --git a/samples/helloworld/dubbo/src/main/java/org/cloudwego/kitex/samples/enumeration/KitexEnum.java b/samples/helloworld/dubbo/src/main/java/org/cloudwego/kitex/samples/enumeration/KitexEnum.java new file mode 100644 index 00000000..227a2414 --- /dev/null +++ b/samples/helloworld/dubbo/src/main/java/org/cloudwego/kitex/samples/enumeration/KitexEnum.java @@ -0,0 +1,38 @@ +/* + * Copyright 2024 CloudWeGo Authors + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.cloudwego.kitex.samples.enumeration; + +import java.io.Serializable; + +public enum KitexEnum implements Serializable { + + ONE("1"),TWO("2"),THREE("3"),FOUR("4"),FIVE("5"); + + final String codeStr ; + + KitexEnum(String number) { + this.codeStr = number; + } + + // 枚举类型的 getter 方法 + public String getCode() { + return this.codeStr; + } +} diff --git a/samples/helloworld/kitex/api.thrift b/samples/helloworld/kitex/api.thrift index 166c8658..27528292 100644 --- a/samples/helloworld/kitex/api.thrift +++ b/samples/helloworld/kitex/api.thrift @@ -1,5 +1,13 @@ namespace go hello +enum KitexEnum { + ONE, + TWO, + THREE, + FOUR, + FIVE, +}(JavaClassName="org.cloudwego.kitex.samples.enumeration.KitexEnum") + struct GreetRequest { 1: required string req, }(JavaClassName="org.cloudwego.kitex.samples.api.GreetRequest") @@ -11,4 +19,8 @@ struct GreetResponse { service GreetService { string Greet(1: string req) GreetResponse GreetWithStruct(1: GreetRequest req) +} + +service GreetEnumService { + KitexEnum GreetEnum(1: KitexEnum req) } \ No newline at end of file diff --git a/samples/helloworld/kitex/go.mod b/samples/helloworld/kitex/go.mod index cdb373c2..cbfb6b6f 100644 --- a/samples/helloworld/kitex/go.mod +++ b/samples/helloworld/kitex/go.mod @@ -9,7 +9,7 @@ replace github.com/kitex-contrib/codec-dubbo => ../../../ require ( github.com/apache/thrift v0.16.0 github.com/cloudwego/kitex v0.9.0 - github.com/kitex-contrib/codec-dubbo v0.0.0-20231009160704-aad6a2705290 + github.com/kitex-contrib/codec-dubbo v0.2.5 github.com/kitex-contrib/obs-opentelemetry v0.2.6 github.com/pkg/errors v0.9.1 ) diff --git a/samples/helloworld/kitex/kitex_gen/hello/api.go b/samples/helloworld/kitex/kitex_gen/hello/api.go index ecbbe611..02f31976 100644 --- a/samples/helloworld/kitex/kitex_gen/hello/api.go +++ b/samples/helloworld/kitex/kitex_gen/hello/api.go @@ -1,12 +1,72 @@ -// Code generated by thriftgo (0.3.2). DO NOT EDIT. +// Code generated by thriftgo (0.3.10). DO NOT EDIT. package hello import ( "context" + "database/sql" + "database/sql/driver" "fmt" + "strings" ) +type KitexEnum int64 + +const ( + KitexEnum_ONE KitexEnum = 0 + KitexEnum_TWO KitexEnum = 1 + KitexEnum_THREE KitexEnum = 2 + KitexEnum_FOUR KitexEnum = 3 + KitexEnum_FIVE KitexEnum = 4 +) + +func (p KitexEnum) String() string { + switch p { + case KitexEnum_ONE: + return "ONE" + case KitexEnum_TWO: + return "TWO" + case KitexEnum_THREE: + return "THREE" + case KitexEnum_FOUR: + return "FOUR" + case KitexEnum_FIVE: + return "FIVE" + } + return "" +} + +func KitexEnumFromString(s string) (KitexEnum, error) { + switch s { + case "ONE": + return KitexEnum_ONE, nil + case "TWO": + return KitexEnum_TWO, nil + case "THREE": + return KitexEnum_THREE, nil + case "FOUR": + return KitexEnum_FOUR, nil + case "FIVE": + return KitexEnum_FIVE, nil + } + return KitexEnum(0), fmt.Errorf("not a valid KitexEnum string") +} + +func KitexEnumPtr(v KitexEnum) *KitexEnum { return &v } +func (p *KitexEnum) Scan(value interface{}) (err error) { + var result sql.NullInt64 + err = result.Scan(value) + *p = KitexEnum(result.Int64) + return +} + +func (p *KitexEnum) Value() (driver.Value, error) { + if p == nil { + return nil, nil + } + return int64(*p), nil +} + type GreetRequest struct { Req string `thrift:"req,1,required" frugal:"1,required,string" json:"req"` } @@ -33,6 +93,26 @@ func (p *GreetRequest) String() string { return fmt.Sprintf("GreetRequest(%+v)", *p) } +func (p *GreetRequest) DeepEqual(ano *GreetRequest) bool { + if p == ano { + return true + } else if p == nil || ano == nil { + return false + } + if !p.Field1DeepEqual(ano.Req) { + return false + } + return true +} + +func (p *GreetRequest) Field1DeepEqual(src string) bool { + + if strings.Compare(p.Req, src) != 0 { + return false + } + return true +} + type GreetResponse struct { Resp string `thrift:"resp,1,required" frugal:"1,required,string" json:"resp"` } @@ -59,8 +139,32 @@ func (p *GreetResponse) String() string { return fmt.Sprintf("GreetResponse(%+v)", *p) } +func (p *GreetResponse) DeepEqual(ano *GreetResponse) bool { + if p == ano { + return true + } else if p == nil || ano == nil { + return false + } + if !p.Field1DeepEqual(ano.Resp) { + return false + } + return true +} + +func (p *GreetResponse) Field1DeepEqual(src string) bool { + + if strings.Compare(p.Resp, src) != 0 { + return false + } + return true +} + type GreetService interface { Greet(ctx context.Context, req string) (r string, err error) GreetWithStruct(ctx context.Context, req *GreetRequest) (r *GreetResponse, err error) } + +type GreetEnumService interface { + GreetEnum(ctx context.Context, req KitexEnum) (r KitexEnum, err error) +} diff --git a/samples/helloworld/kitex/kitex_gen/hello/greetservice/client.go b/samples/helloworld/kitex/kitex_gen/hello/greetservice/client.go index b6d1a5d9..5d814588 100644 --- a/samples/helloworld/kitex/kitex_gen/hello/greetservice/client.go +++ b/samples/helloworld/kitex/kitex_gen/hello/greetservice/client.go @@ -1,4 +1,4 @@ -// Code generated by Kitex v0.7.2. DO NOT EDIT. +// Code generated by Kitex v0.9.1. DO NOT EDIT. package greetservice @@ -22,7 +22,7 @@ func NewClient(destService string, opts ...client.Option) (Client, error) { options = append(options, opts...) - kc, err := client.NewClient(serviceInfo(), options...) + kc, err := client.NewClient(serviceInfoForClient(), options...) if err != nil { return nil, err } diff --git a/samples/helloworld/kitex/kitex_gen/hello/greetservice/greetservice.go b/samples/helloworld/kitex/kitex_gen/hello/greetservice/greetservice.go index bb5cd305..d2ef7b8c 100644 --- a/samples/helloworld/kitex/kitex_gen/hello/greetservice/greetservice.go +++ b/samples/helloworld/kitex/kitex_gen/hello/greetservice/greetservice.go @@ -1,36 +1,93 @@ -// Code generated by Kitex v0.7.2. DO NOT EDIT. +// Code generated by Kitex v0.9.1. DO NOT EDIT. package greetservice import ( "context" + "errors" client "github.com/cloudwego/kitex/client" kitex "github.com/cloudwego/kitex/pkg/serviceinfo" hello "github.com/kitex-contrib/codec-dubbo/samples/helloworld/kitex/kitex_gen/hello" ) +var errInvalidMessageType = errors.New("invalid message type for service method handler") + +var serviceMethods = map[string]kitex.MethodInfo{ + "Greet": kitex.NewMethodInfo( + greetHandler, + newGreetServiceGreetArgs, + newGreetServiceGreetResult, + false, + kitex.WithStreamingMode(kitex.StreamingNone), + ), + "GreetWithStruct": kitex.NewMethodInfo( + greetWithStructHandler, + newGreetServiceGreetWithStructArgs, + newGreetServiceGreetWithStructResult, + false, + kitex.WithStreamingMode(kitex.StreamingNone), + ), +} + +var ( + greetServiceServiceInfo = NewServiceInfo() + greetServiceServiceInfoForClient = NewServiceInfoForClient() + greetServiceServiceInfoForStreamClient = NewServiceInfoForStreamClient() +) + +// for server func serviceInfo() *kitex.ServiceInfo { return greetServiceServiceInfo } -var greetServiceServiceInfo = NewServiceInfo() +// for client +func serviceInfoForStreamClient() *kitex.ServiceInfo { + return greetServiceServiceInfoForStreamClient +} + +// for stream client +func serviceInfoForClient() *kitex.ServiceInfo { + return greetServiceServiceInfoForClient +} +// NewServiceInfo creates a new ServiceInfo containing all methods func NewServiceInfo() *kitex.ServiceInfo { + return newServiceInfo(false, true, true) +} + +// NewServiceInfo creates a new ServiceInfo containing non-streaming methods +func NewServiceInfoForClient() *kitex.ServiceInfo { + return newServiceInfo(false, false, true) +} +func NewServiceInfoForStreamClient() *kitex.ServiceInfo { + return newServiceInfo(true, true, false) +} + +func newServiceInfo(hasStreaming bool, keepStreamingMethods bool, keepNonStreamingMethods bool) *kitex.ServiceInfo { serviceName := "GreetService" handlerType := (*hello.GreetService)(nil) - methods := map[string]kitex.MethodInfo{ - "Greet": kitex.NewMethodInfo(greetHandler, newGreetServiceGreetArgs, newGreetServiceGreetResult, false), - "GreetWithStruct": kitex.NewMethodInfo(greetWithStructHandler, newGreetServiceGreetWithStructArgs, newGreetServiceGreetWithStructResult, false), + methods := map[string]kitex.MethodInfo{} + for name, m := range serviceMethods { + if m.IsStreaming() && !keepStreamingMethods { + continue + } + if !m.IsStreaming() && !keepNonStreamingMethods { + continue + } + methods[name] = m } extra := map[string]interface{}{ "PackageName": "hello", "ServiceFilePath": `api.thrift`, } + if hasStreaming { + extra["streaming"] = hasStreaming + } svcInfo := &kitex.ServiceInfo{ ServiceName: serviceName, HandlerType: handlerType, Methods: methods, - KiteXGenVersion: "v0.7.2", + KiteXGenVersion: "v0.9.1", Extra: extra, } return svcInfo diff --git a/samples/helloworld/kitex/kitex_gen/hello/greetservice/invoker.go b/samples/helloworld/kitex/kitex_gen/hello/greetservice/invoker.go index 6b5b75e5..1628cbc7 100644 --- a/samples/helloworld/kitex/kitex_gen/hello/greetservice/invoker.go +++ b/samples/helloworld/kitex/kitex_gen/hello/greetservice/invoker.go @@ -1,4 +1,4 @@ -// Code generated by Kitex v0.7.2. DO NOT EDIT. +// Code generated by Kitex v0.9.1. DO NOT EDIT. package greetservice diff --git a/samples/helloworld/kitex/kitex_gen/hello/greetservice/server.go b/samples/helloworld/kitex/kitex_gen/hello/greetservice/server.go index da43d92b..41f2caf2 100644 --- a/samples/helloworld/kitex/kitex_gen/hello/greetservice/server.go +++ b/samples/helloworld/kitex/kitex_gen/hello/greetservice/server.go @@ -1,4 +1,4 @@ -// Code generated by Kitex v0.7.2. DO NOT EDIT. +// Code generated by Kitex v0.9.1. DO NOT EDIT. package greetservice import ( @@ -11,6 +11,7 @@ func NewServer(handler hello.GreetService, opts ...server.Option) server.Server var options []server.Option options = append(options, opts...) + options = append(options, server.WithCompatibleMiddlewareForUnary()) svr := server.NewServer(options...) if err := svr.RegisterService(serviceInfo(), handler); err != nil { @@ -18,3 +19,7 @@ func NewServer(handler hello.GreetService, opts ...server.Option) server.Server } return svr } + +func RegisterService(svr server.Server, handler hello.GreetService, opts ...server.RegisterOption) error { + return svr.RegisterService(serviceInfo(), handler, opts...) +} diff --git a/samples/helloworld/kitex/kitex_gen/hello/hessian2-register-api.go b/samples/helloworld/kitex/kitex_gen/hello/hessian2-register-api.go index 877711d9..95d7395c 100644 --- a/samples/helloworld/kitex/kitex_gen/hello/hessian2-register-api.go +++ b/samples/helloworld/kitex/kitex_gen/hello/hessian2-register-api.go @@ -2,8 +2,8 @@ package hello import ( "fmt" - "github.com/kitex-contrib/codec-dubbo/pkg/hessian2" + "github.com/kitex-contrib/codec-dubbo/pkg/hessian2/enum" codec "github.com/kitex-contrib/codec-dubbo/pkg/iface" "github.com/pkg/errors" ) @@ -11,6 +11,11 @@ import ( var objectsApi = []interface{}{ &GreetRequest{}, &GreetResponse{}, + KitexEnum_ONE, + KitexEnum_TWO, + KitexEnum_THREE, + KitexEnum_FOUR, + KitexEnum_FIVE, } func init() { @@ -20,6 +25,9 @@ func init() { func GetGreetServiceIDLAnnotations() map[string][]string { return map[string][]string{} } +func GetGreetEnumServiceIDLAnnotations() map[string][]string { + return map[string][]string{} +} func (p *GreetRequest) Encode(e codec.Encoder) error { var err error @@ -83,6 +91,26 @@ func (p *GreetResponse) JavaClassName() string { return "org.cloudwego.kitex.samples.api.GreetResponse" } +var KitexEnumValues = map[string]KitexEnum{ + "ONE": KitexEnum_ONE, + "TWO": KitexEnum_TWO, + "THREE": KitexEnum_THREE, + "FOUR": KitexEnum_FOUR, + "FIVE": KitexEnum_FIVE, +} + +func (KitexEnum) JavaClassName() string { + return "org.cloudwego.kitex.samples.enumeration.KitexEnum" +} + +func (KitexEnum) EnumValue(s string) enum.JavaEnum { + v, ok := KitexEnumValues[s] + if ok { + return enum.JavaEnum(v) + } + return enum.InvalidJavaEnum +} + func (p *GreetServiceGreetArgs) Encode(e codec.Encoder) error { var err error err = e.Encode(p.Req) @@ -190,3 +218,57 @@ func (p *GreetServiceGreetWithStructResult) Decode(d codec.Decoder) error { return nil } + +func (p *GreetEnumServiceGreetEnumArgs) Encode(e codec.Encoder) error { + var err error + err = e.Encode(p.Req) + if err != nil { + return err + } + + return nil +} + +func (p *GreetEnumServiceGreetEnumArgs) Decode(d codec.Decoder) error { + var ( + err error + v interface{} + ) + v, err = d.Decode() + if err != nil { + return err + } + err = hessian2.ReflectResponse(v, &p.Req) + if err != nil { + return errors.Wrap(err, fmt.Sprintf("invalid data type: %T", v)) + } + + return nil +} + +func (p *GreetEnumServiceGreetEnumResult) Encode(e codec.Encoder) error { + var err error + err = e.Encode(p.Success) + if err != nil { + return err + } + + return nil +} + +func (p *GreetEnumServiceGreetEnumResult) Decode(d codec.Decoder) error { + var ( + err error + v interface{} + ) + v, err = d.Decode() + if err != nil { + return err + } + err = hessian2.ReflectResponse(v, &p.Success) + if err != nil { + return errors.Wrap(err, fmt.Sprintf("invalid data type: %T", v)) + } + + return nil +} diff --git a/samples/helloworld/kitex/kitex_gen/hello/k-api.go b/samples/helloworld/kitex/kitex_gen/hello/k-api.go index 0de660f7..db0584ad 100644 --- a/samples/helloworld/kitex/kitex_gen/hello/k-api.go +++ b/samples/helloworld/kitex/kitex_gen/hello/k-api.go @@ -1,4 +1,4 @@ -// Code generated by Kitex v0.7.2. DO NOT EDIT. +// Code generated by Kitex v0.9.1. DO NOT EDIT. package hello @@ -162,3 +162,70 @@ func (p *GreetServiceGreetWithStructResult) String() string { func (p *GreetServiceGreetWithStructResult) GetResult() interface{} { return p.Success } + +type GreetEnumServiceGreetEnumArgs struct { + Req KitexEnum `thrift:"req,1" frugal:"1,default,KitexEnum" json:"req"` +} + +func NewGreetEnumServiceGreetEnumArgs() *GreetEnumServiceGreetEnumArgs { + return &GreetEnumServiceGreetEnumArgs{} +} + +func (p *GreetEnumServiceGreetEnumArgs) InitDefault() { + *p = GreetEnumServiceGreetEnumArgs{} +} + +func (p *GreetEnumServiceGreetEnumArgs) GetReq() (v KitexEnum) { + return p.Req +} +func (p *GreetEnumServiceGreetEnumArgs) SetReq(val KitexEnum) { + p.Req = val +} + +func (p *GreetEnumServiceGreetEnumArgs) String() string { + if p == nil { + return "" + } + return fmt.Sprintf("GreetEnumServiceGreetEnumArgs(%+v)", *p) +} +func (p *GreetEnumServiceGreetEnumArgs) GetFirstArgument() interface{} { + return p.Req +} + +type GreetEnumServiceGreetEnumResult struct { + Success *KitexEnum `thrift:"success,0,optional" frugal:"0,optional,KitexEnum" json:"success,omitempty"` +} + +func NewGreetEnumServiceGreetEnumResult() *GreetEnumServiceGreetEnumResult { + return &GreetEnumServiceGreetEnumResult{} +} + +func (p *GreetEnumServiceGreetEnumResult) InitDefault() { + *p = GreetEnumServiceGreetEnumResult{} +} + +var GreetEnumServiceGreetEnumResult_Success_DEFAULT KitexEnum + +func (p *GreetEnumServiceGreetEnumResult) GetSuccess() (v KitexEnum) { + if !p.IsSetSuccess() { + return GreetEnumServiceGreetEnumResult_Success_DEFAULT + } + return *p.Success +} +func (p *GreetEnumServiceGreetEnumResult) SetSuccess(x interface{}) { + p.Success = x.(*KitexEnum) +} + +func (p *GreetEnumServiceGreetEnumResult) IsSetSuccess() bool { + return p.Success != nil +} + +func (p *GreetEnumServiceGreetEnumResult) String() string { + if p == nil { + return "" + } + return fmt.Sprintf("GreetEnumServiceGreetEnumResult(%+v)", *p) +} +func (p *GreetEnumServiceGreetEnumResult) GetResult() interface{} { + return p.Success +} From fb479aefde59e2acff12cd7b3fbdd9999fb10361 Mon Sep 17 00:00:00 2001 From: leiliang <1354370192@qq.com> Date: Thu, 25 Apr 2024 14:50:03 +0800 Subject: [PATCH 9/9] feat(p2) go num (#88) --- go.mod | 2 +- go.sum | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/go.mod b/go.mod index a9a69dc1..ba693ecf 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,6 @@ require ( github.com/apache/dubbo-go-hessian2 v1.12.4 github.com/cloudwego/kitex v0.8.0 github.com/cloudwego/thriftgo v0.3.3 - github.com/kitex-contrib/codec-dubbo v0.2.5 github.com/stretchr/testify v1.8.2 + golang.org/x/net v0.17.0 // indirect ) diff --git a/go.sum b/go.sum index 83e08797..060ff29b 100644 --- a/go.sum +++ b/go.sum @@ -319,8 +319,6 @@ github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvW github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= -github.com/kitex-contrib/codec-dubbo v0.2.5 h1:hM6MzbWyVFiNjXzB42sN+BtUXrbPr/N0UBocLxU+rKQ= -github.com/kitex-contrib/codec-dubbo v0.2.5/go.mod h1:0pp4flNA+oPBDxAcOCxH7D+McLAjFrLzGXVABdw22xE= github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= github.com/klauspost/cpuid/v2 v2.1.0/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY= github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk=