From 2d265641400536fd0f9fd2dbf8acc20046b2ed38 Mon Sep 17 00:00:00 2001 From: Cody Schroeder Date: Wed, 27 Jan 2021 13:27:35 -0800 Subject: [PATCH] fix(tooling): use protojson for metadata marshalling (#4806) --- kythe/go/util/metadata/BUILD | 5 ++-- kythe/go/util/metadata/metadata.go | 32 +++++++++++++++++++------ kythe/go/util/metadata/metadata_test.go | 29 +++++++++++----------- 3 files changed, 42 insertions(+), 24 deletions(-) diff --git a/kythe/go/util/metadata/BUILD b/kythe/go/util/metadata/BUILD index 39fd2bb78e..028542d728 100644 --- a/kythe/go/util/metadata/BUILD +++ b/kythe/go/util/metadata/BUILD @@ -9,6 +9,7 @@ go_library( "//kythe/go/util/schema/edges", "//kythe/proto:storage_go_proto", "@io_bazel_rules_go//proto/wkt:descriptor_go_proto", + "@org_golang_google_protobuf//encoding/protojson:go_default_library", ], ) @@ -18,7 +19,7 @@ go_test( srcs = ["metadata_test.go"], library = ":metadata", deps = [ - "//kythe/go/test/testutil", - "@com_github_golang_protobuf//proto:go_default_library", + "//kythe/go/util/compare", + "@org_golang_google_protobuf//proto:go_default_library", ], ) diff --git a/kythe/go/util/metadata/metadata.go b/kythe/go/util/metadata/metadata.go index 6cbe742420..a607c1f9f4 100644 --- a/kythe/go/util/metadata/metadata.go +++ b/kythe/go/util/metadata/metadata.go @@ -29,6 +29,8 @@ import ( "kythe.io/kythe/go/util/schema/edges" + "google.golang.org/protobuf/encoding/protojson" + protopb "github.com/golang/protobuf/protoc-gen-go/descriptor" spb "kythe.io/kythe/proto/storage_go_proto" ) @@ -53,11 +55,19 @@ func (rs Rules) MarshalJSON() ([]byte, error) { if r.EdgeIn == edges.DefinesBinding { rtype = "anchor_defines" } + var v json.RawMessage + if r.VName != nil { + var err error + v, err = protojson.Marshal(r.VName) + if err != nil { + return nil, err + } + } f.Meta[i] = rule{ Type: rtype, Begin: r.Begin, End: r.End, - VName: r.VName, + VName: v, Edge: kind, } } @@ -89,11 +99,11 @@ type file struct { // A rule is the encoded format of a single rule. type rule struct { - Type string `json:"type"` - Begin int `json:"begin"` - End int `json:"end"` - Edge string `json:"edge,omitempty"` - VName *spb.VName `json:"vname,omitempty"` + Type string `json:"type"` + Begin int `json:"begin"` + End int `json:"end"` + Edge string `json:"edge,omitempty"` + VName json.RawMessage `json:"vname,omitempty"` } // Parse parses a single JSON metadata object from r and returns the @@ -113,12 +123,20 @@ func Parse(r io.Reader) (Rules, error) { rs := make(Rules, len(f.Meta)) for i, meta := range f.Meta { + var v *spb.VName + if len(meta.VName) != 0 { + var msg spb.VName + if err := protojson.Unmarshal(meta.VName, &msg); err != nil { + return nil, err + } + v = &msg + } rs[i] = Rule{ Begin: meta.Begin, End: meta.End, EdgeOut: edges.Canonical(meta.Edge), Reverse: edges.IsReverse(meta.Edge), - VName: meta.VName, + VName: v, } switch t := meta.Type; t { case "nop": diff --git a/kythe/go/util/metadata/metadata_test.go b/kythe/go/util/metadata/metadata_test.go index 1ab210f819..572352cf61 100644 --- a/kythe/go/util/metadata/metadata_test.go +++ b/kythe/go/util/metadata/metadata_test.go @@ -22,11 +22,11 @@ import ( "strings" "testing" - "github.com/golang/protobuf/proto" - - "kythe.io/kythe/go/test/testutil" + "kythe.io/kythe/go/util/compare" "kythe.io/kythe/go/util/schema/edges" + "google.golang.org/protobuf/proto" + protopb "github.com/golang/protobuf/protoc-gen-go/descriptor" spb "kythe.io/kythe/proto/storage_go_proto" ) @@ -37,7 +37,7 @@ func TestParse(t *testing.T) { want Rules }{ // Minimal value: Just a plain type tag. - {`{"type":"kythe0"}`, nil}, + {`{"type":"kythe0"}`, Rules{}}, // NOP values, multiple rules. {`{"type":"kythe0","meta":[ @@ -82,15 +82,14 @@ func TestParse(t *testing.T) { continue } - if err := testutil.DeepEqual(test.want, got); err != nil { - t.Errorf("Parse %q: %v", test.input, err) + if diff := compare.ProtoDiff(test.want, got); diff != "" { + t.Errorf("Parse %q: %s", test.input, diff) } } } func TestRoundTrip(t *testing.T) { tests := []Rules{ - nil, Rules{}, Rules{{}}, Rules{ @@ -125,8 +124,8 @@ func TestRoundTrip(t *testing.T) { continue } - if err := testutil.DeepEqual(test, dec); err != nil { - t.Errorf("Round-trip of %+v failed: %v", test, err) + if diff := compare.ProtoDiff(test, dec); diff != "" { + t.Errorf("Round-trip of %+v failed: %s", test, diff) } } } @@ -136,8 +135,8 @@ func TestGeneratedCodeInfo(t *testing.T) { Annotation: []*protopb.GeneratedCodeInfo_Annotation{{ Path: []int32{1, 2, 3, 4, 5}, SourceFile: proto.String("a"), - Begin: proto.Int(1), - End: proto.Int(100), + Begin: proto.Int32(1), + End: proto.Int32(100), }}, } want := Rules{{ @@ -154,8 +153,8 @@ func TestGeneratedCodeInfo(t *testing.T) { }} { got := FromGeneratedCodeInfo(in, nil) - if err := testutil.DeepEqual(got, want); err != nil { - t.Errorf("FromGeneratedCodeInfo failed: %v", err) + if diff := compare.ProtoDiff(got, want); diff != "" { + t.Errorf("FromGeneratedCodeInfo failed: %s", diff) } } { @@ -163,8 +162,8 @@ func TestGeneratedCodeInfo(t *testing.T) { Corpus: "blargh", }) want[0].VName.Corpus = "blargh" - if err := testutil.DeepEqual(got, want); err != nil { - t.Errorf("FromGeneratedCodeInfo failed: %v", err) + if diff := compare.ProtoDiff(got, want); diff != "" { + t.Errorf("FromGeneratedCodeInfo failed: %s", diff) } } }