Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grpcreplay: remove extra spaces from prototext output #54

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
Loading