Skip to content

Commit 72016a8

Browse files
committed
feat: suuport trimming enums
1 parent 3464bbb commit 72016a8

7 files changed

Lines changed: 366 additions & 2 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2025 CloudWeGo Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// This enum will be referenced and used
16+
enum AccountType {
17+
FREE = 1,
18+
PREMIUM = 2,
19+
ENTERPRISE = 3
20+
}
21+
22+
// This enum will not be used and should be trimmed
23+
enum UnusedLevel {
24+
LEVEL1 = 1,
25+
LEVEL2 = 2,
26+
LEVEL3 = 3
27+
}
28+
29+
// This enum will be used via typedef
30+
enum Country {
31+
US = 1,
32+
UK = 2,
33+
CN = 3
34+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2025 CloudWeGo Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// This enum is used by Person struct
16+
enum Status {
17+
ACTIVE = 1,
18+
INACTIVE = 2,
19+
PENDING = 3
20+
}
21+
22+
// This enum is NOT used anywhere and should be trimmed
23+
enum UnusedColor {
24+
RED = 1,
25+
GREEN = 2,
26+
BLUE = 3
27+
}
28+
29+
// This enum is used as a field type
30+
enum Gender {
31+
MALE = 1,
32+
FEMALE = 2,
33+
OTHER = 3
34+
}
35+
36+
// This enum is NOT used and should be trimmed
37+
enum UnusedPriority {
38+
LOW = 1,
39+
MEDIUM = 2,
40+
HIGH = 3
41+
}
42+
43+
// This enum is used in service method
44+
enum ResponseCode {
45+
SUCCESS = 0,
46+
ERROR = 1,
47+
TIMEOUT = 2
48+
}
49+
50+
struct Person {
51+
1: string name
52+
2: Status status
53+
3: Gender gender
54+
}
55+
56+
service TestService {
57+
ResponseCode getStatus(1: string id)
58+
Person getPerson(1: string id)
59+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2025 CloudWeGo Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
include "base.thrift"
16+
17+
typedef base.Country Location
18+
19+
struct Account {
20+
1: string username
21+
2: base.AccountType type
22+
3: Location location
23+
}
24+
25+
service AccountService {
26+
Account getAccount(1: string id)
27+
}

tool/trimmer/trim/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type YamlArguments struct {
3131
DisablePreserveComment *bool `yaml:"disable_preserve_comment,omitempty"`
3232
MatchGoName *bool `yaml:"match_go_name,omitempty"`
3333
PreservedFiles []string `yaml:"preserved_files,omitempty"`
34+
TrimEnums *bool `yaml:"trim_enums,omitempty"`
3435
}
3536

3637
func ParseYamlConfig(path string) *YamlArguments {

tool/trimmer/trim/traversal.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,15 @@ func (t *Trimmer) traversal(ast *parser.Thrift, filename string) {
8888
}
8989

9090
t.structsTrimmed -= len(ast.Structs) + len(ast.Includes) + len(ast.Services) + len(ast.Unions) + len(ast.Exceptions)
91+
if t.trimEnums {
92+
listEnum := make([]*parser.Enum, 0, len(ast.Enums))
93+
for i := range ast.Enums {
94+
_, ok := currentMap[ast.Enums[i]]
95+
if ok {
96+
listEnum = append(listEnum, ast.Enums[i])
97+
}
98+
}
99+
ast.Enums = listEnum
100+
t.structsTrimmed -= len(ast.Enums)
101+
}
91102
}

tool/trimmer/trim/trimmer.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type Trimmer struct {
4343
forceTrimming bool
4444
preservedStructsMap map[string]struct{}
4545
disablePreserveComment bool
46+
trimEnums bool
4647
structsTrimmed int
4748
fieldsTrimmed int
4849
extServices []*parser.Service
@@ -60,6 +61,7 @@ type TrimASTArg struct {
6061
PreserveStructs []string
6162
PreservedFiles []string
6263
IncludeDirs []string
64+
TrimEnums *bool
6365
}
6466

6567
type TrimResultInfo struct {
@@ -106,6 +108,9 @@ func TrimAST(arg *TrimASTArg) (trimResultInfo *TrimResultInfo, err error) {
106108
if arg.DisablePreserveComment == nil && cfg.DisablePreserveComment != nil {
107109
arg.DisablePreserveComment = cfg.DisablePreserveComment
108110
}
111+
if arg.TrimEnums == nil && cfg.TrimEnums != nil {
112+
arg.TrimEnums = cfg.TrimEnums
113+
}
109114
if len(preservedStructs) == 0 {
110115
preservedStructs = cfg.PreservedStructs
111116
}
@@ -127,11 +132,15 @@ func TrimAST(arg *TrimASTArg) (trimResultInfo *TrimResultInfo, err error) {
127132
if arg.DisablePreserveComment != nil {
128133
disablePreserveComment = *arg.DisablePreserveComment
129134
}
130-
return doTrimAST(arg.Ast, arg.TrimMethods, forceTrim, matchGoName, disablePreserveComment, preservedStructs, preservedFiles)
135+
trimEnums := false
136+
if arg.TrimEnums != nil {
137+
trimEnums = *arg.TrimEnums
138+
}
139+
return doTrimAST(arg.Ast, arg.TrimMethods, forceTrim, matchGoName, disablePreserveComment, trimEnums, preservedStructs, preservedFiles)
131140
}
132141

133142
// doTrimAST trim the single AST, pass method names if -m specified
134-
func doTrimAST(ast *parser.Thrift, trimMethods []string, forceTrimming, matchGoName, disablePreserveComment bool, preservedStructs, preserveFiles []string) (
143+
func doTrimAST(ast *parser.Thrift, trimMethods []string, forceTrimming, matchGoName, disablePreserveComment, trimEnums bool, preservedStructs, preserveFiles []string) (
135144
trimResultInfo *TrimResultInfo, err error) {
136145
trimmer, err := newTrimmer(nil, "")
137146
if err != nil {
@@ -142,6 +151,7 @@ func doTrimAST(ast *parser.Thrift, trimMethods []string, forceTrimming, matchGoN
142151
trimmer.trimMethodValid = make([]bool, len(trimMethods))
143152
trimmer.forceTrimming = forceTrimming
144153
trimmer.matchGoName = matchGoName
154+
trimmer.trimEnums = trimEnums
145155
if len(ast.Services) > 0 {
146156
for i, method := range trimMethods {
147157
parts := strings.Split(method, ".")
@@ -229,6 +239,9 @@ func Trim(files, includeDir []string, outDir string) error {
229239

230240
func (t *Trimmer) countStructs(ast *parser.Thrift) {
231241
t.structsTrimmed += len(ast.Structs) + len(ast.Includes) + len(ast.Services) + len(ast.Unions) + len(ast.Exceptions)
242+
if t.trimEnums {
243+
t.structsTrimmed += len(ast.Enums)
244+
}
232245
for _, v := range ast.Structs {
233246
t.fieldsTrimmed += len(v.Fields)
234247
}

0 commit comments

Comments
 (0)