From 1f91973433d562f1e1b830c2125c1f3c1d466493 Mon Sep 17 00:00:00 2001 From: HeyJavaBean Date: Fri, 18 Oct 2024 16:06:13 +0800 Subject: [PATCH] feat: remove_apache_codec --- generator/golang/imports.go | 1 + generator/golang/option.go | 5 ++-- generator/golang/templates/struct.go | 8 ++++++ generator/golang/util.go | 1 + utils/log_util.go | 37 ++++++++++++++++++++++++++++ version/version.go | 2 +- 6 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 utils/log_util.go diff --git a/generator/golang/imports.go b/generator/golang/imports.go index 229f2ac..b4f8ed5 100644 --- a/generator/golang/imports.go +++ b/generator/golang/imports.go @@ -91,6 +91,7 @@ func (im *importManager) init(cu *CodeUtils, ast *parser.Thrift) { "fieldmask": ThriftFieldMaskLib, "streaming": KitexStreamingLib, "thrift_option": ThriftOptionLib, + "apache_warning": ApacheWarningLib, } for pkg, path := range std { ns.Add(pkg, path) diff --git a/generator/golang/option.go b/generator/golang/option.go index e894f57..4c52ab0 100644 --- a/generator/golang/option.go +++ b/generator/golang/option.go @@ -52,8 +52,8 @@ type Features struct { EnumAsINT32 bool `enum_as_int_32:"Generate enum type as int32"` CodeRefSlim bool `code_ref_slim:"Generate code ref by given idl-ref.yaml with less refs to avoid conflict"` CodeRef bool `code_ref:"Generate code ref by given idl-ref.yaml"` - ExpCodeRef bool `exp_code_ref:"Generate code ref by given idl-ref.yaml with less refs to avoid conflict, but remind some struct as local.( this is a exp feature )"` - KeepCodeRefName bool `keep_code_ref_name:"Generate code ref but still keep file name."` + ExpCodeRef bool `exp_code_ref:"Generate code ref by given idl-ref.yaml with less refs to avoid conflict, but remind some struct as local.( this is a exp feature )"` + KeepCodeRefName bool `keep_code_ref_name:"Generate code ref but still keep file name."` TrimIDL bool `trim_idl:"Simplify IDL to the most concise form before generating code."` EnableNestedStruct bool `enable_nested_struct:"Generate nested field when 'thrift.nested=\"true\"' annotation is set to field, valid only in 'slim and raw_struct template'"` JSONStringer bool `json_stringer:"Generate the JSON marshal method in String() method."` @@ -70,6 +70,7 @@ type Features struct { SkipEmpty bool `skip_empty:"If there's not content in file, just skip it. Later this feature will be a default feature."` NoProcessor bool `no_processor:" Do not generate default thrift processor and client. Later this feature will be a default feature."` GetEnumAnnotation bool `get_enum_annotation:"Generate GetAnnotation method for enum types."` + ApacheWarning bool `apache_warning:"Generate Apache Codec with warning on the first line."` } var defaultFeatures = Features{ diff --git a/generator/golang/templates/struct.go b/generator/golang/templates/struct.go index bcb8187..3ef99a4 100644 --- a/generator/golang/templates/struct.go +++ b/generator/golang/templates/struct.go @@ -168,6 +168,10 @@ var StructLikeRead = ` {{- UseStdLibrary "thrift" "fmt"}} {{- $TypeName := .GoName}} func (p *{{$TypeName}}) Read(iprot thrift.TProtocol) (err error) { + {{if Features.ApacheWarning}} + {{- UseStdLibrary "apache_warning"}} + apache_warning.WarningApache("{{$TypeName}}") + {{end}} {{if Features.KeepUnknownFields}}var name string{{end}} var fieldTypeId thrift.TType var fieldId int16 @@ -314,6 +318,10 @@ var StructLikeWrite = ` {{- UseStdLibrary "thrift" "fmt"}} {{- $TypeName := .GoName}} func (p *{{$TypeName}}) Write(oprot thrift.TProtocol) (err error) { + {{if Features.ApacheWarning}} + {{- UseStdLibrary "apache_warning"}} + apache_warning.WarningApache("{{$TypeName}}") + {{end}} {{- if gt (len .Fields) 0 }} var fieldId int16 {{- end}} diff --git a/generator/golang/util.go b/generator/golang/util.go index c849ce8..76f9c0d 100644 --- a/generator/golang/util.go +++ b/generator/golang/util.go @@ -48,6 +48,7 @@ const ( defaultTemplate = "default" ThriftJSONUtilLib = "github.com/cloudwego/thriftgo/utils/json_utils" KitexStreamingLib = "github.com/cloudwego/kitex/pkg/streaming" + ApacheWarningLib = "github.com/cloudwego/thriftgo/utils" ) var escape = regexp.MustCompile(`\\.`) diff --git a/utils/log_util.go b/utils/log_util.go new file mode 100644 index 0000000..5e064d3 --- /dev/null +++ b/utils/log_util.go @@ -0,0 +1,37 @@ +/** + * Copyright 2023 ByteDance Inc. + * + * Licensed 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 utils + +import ( + "fmt" + "runtime" + "sync" +) + +var onceApacheReport sync.Once + +const DISABLE_ENV = "KITEX_APACHE_CODEC_DISABLE_WARNING" + +func WarningApache(structName string) { + onceApacheReport.Do(func() { + var path string + if _, file, line, ok := runtime.Caller(1); ok { + path = fmt.Sprintf("%s:%d \n", file, line) + } + fmt.Printf("[Kitex Apache Codec Warning] %s is using apache codec, please disable it. Path: %s\n", structName, path) + }) +} diff --git a/version/version.go b/version/version.go index ada9126..20fcf80 100644 --- a/version/version.go +++ b/version/version.go @@ -14,4 +14,4 @@ package version -const ThriftgoVersion = "0.3.17" +const ThriftgoVersion = "0.3.18"