Debian Codegraph @

generator/import_tracker.go

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