Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2e1490d

Browse filesBrowse files
committed
cleanup: Migrate deprecated function ExtractCommentTags
Signed-off-by: Gavin Lam <gavin.oss@tutamail.com>
1 parent 627ce49 commit 2e1490d
Copy full SHA for 2e1490d

File tree

Expand file treeCollapse file tree

8 files changed

+248
-19
lines changed
Filter options
Expand file treeCollapse file tree

8 files changed

+248
-19
lines changed

‎staging/src/k8s.io/code-generator/cmd/applyconfiguration-gen/generators/openapi.go

Copy file name to clipboardExpand all lines: staging/src/k8s.io/code-generator/cmd/applyconfiguration-gen/generators/openapi.go
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ func newTypeModels(openAPISchemaFilePath string, pkgTypes map[string]*types.Pack
5858
gvkToOpenAPIType := map[clientgentypes.GroupVersionKind]string{}
5959
rootDefs := map[string]spec.Schema{}
6060
for _, p := range pkgTypes {
61-
gv := groupVersion(p)
61+
gv, err := groupVersion(p)
62+
if err != nil {
63+
return nil, fmt.Errorf("failed to parse comments of package %s: %w", p.Name, err)
64+
}
6265
for _, t := range p.Types {
6366
tags := genclientTags(t)
6467
hasApply := tags.HasVerb("apply") || tags.HasVerb("applyStatus")

‎staging/src/k8s.io/code-generator/cmd/applyconfiguration-gen/generators/targets.go

Copy file name to clipboardExpand all lines: staging/src/k8s.io/code-generator/cmd/applyconfiguration-gen/generators/targets.go
+29-10Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"k8s.io/code-generator/cmd/applyconfiguration-gen/args"
3333
"k8s.io/code-generator/cmd/client-gen/generators/util"
3434
clientgentypes "k8s.io/code-generator/cmd/client-gen/types"
35+
genutil "k8s.io/code-generator/pkg/util"
3536
)
3637

3738
const (
@@ -75,7 +76,10 @@ func GetTargets(context *generator.Context, args *args.Args) []generator.Target
7576

7677
var targetList []generator.Target
7778
for pkg, p := range pkgTypes {
78-
gv := groupVersion(p)
79+
gv, err := groupVersion(p)
80+
if err != nil {
81+
klog.Fatalf("Failed to parse comments of package %s: %v", p.Name, err)
82+
}
7983

8084
var toGenerate []applyConfig
8185
for _, t := range p.Types {
@@ -128,7 +132,10 @@ func GetTargets(context *generator.Context, args *args.Args) []generator.Target
128132
Package: path.Clean(p.Path),
129133
})
130134

131-
groupGoNames[groupPackageName] = goName(gv, p)
135+
groupGoNames[groupPackageName], err = goName(gv, p)
136+
if err != nil {
137+
klog.Fatalf("Failed to parse comments of group package %s: %v", groupPackageName, err)
138+
}
132139
applyConfigsForGroupVersion[gv] = toGenerate
133140
groupVersions[groupPackageName] = groupVersionsEntry
134141
}
@@ -231,12 +238,18 @@ func targetForInternal(outputDirBase, outputPkgBase string, boilerplate []byte,
231238
}
232239
}
233240

234-
func goName(gv clientgentypes.GroupVersion, p *types.Package) string {
241+
func goName(gv clientgentypes.GroupVersion, p *types.Package) (string, error) {
235242
goName := namer.IC(strings.Split(gv.Group.NonEmpty(), ".")[0])
236-
if override := gengo.ExtractCommentTags("+", p.Comments)["groupGoName"]; override != nil {
237-
goName = namer.IC(override[0])
243+
override, err := genutil.ExtractCommentTagsWithoutArguments("+", []string{"groupGoName"}, p.Comments)
244+
245+
if err != nil {
246+
return goName, err
247+
}
248+
if values, ok := override["groupGoName"]; ok {
249+
goName = namer.IC(values[0])
238250
}
239-
return goName
251+
252+
return goName, nil
240253
}
241254

242255
func packageTypesForInputs(context *generator.Context, outPkgBase string) map[string]*types.Package {
@@ -259,18 +272,24 @@ func packageTypesForInputs(context *generator.Context, outPkgBase string) map[st
259272
return pkgTypes
260273
}
261274

262-
func groupVersion(p *types.Package) (gv clientgentypes.GroupVersion) {
275+
func groupVersion(p *types.Package) (gv clientgentypes.GroupVersion, err error) {
263276
parts := strings.Split(p.Path, "/")
264277
gv.Group = clientgentypes.Group(parts[len(parts)-2])
265278
gv.Version = clientgentypes.Version(parts[len(parts)-1])
266279

267280
// If there's a comment of the form "// +groupName=somegroup" or
268281
// "// +groupName=somegroup.foo.bar.io", use the first field (somegroup) as the name of the
269282
// group when generating.
270-
if override := gengo.ExtractCommentTags("+", p.Comments)["groupName"]; override != nil {
271-
gv.Group = clientgentypes.Group(override[0])
283+
override, err := genutil.ExtractCommentTagsWithoutArguments("+", []string{"groupName"}, p.Comments)
284+
285+
if err != nil {
286+
return gv, err
272287
}
273-
return gv
288+
if values, ok := override["groupName"]; ok {
289+
gv.Group = clientgentypes.Group(values[0])
290+
}
291+
292+
return gv, nil
274293
}
275294

276295
// isInternalPackage returns true if the package is an internal package

‎staging/src/k8s.io/code-generator/cmd/client-gen/generators/generator_for_group.go

Copy file name to clipboardExpand all lines: staging/src/k8s.io/code-generator/cmd/client-gen/generators/generator_for_group.go
+7-3Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"io"
2121
"path"
2222

23-
"k8s.io/gengo/v2"
23+
genutil "k8s.io/code-generator/pkg/util"
2424
"k8s.io/gengo/v2/generator"
2525
"k8s.io/gengo/v2/namer"
2626
"k8s.io/gengo/v2/types"
@@ -73,8 +73,12 @@ func (g *genGroup) GenerateType(c *generator.Context, t *types.Type, w io.Writer
7373
// allow user to define a group name that's different from the one parsed from the directory.
7474
p := c.Universe.Package(g.inputPackage)
7575
groupName := g.group
76-
if override := gengo.ExtractCommentTags("+", p.Comments)["groupName"]; override != nil {
77-
groupName = override[0]
76+
override, err := genutil.ExtractCommentTagsWithoutArguments("+", []string{"groupName"}, p.Comments)
77+
if err != nil {
78+
return err
79+
}
80+
if values, ok := override["groupName"]; ok {
81+
groupName = values[0]
7882
}
7983

8084
apiPath := `"` + g.apiPath + `"`

‎staging/src/k8s.io/code-generator/cmd/client-gen/generators/util/tags.go

Copy file name to clipboardExpand all lines: staging/src/k8s.io/code-generator/cmd/client-gen/generators/util/tags.go
+6-3Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"fmt"
2222
"strings"
2323

24-
"k8s.io/gengo/v2"
24+
genutil "k8s.io/code-generator/pkg/util"
2525
)
2626

2727
var supportedTags = []string{
@@ -192,7 +192,11 @@ func MustParseClientGenTags(lines []string) Tags {
192192
// tags are provided.
193193
func ParseClientGenTags(lines []string) (Tags, error) {
194194
ret := Tags{}
195-
values := gengo.ExtractCommentTags("+", lines)
195+
values, err := genutil.ExtractCommentTagsWithoutArguments("+", nil, lines)
196+
if err != nil {
197+
return ret, fmt.Errorf("failed to parse comments: %w", err)
198+
}
199+
196200
var value []string
197201
value, ret.GenerateClient = values["genclient"]
198202
// Check the old format and error when used to avoid generating client when //+genclient=false
@@ -242,7 +246,6 @@ func ParseClientGenTags(lines []string) (Tags, error) {
242246
}
243247
ret.SkipVerbs = skipVerbs
244248
}
245-
var err error
246249
if ret.Extensions, err = parseClientExtensions(values); err != nil {
247250
return ret, err
248251
}

‎staging/src/k8s.io/code-generator/cmd/deepcopy-gen/generators/deepcopy.go

Copy file name to clipboardExpand all lines: staging/src/k8s.io/code-generator/cmd/deepcopy-gen/generators/deepcopy.go
+7-1Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"strings"
2525

2626
"k8s.io/code-generator/cmd/deepcopy-gen/args"
27+
genutil "k8s.io/code-generator/pkg/util"
2728
"k8s.io/gengo/v2"
2829
"k8s.io/gengo/v2/generator"
2930
"k8s.io/gengo/v2/namer"
@@ -469,7 +470,12 @@ func extractInterfacesTag(t *types.Type) []string {
469470

470471
func extractNonPointerInterfaces(t *types.Type) (bool, error) {
471472
comments := append(append([]string{}, t.SecondClosestCommentLines...), t.CommentLines...)
472-
values := gengo.ExtractCommentTags("+", comments)[interfacesNonPointerTagName]
473+
tags, err := genutil.ExtractCommentTagsWithoutArguments("+", []string{interfacesNonPointerTagName}, comments)
474+
if err != nil {
475+
return false, fmt.Errorf("failed to parse comments: %w", err)
476+
}
477+
478+
values := tags[interfacesNonPointerTagName]
473479
if len(values) == 0 {
474480
return false, nil
475481
}

‎staging/src/k8s.io/code-generator/cmd/prerelease-lifecycle-gen/prerelease-lifecycle-generators/status.go

Copy file name to clipboardExpand all lines: staging/src/k8s.io/code-generator/cmd/prerelease-lifecycle-gen/prerelease-lifecycle-generators/status.go
+6-1Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"strings"
2626

2727
"k8s.io/code-generator/cmd/prerelease-lifecycle-gen/args"
28+
genutil "k8s.io/code-generator/pkg/util"
2829
"k8s.io/gengo/v2"
2930
"k8s.io/gengo/v2/generator"
3031
"k8s.io/gengo/v2/namer"
@@ -98,7 +99,11 @@ func extractRemovedTag(t *types.Type) (*tagValue, int, int, error) {
9899
func extractReplacementTag(t *types.Type) (group, version, kind string, hasReplacement bool, err error) {
99100
comments := append(append([]string{}, t.SecondClosestCommentLines...), t.CommentLines...)
100101

101-
tagVals := gengo.ExtractCommentTags("+", comments)[replacementTagName]
102+
tags, err := genutil.ExtractCommentTagsWithoutArguments("+", []string{replacementTagName}, comments)
103+
if err != nil {
104+
return "", "", "", false, fmt.Errorf("failed to parse comments: %w", err)
105+
}
106+
tagVals := tags[replacementTagName]
102107
if len(tagVals) == 0 {
103108
// No match for the tag.
104109
return "", "", "", false, nil
+88Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package util
18+
19+
import (
20+
"k8s.io/gengo/v2"
21+
)
22+
23+
// ExtractCommentTagsWithoutArguments parses comments for special metadata tags. The
24+
// marker argument should be unique enough to identify the tags needed, and
25+
// should not be a marker for tags you don't want, or else the caller takes
26+
// responsibility for making that distinction.
27+
//
28+
// The tagNames argument is a list of specific tags being extracted. If this is
29+
// nil or empty, all lines which match the marker are considered. If this is
30+
// specified, only lines with begin with marker + one of the tags will be
31+
// considered. This is useful when a common marker is used which may match
32+
// lines which fail this syntax (e.g. which predate this definition).
33+
//
34+
// This function looks for input lines of the following forms:
35+
// - 'marker' + "key=value"
36+
// - 'marker' + "key()=value"
37+
// - 'marker' + "key(arg)=value"
38+
//
39+
// The arg is optional. This function only consider tags with no arguments specified
40+
// (either as "key=value" or as // "key()=value").
41+
//
42+
// The value is optional. If not specified, the resulting Tag will have "" as
43+
// the value.
44+
//
45+
// Tag comment-lines may have a trailing end-of-line comment.
46+
//
47+
// The map returned here is keyed by the Tag's name without args.
48+
//
49+
// A tag can be specified more than one time and all values are returned. If
50+
// the resulting map has an entry for a key, the value (a slice) is guaranteed
51+
// to have at least 1 element.
52+
//
53+
// Example: if you pass "+" for 'marker', and the following lines are in
54+
// the comments:
55+
//
56+
// +foo=val1 // foo
57+
// +bar
58+
// +foo=val2 // also foo
59+
// +foo()=val3 // still foo
60+
// +baz="qux"
61+
// +foo(arg) // tags with arguments are ignored
62+
//
63+
// Then this function will return:
64+
//
65+
// map[string][]string{"foo":{"val1", "val2", "val3"}, "bar": {""}, "baz": {`"qux"`}}
66+
func ExtractCommentTagsWithoutArguments(marker string, tagNames []string, lines []string) (map[string][]string, error) {
67+
functionStyleTags, err := gengo.ExtractFunctionStyleCommentTags(marker, tagNames, lines)
68+
if err != nil {
69+
return nil, err
70+
}
71+
72+
out := make(map[string][]string)
73+
for tagName, tags := range functionStyleTags {
74+
values := make([]string, 0)
75+
76+
for _, tag := range tags {
77+
if tag.Args == nil {
78+
values = append(values, tag.Value)
79+
}
80+
}
81+
82+
if len(values) > 0 {
83+
out[tagName] = values
84+
}
85+
}
86+
87+
return out, nil
88+
}
+101Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package util
18+
19+
import (
20+
"errors"
21+
"reflect"
22+
"testing"
23+
24+
"github.com/google/go-cmp/cmp"
25+
)
26+
27+
func TestExtractCommentTagsWithoutArguments(t *testing.T) {
28+
cases := []struct {
29+
tagNames []string
30+
lines []string
31+
expected map[string][]string
32+
expectedErr error
33+
}{{
34+
tagNames: nil,
35+
lines: []string{
36+
"Human comment that is ignored.",
37+
"+foo=value1",
38+
"+bar",
39+
"+foo=value2",
40+
"+foo()=value3",
41+
"+foo(arg)=value4",
42+
"+baz=qux,zrb=true",
43+
"+bip=\"value3\"",
44+
"+bap(arg)",
45+
"+",
46+
"+=",
47+
"+=a",
48+
"+()=b",
49+
"+(arg)=c",
50+
},
51+
expected: map[string][]string{
52+
"foo": {"value1", "value2", "value3"},
53+
"bar": {""},
54+
"baz": {"qux,zrb=true"},
55+
"bip": {`"value3"`},
56+
},
57+
expectedErr: nil,
58+
}, {
59+
tagNames: nil,
60+
lines: []string{"+tag(arg1,arg2)"},
61+
expected: nil,
62+
expectedErr: errors.New(`failed to parse tag args: multiple arguments are not supported: "arg1,arg2)"`),
63+
}, {
64+
tagNames: []string{"foo", "bar"},
65+
lines: []string{
66+
"+foo=value1",
67+
"+bar",
68+
"+foo=value2",
69+
"+foo()=value3",
70+
"+foo(arg)=value4",
71+
"+baz=qux,zrb=true",
72+
},
73+
expected: map[string][]string{
74+
"foo": {"value1", "value2", "value3"},
75+
"bar": {""},
76+
},
77+
expectedErr: nil,
78+
}, {
79+
tagNames: []string{"lorem"},
80+
lines: []string{
81+
"+foo=value1",
82+
"+bar",
83+
},
84+
expected: map[string][]string{},
85+
expectedErr: nil,
86+
}}
87+
88+
for _, tc := range cases {
89+
values, err := ExtractCommentTagsWithoutArguments("+", tc.tagNames, tc.lines)
90+
91+
if tc.expectedErr == nil && err != nil {
92+
t.Errorf("Failed to parse comments: %v", err)
93+
}
94+
if tc.expectedErr != nil && tc.expectedErr.Error() != err.Error() {
95+
t.Errorf("Expectng error %v, got %v", tc.expectedErr.Error(), err.Error())
96+
}
97+
if !reflect.DeepEqual(tc.expected, values) {
98+
t.Errorf("Wrong result:\n%v", cmp.Diff(tc.expected, values))
99+
}
100+
}
101+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.