| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package apidiff |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
"go/types" |
| 9 |
"sort" |
| 10 |
"strings" |
| 11 |
) |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
type objectWithSide struct { |
| 18 |
object types.Object |
| 19 |
isNew bool |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
type messageSet map[objectWithSide]map[string]string |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
func (m messageSet) add(obj objectWithSide, part, msg string) { |
| 33 |
s := m[obj] |
| 34 |
if s == nil { |
| 35 |
s = map[string]string{} |
| 36 |
m[obj] = s |
| 37 |
} |
| 38 |
if f, ok := s[part]; ok && f != msg { |
| 39 |
fmt.Printf("! second, different message for obj %s, isNew %v, part %q\n", obj.object, obj.isNew, part) |
| 40 |
fmt.Printf(" first: %s\n", f) |
| 41 |
fmt.Printf(" second: %s\n", msg) |
| 42 |
} |
| 43 |
s[part] = msg |
| 44 |
} |
| 45 |
|
| 46 |
func (m messageSet) collect(oldRootPackagePath, newRootPackagePath string) []string { |
| 47 |
var s []string |
| 48 |
for obj, parts := range m { |
| 49 |
rootPackagePath := oldRootPackagePath |
| 50 |
if obj.isNew { |
| 51 |
rootPackagePath = newRootPackagePath |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
objstring := objectString(obj.object, rootPackagePath) |
| 56 |
for part, msg := range parts { |
| 57 |
var p string |
| 58 |
|
| 59 |
if strings.HasPrefix(part, ",") { |
| 60 |
p = objstring + part |
| 61 |
} else { |
| 62 |
p = dotjoin(objstring, part) |
| 63 |
} |
| 64 |
s = append(s, p+": "+msg) |
| 65 |
} |
| 66 |
} |
| 67 |
sort.Strings(s) |
| 68 |
return s |
| 69 |
} |
| 70 |
|
| 71 |
func objectString(obj types.Object, rootPackagePath string) string { |
| 72 |
thisPackagePath := obj.Pkg().Path() |
| 73 |
|
| 74 |
var packagePrefix string |
| 75 |
if thisPackagePath == rootPackagePath { |
| 76 |
|
| 77 |
packagePrefix = "" |
| 78 |
} else if strings.HasPrefix(thisPackagePath, rootPackagePath+"/") { |
| 79 |
|
| 80 |
|
| 81 |
packagePrefix = "./" + thisPackagePath[len(rootPackagePath)+1:] + "." |
| 82 |
} else { |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
packagePrefix = thisPackagePath + "." |
| 88 |
} |
| 89 |
|
| 90 |
if f, ok := obj.(*types.Func); ok { |
| 91 |
sig := f.Type().(*types.Signature) |
| 92 |
if recv := sig.Recv(); recv != nil { |
| 93 |
tn := types.TypeString(recv.Type(), types.RelativeTo(obj.Pkg())) |
| 94 |
if tn[0] == '*' { |
| 95 |
tn = "(" + tn + ")" |
| 96 |
} |
| 97 |
return fmt.Sprintf("%s%s.%s", packagePrefix, tn, obj.Name()) |
| 98 |
} |
| 99 |
} |
| 100 |
return fmt.Sprintf("%s%s", packagePrefix, obj.Name()) |
| 101 |
} |
| 102 |
|
| 103 |
func dotjoin(s1, s2 string) string { |
| 104 |
if s1 == "" { |
| 105 |
return s2 |
| 106 |
} |
| 107 |
if s2 == "" { |
| 108 |
return s1 |
| 109 |
} |
| 110 |
return s1 + "." + s2 |
| 111 |
} |