Skip to content

Commit

Permalink
feat: remove_apache_codec (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
HeyJavaBean authored Oct 18, 2024
1 parent 2bac1d3 commit 7c2e919
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 3 deletions.
1 change: 1 addition & 0 deletions generator/golang/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions generator/golang/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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."`
Expand All @@ -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{
Expand Down
8 changes: 8 additions & 0 deletions generator/golang/templates/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}}
Expand Down
1 change: 1 addition & 0 deletions generator/golang/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(`\\.`)
Expand Down
37 changes: 37 additions & 0 deletions utils/log_util.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
2 changes: 1 addition & 1 deletion version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@

package version

const ThriftgoVersion = "0.3.17"
const ThriftgoVersion = "0.3.18"

0 comments on commit 7c2e919

Please sign in to comment.