-
Notifications
You must be signed in to change notification settings - Fork 331
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
100 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
Copyright 2016 The Kubernetes Authors. | ||
Check failure on line 2 in codegen/cmd/injection-gen/generators/namer.go GitHub Actions / style / Golang / Boilerplate Check (go)
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package generators | ||
|
||
import ( | ||
"k8s.io/gengo/namer" | ||
"k8s.io/gengo/types" | ||
) | ||
|
||
// TagOverrideNamer is a namer which pulls names from a given tag, if specified, | ||
// and otherwise falls back to a different namer. | ||
type tagOverrideNamer struct { | ||
tagName string | ||
fallback namer.Namer | ||
} | ||
|
||
// Name returns the tag value if it exists. It no tag was found the fallback namer will be used | ||
func (n *tagOverrideNamer) Name(t *types.Type) string { | ||
if nameOverride := extractTag(n.tagName, append(t.SecondClosestCommentLines, t.CommentLines...)); nameOverride != "" { | ||
return nameOverride | ||
} | ||
|
||
return n.fallback.Name(t) | ||
} | ||
|
||
// NewTagOverrideNamer creates a namer.Namer which uses the contents of the given tag as | ||
// the name, or falls back to another Namer if the tag is not present. | ||
func newTagOverrideNamer(tagName string, fallback namer.Namer) namer.Namer { | ||
return &tagOverrideNamer{ | ||
tagName: tagName, | ||
fallback: fallback, | ||
} | ||
} | ||
|
||
// extractTag gets the comment-tags for the key. If the tag did not exist, it | ||
// returns the empty string. | ||
func extractTag(key string, lines []string) string { | ||
val, present := types.ExtractCommentTags("+", lines)[key] | ||
if !present || len(val) < 1 { | ||
return "" | ||
} | ||
|
||
return val[0] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters