each delta has .with and LikeWith to find X related to Y

main
Bel LaPointe 2025-04-03 17:28:57 -06:00
parent 8621619433
commit 697fc16b52
5 changed files with 52 additions and 2 deletions

View File

@ -20,6 +20,7 @@ type Delta struct {
isSet bool isSet bool
fileName string fileName string
lineNo int lineNo int
with []Delta
} }
func newDelta(transaction string, payee bool, d, desc, name string, v float64, c string, isSet bool, fileName string, lineNo int) Delta { func newDelta(transaction string, payee bool, d, desc, name string, v float64, c string, isSet bool, fileName string, lineNo int) Delta {
@ -38,12 +39,20 @@ func newDelta(transaction string, payee bool, d, desc, name string, v float64, c
} }
} }
func (delta Delta) withWith(other Delta) Delta {
other.with = nil
delta.with = append(delta.with, other)
return delta
}
func (delta Delta) equivalent(other Delta) bool { func (delta Delta) equivalent(other Delta) bool {
delta.fileName = "" delta.fileName = ""
delta.lineNo = 0 delta.lineNo = 0
delta.with = nil
other.fileName = "" other.fileName = ""
other.lineNo = 0 other.lineNo = 0
return delta == other other.with = nil
return fmt.Sprintf("%+v", delta) == fmt.Sprintf("%+v", other)
} }
func (delta Delta) Debug() string { func (delta Delta) Debug() string {

View File

@ -443,7 +443,7 @@ func TestFileDeltas(t *testing.T) {
t.Error(deltas[i].Transaction) t.Error(deltas[i].Transaction)
} }
deltas[i].Transaction = "" deltas[i].Transaction = ""
if want[i] != deltas[i] { if !want[i].equivalent(deltas[i]) {
t.Errorf("[%d] \n\twant=%s, \n\t got=%s", i, want[i].Debug(), deltas[i].Debug()) t.Errorf("[%d] \n\twant=%s, \n\t got=%s", i, want[i].Debug(), deltas[i].Debug())
} }
} }

View File

@ -8,6 +8,21 @@ type Like func(Delta) bool
type Likes []Like type Likes []Like
func LikeWith(pattern string) Like {
l := LikeName(pattern)
return func(d Delta) bool {
if l(d) {
return true
}
for _, d := range d.with {
if l(d) {
return true
}
}
return false
}
}
func LikeTransactions(deltas ...Delta) Like { func LikeTransactions(deltas ...Delta) Like {
return func(d Delta) bool { return func(d Delta) bool {
for i := range deltas { for i := range deltas {

View File

@ -36,3 +36,20 @@ func TestLikesAll(t *testing.T) {
t.Error(likes.All(delta)) t.Error(likes.All(delta))
} }
} }
func TestLikeWith(t *testing.T) {
delta := Delta{
Name: "x",
with: []Delta{
Delta{Name: "y"},
Delta{Name: "z"},
},
}
if !LikeWith("x")(delta) {
t.Error("like with self not caught")
}
if !LikeWith("z")(delta) {
t.Error("like with reverse not caught")
}
}

View File

@ -165,6 +165,15 @@ func (t transaction) deltas() Deltas {
)) ))
} }
} }
for i := range result {
for j := range result {
if i != j {
result[i] = result[i].withWith(result[j])
}
}
}
return result return result
} }