From 041e086267171b4be54dfe1bdc26571c7051f4f3 Mon Sep 17 00:00:00 2001 From: "Farmer.Chillax" <48387781+FarmerChillax@users.noreply.github.com> Date: Mon, 19 Jun 2023 17:55:05 +0800 Subject: [PATCH] =?UTF-8?q?docs:=20=E6=B7=BB=E5=8A=A0=E3=80=8C=E5=A6=82?= =?UTF-8?q?=E4=BD=95=E6=8E=A7=E5=88=B6=20http=20=E8=BF=94=E5=9B=9E=20enum?= =?UTF-8?q?=EF=BC=88=E6=9E=9A=E4=B8=BE=E7=B1=BB=E5=9E=8B=EF=BC=89=E7=9A=84?= =?UTF-8?q?=E5=90=8D=E7=A7=B0=E5=92=8C=E6=95=B0=E5=AD=97=E3=80=8D=20(#194)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * docs: 添加「如何控制 http 返回 enum(枚举类型)的名称和数字」 * docs: add en documentation --- docs/intro/03-faq.md | 21 ++++++++++++++++++ .../current/intro/03-faq.md | 22 +++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/docs/intro/03-faq.md b/docs/intro/03-faq.md index 28a17d08d..65608c07f 100644 --- a/docs/intro/03-faq.md +++ b/docs/intro/03-faq.md @@ -81,3 +81,24 @@ func init() { } ``` +### 11、控制 http 返回 enum(枚举类型)的名称和数字 + +可以在http服务的main.go中引入 +``` +import ( + "github.com/go-kratos/kratos/v2/encoding/json" + "google.golang.org/protobuf/encoding/protojson" +) +``` +在init方法中设置json.MarshalOptions +``` +func init() { + ... + //增加这段代码 + json.MarshalOptions = protojson.MarshalOptions{ + UseEnumNumbers: true, // 将枚举值作为数字发出,默认为枚举值的字符串 + } +} +``` + +更多控制 http 返回内容,请参考文档:https://pkg.go.dev/google.golang.org/protobuf@v1.30.0/encoding/protojson#MarshalOptions \ No newline at end of file diff --git a/i18n/en/docusaurus-plugin-content-docs/current/intro/03-faq.md b/i18n/en/docusaurus-plugin-content-docs/current/intro/03-faq.md index 061454942..4439e9786 100644 --- a/i18n/en/docusaurus-plugin-content-docs/current/intro/03-faq.md +++ b/i18n/en/docusaurus-plugin-content-docs/current/intro/03-faq.md @@ -81,8 +81,30 @@ func init() { } ``` +### 11、Controls the name and number of the enum (enumeration type) returned by http +You can import it in the `main.go` of the http service + +``` +import ( + "github.com/go-kratos/kratos/v2/encoding/json" + "google.golang.org/protobuf/encoding/protojson" +) +``` + +Set `json.MarshalOptions` in the init method + +``` +func init() { + flag.StringVar(&flagconf, "conf", "../../configs", "config path, eg: -conf config.yaml") + // Add this code + json.MarshalOptions = protojson.MarshalOptions{ + UseEnumNumbers: true, // UseEnumNumbers emits enum values as numbers. + } +} +``` +For more control over http return content, please refer to the documentation: https://pkg.go.dev/google.golang.org/protobuf@v1.30.0/encoding/protojson#MarshalOptions