From 7088b49b8ef1121d98ae724e70071aebc0ca075e Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 3 Apr 2023 02:41:41 +0800 Subject: [PATCH] optimize locateObject function code --- cmd/requestgen/main.go | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/cmd/requestgen/main.go b/cmd/requestgen/main.go index 49e119b..c0264c4 100644 --- a/cmd/requestgen/main.go +++ b/cmd/requestgen/main.go @@ -1264,12 +1264,18 @@ func main() { } } -func locateObject(ts *requestgen.TypeSelector) (types.Object, error) { - log.Infof("locating object: %#v", ts) +func locateObject(ts *requestgen.TypeSelector, selectedPkgs []*packages.Package) (types.Object, error) { + log.Debugf("locating object: %#v", ts) - packages, err := loadPackages([]string{ts.Package}, []string{}) - if err != nil { - return nil, err + var packages []*packages.Package + if len(selectedPkgs) > 0 && (ts.Package == "." || ts.Package == selectedPkgs[0].PkgPath) { + packages = selectedPkgs + } else { + var err error + packages, err = loadPackages([]string{ts.Package}, []string{}) + if err != nil { + return nil, err + } } if len(packages) == 0 { @@ -1319,7 +1325,7 @@ func locateObject(ts *requestgen.TypeSelector) (types.Object, error) { return nil, fmt.Errorf("can not find type matches the type selector %+v in the packages %+v", ts, packages) } -func parseTypeSelector(sel string, pkgs []*packages.Package) (types.Object, *requestgen.TypeSelector, error) { +func parseTypeSelector(sel string, selectedPkgs []*packages.Package) (types.Object, *requestgen.TypeSelector, error) { log.Debugf("parsing type selector: %s", sel) ts, err := requestgen.ParseTypeSelector(sel) @@ -1330,10 +1336,10 @@ func parseTypeSelector(sel string, pkgs []*packages.Package) (types.Object, *req log.Debugf("parsed type selector: %#v", ts) if ts.Package == "." { - ts.Package = pkgs[0].PkgPath + ts.Package = selectedPkgs[0].PkgPath } - o, err := locateObject(ts) + o, err := locateObject(ts, selectedPkgs) if err != nil { return nil, ts, err }