mirror of
https://github.com/netbirdio/netbird.git
synced 2025-02-21 20:51:50 +01:00
Refactor duplicate diff handling logic
This commit is contained in:
parent
ac06346f5c
commit
ca8565de1f
@ -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 {
|
func (d *NameServerComparator) Diff(cl *diff.Changelog, path []string, a, b reflect.Value) error {
|
||||||
if a.Kind() == reflect.Invalid {
|
if err := handleInvalidKind(cl, path, a, b); err != nil {
|
||||||
cl.Add(diff.CREATE, path, nil, b.Interface())
|
return err
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if b.Kind() == reflect.Invalid {
|
|
||||||
cl.Add(diff.DELETE, path, a.Interface(), nil)
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if a.Kind() == reflect.Slice && b.Kind() == reflect.Slice {
|
if a.Kind() == reflect.Slice && b.Kind() == reflect.Slice {
|
||||||
if a.Len() != b.Len() {
|
return handleSliceKind(d, cl, path, a, b)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ns1, ok1 := a.Interface().(nbdns.NameServer)
|
ns1, ok1 := a.Interface().(nbdns.NameServer)
|
||||||
@ -62,3 +46,29 @@ func (d *NameServerComparator) Diff(cl *diff.Changelog, path []string, a, b refl
|
|||||||
|
|
||||||
return nil
|
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
|
||||||
|
}
|
||||||
|
@ -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 {
|
func (d *RouteComparator) Diff(cl *diff.Changelog, path []string, a, b reflect.Value) error {
|
||||||
if a.Kind() == reflect.Invalid {
|
if err := handleInvalidKind(cl, path, a, b); err != nil {
|
||||||
cl.Add(diff.CREATE, path, nil, b.Interface())
|
return err
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if b.Kind() == reflect.Invalid {
|
|
||||||
cl.Add(diff.DELETE, path, a.Interface(), nil)
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle slice comparison
|
|
||||||
if a.Kind() == reflect.Slice && b.Kind() == reflect.Slice {
|
if a.Kind() == reflect.Slice && b.Kind() == reflect.Slice {
|
||||||
if a.Len() != b.Len() {
|
return handleSliceKind(d, cl, path, a, b)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
route1, ok1 := a.Interface().(*nbroute.Route)
|
route1, ok1 := a.Interface().(*nbroute.Route)
|
||||||
|
Loading…
Reference in New Issue
Block a user