each delta has .with and LikeWith to find X related to Y
parent
8621619433
commit
697fc16b52
|
|
@ -20,6 +20,7 @@ type Delta struct {
|
|||
isSet bool
|
||||
fileName string
|
||||
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 {
|
||||
|
|
@ -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 {
|
||||
delta.fileName = ""
|
||||
delta.lineNo = 0
|
||||
delta.with = nil
|
||||
other.fileName = ""
|
||||
other.lineNo = 0
|
||||
return delta == other
|
||||
other.with = nil
|
||||
return fmt.Sprintf("%+v", delta) == fmt.Sprintf("%+v", other)
|
||||
}
|
||||
|
||||
func (delta Delta) Debug() string {
|
||||
|
|
|
|||
|
|
@ -443,7 +443,7 @@ func TestFileDeltas(t *testing.T) {
|
|||
t.Error(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())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,21 @@ type Like func(Delta) bool
|
|||
|
||||
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 {
|
||||
return func(d Delta) bool {
|
||||
for i := range deltas {
|
||||
|
|
|
|||
|
|
@ -36,3 +36,20 @@ func TestLikesAll(t *testing.T) {
|
|||
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")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue