Skip to content

Commit f5b3c7f

Browse files
DavidS-ovmactions-user
authored andcommitted
Switch back to full item yaml text diffs (#2655)
Since we removed the per-attribute signal rendering, this can go back to the more detailed text diffs over the whole yaml. This provides more information and has been bugging me since we changed it. This should also reduce the overall text size of the PR Comment and email notifications. GitOrigin-RevId: 973c988a8b59ceefa9ea558f52eafa25febaa3a4
1 parent ca446c7 commit f5b3c7f

File tree

2 files changed

+0
-121
lines changed

2 files changed

+0
-121
lines changed

sdp-go/util.go

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package sdp
22

33
import (
44
"fmt"
5-
"maps"
65
"math"
7-
"slices"
86
"strings"
97

108
"github.com/google/uuid"
@@ -84,61 +82,6 @@ func (a *AdapterMetadataList) Register(metadata *AdapterMetadata) *AdapterMetada
8482
return metadata
8583
}
8684

87-
// flatten the data map, so that we can use it to compare the attributes of two
88-
// items. It will recursively flatten the map and return a new map with the
89-
// flattened data.
90-
func flatten(data map[string]any) map[string]any {
91-
flattened := make(map[string]any)
92-
for k, v := range data {
93-
switch v := v.(type) {
94-
case map[string]any:
95-
for subK, subV := range flatten(v) {
96-
flattened[k+"."+subK] = subV
97-
}
98-
default:
99-
flattened[k] = v
100-
}
101-
}
102-
return flattened
103-
}
104-
105-
// RenderItemDiff generates a diff between two items
106-
func RenderItemDiff(before, after map[string]any) string {
107-
flatB := flatten(before)
108-
flatA := flatten(after)
109-
110-
allKeys := slices.Collect(maps.Keys(flatB))
111-
allKeys = slices.AppendSeq(allKeys, maps.Keys(flatA))
112-
113-
slices.Sort(allKeys)
114-
allKeys = slices.Compact(allKeys)
115-
116-
// allKeys now contains every attribute present in either the before or
117-
// after, so we can iterate over it to generate the diff and append stats.
118-
para := []string{}
119-
for _, key := range allKeys {
120-
beforeValue, beforeExists := flatB[key]
121-
afterValue, afterExists := flatA[key]
122-
123-
beforeValueStr := fmt.Sprintf("%v", beforeValue)
124-
afterValueStr := fmt.Sprintf("%v", afterValue)
125-
126-
if beforeExists && afterExists {
127-
if beforeValueStr != afterValueStr {
128-
// This is an update
129-
para = append(para, fmt.Sprintf("- %s: %s\n+ %s: %s", key, beforeValueStr, key, afterValueStr))
130-
}
131-
} else if beforeExists && !afterExists {
132-
// This is a deletion
133-
para = append(para, fmt.Sprintf("- %s: %s", key, beforeValueStr))
134-
} else if !beforeExists && afterExists {
135-
// This is a creation
136-
para = append(para, fmt.Sprintf("+ %s: %s", key, afterValueStr))
137-
}
138-
}
139-
return strings.Join(para, "\n")
140-
}
141-
14285
type RoutineRollUp struct {
14386
ChangeId uuid.UUID
14487
Gun string

sdp-go/util_test.go

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -64,70 +64,6 @@ func TestCalculatePaginationOffsetLimit(t *testing.T) {
6464
})
6565
}
6666

67-
func TestItemDiffParagraphRendering(t *testing.T) {
68-
t.Parallel()
69-
70-
// table driven tests for rendering item diffs
71-
tests := []struct {
72-
Name string
73-
Before map[string]any
74-
After map[string]any
75-
ExpectedDiffParagraph string
76-
}{
77-
{
78-
Name: "no changes",
79-
Before: map[string]any{
80-
"name": "test",
81-
"age": 30,
82-
},
83-
After: map[string]any{
84-
"name": "test",
85-
"age": 30,
86-
},
87-
ExpectedDiffParagraph: "",
88-
},
89-
{
90-
Name: "update changes",
91-
Before: map[string]any{
92-
"name": "test",
93-
"age": 30,
94-
},
95-
After: map[string]any{
96-
"name": "updated",
97-
"age": 30,
98-
},
99-
ExpectedDiffParagraph: "- name: test\n+ name: updated",
100-
},
101-
{
102-
Name: "nested map",
103-
Before: map[string]any{
104-
"name": map[string]any{
105-
"first": "test",
106-
"last": "user",
107-
},
108-
},
109-
After: map[string]any{
110-
"name": map[string]any{
111-
"first": "test",
112-
"last": "updated",
113-
},
114-
},
115-
ExpectedDiffParagraph: "- name.last: user\n+ name.last: updated",
116-
},
117-
}
118-
119-
for _, test := range tests {
120-
t.Run(test.Name, func(t *testing.T) {
121-
diff := RenderItemDiff(test.Before, test.After)
122-
123-
if diff != test.ExpectedDiffParagraph {
124-
t.Errorf("expected diff paragraph to be '%s', got '%s'", test.ExpectedDiffParagraph, diff)
125-
}
126-
})
127-
}
128-
129-
}
130-
13167
func TestGcpSANameFromAccountName(t *testing.T) {
13268
t.Parallel()
13369

0 commit comments

Comments
 (0)