Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions tool/cmd/kitex/args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func (a *Arguments) buildFlags(version string) *flag.FlagSet {
f.Var(&a.FrugalStruct, "frugal-struct", "Replace fastCodec code to frugal. Use `-frugal-struct @all` for all, `-frugal-struct @auto` for annotated structs (go.codec=\"frugal\"), or specify multiple structs (e.g., `-frugal-struct A -frugal-struct B`).")

f.BoolVar(&a.NoRecurse, "no-recurse", false, `Don't generate thrift files recursively, just generate the given file.'`)
f.BoolVar(&a.HTTPTags, "http-tags", false, `Recognize api.xx tags in IDL and generate HTTP go tags and router info in kitex_gen.'`)

if env.UseProtoc() {
f.Var(&a.ProtobufOptions, "protobuf", "Specify arguments for the protobuf compiler.")
Expand All @@ -151,6 +152,10 @@ func (a *Arguments) buildFlags(version string) *flag.FlagSet {
"no_processor",
)

if a.HTTPTags {
a.ThriftOptions = append(a.ThriftOptions, "gen_json_tag=false")
}

f.Usage = func() {
fmt.Fprintf(os.Stderr, `Version %s
Usage: %s [flags] IDL
Expand Down
12 changes: 0 additions & 12 deletions tool/cmd/kitex/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
package utils

import (
"os/exec"
"strings"

kargs "github.com/cloudwego/kitex/tool/cmd/kitex/args"
"github.com/cloudwego/kitex/tool/internal_pkg/log"
"github.com/cloudwego/kitex/tool/internal_pkg/pluginmode/thriftgo"
Expand All @@ -26,15 +23,6 @@ import (
func OnKitexToolNormalExit(args kargs.Arguments) {
log.Info("Code Generation is Done!")

if args.IDLType == "thrift" {
cmd := "go mod edit -replace github.com/apache/thrift=github.com/apache/thrift@v0.13.0"
argv := strings.Split(cmd, " ")
err := exec.Command(argv[0], argv[1:]...).Run()
if err != nil {
log.Warnf("Tips: please execute the following command to fix apache thrift lib version.\n%s", cmd)
}
}

// If hessian option is java_extension, replace *java.Object to java.Object
if thriftgo.EnableJavaExtension(args.Config) {
if err := thriftgo.Hessian2PatchByReplace(args.Config, ""); err != nil {
Expand Down
1 change: 1 addition & 0 deletions tool/internal_pkg/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ type Config struct {

FrugalStruct util.StringSlice
NoRecurse bool
HTTPTags bool

BuiltinTpl util.StringSlice // specify the built-in template to use

Expand Down
132 changes: 132 additions & 0 deletions tool/internal_pkg/pluginmode/thriftgo/http_tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// Copyright 2025 CloudWeGo Authors
//
// 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 thriftgo

import (
"sort"
"strings"

"github.com/cloudwego/kitex/tool/internal_pkg/util"

"github.com/cloudwego/thriftgo/parser"
)

const (
AnnotationQuery = "api.query"
AnnotationForm = "api.form"
AnnotationPath = "api.path"
AnnotationHeader = "api.header"
AnnotationCookie = "api.cookie"
AnnotationBody = "api.body"
AnnotationRawBody = "api.raw_body"
)

var bindingTags = map[string]string{
AnnotationPath: "path",
AnnotationQuery: "query",
AnnotationHeader: "header",
AnnotationCookie: "cookie",
AnnotationBody: "form",
AnnotationForm: "form",
AnnotationRawBody: "raw_body",
}

type genHTTPTagOption struct {
// thriftgo: -thrift snake_type_json_tag
snakeTyleJSONTag bool
// thriftgo: -thrift low_camel_case_json
lowerCamelCaseJSONTag bool
// hertz: --unset_omitempty
unsetOmitempty bool
// hertz: --snake_tag
snakeStyleHTTPTag bool
}

// genHTTPTags append api.xx and json tag into struct field for http binding usage.
func genHTTPTags(f *parser.Field, opt genHTTPTagOption) string {
w := initTagWriter(f, opt)
var found bool
for _, a := range f.Annotations {
if tag, ok := bindingTags[a.Key]; ok {
tagVal := a.Values[len(a.Values)-1]
if a.Key == AnnotationBody {
w.resetJsonVal(tagVal)
}
w.addTag(tag, tagVal)
found = true
}
}
if !found {
w.addTag("form", f.Name)
w.addTag("query", f.Name)
}
return w.dump()
}

type tagWriter struct {
jsonVal string
tags map[string]string
opt genHTTPTagOption
f *parser.Field
}

func (tw *tagWriter) resetJsonVal(jsonVal string) {
tw.jsonVal = jsonVal
}

func (tw *tagWriter) addTag(k, v string) {
tw.tags[k] = v
}

func initTagWriter(f *parser.Field, opt genHTTPTagOption) *tagWriter {
return &tagWriter{f.Name, make(map[string]string), opt, f}
}

func (tw tagWriter) dump() string {
var tagSuffix, jsonSuffix string
if tw.f.GetRequiredness().IsRequired() {
tagSuffix = ",required"
jsonSuffix = ",required"
} else if tw.f.GetRequiredness().IsOptional() && !tw.opt.unsetOmitempty {
jsonSuffix = ",omitempty"
}
// same as github.com/cloudwego/thriftgo/generator/golang/utils.go genFieldTags
if tw.opt.snakeTyleJSONTag {
tw.jsonVal = util.Snakify(tw.jsonVal)
}
if tw.opt.lowerCamelCaseJSONTag {
tw.jsonVal = util.LowerCamelCase(tw.jsonVal)
}
var sb strings.Builder
tw.tags["json"] = tw.jsonVal
keys := make([]string, 0, len(tw.tags))
for tag := range tw.tags {
keys = append(keys, tag)
}
// tags are arranged in alphabet order.
sort.Strings(keys)
for _, tag := range keys {
tagVal := tw.tags[tag]
if tag == "json" {
sb.WriteString(` json:"` + tw.jsonVal + jsonSuffix + `"`)
continue
}
if tw.opt.snakeStyleHTTPTag {
tagVal = util.Snakify(tagVal)
}
sb.WriteString(` ` + tag + `:"` + tagVal + tagSuffix + `"`)
}
return sb.String()
}
Loading
Loading