-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathspec.go
49 lines (40 loc) · 927 Bytes
/
spec.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
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"errors"
"go/ast"
"go/parser"
"go/token"
"strconv"
)
var errBadSpec = errors.New("bad spec expression")
func parseFieldSpec(expr string) (pkgPath, typeName, fieldName string, err error) {
// Inspired by x/tools/refactor/rename/spec.go.
e, err := parser.ParseExpr(expr)
if err != nil {
return "", "", "", err
}
x, ok := e.(*ast.SelectorExpr)
if !ok {
return "", "", "", errBadSpec
}
fieldName = x.Sel.Name
x, ok = x.X.(*ast.SelectorExpr)
if !ok {
return "", "", "", errBadSpec
}
typeName = x.Sel.Name
switch x := x.X.(type) {
case *ast.Ident:
pkgPath = x.Name
case *ast.BasicLit:
if x.Kind == token.STRING {
pkgPath, _ = strconv.Unquote(x.Value)
}
default:
return "", "", "", errBadSpec
}
return
}