124 lines
3.6 KiB
Go
124 lines
3.6 KiB
Go
package uimigrate
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"bytes"
|
||
|
|
"flag"
|
||
|
|
"fmt"
|
||
|
|
"go/format"
|
||
|
|
"go/importer"
|
||
|
|
"go/token"
|
||
|
|
"go/types"
|
||
|
|
"io"
|
||
|
|
"os"
|
||
|
|
"os/exec"
|
||
|
|
"sort"
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
var update = flag.Bool("update", false, "rewrite uiapi.go from package ui")
|
||
|
|
|
||
|
|
const uiPath = "github.com/egoist/mygo/ui"
|
||
|
|
|
||
|
|
// TestUIAPI derives uiapi.go from the compiled signatures of package ui, so
|
||
|
|
// the migrator's facts about its functions and methods cannot drift.
|
||
|
|
func TestUIAPI(t *testing.T) {
|
||
|
|
pkg, err := uiImporter(t).Import(uiPath)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
scope := pkg.Scope()
|
||
|
|
element := scope.Lookup("Element").Type()
|
||
|
|
parts := map[string]bool{}
|
||
|
|
for _, name := range scope.Names() {
|
||
|
|
if obj, ok := scope.Lookup(name).(*types.TypeName); ok && obj.Exported() && strings.HasSuffix(name, "Parts") {
|
||
|
|
if _, ok := obj.Type().Underlying().(*types.Struct); ok {
|
||
|
|
parts[name] = true
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
// kind names a result that the migrator converts: Element or parts.
|
||
|
|
kind := func(results *types.Tuple) string {
|
||
|
|
if results.Len() != 1 {
|
||
|
|
return ""
|
||
|
|
}
|
||
|
|
named, ok := results.At(0).Type().(*types.Named)
|
||
|
|
if !ok || named.Obj().Pkg() != pkg {
|
||
|
|
return ""
|
||
|
|
}
|
||
|
|
if name := named.Origin().Obj().Name(); name == "Element" || parts[name] {
|
||
|
|
return name
|
||
|
|
}
|
||
|
|
return ""
|
||
|
|
}
|
||
|
|
results := map[string]string{}
|
||
|
|
for _, name := range scope.Names() {
|
||
|
|
if fn, ok := scope.Lookup(name).(*types.Func); ok && fn.Exported() {
|
||
|
|
if k := kind(fn.Signature().Results()); k != "" {
|
||
|
|
results[name] = k
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
chains := map[string]bool{}
|
||
|
|
methods := types.NewMethodSet(types.NewPointer(element))
|
||
|
|
for i := range methods.Len() {
|
||
|
|
fn := methods.At(i).Obj().(*types.Func)
|
||
|
|
if fn.Exported() && kind(fn.Signature().Results()) == "Element" {
|
||
|
|
chains[fn.Name()] = true
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
var src bytes.Buffer
|
||
|
|
src.WriteString("// Code generated by go test ./internal/uimigrate -update from package ui; DO NOT EDIT.\n\npackage uimigrate\n\n")
|
||
|
|
src.WriteString("// uiResults maps each function of package ui that returns one Element or\n// custom parts value to that result's type. Other calls make neither.\nvar uiResults = map[string]string{\n")
|
||
|
|
for _, name := range sorted(results) {
|
||
|
|
fmt.Fprintf(&src, "%q: %q,\n", name, results[name])
|
||
|
|
}
|
||
|
|
src.WriteString("}\n\n// elementChains lists the methods of Element that return an Element.\nvar elementChains = map[string]bool{\n")
|
||
|
|
for _, name := range sorted(chains) {
|
||
|
|
fmt.Fprintf(&src, "%q: true,\n", name)
|
||
|
|
}
|
||
|
|
src.WriteString("}\n\n// uiParts lists package ui's custom parts types.\nvar uiParts = map[string]bool{\n")
|
||
|
|
for _, name := range sorted(parts) {
|
||
|
|
fmt.Fprintf(&src, "%q: true,\n", name)
|
||
|
|
}
|
||
|
|
src.WriteString("}\n")
|
||
|
|
got, err := format.Source(src.Bytes())
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if *update {
|
||
|
|
if err := os.WriteFile("uiapi.go", got, 0o644); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
return
|
||
|
|
}
|
||
|
|
if want, err := os.ReadFile("uiapi.go"); err != nil || !bytes.Equal(want, got) {
|
||
|
|
t.Fatalf("uiapi.go differs from package ui's signatures; go test ./internal/uimigrate -run TestUIAPI -update rewrites it:\n%s", got)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// uiImporter imports package ui from its compiled export data.
|
||
|
|
func uiImporter(t *testing.T) types.Importer {
|
||
|
|
t.Helper()
|
||
|
|
out, err := exec.Command("go", "list", "-export", "-f", "{{.Export}}", uiPath).Output()
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal("export data of package ui:", err)
|
||
|
|
}
|
||
|
|
export := strings.TrimSpace(string(out))
|
||
|
|
return importer.ForCompiler(token.NewFileSet(), "gc", func(path string) (io.ReadCloser, error) {
|
||
|
|
if path != uiPath {
|
||
|
|
return nil, fmt.Errorf("unexpected import %s", path)
|
||
|
|
}
|
||
|
|
return os.Open(export)
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
func sorted[V any](m map[string]V) []string {
|
||
|
|
var names []string
|
||
|
|
for name := range m {
|
||
|
|
names = append(names, name)
|
||
|
|
}
|
||
|
|
sort.Strings(names)
|
||
|
|
return names
|
||
|
|
}
|