| 1 |
/* |
| 2 |
Copyright 2015 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 generator |
| 18 |
|
| 19 |
import ( |
| 20 |
"go/token" |
| 21 |
"path/filepath" |
| 22 |
"strings" |
| 23 |
|
| 24 |
"k8s.io/klog/v2" |
| 25 |
|
| 26 |
"k8s.io/gengo/v2/namer" |
| 27 |
"k8s.io/gengo/v2/types" |
| 28 |
) |
| 29 |
|
| 30 |
// NewImportTrackerForPackage creates a new import tracker which is aware |
| 31 |
// of a generator's output package. The tracker will not add import lines |
| 32 |
// when symbols or types are added from the same package, and LocalNameOf |
| 33 |
// will return empty string for the output package. |
| 34 |
// |
| 35 |
// e.g.: |
| 36 |
// |
| 37 |
// tracker := NewImportTrackerForPackage("bar.com/pkg/foo") |
| 38 |
// tracker.AddSymbol(types.Name{"bar.com/pkg/foo.MyType"}) |
| 39 |
// tracker.AddSymbol(types.Name{"bar.com/pkg/baz.MyType"}) |
| 40 |
// tracker.AddSymbol(types.Name{"bar.com/pkg/baz/baz.MyType"}) |
| 41 |
// |
| 42 |
// tracker.LocalNameOf("bar.com/pkg/foo") -> "" |
| 43 |
// tracker.LocalNameOf("bar.com/pkg/baz") -> "baz" |
| 44 |
// tracker.LocalNameOf("bar.com/pkg/baz/baz") -> "bazbaz" |
| 45 |
// tracker.ImportLines() -> {`baz "bar.com/pkg/baz"`, `bazbaz "bar.com/pkg/baz/baz"`} |
| 46 |
func NewImportTrackerForPackage(local string, typesToAdd ...*types.Type) *namer.DefaultImportTracker { |
| 47 |
tracker := namer.NewDefaultImportTracker(types.Name{Package: local}) |
| 48 |
tracker.IsInvalidType = func(*types.Type) bool { return false } |
| 49 |
tracker.LocalName = func(name types.Name) string { return goTrackerLocalName(&tracker, local, name) } |
| 50 |
tracker.PrintImport = func(path, name string) string { return name + " \"" + path + "\"" } |
| 51 |
|
| 52 |
tracker.AddTypes(typesToAdd...) |
| 53 |
return &tracker |
| 54 |
} |
| 55 |
|
| 56 |
func NewImportTracker(typesToAdd ...*types.Type) *namer.DefaultImportTracker { |
| 57 |
return NewImportTrackerForPackage("", typesToAdd...) |
| 58 |
} |
| 59 |
|
| 60 |
func goTrackerLocalName(tracker namer.ImportTracker, localPkg string, t types.Name) string { |
| 61 |
path := t.Package |
| 62 |
|
| 63 |
// Using backslashes in package names causes gengo to produce Go code which |
| 64 |
// will not compile with the gc compiler. See the comment on GoSeperator. |
| 65 |
if strings.ContainsRune(path, '\\') { |
| 66 |
klog.Warningf("Warning: backslash used in import path '%v', this is unsupported.\n", path) |
| 67 |
} |
| 68 |
localLeaf := filepath.Base(localPkg) |
| 69 |
|
| 70 |
dirs := strings.Split(path, namer.GoSeperator) |
| 71 |
for n := len(dirs) - 1; n >= 0; n-- { |
| 72 |
// follow kube convention of not having anything between directory names |
| 73 |
name := strings.Join(dirs[n:], "") |
| 74 |
name = strings.ReplaceAll(name, "_", "") |
| 75 |
// These characters commonly appear in import paths for go |
| 76 |
// packages, but aren't legal go names. So we'll sanitize. |
| 77 |
name = strings.ReplaceAll(name, ".", "") |
| 78 |
name = strings.ReplaceAll(name, "-", "") |
| 79 |
if _, found := tracker.PathOf(name); found || name == localLeaf { |
| 80 |
// This name collides with some other package. |
| 81 |
// Or, this name is tne same name as the local package, |
| 82 |
// which we avoid because it can be confusing. For example, |
| 83 |
// if the local package is v1, we to avoid importing |
| 84 |
// another package using the v1 name, and instead import |
| 85 |
// it with a more qualified name, such as metav1. |
| 86 |
continue |
| 87 |
} |
| 88 |
|
| 89 |
// If the import name is a Go keyword, prefix with an underscore. |
| 90 |
if token.Lookup(name).IsKeyword() { |
| 91 |
name = "_" + name |
| 92 |
} |
| 93 |
return name |
| 94 |
} |
| 95 |
panic("can't find import for " + path) |
| 96 |
} |