Create our own comment parser for codegen (#1277)

* Adapt our own comment parser

* Use our new comment parser to extract tags

* Update codegen/cmd/injection-gen/generators/comment_parser.go

Co-authored-by: Victor Agababov <vagababov@gmail.com>

* Update codegen/cmd/injection-gen/generators/comment_parser_test.go

Co-authored-by: Victor Agababov <vagababov@gmail.com>

* Update codegen/cmd/injection-gen/generators/comment_parser.go

Co-authored-by: Victor Agababov <vagababov@gmail.com>

* Update codegen/cmd/injection-gen/generators/comment_parser.go

Co-authored-by: Victor Agababov <vagababov@gmail.com>

* Update codegen/cmd/injection-gen/generators/comment_parser_test.go

Co-authored-by: Victor Agababov <vagababov@gmail.com>

* Update codegen/cmd/injection-gen/generators/packages.go

Co-authored-by: Victor Agababov <vagababov@gmail.com>

* suggestion didn't compile

* Update codegen/cmd/injection-gen/generators/comment_parser.go

Co-authored-by: Victor Agababov <vagababov@gmail.com>

* trimspace

* include test for ,,, case

* add empty check

* fix comment

Co-authored-by: Victor Agababov <vagababov@gmail.com>
This commit is contained in:
Weston Haught 2020-05-04 09:42:43 -07:00 committed by GitHub
parent f591fc672a
commit 10ba977860
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 180 additions and 15 deletions

View File

@ -0,0 +1,75 @@
/*
Copyright 2020 The Knative Authors.
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 "strings"
// Adapted from the k8s.io comment parser https://github.com/kubernetes/gengo/blob/master/types/comments.go
// ExtractCommentTags parses comments for lines of the form:
//
// 'marker' + ':' "key=value,key2=value2".
//
// Values are optional; empty map is the default. A tag can be specified more than
// one time and all values are returned. If the resulting map has an entry for
// a key, the value (a slice) is guaranteed to have at least 1 element.
//
// Example: if you pass "+" for 'marker', and the following lines are in
// the comments:
// +foo:key=value1,key2=value2
// +bar
//
// Then this function will return:
// map[string]map[string]string{"foo":{"key":value1","key2":"value2"}, "bar": nil}
//
// Users are not expected to repeat values.
func ExtractCommentTags(marker string, lines []string) map[string]map[string]string {
out := map[string]map[string]string{}
for _, line := range lines {
line = strings.TrimSpace(line)
if len(line) == 0 || !strings.HasPrefix(line, marker) {
continue
}
options := strings.SplitN(line[len(marker):], ":", 2)
if len(options) == 2 {
vals := strings.Split(options[1], ",")
opts := out[options[0]]
if opts == nil {
opts = make(map[string]string, len(vals))
}
for _, pair := range vals {
if kv := strings.SplitN(pair, "=", 2); len(kv) == 2 {
opts[kv[0]] = kv[1]
} else if kv[0] != "" {
opts[kv[0]] = ""
}
}
if len(opts) == 0 {
out[options[0]] = nil
} else {
out[options[0]] = opts
}
} else if len(options) == 1 && options[0] != "" {
if _, has := out[options[0]]; !has {
out[options[0]] = nil
}
}
}
return out
}

View File

@ -0,0 +1,84 @@
/*
Copyright 2020 The Knative Authors.
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 "testing"
func TestParseComments(t *testing.T) {
comment := []string{
"This is an example comment to parse",
"",
" +foo",
"+bar",
"+with:option",
"+pair:key=value",
"+manypairs:key1=value1,key2=value2",
}
extracted := ExtractCommentTags("+", comment)
if val, ok := extracted["foo"]; !ok || val != nil {
t.Errorf("Failed to extract single key got=%t,%v want=true,nil", ok, val)
}
if val, ok := extracted["bar"]; !ok || val != nil {
t.Errorf("Failed to extract single key got=%t,%v want=true,nil", ok, val)
}
if val, ok := extracted["with"]; !ok || val["option"] != "" {
t.Errorf("Failed to extract single key got=%t,%v want=true,{\"option\":\"\"}", ok, val)
}
if val, ok := extracted["pair"]; !ok || val["key"] != "value" {
t.Errorf("Failed to extract single key got=%t,%v want=true,{\"key\":\"value\"}", ok, val)
}
if val, ok := extracted["manypairs"]; !ok || val["key1"] != "value1" || val["key2"] != "value2" {
t.Errorf("Failed to extract single key got=%t,%v want=true,{\"key\":\"value\"}", ok, val)
}
}
func TestMergeDuplicates(t *testing.T) {
comment := []string{
"This is an example comment to parse",
"",
"+foo",
" +foo",
"+bar:key=value",
"+bar",
"+manypairs:key1=value1",
"+manypairs:key2=value2",
"+oops:,,,",
}
extracted := ExtractCommentTags("+", comment)
if val, ok := extracted["foo"]; !ok || val != nil {
t.Errorf("Failed to extract single key got=%t,%v want=true,nil", ok, val)
}
if val, ok := extracted["bar"]; !ok || val["key"] != "value" {
t.Errorf("Failed to extract single key got=%t,%v want=true,{\"key\":\"value\"}", ok, val)
}
if val, ok := extracted["manypairs"]; !ok || val["key1"] != "value1" || val["key2"] != "value2" {
t.Errorf("Failed to extract single key got=%t,%v want=true,{\"key\":\"value\"}", ok, val)
}
if val, ok := extracted["oops"]; !ok || val != nil {
t.Errorf("Failed to extract single key got=%t,%v want=true,{\"oops\":nil}", ok, val)
}
}

View File

@ -188,22 +188,27 @@ func MustParseClientGenTags(lines []string) Tags {
return ret
}
func extractReconcilerClassTag(t *types.Type) (string, bool) {
func extractCommentTags(t *types.Type) map[string]map[string]string {
comments := append(append([]string{}, t.SecondClosestCommentLines...), t.CommentLines...)
values := types.ExtractCommentTags("+", comments)["genreconciler:class"]
for _, v := range values {
if len(v) == 0 {
continue
}
return v, true
}
return "", false
return ExtractCommentTags("+", comments)
}
func isNonNamespaced(t *types.Type) bool {
comments := append(append([]string{}, t.SecondClosestCommentLines...), t.CommentLines...)
_, nonNamespaced := types.ExtractCommentTags("+", comments)["genclient:nonNamespaced"]
return nonNamespaced
func extractReconcilerClassTag(tags map[string]map[string]string) (classname string, has bool) {
vals, has := tags["genreconciler"]
if !has {
return
}
classname, _ = vals["class"]
return
}
func isNonNamespaced(tags map[string]map[string]string) bool {
vals, has := tags["genclient"]
if !has {
return false
}
_, has = vals["nonNamespaced"]
return has
}
func vendorless(p string) string {
@ -416,8 +421,9 @@ func reconcilerPackages(basePackage string, groupPkgName string, gv clientgentyp
// Fix for golang iterator bug.
t := t
reconcilerClass, hasReconcilerClass := extractReconcilerClassTag(t)
nonNamespaced := isNonNamespaced(t)
extracted := extractCommentTags(t)
reconcilerClass, hasReconcilerClass := extractReconcilerClassTag(extracted)
nonNamespaced := isNonNamespaced(extracted)
packagePath := filepath.Join(packagePath, strings.ToLower(t.Name.Name))