| 1 | /* |
| 2 | Copyright 2018 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 generators |
| 18 | |
| 19 | import ( |
| 20 | "path" |
| 21 | |
| 22 | "k8s.io/gengo/v2" |
| 23 | "k8s.io/gengo/v2/generator" |
| 24 | "k8s.io/gengo/v2/namer" |
| 25 | "k8s.io/gengo/v2/types" |
| 26 | "k8s.io/klog/v2" |
| 27 | "k8s.io/kube-openapi/cmd/openapi-gen/args" |
| 28 | ) |
| 29 | |
| 30 | type identityNamer struct{} |
| 31 | |
| 32 | func (_ identityNamer) Name(t *types.Type) string { |
| 33 | return t.Name.String() |
| 34 | } |
| 35 | |
| 36 | var _ namer.Namer = identityNamer{} |
| 37 | |
| 38 | // NameSystems returns the name system used by the generators in this package. |
| 39 | func NameSystems() namer.NameSystems { |
| 40 | return namer.NameSystems{ |
| 41 | "raw": namer.NewRawNamer("", nil), |
| 42 | "sorting_namer": identityNamer{}, |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | // DefaultNameSystem returns the default name system for ordering the types to be |
| 47 | // processed by the generators in this package. |
| 48 | func DefaultNameSystem() string { |
| 49 | return "sorting_namer" |
| 50 | } |
| 51 | |
| 52 | func GetTargets(context *generator.Context, args *args.Args) []generator.Target { |
| 53 | boilerplate, err := gengo.GoBoilerplate(args.GoHeaderFile, gengo.StdBuildTag, gengo.StdGeneratedBy) |
| 54 | if err != nil { |
| 55 | klog.Fatalf("Failed loading boilerplate: %v", err) |
| 56 | } |
| 57 | |
| 58 | reportPath := "-" |
| 59 | if args.ReportFilename != "" { |
| 60 | reportPath = args.ReportFilename |
| 61 | } |
| 62 | context.FileTypes[apiViolationFileType] = apiViolationFile{ |
| 63 | unmangledPath: reportPath, |
| 64 | } |
| 65 | |
| 66 | return []generator.Target{ |
| 67 | &generator.SimpleTarget{ |
| 68 | PkgName: path.Base(args.OutputPkg), // `path` vs. `filepath` because packages use '/' |
| 69 | PkgPath: args.OutputPkg, |
| 70 | PkgDir: args.OutputDir, |
| 71 | HeaderComment: boilerplate, |
| 72 | GeneratorsFunc: func(c *generator.Context) (generators []generator.Generator) { |
| 73 | return []generator.Generator{ |
| 74 | newOpenAPIGen( |
| 75 | args.OutputFile, |
| 76 | args.OutputPkg, |
| 77 | ), |
| 78 | newAPIViolationGen(), |
| 79 | } |
| 80 | }, |
| 81 | FilterFunc: apiTypeFilterFunc, |
| 82 | }, |
| 83 | } |
| 84 | } |