-
Notifications
You must be signed in to change notification settings - Fork 2
/
protobuf.go
45 lines (35 loc) · 897 Bytes
/
protobuf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package utils
import (
"fmt"
"github.com/wI2L/jsondiff"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
func ProtobufDifference(expected, actual proto.Message) (string, error) {
if proto.Equal(expected, actual) {
return "", nil
}
opts := protojson.MarshalOptions{
Indent: " ",
}
expectedDump, err := opts.Marshal(expected)
if err != nil {
return "", fmt.Errorf("marshal expected: %w", err)
}
actualDump, err := opts.Marshal(actual)
if err != nil {
return "", fmt.Errorf("marshal actual: %w", err)
}
patch, err := jsondiff.CompareJSON(actualDump, expectedDump)
if err != nil {
return "", fmt.Errorf("compare json: %w", err)
}
return patch.String(), nil
}
func MustProtobufDifference(expected, actual proto.Message) string {
out, err := ProtobufDifference(expected, actual)
if err != nil {
panic(err)
}
return out
}