Skip to content

Commit

Permalink
fix(tooling): use protojson for metadata marshalling (#4806)
Browse files Browse the repository at this point in the history
  • Loading branch information
schroederc committed Jan 27, 2021
1 parent bd329db commit 2d26564
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 24 deletions.
5 changes: 3 additions & 2 deletions kythe/go/util/metadata/BUILD
Expand Up @@ -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",
],
)

Expand All @@ -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",
],
)
32 changes: 25 additions & 7 deletions kythe/go/util/metadata/metadata.go
Expand Up @@ -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"
)
Expand All @@ -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,
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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":
Expand Down
29 changes: 14 additions & 15 deletions kythe/go/util/metadata/metadata_test.go
Expand Up @@ -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"
)
Expand All @@ -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":[
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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)
}
}
}
Expand All @@ -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{{
Expand All @@ -154,17 +153,17 @@ 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)
}
}
{
got := FromGeneratedCodeInfo(in, &spb.VName{
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)
}
}
}

0 comments on commit 2d26564

Please sign in to comment.