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

     1  /*
     2  Copyright 2019 The Knative 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 duck
    18  
    19  import (
    20  	"io"
    21  
    22  	clientgentypes "k8s.io/code-generator/cmd/client-gen/types"
    23  	"k8s.io/gengo/v2/generator"
    24  	"k8s.io/gengo/v2/namer"
    25  	"k8s.io/gengo/v2/types"
    26  	"k8s.io/klog/v2"
    27  
    28  	gennamer "knative.dev/pkg/codegen/cmd/injection-gen/namer"
    29  )
    30  
    31  // fakeDuckGenerator produces a file of listers for a given GroupVersion and
    32  // type.
    33  type fakeDuckGenerator struct {
    34  	generator.GoGenerator
    35  	outputPackage string
    36  	imports       namer.ImportTracker
    37  
    38  	typeToGenerate   *types.Type
    39  	groupVersion     clientgentypes.GroupVersion
    40  	groupGoName      string
    41  	duckInjectionPkg string
    42  }
    43  
    44  var _ generator.Generator = (*fakeDuckGenerator)(nil)
    45  
    46  func (g *fakeDuckGenerator) Filter(c *generator.Context, t *types.Type) bool {
    47  	// Only process the type for this duck generator.
    48  	return t == g.typeToGenerate
    49  }
    50  
    51  func (g *fakeDuckGenerator) Namers(c *generator.Context) namer.NameSystems {
    52  	publicPluralNamer := &gennamer.ExceptionNamer{
    53  		Exceptions: map[string]string{
    54  			// these exceptions are used to deconflict the generated code
    55  			// you can put your fully qualified package like
    56  			// to generate a name that doesn't conflict with your group.
    57  			// "k8s.io/apis/events/v1beta1.Event": "EventResource"
    58  		},
    59  		KeyFunc: func(t *types.Type) string {
    60  			return t.Name.Package + "." + t.Name.Name
    61  		},
    62  		Delegate: namer.NewPublicPluralNamer(map[string]string{
    63  			"Endpoints": "Endpoints",
    64  		}),
    65  	}
    66  
    67  	return namer.NameSystems{
    68  		"raw":          namer.NewRawNamer(g.outputPackage, g.imports),
    69  		"publicPlural": publicPluralNamer,
    70  	}
    71  }
    72  
    73  func (g *fakeDuckGenerator) Imports(c *generator.Context) (imports []string) {
    74  	imports = append(imports, g.imports.ImportLines()...)
    75  	return imports
    76  }
    77  
    78  func (g *fakeDuckGenerator) GenerateType(c *generator.Context, t *types.Type, w io.Writer) error {
    79  	sw := generator.NewSnippetWriter(w, c, "{{", "}}")
    80  
    81  	klog.V(5).Info("processing type ", t)
    82  
    83  	m := map[string]interface{}{
    84  		"withDuck": c.Universe.Type(types.Name{Package: g.duckInjectionPkg, Name: "WithDuck"}),
    85  		"duckGet":  c.Universe.Function(types.Name{Package: g.duckInjectionPkg, Name: "Get"}),
    86  		"group":    namer.IC(g.groupGoName),
    87  		"type":     t,
    88  		"version":  namer.IC(g.groupVersion.Version.String()),
    89  		"injectionRegisterDuck": c.Universe.Function(types.Name{
    90  			Package: "knative.dev/pkg/injection",
    91  			Name:    "Fake.RegisterDuck",
    92  		}),
    93  	}
    94  
    95  	sw.Do(injectionFakeDuck, m)
    96  
    97  	return sw.Error()
    98  }
    99  
   100  var injectionFakeDuck = `
   101  var Get = {{.duckGet|raw}}
   102  
   103  func init() {
   104  	{{.injectionRegisterDuck|raw}}({{.withDuck|raw}})
   105  }
   106  `