knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/codegen/cmd/injection-gen/namer/namer.go (about)

     1  /*
     2  Copyright 2016 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 namer
    18  
    19  import (
    20  	"strings"
    21  
    22  	"k8s.io/gengo/v2"
    23  	"k8s.io/gengo/v2/namer"
    24  	"k8s.io/gengo/v2/types"
    25  )
    26  
    27  // TagOverrideNamer is a namer which pulls names from a given tag, if specified,
    28  // and otherwise falls back to a different namer.
    29  type tagOverrideNamer struct {
    30  	tagName  string
    31  	fallback namer.Namer
    32  }
    33  
    34  // Name returns the tag value if it exists. It no tag was found the fallback namer will be used
    35  func (n *tagOverrideNamer) Name(t *types.Type) string {
    36  	nameOverride := extractTag(n.tagName, append(t.SecondClosestCommentLines, t.CommentLines...))
    37  	if nameOverride != "" {
    38  		return nameOverride
    39  	}
    40  
    41  	return n.fallback.Name(t)
    42  }
    43  
    44  // NewTagOverrideNamer creates a namer.Namer which uses the contents of the given tag as
    45  // the name, or falls back to another Namer if the tag is not present.
    46  func newTagOverrideNamer(tagName string, fallback namer.Namer) namer.Namer {
    47  	return &tagOverrideNamer{
    48  		tagName:  tagName,
    49  		fallback: fallback,
    50  	}
    51  }
    52  
    53  // extractTag gets the comment-tags for the key.  If the tag did not exist, it
    54  // returns the empty string.
    55  func extractTag(key string, lines []string) string {
    56  	tags, err := gengo.ExtractFunctionStyleCommentTags("+", nil, lines)
    57  	if err != nil {
    58  		panic(err)
    59  	}
    60  
    61  	val, present := tags[key]
    62  	if !present || len(val) < 1 {
    63  		return ""
    64  	}
    65  
    66  	return val[0].Value
    67  }
    68  
    69  type versionedClientsetNamer struct {
    70  	public *ExceptionNamer
    71  }
    72  
    73  func (r *versionedClientsetNamer) Name(t *types.Type) string {
    74  	// Turns type into a GroupVersion type string based on package.
    75  	parts := strings.Split(t.Name.Package, "/")
    76  	group := parts[len(parts)-2]
    77  	version := parts[len(parts)-1]
    78  
    79  	g := r.public.Name(&types.Type{Name: types.Name{Name: group, Package: t.Name.Package}})
    80  	v := r.public.Name(&types.Type{Name: types.Name{Name: version, Package: t.Name.Package}})
    81  
    82  	return g + v
    83  }
    84  
    85  // ExceptionNamer allows you specify exceptional cases with exact names.  This allows you to have control
    86  // for handling various conflicts, like group and resource names for instance.
    87  type ExceptionNamer struct {
    88  	Exceptions map[string]string
    89  	KeyFunc    func(*types.Type) string
    90  
    91  	Delegate namer.Namer
    92  }
    93  
    94  // Name provides the requested name for a type.
    95  func (n *ExceptionNamer) Name(t *types.Type) string {
    96  	key := n.KeyFunc(t)
    97  	if exception, ok := n.Exceptions[key]; ok {
    98  		return exception
    99  	}
   100  	return n.Delegate.Name(t)
   101  }
   102  
   103  // lowercaseSingularNamer implements Namer
   104  type lowercaseSingularNamer struct{}
   105  
   106  // Name returns t's name in all lowercase.
   107  func (n *lowercaseSingularNamer) Name(t *types.Type) string {
   108  	return strings.ToLower(t.Name.Name)
   109  }