Skip to content

Commit

Permalink
feat(kzip_create): allow embedding details messages in kzips (#3538)
Browse files Browse the repository at this point in the history
* feat(kzip_create): allow embedding details messages in kzips

* chore: address comments

* chore: make golint happy
  • Loading branch information
shahms committed Feb 22, 2019
1 parent f8de8e7 commit d0255f5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
5 changes: 4 additions & 1 deletion kythe/go/platform/tools/kzip/createcmd/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ go_library(
"//kythe/go/util/kytheuri",
"//kythe/proto:analysis_go_proto",
"//kythe/proto:storage_go_proto",
"@com_github_golang_protobuf//jsonpb:go_default_library_gen",
"@com_github_golang_protobuf//proto:go_default_library",
"@com_github_golang_protobuf//ptypes:go_default_library_gen",
"@com_github_google_subcommands//:go_default_library",
"@org_bitbucket_creachadair_stringset//:go_default_library",
"@io_bazel_rules_go//proto/wkt:any_go_proto",
],
)
39 changes: 32 additions & 7 deletions kythe/go/platform/tools/kzip/createcmd/createcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,30 @@ package createcmd

import (
"context"
"encoding/json"
"flag"
"os"
"strings"

"kythe.io/kythe/go/platform/kzip"
"kythe.io/kythe/go/util/cmdutil"
"kythe.io/kythe/go/util/kytheuri"

"github.com/golang/protobuf/jsonpb"
"github.com/google/subcommands"

anypb "github.com/golang/protobuf/ptypes/any"
apb "kythe.io/kythe/proto/analysis_go_proto"
spb "kythe.io/kythe/proto/storage_go_proto"
)

type createCommand struct {
cmdutil.Info

uri string
output string
source string
uri string
output string
source string
details string
}

// New creates a new subcommand for merging kzip files.
Expand All @@ -60,6 +65,7 @@ func (c *createCommand) SetFlags(fs *flag.FlagSet) {
fs.StringVar(&c.uri, "uri", "", "A Kythe URI naming the compilation unit")
fs.StringVar(&c.output, "output", "", "Path for output kzip file")
fs.StringVar(&c.source, "source", "", "Path for input source file")
fs.StringVar(&c.details, "details", "", "JSON-encoded Any messages to embed as compilation details.")
}

// Execute implements the subcommands interface and creates the requested file.
Expand All @@ -77,7 +83,7 @@ func (c *createCommand) Execute(ctx context.Context, fs *flag.FlagSet, _ ...inte
// within the specified Kythe URI.
uri, err := kytheuri.Parse(c.uri)
if err != nil {
return c.Fail("error parsing -uri: ", err)
return c.Fail("error parsing -uri: %v", err)
}
cu := &apb.CompilationUnit{
VName: &spb.VName{
Expand All @@ -90,21 +96,27 @@ func (c *createCommand) Execute(ctx context.Context, fs *flag.FlagSet, _ ...inte
SourceFile: []string{c.source},
}

details, err := unmarshalDetails(c.details)
if err != nil {
return c.Fail("error parsing -details: %v", err)
}
cu.Details = details

out, err := openWriter(c.output)
if err != nil {
return c.Fail("error opening -output: ", err)
return c.Fail("error opening -output: %v", err)
}

for _, path := range append([]string{c.source}, fs.Args()...) {
err := addFile(out, cu, path)
if err != nil {
return c.Fail("error adding file to -output: ", err)
return c.Fail("error adding file to -output: %v", err)
}
}

_, err = out.AddUnit(cu, nil)
if err != nil {
return c.Fail("error writing compilation to -output: ", err)
return c.Fail("error writing compilation to -output: %v", err)
}

if err := out.Close(); err != nil {
Expand Down Expand Up @@ -145,3 +157,16 @@ func addFile(out *kzip.Writer, cu *apb.CompilationUnit, path string) error {
})
return nil
}

func unmarshalDetails(details string) ([]*anypb.Any, error) {
var msg []*anypb.Any
dec := json.NewDecoder(strings.NewReader(details))
for dec.More() {
var detail anypb.Any
if err := jsonpb.UnmarshalNext(dec, &detail); err != nil {
return msg, err
}
msg = append(msg, &detail)
}
return msg, nil
}

0 comments on commit d0255f5

Please sign in to comment.