-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: add builder for keeping track of references found #76
Changes from 7 commits
56a84a4
77bf429
2e4f104
59cc110
7522025
3d82cf8
3d5b93d
ae03e70
dd0cafb
15ba865
3c9ce4e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,9 +131,7 @@ func ProcessFlags(flagsRef lflags.FlagsRef, flags []ldapi.FeatureFlag, config *l | |
// sort keys so hashing can work for checking if comment already exists | ||
sort.Strings(addedKeys) | ||
for _, flagKey := range addedKeys { | ||
// If flag is in both added and removed then it is being modified | ||
delete(flagsRef.FlagsRemoved, flagKey) | ||
flagAliases := uniqueAliases(flagsRef.FlagsAdded[flagKey]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove duplicate or empty alias strings is now handled by |
||
flagAliases := flagsRef.FlagsAdded[flagKey] | ||
idx, _ := find(flags, flagKey) | ||
createComment, err := githubFlagComment(flags[idx], flagAliases, true, config) | ||
buildComment.CommentsAdded = append(buildComment.CommentsAdded, createComment) | ||
|
@@ -147,7 +145,7 @@ func ProcessFlags(flagsRef lflags.FlagsRef, flags []ldapi.FeatureFlag, config *l | |
} | ||
sort.Strings(removedKeys) | ||
for _, flagKey := range removedKeys { | ||
flagAliases := uniqueAliases(flagsRef.FlagsRemoved[flagKey]) | ||
flagAliases := flagsRef.FlagsRemoved[flagKey] | ||
idx, _ := find(flags, flagKey) | ||
removedComment, err := githubFlagComment(flags[idx], flagAliases, false, config) | ||
buildComment.CommentsRemoved = append(buildComment.CommentsRemoved, removedComment) | ||
|
@@ -184,16 +182,6 @@ func uniqueFlagKeys(a, b lflags.FlagAliasMap) []string { | |
return allKeys | ||
} | ||
|
||
func uniqueAliases(aliases lflags.AliasSet) []string { | ||
flagAliases := make([]string, 0, len(aliases)) | ||
for alias := range aliases { | ||
if len(strings.TrimSpace(alias)) > 0 { | ||
flagAliases = append(flagAliases, alias) | ||
} | ||
} | ||
return flagAliases | ||
} | ||
|
||
func pluralize(str string, strLength int) string { | ||
tmpl := "%d %s" | ||
if strLength != 1 { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,17 +50,9 @@ func CheckDiff(parsedDiff *diff.FileDiff, workspace string) *DiffPaths { | |
return &diffPaths | ||
} | ||
|
||
func ProcessDiffs(matcher lsearch.Matcher, hunk *diff.Hunk, flagsRef lflags.FlagsRef, maxFlags int) { | ||
flagMap := map[Operation]lflags.FlagAliasMap{ | ||
Add: flagsRef.FlagsAdded, | ||
Delete: flagsRef.FlagsRemoved, | ||
} | ||
func ProcessDiffs(matcher lsearch.Matcher, hunk *diff.Hunk, builder *lflags.ReferenceBuilder) { | ||
diffLines := strings.Split(string(hunk.Body), "\n") | ||
for _, line := range diffLines { | ||
if flagsRef.Count() >= maxFlags { | ||
break | ||
} | ||
|
||
op := operation(line) | ||
if op == Equal { | ||
continue | ||
|
@@ -69,21 +61,13 @@ func ProcessDiffs(matcher lsearch.Matcher, hunk *diff.Hunk, flagsRef lflags.Flag | |
// only one for now | ||
elementMatcher := matcher.Elements[0] | ||
for _, flagKey := range elementMatcher.FindMatches(line) { | ||
if _, ok := flagMap[op][flagKey]; !ok { | ||
flagMap[op][flagKey] = make(lflags.AliasSet) | ||
} | ||
if aliasMatches := matcher.FindAliases(line, flagKey); len(aliasMatches) > 0 { | ||
if _, ok := flagMap[op][flagKey]; !ok { | ||
flagMap[op][flagKey] = make(lflags.AliasSet) | ||
} | ||
for _, alias := range aliasMatches { | ||
flagMap[op][flagKey][alias] = true | ||
} | ||
} | ||
aliasMatches := elementMatcher.FindAliases(line, flagKey) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was always supposed to be calling |
||
builder.AddReference(flagKey, op.String(), aliasMatches) | ||
} | ||
if builder.MaxReferences() { | ||
break | ||
} | ||
} | ||
flagsRef.FlagsAdded = flagMap[Add] | ||
flagsRef.FlagsRemoved = flagMap[Delete] | ||
} | ||
|
||
// Operation defines the operation of a diff item. | ||
|
@@ -108,3 +92,14 @@ func operation(row string) Operation { | |
|
||
return Equal | ||
} | ||
|
||
func (o Operation) String() string { | ||
jazanne marked this conversation as resolved.
Show resolved
Hide resolved
|
||
switch o { | ||
case Add: | ||
return "+" | ||
case Delete: | ||
return "-" | ||
} | ||
|
||
return "" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package flags | ||
|
||
import ( | ||
"sort" | ||
"strings" | ||
) | ||
|
||
type ReferenceBuilder struct { | ||
max int // maximum number of flags to find | ||
flagsAdded map[string][]string | ||
flagsRemoved map[string][]string | ||
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, | ||
} | ||
} | ||
|
||
func (b *ReferenceBuilder) MaxReferences() bool { | ||
return len(b.foundFlags) >= b.max | ||
} | ||
|
||
func (b *ReferenceBuilder) AddReference(flagKey string, op string, aliases []string) { | ||
if op == "+" { | ||
b.AddedFlag(flagKey, aliases) | ||
} else if op == "-" { | ||
b.RemovedFlag(flagKey, aliases) | ||
} | ||
// ignore | ||
jazanne marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
func (b *ReferenceBuilder) AddedFlag(flagKey string, aliases []string) { | ||
b.foundFlags[flagKey] = struct{}{} | ||
if _, ok := b.flagsAdded[flagKey]; !ok { | ||
b.flagsAdded[flagKey] = make([]string, 0, len(aliases)) | ||
} | ||
b.flagsAdded[flagKey] = append(b.flagsAdded[flagKey], aliases...) | ||
} | ||
|
||
func (b *ReferenceBuilder) RemovedFlag(flagKey string, aliases []string) { | ||
b.foundFlags[flagKey] = struct{}{} | ||
if _, ok := b.flagsRemoved[flagKey]; !ok { | ||
b.flagsRemoved[flagKey] = make([]string, 0, len(aliases)) | ||
} | ||
b.flagsRemoved[flagKey] = append(b.flagsRemoved[flagKey], aliases...) | ||
} | ||
|
||
func (b *ReferenceBuilder) Build() FlagsRef { | ||
added := make(map[string][]string, len(b.flagsAdded)) | ||
removed := make(map[string][]string, len(b.flagsRemoved)) | ||
|
||
for flagKey := range b.foundFlags { | ||
if aliases, ok := b.flagsAdded[flagKey]; ok { | ||
// if there are any removed aliases, we should add them | ||
aliases := append(aliases, b.flagsRemoved[flagKey]...) | ||
aliases = uniqueStrs(aliases) | ||
sort.Strings(aliases) | ||
added[flagKey] = aliases | ||
} else if aliases, ok := b.flagsRemoved[flagKey]; ok { | ||
// only add to removed if it wasn't also added | ||
aliases := uniqueStrs(aliases) | ||
sort.Strings(aliases) | ||
removed[flagKey] = aliases | ||
} | ||
} | ||
|
||
return FlagsRef{ | ||
FlagsAdded: added, | ||
FlagsRemoved: removed, | ||
} | ||
} | ||
|
||
// get slice with unique, non-empty strings | ||
func uniqueStrs(s []string) []string { | ||
if len(s) <= 1 { | ||
return s | ||
} | ||
keys := make(map[string]struct{}, len(s)) | ||
ret := make([]string, 0, len(s)) | ||
for _, elem := range s { | ||
trimmed := strings.TrimSpace(elem) | ||
if len(trimmed) == 0 { | ||
continue | ||
} | ||
if _, ok := keys[trimmed]; !ok { | ||
keys[trimmed] = struct{}{} | ||
ret = append(ret, trimmed) | ||
} | ||
} | ||
return ret | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
deduping "modified" flags now happens in
Build()