config.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package generators
  14. import (
  15. "fmt"
  16. "path/filepath"
  17. "k8s.io/gengo/args"
  18. "k8s.io/gengo/generator"
  19. "k8s.io/gengo/namer"
  20. "k8s.io/gengo/types"
  21. "k8s.io/klog"
  22. generatorargs "k8s.io/kube-openapi/cmd/openapi-gen/args"
  23. )
  24. type identityNamer struct{}
  25. func (_ identityNamer) Name(t *types.Type) string {
  26. return t.Name.String()
  27. }
  28. var _ namer.Namer = identityNamer{}
  29. // NameSystems returns the name system used by the generators in this package.
  30. func NameSystems() namer.NameSystems {
  31. return namer.NameSystems{
  32. "raw": namer.NewRawNamer("", nil),
  33. "sorting_namer": identityNamer{},
  34. }
  35. }
  36. // DefaultNameSystem returns the default name system for ordering the types to be
  37. // processed by the generators in this package.
  38. func DefaultNameSystem() string {
  39. return "sorting_namer"
  40. }
  41. func Packages(context *generator.Context, arguments *args.GeneratorArgs) generator.Packages {
  42. boilerplate, err := arguments.LoadGoBoilerplate()
  43. if err != nil {
  44. klog.Fatalf("Failed loading boilerplate: %v", err)
  45. }
  46. header := append([]byte(fmt.Sprintf("// +build !%s\n\n", arguments.GeneratedBuildTag)), boilerplate...)
  47. header = append(header, []byte(
  48. `
  49. // This file was autogenerated by openapi-gen. Do not edit it manually!
  50. `)...)
  51. reportPath := "-"
  52. if customArgs, ok := arguments.CustomArgs.(*generatorargs.CustomArgs); ok {
  53. reportPath = customArgs.ReportFilename
  54. }
  55. context.FileTypes[apiViolationFileType] = apiViolationFile{
  56. unmangledPath: reportPath,
  57. }
  58. return generator.Packages{
  59. &generator.DefaultPackage{
  60. PackageName: filepath.Base(arguments.OutputPackagePath),
  61. PackagePath: arguments.OutputPackagePath,
  62. HeaderText: header,
  63. GeneratorFunc: func(c *generator.Context) (generators []generator.Generator) {
  64. return []generator.Generator{
  65. newOpenAPIGen(
  66. arguments.OutputFileBaseName,
  67. arguments.OutputPackagePath,
  68. ),
  69. newAPIViolationGen(),
  70. }
  71. },
  72. FilterFunc: apiTypeFilterFunc,
  73. },
  74. }
  75. }