Skip to content

Commit

Permalink
opt: support quoted go.tag
Browse files Browse the repository at this point in the history
  • Loading branch information
AsterDY committed Jul 10, 2023
1 parent c6daa2a commit 15c9d3f
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 18 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/cloudwego/kitex v0.6.1
github.com/cloudwego/thriftgo v0.2.11
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
github.com/fatih/structtag v1.2.0
github.com/iancoleman/strcase v0.2.0
github.com/klauspost/cpuid/v2 v2.2.4
github.com/stretchr/testify v1.8.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymF
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/fatih/structtag v1.2.0 h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4=
github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4/aAZl94=
github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/go-fonts/dejavu v0.1.0/go.mod h1:4Wt4I4OU2Nq9asgDCteaAaWZOV24E+0/Pwo0gppep4g=
Expand Down
38 changes: 26 additions & 12 deletions internal/util/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
package util

import (
"fmt"
"strconv"
"strings"

"github.com/cloudwego/dynamicgo/meta"
"github.com/fatih/structtag"
"github.com/iancoleman/strcase"
)

Expand Down Expand Up @@ -54,18 +53,33 @@ func ToLowerCamelCase(name string) string {
}

func SplitTagOptions(tag string) (ret []string, err error) {
kv := strings.Trim(tag, " ")
i := strings.Index(kv, ":")
if i <= 0 {
return nil, fmt.Errorf("invalid go tag: %s", tag)
}
ret = append(ret, kv[:i])
v, err := strconv.Unquote(kv[i+1:])
tags, err := structtag.Parse(tag)
if err != nil {
return ret, err
// try replace `\"`
tag = strings.Replace(tag, `\"`, `"`, -1)
tags, err = structtag.Parse(tag)
if err != nil {
return nil, err
}
}
// kv := strings.Trim(tag, "\n\b\t\r")
// i := strings.Index(kv, ":")
// if i <= 0 {
// return nil, fmt.Errorf("invalid go tag: %s", tag)
// }
// ret = append(ret, kv[:i])
// val := strings.Trim(kv[i+1:], "\n\b\t\r")

// v, err := strconv.Unquote(kv[i+1:])
// if err != nil {
// return ret, err
// }
// vs := strings.Split(v, ",")
// ret = append(ret, vs...)
for _, t := range tags.Tags() {
ret = append(ret, t.Key)
ret = append(ret, t.Name)
}
vs := strings.Split(v, ",")
ret = append(ret, vs...)
return ret, nil
}

Expand Down
72 changes: 72 additions & 0 deletions internal/util/strings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* Copyright 2023 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 util

import (
"reflect"
"testing"
)

func TestSplitTagOptions(t *testing.T) {
type args struct {
tag string
}
tests := []struct {
name string
args args
wantRet []string
wantErr bool
}{
{
name: "single",
args: args{
tag: `json:"k1,omitempty"`,
},
wantRet: []string{"json", "k1"},
wantErr: false,
},
{
name: "multi",
args: args{
tag: `json:"k1,omitempty" thrift:"k2,xxx"`,
},
wantRet: []string{"json", "k1", "thrift", "k2"},
wantErr: false,
},
{
name: "quoted",
args: args{
tag: `json:\"k1,omitempty\"`,
},
wantRet: []string{"json", "k1"},
wantErr: false,
},

}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotRet, err := SplitTagOptions(tt.args.tag)
if (err != nil) != tt.wantErr {
t.Errorf("SplitTagOptions() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotRet, tt.wantRet) {
t.Errorf("SplitTagOptions() = %v, want %v", gotRet, tt.wantRet)
}
})
}
}
9 changes: 3 additions & 6 deletions thrift/annotation/anno_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (m goTagMapper) Map(ctx context.Context, anns []parser.Annotation, desc int
}
switch kv[0] {
case "json":
if err := handleGoJSON(kv, &ret); err != nil {
if err := handleGoJSON(kv[1], &ret); err != nil {
return nil, nil, err
}
}
Expand All @@ -53,13 +53,10 @@ func (m goTagMapper) Map(ctx context.Context, anns []parser.Annotation, desc int
return
}

func handleGoJSON(kv []string, ret *[]parser.Annotation) error {
if len(kv) < 2 {
return fmt.Errorf("invalid json tag: %v", kv)
}
func handleGoJSON(name string, ret *[]parser.Annotation) error {
*ret = append(*ret, parser.Annotation{
Key: APIKeyName,
Values: []string{kv[1]},
Values: []string{name},
})
return nil
}
Expand Down

0 comments on commit 15c9d3f

Please sign in to comment.