Index fields using a map

This commit is contained in:
Alex Dadgar 2016-05-04 14:29:08 -07:00
parent 11289526b5
commit 789722bf3f
2 changed files with 15 additions and 4 deletions

View File

@ -255,7 +255,7 @@ type PortsDiff struct {
// primitive fields.
type PrimitiveStructDiff struct {
DiffEntry
PrimitiveFields []*FieldDiff
PrimitiveFields map[string]*FieldDiff
}
// DiffFields performs the diff of the passed fields against the old and new
@ -266,7 +266,11 @@ func (p *PrimitiveStructDiff) DiffFields(old, new interface{}, fields []string)
newV := getField(new, field)
pDiff := NewFieldDiff(field, oldV, newV)
if pDiff != nil {
p.PrimitiveFields = append(p.PrimitiveFields, pDiff)
if p.PrimitiveFields == nil {
p.PrimitiveFields = make(map[string]*FieldDiff)
}
p.PrimitiveFields[field] = pDiff
}
}
}

View File

@ -337,7 +337,11 @@ func TestNewPrimitiveStructDiff(t *testing.T) {
t.Fatalf("unexpected number of field diffs: %#v", pdiff.PrimitiveFields)
}
f := pdiff.PrimitiveFields[0]
f, ok := pdiff.PrimitiveFields["Label"]
if !ok {
t.Fatalf("expected diff on field %q", "label")
}
if f.Type != DiffTypeEdited {
t.Fatalf("unexpected type: got %v; want %v", f.Type, DiffTypeEdited)
}
@ -358,7 +362,10 @@ func TestNewPrimitiveStructDiff(t *testing.T) {
t.Fatalf("unexpected number of field diffs: %#v", pdiff.PrimitiveFields)
}
f = pdiff.PrimitiveFields[0]
f = pdiff.PrimitiveFields["Label"]
if !ok {
t.Fatalf("expected diff on field %q", "Label")
}
if f.Type != DiffTypeEdited {
t.Fatalf("unexpected type: got %v; want %v", f.Type, DiffTypeEdited)
}