Skip to content

Commit

Permalink
move to builder pattern for extinctions
Browse files Browse the repository at this point in the history
  • Loading branch information
jazanne committed Nov 3, 2023
1 parent ded6217 commit 23de271
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 31 deletions.
36 changes: 28 additions & 8 deletions flags/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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,
}
}

Expand Down
25 changes: 6 additions & 19 deletions internal/extinctions/extinctions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
6 changes: 2 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 23de271

Please sign in to comment.