Index fields using a map
This commit is contained in:
parent
11289526b5
commit
789722bf3f
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue