-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
7 changed files
with
86 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package internal | ||
|
||
import ( | ||
"bufio" | ||
"encoding/binary" | ||
"fmt" | ||
"google.golang.org/protobuf/proto" | ||
"io" | ||
) | ||
|
||
type ProtoDelimitedReader struct { | ||
buf *bufio.Reader | ||
} | ||
|
||
func NewReader(r io.Reader) *ProtoDelimitedReader { | ||
return &ProtoDelimitedReader{ | ||
buf: bufio.NewReader(r), | ||
} | ||
} | ||
|
||
func (r *ProtoDelimitedReader) Next() ([]byte, error) { | ||
size, err := binary.ReadUvarint(r.buf) | ||
if err != nil { | ||
return nil, err | ||
} | ||
data := make([]byte, size) | ||
if _, err := io.ReadFull(r.buf, data); err != nil { | ||
return nil, err | ||
} | ||
return data, nil | ||
} | ||
|
||
func (r *ProtoDelimitedReader) ReadTargets() ([]*Target, error) { | ||
var targets []*Target | ||
var err error | ||
for buf, err := r.Next(); err == nil; buf, err = r.Next() { | ||
var target Target | ||
if err := proto.Unmarshal(buf, &target); err != nil { | ||
return nil, fmt.Errorf("failed to unmarshal Target message: %w", err) | ||
} | ||
targets = append(targets, &target) | ||
} | ||
if err != nil && err != io.EOF { | ||
return nil, fmt.Errorf("error while reading stdout from bazel command: %w", err) | ||
} | ||
return targets, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package internal | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"testing" | ||
) | ||
|
||
func TestDelimitedReader(t *testing.T) { | ||
protoBytes, err := ioutil.ReadFile("query.protodelim") | ||
if err != nil { | ||
t.Errorf("Error reading file") | ||
} | ||
reader := NewReader(bytes.NewReader(protoBytes)) | ||
targets, err := reader.ReadTargets() | ||
if err != nil { | ||
t.Errorf("error marshalling: %s", err) | ||
t.FailNow() | ||
} | ||
if len(targets) != 49 { | ||
t.Errorf("Expecting 49 targets but got %d", len(targets)) | ||
} | ||
} |
Binary file not shown.