Add diffing logic for assets/archives
This commit is contained in:
parent
002618e605
commit
91c90cf2a9
|
@ -98,6 +98,11 @@ func (a Asset) GetURIURL() (*url.URL, bool, error) {
|
|||
return nil, false, nil
|
||||
}
|
||||
|
||||
// Equals returns true if a is value-equal to other.
|
||||
func (a Asset) Equals(other Asset) bool {
|
||||
return a.Text == other.Text && a.Path == other.Path && a.URI == other.URI
|
||||
}
|
||||
|
||||
// Serialize returns a weakly typed map that contains the right signature for serialization purposes.
|
||||
func (a Asset) Serialize() map[string]interface{} {
|
||||
return map[string]interface{}{
|
||||
|
@ -336,6 +341,26 @@ func (a Archive) GetURIURL() (*url.URL, bool, error) {
|
|||
return nil, false, nil
|
||||
}
|
||||
|
||||
// Equals returns true if a is value-equal to other.
|
||||
func (a Archive) Equals(other Archive) bool {
|
||||
if a.Assets != nil {
|
||||
if other.Assets == nil {
|
||||
return false
|
||||
}
|
||||
if len(a.Assets) != len(other.Assets) {
|
||||
return false
|
||||
}
|
||||
for key, value := range a.Assets {
|
||||
if other.Assets[key] != value {
|
||||
return false
|
||||
}
|
||||
}
|
||||
} else if other.Assets != nil {
|
||||
return false
|
||||
}
|
||||
return a.Path == other.Path && a.URI == other.URI
|
||||
}
|
||||
|
||||
// Serialize returns a weakly typed map that contains the right signature for serialization purposes.
|
||||
func (a Archive) Serialize() map[string]interface{} {
|
||||
var assets map[string]interface{}
|
||||
|
|
|
@ -207,7 +207,7 @@ func (v PropertyValue) Diff(other PropertyValue) *ValueDiff {
|
|||
}
|
||||
|
||||
// If we got here, either the values are primitives, or they weren't the same type; do a simple diff.
|
||||
if v.V == other.V {
|
||||
if v.DeepEquals(other) {
|
||||
return nil
|
||||
}
|
||||
return &ValueDiff{Old: v, New: other}
|
||||
|
@ -257,6 +257,19 @@ func (v PropertyValue) DeepEquals(other PropertyValue) bool {
|
|||
return true
|
||||
}
|
||||
|
||||
// Assets and archives enjoy value equality.
|
||||
if v.IsAsset() {
|
||||
if !other.IsAsset() {
|
||||
return false
|
||||
}
|
||||
return v.AssetValue().Equals(other.AssetValue())
|
||||
} else if v.IsArchive() {
|
||||
if !other.IsArchive() {
|
||||
return false
|
||||
}
|
||||
return v.ArchiveValue().Equals(other.ArchiveValue())
|
||||
}
|
||||
|
||||
// Object values are equal if their contents are deeply equal.
|
||||
if v.IsObject() {
|
||||
if !other.IsObject() {
|
||||
|
|
|
@ -267,3 +267,49 @@ func TestObjectPropertyValueDiffs(t *testing.T) {
|
|||
assert.Equal(t, obj2[PropertyKey("prop-b")], d4.Updates[PropertyKey("prop-b")].New)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAssetPropertyValueDiffs(t *testing.T) {
|
||||
t.Parallel()
|
||||
a1 := NewTextAsset("test")
|
||||
d1 := NewAssetProperty(a1).Diff(NewAssetProperty(a1))
|
||||
assert.Nil(t, d1)
|
||||
a2 := NewTextAsset("test2")
|
||||
d2 := NewAssetProperty(a1).Diff(NewAssetProperty(a2))
|
||||
assert.NotNil(t, d2)
|
||||
assert.Nil(t, d2.Array)
|
||||
assert.Nil(t, d2.Object)
|
||||
assert.True(t, d2.Old.IsAsset())
|
||||
assert.Equal(t, "test", d2.Old.AssetValue().Text)
|
||||
assert.True(t, d2.New.IsAsset())
|
||||
assert.Equal(t, "test2", d2.New.AssetValue().Text)
|
||||
d3 := NewAssetProperty(a1).Diff(NewNullProperty())
|
||||
assert.NotNil(t, d3)
|
||||
assert.Nil(t, d3.Array)
|
||||
assert.Nil(t, d3.Object)
|
||||
assert.True(t, d3.Old.IsAsset())
|
||||
assert.Equal(t, "test", d3.Old.AssetValue().Text)
|
||||
assert.True(t, d3.New.IsNull())
|
||||
}
|
||||
|
||||
func TestArchivePropertyValueDiffs(t *testing.T) {
|
||||
t.Parallel()
|
||||
a1 := NewPathArchive("/dev/null")
|
||||
d1 := NewArchiveProperty(a1).Diff(NewArchiveProperty(a1))
|
||||
assert.Nil(t, d1)
|
||||
a2 := NewPathArchive("/dev/tty1")
|
||||
d2 := NewArchiveProperty(a1).Diff(NewArchiveProperty(a2))
|
||||
assert.NotNil(t, d2)
|
||||
assert.Nil(t, d2.Array)
|
||||
assert.Nil(t, d2.Object)
|
||||
assert.True(t, d2.Old.IsArchive())
|
||||
assert.Equal(t, "/dev/null", d2.Old.ArchiveValue().Path)
|
||||
assert.True(t, d2.New.IsArchive())
|
||||
assert.Equal(t, "/dev/tty1", d2.New.ArchiveValue().Path)
|
||||
d3 := NewArchiveProperty(a1).Diff(NewNullProperty())
|
||||
assert.NotNil(t, d3)
|
||||
assert.Nil(t, d3.Array)
|
||||
assert.Nil(t, d3.Object)
|
||||
assert.True(t, d3.Old.IsArchive())
|
||||
assert.Equal(t, "/dev/null", d3.Old.ArchiveValue().Path)
|
||||
assert.True(t, d3.New.IsNull())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue