Skip to content

Commit

Permalink
grpcreplay: remove extra spaces from prototext output (#54)
Browse files Browse the repository at this point in the history
This should reduce spurious diffs in PRs/CLs.
  • Loading branch information
jba authored Aug 7, 2024
1 parent af2830b commit 950aeed
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
1 change: 0 additions & 1 deletion grpcreplay/grpcreplay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -703,5 +703,4 @@ func TestSetInitial(t *testing.T) {
if got, want := rep.Initial(), initialState; !bytes.Equal(got, want) {
t.Errorf("got initial state %q, want %q", got, want)
}

}
26 changes: 26 additions & 0 deletions grpcreplay/text_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,39 @@ func (w *textWriter) writeEntry(e *entry) error {
if err != nil {
return err
}
bytes = removeExtraSpaces(bytes)
if _, err := fmt.Fprintln(w.w, len(bytes)); err != nil {
return err
}
_, err = fmt.Fprint(w.w, string(bytes))
return err
}

// Remove extra spaces after a colon in "field: value" lines.
// prototext inserts these to discourage its use as a stable format,
// but the more stability the better to avoid spurious diffs in CLs.
func removeExtraSpaces(bs []byte) []byte {
var buf bytes.Buffer
for len(bs) > 0 {
var line []byte
var nlfound bool
line, bs, nlfound = bytes.Cut(bs, []byte{'\n'})
field, value, found := bytes.Cut(line, []byte{':'})
if !found {
buf.Write(line)
} else {
buf.Write(field)
buf.WriteByte(':')
buf.WriteByte(' ')
buf.Write(bytes.TrimSpace(value))
}
if nlfound {
buf.WriteByte('\n')
}
}
return buf.Bytes()
}

type textReader struct {
r *bufio.Reader
file string
Expand Down
54 changes: 54 additions & 0 deletions grpcreplay/text_format_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package grpcreplay

import "testing"

func TestRemoveExtraSpaces(t *testing.T) {
in := `
fields: {
key: "value"
value: {
array_value: {
values: {
double_value: 0.30135461688041687
}
values: {
double_value: 0.3094993531703949
}
}
}
}
`
want := `
fields: {
key: "value"
value: {
array_value: {
values: {
double_value: 0.30135461688041687
}
values: {
double_value: 0.3094993531703949
}
}
}
}
`
got := string(removeExtraSpaces([]byte(in)))
if got != want {
t.Errorf("got\n%s\nwant\n%s", got, want)
}
}

0 comments on commit 950aeed

Please sign in to comment.