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

Remove more uses of fmt. #478

Merged
merged 4 commits into from
Mar 3, 2023
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
5 changes: 0 additions & 5 deletions capnpc-go/fileparts.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ var (
{path: flowcontrolImport, name: "fc"},

// stdlib imports
{path: "fmt", name: "fmt"},
{path: "context", name: "context"},
{path: "math", name: "math"},
{path: "strconv", name: "strconv"},
Expand Down Expand Up @@ -106,10 +105,6 @@ func (i *imports) FlowControl() string {
return i.add(importSpec{path: flowcontrolImport, name: "fc"})
}

func (i *imports) Fmt() string {
return i.add(importSpec{path: "fmt", name: "fmt"})
}

func (i *imports) Context() string {
return i.add(importSpec{path: "context", name: "context"})
}
Expand Down
2 changes: 1 addition & 1 deletion capnpc-go/templates/interfaceClient
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (c {{$.Node.Name}}) WaitStreaming() error {
// should not be used to compare clients. Use IsSame to compare clients
// for equality.
func (c {{$.Node.Name}}) String() string {
return {{$.G.Imports.Fmt}}.Sprintf("%T(%v)", c, capnp.Client(c))
return "{{$.Node.Name}}(" + capnp.Client(c).String() + ")"
}

// AddRef creates a new Client that refers to the same capability as c.
Expand Down
19 changes: 13 additions & 6 deletions encoding/text/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package text

import (
"bytes"
"fmt"
"errors"
"io"
"math"
"strconv"

"capnproto.org/go/capnp/v3"
"capnproto.org/go/capnp/v3/internal/nodemap"
"capnproto.org/go/capnp/v3/internal/schema"
"capnproto.org/go/capnp/v3/internal/str"
"capnproto.org/go/capnp/v3/internal/strquote"
"capnproto.org/go/capnp/v3/schemas"
)
Expand Down Expand Up @@ -119,7 +120,7 @@ func (enc *Encoder) marshalStruct(typeID uint64, s capnp.Struct) error {
return err
}
if !n.IsValid() || n.Which() != schema.Node_Which_structNode {
return fmt.Errorf("cannot find struct type %#x", typeID)
return errors.New("cannot find struct type " + str.UToHex(typeID))
}
var discriminant uint16
if n.StructNode().DiscriminantCount() > 0 {
Expand Down Expand Up @@ -171,7 +172,11 @@ func (enc *Encoder) marshalFieldValue(s capnp.Struct, f schema.Field) error {
}
if dv.IsValid() && int(typ.Which()) != int(dv.Which()) {
name, _ := f.Name()
return fmt.Errorf("marshal field %s: default value is a %v, want %v", name, dv.Which(), typ.Which())
return errors.New(
"marshal field " + name +
": default value is a " + dv.Which().String() +
", want " + typ.Which().String(),
)
}
switch typ.Which() {
case schema.Type_Which_void:
Expand Down Expand Up @@ -277,7 +282,7 @@ func (enc *Encoder) marshalFieldValue(s capnp.Struct, f schema.Field) error {
case schema.Type_Which_anyPointer:
enc.w.WriteString(anyPointerMarker)
default:
return fmt.Errorf("unknown field type %v", typ.Which())
return errors.New("unknown field type " + typ.Which().String())
}
return nil
}
Expand Down Expand Up @@ -394,7 +399,7 @@ func (enc *Encoder) marshalList(elem schema.Type, l capnp.List) error {
}
enc.w.WriteByte(']')
default:
return fmt.Errorf("unknown list type %v", elem.Which())
return errors.New("unknown list type " + elem.Which().String())
}
return nil
}
Expand All @@ -405,7 +410,9 @@ func (enc *Encoder) marshalEnum(typ uint64, val uint16) error {
return err
}
if n.Which() != schema.Node_Which_enum {
return fmt.Errorf("marshaling enum of type @%#x: type is not an enum", typ)
return errors.New(
"marshaling enum of type @" + str.UToHex(typ) + ": type is not an enum",
)
}
enums, err := n.Enum().Enumerants()
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions flowcontrol/internal/test-tool/writer.capnp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions internal/aircraftlib/aircraft.capnp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 0 additions & 16 deletions list.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package capnp

import (
"bytes"
"errors"
"fmt"
"math"
"strconv"

Expand Down Expand Up @@ -1080,20 +1078,6 @@ func (s StructList[T]) Set(i int, v T) error {
return List(s).SetStruct(i, Struct(v))
}

// String returns the list in Cap'n Proto schema format (e.g. "[(x = 1), (x = 2)]").
func (s StructList[T]) String() string {
buf := &bytes.Buffer{}
buf.WriteByte('[')
for i := 0; i < s.Len(); i++ {
if i > 0 {
buf.WriteString(", ")
}
fmt.Fprint(buf, s.At(i))
}
buf.WriteByte(']')
return buf.String()
}

// A list of some Cap'n Proto capability type T.
type CapList[T ~ClientKind] PointerList

Expand Down
9 changes: 4 additions & 5 deletions rpc/internal/testcapnp/test.capnp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions schemas/schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import (
"bytes"
"compress/zlib"
"errors"
"fmt"
"io"
"io/ioutil"
"strings"
"sync"

"capnproto.org/go/capnp/v3/internal/str"
"capnproto.org/go/capnp/v3/packed"
)

Expand Down Expand Up @@ -164,15 +164,15 @@ type dupeError struct {
}

func (e *dupeError) Error() string {
return fmt.Sprintf("schemas: registered @%#x twice", e.id)
return "schemas: registered @" + str.UToHex(e.id) + " twice"
}

type notFoundError struct {
id uint64
}

func (e *notFoundError) Error() string {
return fmt.Sprintf("schemas: could not find @%#x", e.id)
return "schemas: could not find @" + str.UToHex(e.id)
}

type decompressError struct {
Expand All @@ -181,5 +181,5 @@ type decompressError struct {
}

func (e *decompressError) Error() string {
return fmt.Sprintf("schemas: decompressing schema for @%#x: %v", e.id, e.err)
return "schemas: decompressing schema for @" + str.UToHex(e.id) + ": " + e.err.Error()
}
3 changes: 1 addition & 2 deletions std/capnp/persistent/persistent.capnp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.