From 23de271ecff4488325ac207413238ed18de5a5f5 Mon Sep 17 00:00:00 2001 From: Jaz Date: Fri, 3 Nov 2023 17:15:21 -0400 Subject: [PATCH] move to builder pattern for extinctions --- flags/builder.go | 36 ++++++++++++++++++++++------- internal/extinctions/extinctions.go | 25 +++++--------------- main.go | 6 ++--- 3 files changed, 36 insertions(+), 31 deletions(-) diff --git a/flags/builder.go b/flags/builder.go index 4e311885..9afad2fa 100644 --- a/flags/builder.go +++ b/flags/builder.go @@ -9,18 +9,20 @@ import ( ) type ReferenceBuilder struct { - max int // maximum number of flags to find - flagsAdded map[string][]string - flagsRemoved map[string][]string - foundFlags map[string]struct{} + max int // maximum number of flags to find + flagsAdded map[string][]string + flagsRemoved map[string][]string + existingFlags map[string]struct{} // TODO rename this + foundFlags map[string]struct{} } func NewReferenceBuilder(max int) *ReferenceBuilder { return &ReferenceBuilder{ - flagsAdded: make(map[string][]string), - flagsRemoved: make(map[string][]string), - foundFlags: make(map[string]struct{}), - max: max, + flagsAdded: make(map[string][]string), + flagsRemoved: make(map[string][]string), + foundFlags: make(map[string]struct{}), + existingFlags: make(map[string]struct{}), + max: max, } } @@ -41,6 +43,11 @@ func (b *ReferenceBuilder) AddReference(flagKey string, op diff_util.Operation, return nil } +// TODO rename +func (b *ReferenceBuilder) ExistingFlag(flagKey string) { + b.existingFlags[flagKey] = struct{}{} +} + func (b *ReferenceBuilder) AddedFlag(flagKey string, aliases []string) { b.foundFlags[flagKey] = struct{}{} if _, ok := b.flagsAdded[flagKey]; !ok { @@ -57,9 +64,18 @@ func (b *ReferenceBuilder) RemovedFlag(flagKey string, aliases []string) { b.flagsRemoved[flagKey] = append(b.flagsRemoved[flagKey], aliases...) } +func (b *ReferenceBuilder) RemovedFlagKeys() []string { + keys := make([]string, 0, len(b.flagsRemoved)) + for k := range b.flagsRemoved { + keys = append(keys, k) + } + return keys +} + func (b *ReferenceBuilder) Build() FlagsRef { added := make(map[string][]string, len(b.flagsAdded)) removed := make(map[string][]string, len(b.flagsRemoved)) + extinctions := make(map[string]struct{}, len(b.flagsRemoved)) for flagKey := range b.foundFlags { if aliases, ok := b.flagsAdded[flagKey]; ok { @@ -73,12 +89,16 @@ func (b *ReferenceBuilder) Build() FlagsRef { aliases := uniqueStrs(aliases) sort.Strings(aliases) removed[flagKey] = aliases + if _, ok := b.existingFlags[flagKey]; !ok { + extinctions[flagKey] = struct{}{} + } } } return FlagsRef{ FlagsAdded: added, FlagsRemoved: removed, + ExtinctFlags: extinctions, } } diff --git a/internal/extinctions/extinctions.go b/internal/extinctions/extinctions.go index 590c9859..49a9e032 100644 --- a/internal/extinctions/extinctions.go +++ b/internal/extinctions/extinctions.go @@ -7,37 +7,24 @@ import ( lsearch "github.com/launchdarkly/ld-find-code-refs/v2/search" ) -func CheckExtinctions(opts options.Options, removedFlags lflags.FlagAliasMap) (map[string]struct{}, error) { - flags := make([]string, 0, len(removedFlags)) - flagMap := make(map[string]struct{}, len(removedFlags)) - - for flagKey := range removedFlags { - flags = append(flags, flagKey) - flagMap[flagKey] = struct{}{} - } +func CheckExtinctions(opts options.Options, builder *lflags.ReferenceBuilder) error { + flags := make([]string, 0, len(builder.RemovedFlagKeys())) matcher, err := search.GetMatcher(opts, flags) if err != nil { - return nil, err + return err } references, err := lsearch.SearchForRefs(opts.Dir, matcher) if err != nil { - return nil, err + return err } -check: for _, ref := range references { for _, hunk := range ref.Hunks { - if _, ok := flagMap[hunk.FlagKey]; ok { - delete(flagMap, hunk.FlagKey) - } - if len(flagMap) == 0 { - break check - } + builder.ExistingFlag(hunk.FlagKey) } } - // remaining flags are extinct - return flagMap, nil + return nil } diff --git a/main.go b/main.go index e366631e..ba6e8ed1 100644 --- a/main.go +++ b/main.go @@ -70,13 +70,11 @@ func main() { ldiff.ProcessDiffs(matcher, hunk, builder) } } - flagsRef := builder.Build() - extinctFlags, err := extinctions.CheckExtinctions(opts, flagsRef.FlagsRemoved) - if err != nil { + if err := extinctions.CheckExtinctions(opts, builder); err != nil { gha.LogWarning("Error checking for extinct flags") } - flagsRef.ExtinctFlags = extinctFlags + flagsRef := builder.Build() // Add comment existingComment := checkExistingComments(event, config, ctx)