Refactor duplicate diff handling logic

This commit is contained in:
bcmmbaga 2024-08-15 10:57:45 +03:00
parent ac06346f5c
commit ca8565de1f
No known key found for this signature in database
GPG Key ID: 7249A19D20613553
2 changed files with 32 additions and 39 deletions

View File

@ -20,28 +20,12 @@ func (d *NameServerComparator) Match(a, b reflect.Value) bool {
}
func (d *NameServerComparator) Diff(cl *diff.Changelog, path []string, a, b reflect.Value) error {
if a.Kind() == reflect.Invalid {
cl.Add(diff.CREATE, path, nil, b.Interface())
return nil
}
if b.Kind() == reflect.Invalid {
cl.Add(diff.DELETE, path, a.Interface(), nil)
return nil
if err := handleInvalidKind(cl, path, a, b); err != nil {
return err
}
if a.Kind() == reflect.Slice && b.Kind() == reflect.Slice {
if a.Len() != b.Len() {
cl.Add(diff.UPDATE, append(path, "length"), a.Len(), b.Len())
return nil
}
for i := 0; i < min(a.Len(), b.Len()); i++ {
err := d.Diff(cl, append(path, fmt.Sprintf("[%d]", i)), a.Index(i), b.Index(i))
if err != nil {
return err
}
}
return nil
return handleSliceKind(d, cl, path, a, b)
}
ns1, ok1 := a.Interface().(nbdns.NameServer)
@ -62,3 +46,29 @@ func (d *NameServerComparator) Diff(cl *diff.Changelog, path []string, a, b refl
return nil
}
func handleInvalidKind(cl *diff.Changelog, path []string, a, b reflect.Value) error {
if a.Kind() == reflect.Invalid {
cl.Add(diff.CREATE, path, nil, b.Interface())
return fmt.Errorf("invalid kind")
}
if b.Kind() == reflect.Invalid {
cl.Add(diff.DELETE, path, a.Interface(), nil)
return fmt.Errorf("invalid kind")
}
return nil
}
func handleSliceKind(comparator diff.ValueDiffer, cl *diff.Changelog, path []string, a, b reflect.Value) error {
if a.Len() != b.Len() {
cl.Add(diff.UPDATE, append(path, "length"), a.Len(), b.Len())
return nil
}
for i := 0; i < min(a.Len(), b.Len()); i++ {
if err := comparator.Diff(cl, append(path, fmt.Sprintf("[%d]", i)), a.Index(i), b.Index(i)); err != nil {
return err
}
}
return nil
}

View File

@ -21,29 +21,12 @@ func (d *RouteComparator) Match(a, b reflect.Value) bool {
}
func (d *RouteComparator) Diff(cl *diff.Changelog, path []string, a, b reflect.Value) error {
if a.Kind() == reflect.Invalid {
cl.Add(diff.CREATE, path, nil, b.Interface())
return nil
}
if b.Kind() == reflect.Invalid {
cl.Add(diff.DELETE, path, a.Interface(), nil)
return nil
if err := handleInvalidKind(cl, path, a, b); err != nil {
return err
}
// Handle slice comparison
if a.Kind() == reflect.Slice && b.Kind() == reflect.Slice {
if a.Len() != b.Len() {
cl.Add(diff.UPDATE, append(path, "length"), a.Len(), b.Len())
return nil
}
for i := 0; i < min(a.Len(), b.Len()); i++ {
err := d.Diff(cl, append(path, fmt.Sprintf("[%d]", i)), a.Index(i), b.Index(i))
if err != nil {
return err
}
}
return nil
return handleSliceKind(d, cl, path, a, b)
}
route1, ok1 := a.Interface().(*nbroute.Route)