/
resolver.go
133 lines (121 loc) · 4.48 KB
/
resolver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright The OpenTelemetry 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 configschema // import "github.com/open-telemetry/opentelemetry-collector-contrib/lib/configschema"
import (
"fmt"
"go/build"
"os"
"path/filepath"
"reflect"
"strings"
"golang.org/x/mod/modfile"
"golang.org/x/mod/module"
)
// dirResolver is used to resolve the base directory of a given reflect.Type.
type dirResolver struct {
srcRoot string
moduleName string
}
// typeToPackagePath accepts a Type and returns the filesystem path. If the
// path does not exist in the current project, returns location in GOPATH.
func (dr dirResolver) typeToPackagePath(t reflect.Type) (string, error) {
pkgPath := t.PkgPath()
if strings.HasPrefix(pkgPath, dr.moduleName) {
return dr.packagePathToVerifiedProjectPath(strings.TrimPrefix(pkgPath, dr.moduleName))
}
verifiedGoPath, err := dr.packagePathToVerifiedGoPath(pkgPath)
if err != nil {
return "", fmt.Errorf("could not find the pkg %q: %w", pkgPath, err)
}
return verifiedGoPath, nil
}
// reflectValueToProjectPath accepts a reflect.Value and returns its directory in the current project. If
// the type doesn't live in the current project, returns "".
func (dr dirResolver) reflectValueToProjectPath(v reflect.Value) string {
t := v.Type().Elem()
if !strings.HasPrefix(t.PkgPath(), dr.moduleName) {
return ""
}
trimmed := strings.TrimPrefix(t.PkgPath(), dr.moduleName+"/")
dir, err := dr.packagePathToVerifiedProjectPath(trimmed)
if err != nil {
return ""
}
return dir
}
func (dr dirResolver) packagePathToVerifiedProjectPath(packagePath string) (string, error) {
dir := dr.packagePathToProjectPath(packagePath)
_, err := os.ReadDir(dir)
return dir, err
}
// packagePathToProjectPath returns the path to a package in the local project.
func (dr dirResolver) packagePathToProjectPath(packagePath string) string {
return filepath.Join(dr.srcRoot, packagePath)
}
func (dr dirResolver) packagePathToVerifiedGoPath(packagePath string) (string, error) {
dir, err := dr.packagePathToGoPath(packagePath)
if err != nil {
return "", err
}
_, err = os.ReadDir(dir)
return dir, err
}
// packagePathToGoPath accepts a package path (e.g.
// "go.opentelemetry.io/collector/receiver/otlpreceiver") and returns the
// filesystem path starting at GOPATH by reading the current module's go.mod
// file to get the version and appending it to the filesystem path.
func (dr dirResolver) packagePathToGoPath(packagePath string) (string, error) {
gomodPath := filepath.Join(dr.srcRoot, "go.mod")
gomodBytes, err := os.ReadFile(gomodPath)
if err != nil {
return "", err
}
parsedModfile, err := modfile.Parse(gomodPath, gomodBytes, nil)
if err != nil {
return "", err
}
for _, modfileRequire := range parsedModfile.Require {
modpath := modfileRequire.Syntax.Token[0]
if strings.HasPrefix(packagePath, modpath) {
return modfileRequreToGoPath(modfileRequire)
}
}
return "", fmt.Errorf("packagePathToGoPath: not found in go.mod: package path %q", packagePath)
}
// modfileRequreToGoPath converts a modfile.Require value to a fully-qualified
// filesystem path with a GOPATH prefix.
func modfileRequreToGoPath(required *modfile.Require) (string, error) {
path, err := requireTokensToPartialPath(required.Syntax.Token)
if err != nil {
return "", err
}
goPath := os.Getenv("GOPATH")
if goPath == "" {
goPath = build.Default.GOPATH
}
return filepath.Join(goPath, "pkg", "mod", path), nil
}
// requireTokensToPartialPath converts a string slice of length two e.g.
// ["github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/collector",
// "42"] from a modfile.Require struct to an escaped, partial filesystem path
// (without the GOPATH prefix) e.g.
// "github.com/!google!cloud!platform/opentelemetry-operations-go/exporter/collector@42"
func requireTokensToPartialPath(tokens []string) (string, error) {
pathPart, err := module.EscapePath(tokens[0])
if err != nil {
return "", err
}
version := tokens[1]
return fmt.Sprintf("%s@%s", pathPart, version), nil
}