Gengo utils moved

This commit is contained in:
Tim Hockin 2024-01-16 22:02:45 -08:00
parent 236ce54aa8
commit cb86010d20
No known key found for this signature in database
14 changed files with 34 additions and 32 deletions

View File

@ -243,7 +243,7 @@ func targetForInternal(outputDirBase, outputPkgBase string, boilerplate []byte,
func goName(gv clientgentypes.GroupVersion, p *types.Package) string { func goName(gv clientgentypes.GroupVersion, p *types.Package) string {
goName := namer.IC(strings.Split(gv.Group.NonEmpty(), ".")[0]) goName := namer.IC(strings.Split(gv.Group.NonEmpty(), ".")[0])
if override := types.ExtractCommentTags("+", p.Comments)["groupGoName"]; override != nil { if override := gengo.ExtractCommentTags("+", p.Comments)["groupGoName"]; override != nil {
goName = namer.IC(override[0]) goName = namer.IC(override[0])
} }
return goName return goName
@ -277,7 +277,7 @@ func groupVersion(p *types.Package) (gv clientgentypes.GroupVersion) {
// If there's a comment of the form "// +groupName=somegroup" or // If there's a comment of the form "// +groupName=somegroup" or
// "// +groupName=somegroup.foo.bar.io", use the first field (somegroup) as the name of the // "// +groupName=somegroup.foo.bar.io", use the first field (somegroup) as the name of the
// group when generating. // group when generating.
if override := types.ExtractCommentTags("+", p.Comments)["groupName"]; override != nil { if override := gengo.ExtractCommentTags("+", p.Comments)["groupName"]; override != nil {
gv.Group = clientgentypes.Group(override[0]) gv.Group = clientgentypes.Group(override[0])
} }
return gv return gv

View File

@ -278,7 +278,7 @@ func applyGroupOverrides(universe types.Universe, args *args.Args) {
changes := make(map[clientgentypes.GroupVersion]clientgentypes.GroupVersion) changes := make(map[clientgentypes.GroupVersion]clientgentypes.GroupVersion)
for gv, inputDir := range args.GroupVersionPackages() { for gv, inputDir := range args.GroupVersionPackages() {
p := universe.Package(inputDir) p := universe.Package(inputDir)
if override := types.ExtractCommentTags("+", p.Comments)["groupName"]; override != nil { if override := gengo.ExtractCommentTags("+", p.Comments)["groupName"]; override != nil {
newGV := clientgentypes.GroupVersion{ newGV := clientgentypes.GroupVersion{
Group: clientgentypes.Group(override[0]), Group: clientgentypes.Group(override[0]),
Version: gv.Version, Version: gv.Version,
@ -361,7 +361,7 @@ func GetTargets(context *generator.Context, args *args.Args) []generator.Target
// If there's a comment of the form "// +groupGoName=SomeUniqueShortName", use that as // If there's a comment of the form "// +groupGoName=SomeUniqueShortName", use that as
// the Go group identifier in CamelCase. It defaults // the Go group identifier in CamelCase. It defaults
groupGoNames[gv] = namer.IC(strings.Split(gv.Group.NonEmpty(), ".")[0]) groupGoNames[gv] = namer.IC(strings.Split(gv.Group.NonEmpty(), ".")[0])
if override := types.ExtractCommentTags("+", p.Comments)["groupGoName"]; override != nil { if override := gengo.ExtractCommentTags("+", p.Comments)["groupGoName"]; override != nil {
groupGoNames[gv] = namer.IC(override[0]) groupGoNames[gv] = namer.IC(override[0])
} }

View File

@ -20,6 +20,7 @@ import (
"io" "io"
"path/filepath" "path/filepath"
"k8s.io/gengo/v2"
"k8s.io/gengo/v2/generator" "k8s.io/gengo/v2/generator"
"k8s.io/gengo/v2/namer" "k8s.io/gengo/v2/namer"
"k8s.io/gengo/v2/types" "k8s.io/gengo/v2/types"
@ -83,7 +84,7 @@ func (g *genGroup) GenerateType(c *generator.Context, t *types.Type, w io.Writer
} }
// allow user to define a group name that's different from the one parsed from the directory. // allow user to define a group name that's different from the one parsed from the directory.
p := c.Universe.Package(g.inputPackage) p := c.Universe.Package(g.inputPackage)
if override := types.ExtractCommentTags("+", p.Comments)["groupName"]; override != nil { if override := gengo.ExtractCommentTags("+", p.Comments)["groupName"]; override != nil {
groupName = override[0] groupName = override[0]
} }

View File

@ -21,7 +21,7 @@ import (
"fmt" "fmt"
"strings" "strings"
"k8s.io/gengo/v2/types" "k8s.io/gengo/v2"
) )
var supportedTags = []string{ var supportedTags = []string{
@ -192,7 +192,7 @@ func MustParseClientGenTags(lines []string) Tags {
// tags are provided. // tags are provided.
func ParseClientGenTags(lines []string) (Tags, error) { func ParseClientGenTags(lines []string) (Tags, error) {
ret := Tags{} ret := Tags{}
values := types.ExtractCommentTags("+", lines) values := gengo.ExtractCommentTags("+", lines)
var value []string var value []string
value, ret.GenerateClient = values["genclient"] value, ret.GenerateClient = values["genclient"]
// Check the old format and error when used to avoid generating client when //+genclient=false // Check the old format and error when used to avoid generating client when //+genclient=false

View File

@ -49,24 +49,24 @@ const (
) )
func extractTag(comments []string) []string { func extractTag(comments []string) []string {
return types.ExtractCommentTags("+", comments)[tagName] return gengo.ExtractCommentTags("+", comments)[tagName]
} }
func extractExplicitFromTag(comments []string) []string { func extractExplicitFromTag(comments []string) []string {
return types.ExtractCommentTags("+", comments)[explicitFromTagName] return gengo.ExtractCommentTags("+", comments)[explicitFromTagName]
} }
func extractExternalTypesTag(comments []string) []string { func extractExternalTypesTag(comments []string) []string {
return types.ExtractCommentTags("+", comments)[externalTypesTagName] return gengo.ExtractCommentTags("+", comments)[externalTypesTagName]
} }
func isCopyOnly(comments []string) bool { func isCopyOnly(comments []string) bool {
values := types.ExtractCommentTags("+", comments)["k8s:conversion-fn"] values := gengo.ExtractCommentTags("+", comments)["k8s:conversion-fn"]
return len(values) == 1 && values[0] == "copy-only" return len(values) == 1 && values[0] == "copy-only"
} }
func isDrop(comments []string) bool { func isDrop(comments []string) bool {
values := types.ExtractCommentTags("+", comments)["k8s:conversion-fn"] values := gengo.ExtractCommentTags("+", comments)["k8s:conversion-fn"]
return len(values) == 1 && values[0] == "drop" return len(values) == 1 && values[0] == "drop"
} }

View File

@ -53,7 +53,7 @@ func extractEnabledTypeTag(t *types.Type) *enabledTagValue {
} }
func extractEnabledTag(comments []string) *enabledTagValue { func extractEnabledTag(comments []string) *enabledTagValue {
tagVals := types.ExtractCommentTags("+", comments)[tagEnabledName] tagVals := gengo.ExtractCommentTags("+", comments)[tagEnabledName]
if tagVals == nil { if tagVals == nil {
// No match for the tag. // No match for the tag.
return nil return nil
@ -454,7 +454,7 @@ func (g *genDeepCopy) needsGeneration(t *types.Type) bool {
func extractInterfacesTag(t *types.Type) []string { func extractInterfacesTag(t *types.Type) []string {
var result []string var result []string
comments := append(append([]string{}, t.SecondClosestCommentLines...), t.CommentLines...) comments := append(append([]string{}, t.SecondClosestCommentLines...), t.CommentLines...)
values := types.ExtractCommentTags("+", comments)[interfacesTagName] values := gengo.ExtractCommentTags("+", comments)[interfacesTagName]
for _, v := range values { for _, v := range values {
if len(v) == 0 { if len(v) == 0 {
continue continue
@ -472,7 +472,7 @@ func extractInterfacesTag(t *types.Type) []string {
func extractNonPointerInterfaces(t *types.Type) (bool, error) { func extractNonPointerInterfaces(t *types.Type) (bool, error) {
comments := append(append([]string{}, t.SecondClosestCommentLines...), t.CommentLines...) comments := append(append([]string{}, t.SecondClosestCommentLines...), t.CommentLines...)
values := types.ExtractCommentTags("+", comments)[interfacesNonPointerTagName] values := gengo.ExtractCommentTags("+", comments)[interfacesNonPointerTagName]
if len(values) == 0 { if len(values) == 0 {
return false, nil return false, nil
} }

View File

@ -65,19 +65,19 @@ const inputTagName = "k8s:defaulter-gen-input"
const defaultTagName = "default" const defaultTagName = "default"
func extractDefaultTag(comments []string) []string { func extractDefaultTag(comments []string) []string {
return types.ExtractCommentTags("+", comments)[defaultTagName] return gengo.ExtractCommentTags("+", comments)[defaultTagName]
} }
func extractTag(comments []string) []string { func extractTag(comments []string) []string {
return types.ExtractCommentTags("+", comments)[tagName] return gengo.ExtractCommentTags("+", comments)[tagName]
} }
func extractInputTag(comments []string) []string { func extractInputTag(comments []string) []string {
return types.ExtractCommentTags("+", comments)[inputTagName] return gengo.ExtractCommentTags("+", comments)[inputTagName]
} }
func checkTag(comments []string, require ...string) bool { func checkTag(comments []string, require ...string) bool {
values := types.ExtractCommentTags("+", comments)[tagName] values := gengo.ExtractCommentTags("+", comments)[tagName]
if len(require) == 0 { if len(require) == 0 {
return len(values) == 1 && values[0] == "" return len(values) == 1 && values[0] == ""
} }

View File

@ -25,11 +25,11 @@ import (
"strconv" "strconv"
"strings" "strings"
"k8s.io/klog/v2" "k8s.io/gengo/v2"
"k8s.io/gengo/v2/generator" "k8s.io/gengo/v2/generator"
"k8s.io/gengo/v2/namer" "k8s.io/gengo/v2/namer"
"k8s.io/gengo/v2/types" "k8s.io/gengo/v2/types"
"k8s.io/klog/v2"
) )
// genProtoIDL produces a .proto IDL. // genProtoIDL produces a .proto IDL.
@ -80,7 +80,7 @@ func (g *genProtoIDL) Namers(c *generator.Context) namer.NameSystems {
// Filter ignores types that are identified as not exportable. // Filter ignores types that are identified as not exportable.
func (g *genProtoIDL) Filter(c *generator.Context, t *types.Type) bool { func (g *genProtoIDL) Filter(c *generator.Context, t *types.Type) bool {
tagVals := types.ExtractCommentTags("+", t.CommentLines)["protobuf"] tagVals := gengo.ExtractCommentTags("+", t.CommentLines)["protobuf"]
if tagVals != nil { if tagVals != nil {
if tagVals[0] == "false" { if tagVals[0] == "false" {
// Type specified "false". // Type specified "false".
@ -308,7 +308,7 @@ func (b bodyGen) doStruct(sw *generator.SnippetWriter) error {
var alias *types.Type var alias *types.Type
var fields []protoField var fields []protoField
options := []string{} options := []string{}
allOptions := types.ExtractCommentTags("+", b.t.CommentLines) allOptions := gengo.ExtractCommentTags("+", b.t.CommentLines)
for k, v := range allOptions { for k, v := range allOptions {
switch { switch {
case strings.HasPrefix(k, "protobuf.options."): case strings.HasPrefix(k, "protobuf.options."):

View File

@ -17,7 +17,7 @@ limitations under the License.
package protobuf package protobuf
import ( import (
"k8s.io/gengo/v2/types" "k8s.io/gengo/v2"
"k8s.io/klog/v2" "k8s.io/klog/v2"
) )
@ -25,7 +25,7 @@ import (
// it exists, the value is boolean. If the tag did not exist, it returns // it exists, the value is boolean. If the tag did not exist, it returns
// false. // false.
func extractBoolTagOrDie(key string, lines []string) bool { func extractBoolTagOrDie(key string, lines []string) bool {
val, err := types.ExtractSingleBoolCommentTag("+", key, false, lines) val, err := gengo.ExtractSingleBoolCommentTag("+", key, false, lines)
if err != nil { if err != nil {
klog.Fatal(err) klog.Fatal(err)
} }

View File

@ -149,14 +149,14 @@ func GetTargets(context *generator.Context, args *args.Args) []generator.Target
// If there's a comment of the form "// +groupName=somegroup" or // If there's a comment of the form "// +groupName=somegroup" or
// "// +groupName=somegroup.foo.bar.io", use the first field (somegroup) as the name of the // "// +groupName=somegroup.foo.bar.io", use the first field (somegroup) as the name of the
// group when generating. // group when generating.
if override := types.ExtractCommentTags("+", p.Comments)["groupName"]; override != nil { if override := gengo.ExtractCommentTags("+", p.Comments)["groupName"]; override != nil {
gv.Group = clientgentypes.Group(override[0]) gv.Group = clientgentypes.Group(override[0])
} }
// If there's a comment of the form "// +groupGoName=SomeUniqueShortName", use that as // If there's a comment of the form "// +groupGoName=SomeUniqueShortName", use that as
// the Go group identifier in CamelCase. It defaults // the Go group identifier in CamelCase. It defaults
groupGoNames[groupPackageName] = namer.IC(strings.Split(gv.Group.NonEmpty(), ".")[0]) groupGoNames[groupPackageName] = namer.IC(strings.Split(gv.Group.NonEmpty(), ".")[0])
if override := types.ExtractCommentTags("+", p.Comments)["groupGoName"]; override != nil { if override := gengo.ExtractCommentTags("+", p.Comments)["groupGoName"]; override != nil {
groupGoNames[groupPackageName] = namer.IC(override[0]) groupGoNames[groupPackageName] = namer.IC(override[0])
} }

View File

@ -100,7 +100,7 @@ func GetTargets(context *generator.Context, args *args.Args) []generator.Target
// If there's a comment of the form "// +groupName=somegroup" or // If there's a comment of the form "// +groupName=somegroup" or
// "// +groupName=somegroup.foo.bar.io", use the first field (somegroup) as the name of the // "// +groupName=somegroup.foo.bar.io", use the first field (somegroup) as the name of the
// group when generating. // group when generating.
if override := types.ExtractCommentTags("+", p.Comments)["groupName"]; override != nil { if override := gengo.ExtractCommentTags("+", p.Comments)["groupName"]; override != nil {
gv.Group = clientgentypes.Group(strings.SplitN(override[0], ".", 2)[0]) gv.Group = clientgentypes.Group(strings.SplitN(override[0], ".", 2)[0])
} }

View File

@ -97,7 +97,7 @@ func extractRemovedTag(t *types.Type) (*tagValue, int, int, error) {
func extractReplacementTag(t *types.Type) (group, version, kind string, hasReplacement bool, err error) { func extractReplacementTag(t *types.Type) (group, version, kind string, hasReplacement bool, err error) {
comments := append(append([]string{}, t.SecondClosestCommentLines...), t.CommentLines...) comments := append(append([]string{}, t.SecondClosestCommentLines...), t.CommentLines...)
tagVals := types.ExtractCommentTags("+", comments)[replacementTagName] tagVals := gengo.ExtractCommentTags("+", comments)[replacementTagName]
if len(tagVals) == 0 { if len(tagVals) == 0 {
// No match for the tag. // No match for the tag.
return "", "", "", false, nil return "", "", "", false, nil
@ -131,7 +131,7 @@ func extractReplacementTag(t *types.Type) (group, version, kind string, hasRepla
} }
func extractTag(tagName string, comments []string) *tagValue { func extractTag(tagName string, comments []string) *tagValue {
tagVals := types.ExtractCommentTags("+", comments)[tagName] tagVals := gengo.ExtractCommentTags("+", comments)[tagName]
if tagVals == nil { if tagVals == nil {
// No match for the tag. // No match for the tag.
return nil return nil

View File

@ -83,7 +83,7 @@ func GetTargets(context *generator.Context, args *args.Args) []generator.Target
// if there is a comment of the form "// +groupName=somegroup" or "// +groupName=somegroup.foo.bar.io", // if there is a comment of the form "// +groupName=somegroup" or "// +groupName=somegroup.foo.bar.io",
// extract the fully qualified API group name from it and overwrite the group inferred from the package path // extract the fully qualified API group name from it and overwrite the group inferred from the package path
if override := types.ExtractCommentTags("+", pkg.Comments)["groupName"]; override != nil { if override := gengo.ExtractCommentTags("+", pkg.Comments)["groupName"]; override != nil {
groupName := override[0] groupName := override[0]
klog.V(5).Infof("overriding the group name with = %s", groupName) klog.V(5).Infof("overriding the group name with = %s", groupName)
gv.Group = clientgentypes.Group(groupName) gv.Group = clientgentypes.Group(groupName)

View File

@ -17,6 +17,7 @@ limitations under the License.
package namer package namer
import ( import (
"k8s.io/gengo/v2"
"k8s.io/gengo/v2/namer" "k8s.io/gengo/v2/namer"
"k8s.io/gengo/v2/types" "k8s.io/gengo/v2/types"
) )
@ -49,7 +50,7 @@ func NewTagOverrideNamer(tagName string, fallback namer.Namer) namer.Namer {
// extractTag gets the comment-tags for the key. If the tag did not exist, it // extractTag gets the comment-tags for the key. If the tag did not exist, it
// returns the empty string. // returns the empty string.
func extractTag(key string, lines []string) string { func extractTag(key string, lines []string) string {
val, present := types.ExtractCommentTags("+", lines)[key] val, present := gengo.ExtractCommentTags("+", lines)[key]
if !present || len(val) < 1 { if !present || len(val) < 1 {
return "" return ""
} }