From fbbb36ab2cdd8a99b49c7d3294dc548e42e3c584 Mon Sep 17 00:00:00 2001 From: Peter Edge Date: Thu, 7 Dec 2017 16:28:33 -0500 Subject: [PATCH] Remove most String methods and change String to SourceRepresentation where appropriate --- comment.go | 3 --- extensions.go | 3 --- extensions_test.go | 2 +- field.go | 3 --- formatter.go | 8 ++++---- group.go | 3 --- message.go | 3 --- option.go | 8 ++++---- range.go | 4 ++-- range_test.go | 6 +++--- reserved_test.go | 4 ++-- 11 files changed, 16 insertions(+), 31 deletions(-) diff --git a/comment.go b/comment.go index bead3fd..7b74e6f 100644 --- a/comment.go +++ b/comment.go @@ -24,7 +24,6 @@ package proto import ( - "fmt" "strings" "text/scanner" ) @@ -161,5 +160,3 @@ func mergeOrReturnComment(elements []Visitee, lit string, pos scanner.Position) } return com } - -func (c *Comment) String() string { return fmt.Sprintf("", c.Message()) } diff --git a/extensions.go b/extensions.go index 567c795..7e52d93 100644 --- a/extensions.go +++ b/extensions.go @@ -24,7 +24,6 @@ package proto import ( - "fmt" "text/scanner" ) @@ -56,5 +55,3 @@ func (e *Extensions) parse(p *Parser) error { e.Ranges = list return nil } - -func (e *Extensions) String() string { return fmt.Sprintf("", e.Ranges) } diff --git a/extensions_test.go b/extensions_test.go index 7d95e0c..b46910f 100644 --- a/extensions_test.go +++ b/extensions_test.go @@ -47,7 +47,7 @@ func TestExtensions(t *testing.T) { if got, want := f.Position.Line, 3; got != want { t.Fatalf("got [%d] want [%d]", got, want) } - if got, want := f.Ranges[1].String(), "20 to max"; got != want { + if got, want := f.Ranges[1].SourceRepresentation(), "20 to max"; got != want { t.Errorf("got [%s] want [%s]", got, want) } if f.Comment == nil { diff --git a/field.go b/field.go index df1f7cb..c9c8a1e 100644 --- a/field.go +++ b/field.go @@ -24,7 +24,6 @@ package proto import ( - "fmt" "strconv" "text/scanner" ) @@ -164,8 +163,6 @@ func parseFieldAfterType(f *Field, p *Parser) error { return nil } -func (n *NormalField) String() string { return fmt.Sprintf("", n.Name, n.Sequence) } - // MapField represents a map entry in a message. type MapField struct { *Field diff --git a/formatter.go b/formatter.go index 8340803..5b8c0f7 100644 --- a/formatter.go +++ b/formatter.go @@ -107,13 +107,13 @@ func (f *Formatter) VisitOption(o *Option) { f.level(1) for _, each := range o.AggregatedConstants { f.indent(0) - fmt.Fprintf(f.w, "%s: %s\n", each.Name, each.Literal.String()) + fmt.Fprintf(f.w, "%s: %s\n", each.Name, each.Literal.SourceRepresentation()) } f.indent(-1) fmt.Fprintf(f.w, "}") } else { // TODO printAs groups with fixed length - fmt.Fprintf(f.w, o.Constant.String()) + fmt.Fprintf(f.w, o.Constant.SourceRepresentation()) } fmt.Fprintf(f.w, ";") if o.InlineComment != nil { @@ -177,7 +177,7 @@ func (f *Formatter) VisitReserved(r *Reserved) { if i > 0 { io.WriteString(f.w, ", ") } - fmt.Fprintf(f.w, "%s", each.String()) + fmt.Fprintf(f.w, "%s", each.SourceRepresentation()) } } else { for i, each := range r.FieldNames { @@ -230,7 +230,7 @@ func (f *Formatter) VisitExtensions(e *Extensions) { if i > 0 { io.WriteString(f.w, ", ") } - fmt.Fprintf(f.w, "%s", each.String()) + fmt.Fprintf(f.w, "%s", each.SourceRepresentation()) } f.endWithComment(e.InlineComment) } diff --git a/group.go b/group.go index 83b7239..ec9f2f2 100644 --- a/group.go +++ b/group.go @@ -24,7 +24,6 @@ package proto import ( - "fmt" "text/scanner" ) @@ -91,5 +90,3 @@ func (g *Group) parse(p *Parser) error { } return parseMessageBody(p, g) } - -func (g *Group) String() string { return fmt.Sprintf("", g.Name) } diff --git a/message.go b/message.go index e64a581..6efb099 100644 --- a/message.go +++ b/message.go @@ -24,7 +24,6 @@ package proto import ( - "fmt" "text/scanner" ) @@ -225,5 +224,3 @@ func (m *Message) takeLastComment() (last *Comment) { func (m *Message) Doc() *Comment { return m.Comment } - -func (m *Message) String() string { return fmt.Sprintf("", m.Name) } diff --git a/option.go b/option.go index be32f5f..3a7229a 100644 --- a/option.go +++ b/option.go @@ -65,9 +65,9 @@ func (o *Option) keyValuePair(embedded bool) (cols []aligned) { equals := alignedEquals name := o.Name if embedded { - return append(cols, leftAligned(name), equals, leftAligned(o.Constant.String())) // numbers right, strings left? TODO + return append(cols, leftAligned(name), equals, leftAligned(o.Constant.SourceRepresentation())) // numbers right, strings left? TODO } - return append(cols, rightAligned(name), equals, rightAligned(o.Constant.String())) + return append(cols, rightAligned(name), equals, rightAligned(o.Constant.SourceRepresentation())) } // parse reads an Option body @@ -145,8 +145,8 @@ type Literal struct { IsString bool } -// String returns the source (if quoted then use double quote). -func (l Literal) String() string { +// SourceRepresentation returns the source (if quoted then use double quote). +func (l Literal) SourceRepresentation() string { var buf bytes.Buffer if l.IsString { buf.WriteRune('"') diff --git a/range.go b/range.go index 881454e..7cc3f1f 100644 --- a/range.go +++ b/range.go @@ -34,8 +34,8 @@ type Range struct { Max bool } -// String return a single number if from = to. Returns to otherwise unless Max then return to max. -func (r Range) String() string { +// SourceRepresentation return a single number if from = to. Returns to otherwise unless Max then return to max. +func (r Range) SourceRepresentation() string { if r.Max { return fmt.Sprintf("%d to max", r.From) } diff --git a/range_test.go b/range_test.go index f698cc1..e08d75f 100644 --- a/range_test.go +++ b/range_test.go @@ -33,7 +33,7 @@ func TestParseRanges(t *testing.T) { if err != nil { t.Fatal(err) } - if got, want := ranges[2].String(), "9 to 11"; got != want { + if got, want := ranges[2].SourceRepresentation(), "9 to 11"; got != want { t.Errorf("got [%v] want [%v]", got, want) } } @@ -46,7 +46,7 @@ func TestParseRangesMax(t *testing.T) { if err != nil { t.Fatal(err) } - if got, want := ranges[0].String(), "3 to max"; got != want { + if got, want := ranges[0].SourceRepresentation(), "3 to max"; got != want { t.Errorf("got [%v] want [%v]", got, want) } } @@ -62,7 +62,7 @@ func TestParseRangesMultiToMax(t *testing.T) { if got, want := len(ranges), 4; got != want { t.Fatalf("got [%v] want [%v]", got, want) } - if got, want := ranges[3].String(), "10 to max"; got != want { + if got, want := ranges[3].SourceRepresentation(), "10 to max"; got != want { t.Errorf("got [%v] want [%v]", got, want) } } diff --git a/reserved_test.go b/reserved_test.go index 572583e..817cb5a 100644 --- a/reserved_test.go +++ b/reserved_test.go @@ -36,10 +36,10 @@ func TestReservedRanges(t *testing.T) { if err != nil { t.Fatal(err) } - if got, want := r.Ranges[0].String(), "2"; got != want { + if got, want := r.Ranges[0].SourceRepresentation(), "2"; got != want { t.Fatalf("got [%v] want [%v]", got, want) } - if got, want := r.Ranges[2].String(), "9 to 11"; got != want { + if got, want := r.Ranges[2].SourceRepresentation(), "9 to 11"; got != want { t.Errorf("got [%v] want [%v]", got, want) } }