From 268a99343419f8aee7c8dabf493d70f1655be599 Mon Sep 17 00:00:00 2001 From: tfrench Date: Wed, 28 Aug 2024 15:53:46 +0000 Subject: [PATCH] Revert "refactor: remove reference to rule.Rule in mapResolver interface (#1873)" This reverts commit 3ddf7eeb0f9ad673bd4fba3a4734a3b7fe0a56d2. --- cmd/gazelle/fix-update.go | 2 +- cmd/gazelle/metaresolver.go | 6 +++--- language/go/resolve_test.go | 6 +++--- language/proto/resolve_test.go | 4 ++-- resolve/index.go | 13 ++++++++----- 5 files changed, 17 insertions(+), 14 deletions(-) diff --git a/cmd/gazelle/fix-update.go b/cmd/gazelle/fix-update.go index c0c8a72ff..52e6af2d5 100644 --- a/cmd/gazelle/fix-update.go +++ b/cmd/gazelle/fix-update.go @@ -490,7 +490,7 @@ func runFixUpdate(wd string, cmd command, args []string) (err error) { for _, v := range visits { for i, r := range v.rules { from := label.New(c.RepoName, v.pkgRel, r.Name()) - if rslv := mrslv.Resolver(r.Kind(), v.pkgRel); rslv != nil { + if rslv := mrslv.Resolver(r, v.pkgRel); rslv != nil { rslv.Resolve(v.c, ruleIndex, rc, r, v.imports[i], from) } } diff --git a/cmd/gazelle/metaresolver.go b/cmd/gazelle/metaresolver.go index 99c6b4061..f289947ef 100644 --- a/cmd/gazelle/metaresolver.go +++ b/cmd/gazelle/metaresolver.go @@ -53,9 +53,9 @@ func (mr *metaResolver) MappedKind(pkgRel string, kind config.MappedKind) { // Resolver returns a resolver for the given rule and package, and a bool // indicating whether one was found. Empty string may be passed for pkgRel, // which results in consulting the builtin kinds only. -func (mr *metaResolver) Resolver(ruleKind string, pkgRel string) resolve.Resolver { +func (mr *metaResolver) Resolver(r *rule.Rule, pkgRel string) resolve.Resolver { for _, mappedKind := range mr.mappedKinds[pkgRel] { - if mappedKind.KindName == ruleKind { + if mappedKind.KindName == r.Kind() { fromKindResolver := mr.builtins[mappedKind.FromKind] if fromKindResolver == nil { return nil @@ -66,7 +66,7 @@ func (mr *metaResolver) Resolver(ruleKind string, pkgRel string) resolve.Resolve } } } - return mr.builtins[ruleKind] + return mr.builtins[r.Kind()] } // inverseMapKindResolver applies an inverse of the map_kind diff --git a/language/go/resolve_test.go b/language/go/resolve_test.go index 981e135a4..c489007a3 100644 --- a/language/go/resolve_test.go +++ b/language/go/resolve_test.go @@ -973,7 +973,7 @@ go_proto_library( } ix.Finish() for i, r := range f.Rules { - mrslv.Resolver(r.Kind(), "").Resolve(c, ix, rc, r, imports[i], label.New("", tc.old.rel, r.Name())) + mrslv.Resolver(r, "").Resolve(c, ix, rc, r, imports[i], label.New("", tc.old.rel, r.Name())) } f.Sync() got := strings.TrimSpace(string(bzl.Format(f.File))) @@ -1319,6 +1319,6 @@ func convertImportsAttr(r *rule.Rule) interface{} { type mapResolver map[string]resolve.Resolver -func (mr mapResolver) Resolver(ruleKind, f string) resolve.Resolver { - return mr[ruleKind] +func (mr mapResolver) Resolver(r *rule.Rule, f string) resolve.Resolver { + return mr[r.Kind()] } diff --git a/language/proto/resolve_test.go b/language/proto/resolve_test.go index 4b60d9638..ec9700e73 100644 --- a/language/proto/resolve_test.go +++ b/language/proto/resolve_test.go @@ -511,6 +511,6 @@ func convertImportsAttr(r *rule.Rule) interface{} { type mapResolver map[string]resolve.Resolver -func (mr mapResolver) Resolver(ruleKind, f string) resolve.Resolver { - return mr[ruleKind] +func (mr mapResolver) Resolver(r *rule.Rule, f string) resolve.Resolver { + return mr[r.Kind()] } diff --git a/resolve/index.go b/resolve/index.go index 109fa6557..3c6c29b22 100644 --- a/resolve/index.go +++ b/resolve/index.go @@ -73,7 +73,7 @@ type CrossResolver interface { // RuleIndex is a table of rules in a workspace, indexed by label and by // import path. Used by Resolver to map import paths to labels. type RuleIndex struct { - mrslv func(ruleKind, pkgRel string) Resolver + mrslv func(r *rule.Rule, pkgRel string) Resolver crossResolvers []CrossResolver // The underlying state of rules. All indexing should be reproducible from this. @@ -110,6 +110,8 @@ type RuleIndex struct { // ruleRecord contains information about a rule relevant to import indexing. type ruleRecord struct { + rule *rule.Rule + Kind string `json:"kind"` Label label.Label `json:"label"` @@ -132,7 +134,7 @@ type ruleRecord struct { // // kindToResolver is a map from rule kinds (for example, "go_library") to // Resolvers that support those kinds. -func NewRuleIndex(mrslv func(ruleKind, pkgRel string) Resolver, exts ...interface{}) *RuleIndex { +func NewRuleIndex(mrslv func(r *rule.Rule, pkgRel string) Resolver, exts ...interface{}) *RuleIndex { var crossResolvers []CrossResolver for _, e := range exts { if cr, ok := e.(CrossResolver); ok { @@ -161,7 +163,7 @@ func (ix *RuleIndex) AddRule(c *config.Config, r *rule.Rule, f *rule.File) { l := label.New(c.RepoName, f.Pkg, r.Name()) - if rslv := ix.mrslv(r.Kind(), f.Pkg); rslv != nil { + if rslv := ix.mrslv(r, f.Pkg); rslv != nil { lang = rslv.Name() if passesLanguageFilter(c.Langs, lang) { imps = rslv.Imports(c, r, f) @@ -178,6 +180,7 @@ func (ix *RuleIndex) AddRule(c *config.Config, r *rule.Rule, f *rule.File) { } record := &ruleRecord{ + rule: r, Kind: r.Kind(), Pkg: f.Pkg, Label: l, @@ -228,7 +231,7 @@ func (ix *RuleIndex) collectRecordEmbeds(r *ruleRecord, didCollectEmbeds map[lab if _, ok := didCollectEmbeds[r.Label]; ok { return } - resolver := ix.mrslv(r.Kind, r.Pkg) + resolver := ix.mrslv(r.rule, r.Pkg) didCollectEmbeds[r.Label] = true ix.embeds[r.Label] = r.Embeds for _, e := range r.Embeds { @@ -237,7 +240,7 @@ func (ix *RuleIndex) collectRecordEmbeds(r *ruleRecord, didCollectEmbeds map[lab continue } ix.collectRecordEmbeds(er, didCollectEmbeds) - erResolver := ix.mrslv(er.Kind, er.Pkg) + erResolver := ix.mrslv(er.rule, er.Pkg) if resolver.Name() == erResolver.Name() { ix.embedded[er.Label] = struct{}{} ix.embeds[r.Label] = append(ix.embeds[r.Label], ix.embeds[er.Label]...)