Skip to content

Commit

Permalink
Revert "refactor: make ruleRecord serializable (bazel-contrib#1878)"
Browse files Browse the repository at this point in the history
This reverts commit 4f4a7e1.
  • Loading branch information
tyler-french committed Aug 28, 2024
1 parent 1a98d35 commit 6b8f4e2
Showing 1 changed file with 30 additions and 30 deletions.
60 changes: 30 additions & 30 deletions resolve/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,22 +110,22 @@ type RuleIndex struct {

// ruleRecord contains information about a rule relevant to import indexing.
type ruleRecord struct {
Kind string `json:"kind"`
Label label.Label `json:"label"`
kind string
label label.Label

Pkg string `json:"pkg"`
pkg string

// A list of ImportSpecs by which this rule may be imported.
ImportedAs []ImportSpec `json:"importedAs"`
importedAs []ImportSpec

// The set of labels (of any language) that this rule directly embeds.
Embeds []label.Label `json:"embeds"`
embeds []label.Label

// The language that this rule is relevant for.
// Due to the presence of mapped kinds, it's otherwise
// impossible to know the underlying builtin rule type for an
// arbitrary import.
Lang string `json:"lang"`
lang string
}

// NewRuleIndex creates a new index.
Expand Down Expand Up @@ -178,12 +178,12 @@ func (ix *RuleIndex) AddRule(c *config.Config, r *rule.Rule, f *rule.File) {
}

record := &ruleRecord{
Kind: r.Kind(),
Pkg: f.Pkg,
Label: l,
ImportedAs: imps,
Embeds: embeds,
Lang: lang,
kind: r.Kind(),
pkg: f.Pkg,
label: l,
importedAs: imps,
embeds: embeds,
lang: lang,
}
ix.rules = append(ix.rules, record)
}
Expand All @@ -199,13 +199,13 @@ func (ix *RuleIndex) Finish() {
ix.imports = make(map[label.Label][]ImportSpec)

for _, r := range ix.rules {
if _, ok := ix.labelMap[r.Label]; ok {
log.Printf("multiple rules found with label %s", r.Label)
if _, ok := ix.labelMap[r.label]; ok {
log.Printf("multiple rules found with label %s", r.label)
continue
}

ix.labelMap[r.Label] = r
ix.imports[r.Label] = r.ImportedAs
ix.labelMap[r.label] = r
ix.imports[r.label] = r.importedAs
}

ix.collectEmbeds()
Expand All @@ -225,36 +225,36 @@ func (ix *RuleIndex) collectEmbeds() {
}
}
func (ix *RuleIndex) collectRecordEmbeds(r *ruleRecord, didCollectEmbeds map[label.Label]bool) {
if _, ok := didCollectEmbeds[r.Label]; ok {
if _, ok := didCollectEmbeds[r.label]; ok {
return
}
resolver := ix.mrslv(r.Kind, r.Pkg)
didCollectEmbeds[r.Label] = true
ix.embeds[r.Label] = r.Embeds
for _, e := range r.Embeds {
resolver := ix.mrslv(r.kind, r.pkg)
didCollectEmbeds[r.label] = true
ix.embeds[r.label] = r.embeds
for _, e := range r.embeds {
er, ok := ix.labelMap[e]
if !ok {
continue
}
ix.collectRecordEmbeds(er, didCollectEmbeds)
erResolver := ix.mrslv(er.Kind, er.Pkg)
erResolver := ix.mrslv(er.kind, 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]...)
ix.embedded[er.label] = struct{}{}
ix.embeds[r.label] = append(ix.embeds[r.label], ix.embeds[er.label]...)
}
ix.imports[r.Label] = append(ix.imports[r.Label], ix.imports[er.Label]...)
ix.imports[r.label] = append(ix.imports[r.label], ix.imports[er.label]...)
}
}

// buildImportIndex constructs the map used by FindRulesByImport.
func (ix *RuleIndex) buildImportIndex() {
ix.importMap = make(map[ImportSpec][]*ruleRecord)
for _, r := range ix.rules {
if _, embedded := ix.embedded[r.Label]; embedded {
if _, embedded := ix.embedded[r.label]; embedded {
continue
}
indexed := make(map[ImportSpec]bool)
for _, imp := range ix.imports[r.Label] {
for _, imp := range ix.imports[r.label] {
if indexed[imp] {
continue
}
Expand Down Expand Up @@ -291,12 +291,12 @@ func (ix *RuleIndex) FindRulesByImport(imp ImportSpec, lang string) []FindResult
matches := ix.importMap[imp]
results := make([]FindResult, 0, len(matches))
for _, m := range matches {
if m.Lang != lang {
if m.lang != lang {
continue
}
results = append(results, FindResult{
Label: m.Label,
Embeds: ix.embeds[m.Label],
Label: m.label,
Embeds: ix.embeds[m.label],
})
}
return results
Expand Down

0 comments on commit 6b8f4e2

Please sign in to comment.