Skip to content
This repository has been archived by the owner on Jan 5, 2019. It is now read-only.

Commit

Permalink
Remove most String methods and change String to SourceRepresentation …
Browse files Browse the repository at this point in the history
…where appropriate
  • Loading branch information
bufdev committed Dec 7, 2017
1 parent fdbb05d commit fbbb36a
Show file tree
Hide file tree
Showing 11 changed files with 16 additions and 31 deletions.
3 changes: 0 additions & 3 deletions comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
package proto

import (
"fmt"
"strings"
"text/scanner"
)
Expand Down Expand Up @@ -161,5 +160,3 @@ func mergeOrReturnComment(elements []Visitee, lit string, pos scanner.Position)
}
return com
}

func (c *Comment) String() string { return fmt.Sprintf("<comment %s>", c.Message()) }
3 changes: 0 additions & 3 deletions extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
package proto

import (
"fmt"
"text/scanner"
)

Expand Down Expand Up @@ -56,5 +55,3 @@ func (e *Extensions) parse(p *Parser) error {
e.Ranges = list
return nil
}

func (e *Extensions) String() string { return fmt.Sprintf("<extensions %v>", e.Ranges) }
2 changes: 1 addition & 1 deletion extensions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 0 additions & 3 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
package proto

import (
"fmt"
"strconv"
"text/scanner"
)
Expand Down Expand Up @@ -164,8 +163,6 @@ func parseFieldAfterType(f *Field, p *Parser) error {
return nil
}

func (n *NormalField) String() string { return fmt.Sprintf("<field %s=%d>", n.Name, n.Sequence) }

// MapField represents a map entry in a message.
type MapField struct {
*Field
Expand Down
8 changes: 4 additions & 4 deletions formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
3 changes: 0 additions & 3 deletions group.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
package proto

import (
"fmt"
"text/scanner"
)

Expand Down Expand Up @@ -91,5 +90,3 @@ func (g *Group) parse(p *Parser) error {
}
return parseMessageBody(p, g)
}

func (g *Group) String() string { return fmt.Sprintf("<group %s>", g.Name) }
3 changes: 0 additions & 3 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
package proto

import (
"fmt"
"text/scanner"
)

Expand Down Expand Up @@ -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("<message %s>", m.Name) }
8 changes: 4 additions & 4 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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('"')
Expand Down
4 changes: 2 additions & 2 deletions range.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ type Range struct {
Max bool
}

// String return a single number if from = to. Returns <from> to <to> otherwise unless Max then return <from> to max.
func (r Range) String() string {
// SourceRepresentation return a single number if from = to. Returns <from> to <to> otherwise unless Max then return <from> to max.
func (r Range) SourceRepresentation() string {
if r.Max {
return fmt.Sprintf("%d to max", r.From)
}
Expand Down
6 changes: 3 additions & 3 deletions range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand All @@ -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)
}
}
Expand All @@ -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)
}
}
4 changes: 2 additions & 2 deletions reserved_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down

0 comments on commit fbbb36a

Please sign in to comment.