bkld empty maps/lists

This commit is contained in:
Ian Gulliver
2023-07-19 20:20:11 -07:00
parent 9f9a491a5b
commit f22307fba9
10 changed files with 24 additions and 2 deletions

View File

@@ -34,7 +34,7 @@ func diffMap(dst map[string]any, src any) (any, error) {
}
}
func diffMapMap(dst, src map[string]any) (map[string]any, error) {
func diffMapMap(dst, src map[string]any) (any, error) {
ret := map[string]any{}
for k, v := range dst {
@@ -63,6 +63,10 @@ func diffMapMap(dst, src map[string]any) (map[string]any, error) {
ret[k] = nil
}
if len(ret) == 0 {
return nil, nil
}
return ret, nil
}
@@ -76,7 +80,7 @@ func diffList(dst []any, src any) (any, error) {
}
}
func diffListList(dst, src []any) ([]any, error) { //nolint:unparam
func diffListList(dst, src []any) (any, error) { //nolint:unparam
ret := []any{}
outer1:
@@ -111,5 +115,9 @@ outer2:
}
}
if len(ret) == 0 {
return nil, nil
}
return ret, nil
}

View File

@@ -44,6 +44,10 @@ func GetFormat(name string) (*Format, error) {
}
func (f *Format) Marshal(v any) ([]byte, error) {
if v == nil {
return []byte{}, nil
}
ret, err := f.marshal(v)
if err != nil {
return nil, polyfill.ErrorsJoin(err, ErrMarshal)

View File

@@ -0,0 +1,2 @@
a:
- b: 2

View File

@@ -0,0 +1,2 @@
a:
- b: 2

1
tests/diff-list-same/cmd Normal file
View File

@@ -0,0 +1 @@
bkld a.yaml b.yaml

View File

View File

@@ -0,0 +1,2 @@
a:
b: 2

View File

@@ -0,0 +1,2 @@
a:
b: 2

1
tests/diff-map-same/cmd Normal file
View File

@@ -0,0 +1 @@
bkld a.yaml b.yaml

View File