Debian Codegraph @

v2/namer/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 namer
18
19 import (
20 "sort"
21
22 "k8s.io/gengo/v2/types"
23 )
24
25 // ImportTracker may be passed to a namer.RawNamer, to track the imports needed
26 // for the types it names.
27 //
28 // TODO: pay attention to the package name (instead of renaming every package).
29 type DefaultImportTracker struct {
30 pathToName map[string]string
31 // forbidden names are in here. (e.g. "go" is a directory in which
32 // there is code, but "go" is not a legal name for a package, so we put
33 // it here to prevent us from naming any package "go")
34 nameToPath map[string]string
35 local types.Name
36
37 // Returns true if a given types is an invalid type and should be ignored.
38 IsInvalidType func(*types.Type) bool
39 // Returns the final local name for the given name
40 LocalName func(types.Name) string
41 // Returns the "import" line for a given (path, name).
42 PrintImport func(string, string) string
43 }
44
45 func NewDefaultImportTracker(local types.Name) DefaultImportTracker {
46 return DefaultImportTracker{
47 pathToName: map[string]string{},
48 nameToPath: map[string]string{},
49 local: local,
50 }
51 }
52
53 func (tracker *DefaultImportTracker) AddTypes(types ...*types.Type) {
54 for _, t := range types {
55 tracker.AddType(t)
56 }
57 }
58 func (tracker *DefaultImportTracker) AddSymbol(symbol types.Name) {
59 if tracker.local.Package == symbol.Package {
60 return
61 }
62
63 if len(symbol.Package) == 0 {
64 return
65 }
66 path := symbol.Path
67 if len(path) == 0 {
68 path = symbol.Package
69 }
70 if _, ok := tracker.pathToName[path]; ok {
71 return
72 }
73
74 name := tracker.LocalName(symbol)
75 tracker.nameToPath[name] = path
76 tracker.pathToName[path] = name
77 }
78
79 func (tracker *DefaultImportTracker) AddType(t *types.Type) {
80 if tracker.local.Package == t.Name.Package {
81 return
82 }
83
84 if tracker.IsInvalidType(t) {
85 if t.Kind == types.Builtin {
86 return
87 }
88 if _, ok := tracker.nameToPath[t.Name.Package]; !ok {
89 tracker.nameToPath[t.Name.Package] = ""
90 }
91 return
92 }
93
94 tracker.AddSymbol(t.Name)
95 }
96
97 func (tracker *DefaultImportTracker) ImportLines() []string {
98 importPaths := []string{}
99 for path := range tracker.pathToName {
100 importPaths = append(importPaths, path)
101 }
102 sort.Strings(importPaths)
103 out := []string{}
104 for _, path := range importPaths {
105 out = append(out, tracker.PrintImport(path, tracker.pathToName[path]))
106 }
107 return out
108 }
109
110 // LocalNameOf returns the name you would use to refer to the package at the
111 // specified path within the body of a file.
112 func (tracker *DefaultImportTracker) LocalNameOf(path string) string {
113 return tracker.pathToName[path]
114 }
115
116 // PathOf returns the path that a given localName is referring to within the
117 // body of a file.
118 func (tracker *DefaultImportTracker) PathOf(localName string) (string, bool) {
119 name, ok := tracker.nameToPath[localName]
120 return name, ok
121 }