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

Get rid of formatting helpers that call out to fmt #412

Merged
merged 4 commits into from
Jan 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
24 changes: 15 additions & 9 deletions address.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ package capnp

import (
"strconv"

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

// An address is an index inside a segment's data (in bytes).
// It is bounded to [0, maxSegmentSize).
type address uint32

// String returns the address in hex format.
func (a address) String() string {
return strconv.FormatUint(uint64(a), 16)
Expand Down Expand Up @@ -58,12 +64,12 @@ func (sz Size) String() string {
if sz == 1 {
return "1 byte"
}
return fmtUdecimal(sz) + " bytes"
return str.Utod(sz) + " bytes"
}

// GoString returns the size as a Go expression.
func (sz Size) GoString() string {
return "capnp.Size(" + fmtUdecimal(sz) + ")"
return "capnp.Size(" + str.Utod(sz) + ")"
}

// times returns the size sz*n. ok is false if the result would be
Expand Down Expand Up @@ -120,12 +126,12 @@ func (off DataOffset) String() string {
if off == 1 {
return "+1 byte"
}
return "+" + fmtUdecimal(off) + " bytes"
return "+" + str.Utod(off) + " bytes"
}

// GoString returns the offset as a Go expression.
func (off DataOffset) GoString() string {
return "capnp.DataOffset(" + fmtUdecimal(off) + ")"
return "capnp.DataOffset(" + str.Utod(off) + ")"
}

// ObjectSize records section sizes for a struct or list.
Expand Down Expand Up @@ -177,13 +183,13 @@ func (sz ObjectSize) totalWordCount() int32 {
// String returns a short, human readable representation of the object
// size.
func (sz ObjectSize) String() string {
return "{datasz=" + fmtUdecimal(sz.DataSize) + " ptrs=" + fmtUdecimal(sz.PointerCount) + "}"
return "{datasz=" + str.Utod(sz.DataSize) + " ptrs=" + str.Utod(sz.PointerCount) + "}"
}

// GoString formats the ObjectSize as a keyed struct literal.
func (sz ObjectSize) GoString() string {
return "capnp.ObjectSize{DataSize: " + fmtUdecimal(sz.DataSize) +
", PointerCount: " + fmtUdecimal(sz.PointerCount) + "}"
return "capnp.ObjectSize{DataSize: " + str.Utod(sz.DataSize) +
", PointerCount: " + str.Utod(sz.PointerCount) + "}"
}

// BitOffset is an offset in bits from the beginning of a struct's data
Expand All @@ -202,10 +208,10 @@ func (bit BitOffset) mask() byte {

// String returns the offset in the format "bit X".
func (bit BitOffset) String() string {
return "bit " + fmtUdecimal(bit)
return "bit " + str.Utod(bit)
}

// GoString returns the offset as a Go expression.
func (bit BitOffset) GoString() string {
return "capnp.BitOffset(" + fmtUdecimal(bit) + ")"
return "capnp.BitOffset(" + str.Utod(bit) + ")"
}
20 changes: 15 additions & 5 deletions answer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package capnp

import (
"context"
"errors"
"strconv"
"sync"

"capnproto.org/go/capnp/v3/exc"
"capnproto.org/go/capnp/v3/internal/str"
"capnproto.org/go/capnp/v3/internal/syncutil"
)

Expand Down Expand Up @@ -568,28 +570,36 @@ func Transform(p Ptr, transform []PipelineOp) (Ptr, error) {
for i, op := range transform[:n-1] {
field, err := s.Ptr(op.Field)
if err != nil {
return Ptr{}, errorf("transform: op %d: pointer field %d: %v", i, op.Field, err)
return Ptr{}, newTransformError(i, op.Field, err, false)
}
s, err = field.StructDefault(op.DefaultValue)
if err != nil {
return Ptr{}, errorf("transform: op %d: pointer field %d with default: %v", i, op.Field, err)
return Ptr{}, newTransformError(i, op.Field, err, true)
}
}
op := transform[n-1]
p, err := s.Ptr(op.Field)
if err != nil {
return Ptr{}, errorf("transform: op %d: pointer field %d: %v", n-1, op.Field, err)
return Ptr{}, newTransformError(n-1, op.Field, err, false)
}
if op.DefaultValue != nil {
p, err = p.Default(op.DefaultValue)
if err != nil {
return Ptr{}, errorf("transform: op %d: pointer field %d with default: %v", n-1, op.Field, err)
return Ptr{}, newTransformError(n-1, op.Field, err, true)
}
return p, nil
}
return p, nil
}

func newTransformError(index int, field uint16, err error, withDefault bool) error {
msg := "transform: op " + str.Itod(index) + ": pointer field " + str.Utod(field)
if withDefault {
msg += " with default"
}
return exc.WrapError(msg, err)
}

// A resolution is the outcome of a future.
type resolution struct {
method Method
Expand Down Expand Up @@ -617,7 +627,7 @@ func (r resolution) client(transform []PipelineOp) Client {
}
iface := p.Interface()
if p.IsValid() && !iface.IsValid() {
return ErrorClient(errorf("not a capability"))
return ErrorClient(errors.New("not a capability"))
}
return iface.Client()
}
Expand Down
33 changes: 19 additions & 14 deletions canonical.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package capnp

import (
"capnproto.org/go/capnp/v3/exc"
"capnproto.org/go/capnp/v3/internal/str"
)

// Canonicalize encodes a struct into its canonical form: a single-
// segment blob without a segment table. The result will be identical
// for equivalent structs, even as the schema evolves. The blob is
Expand All @@ -11,13 +16,13 @@ func Canonicalize(s Struct) ([]byte, error) {
}
root, err := NewRootStruct(seg, canonicalStructSize(s))
if err != nil {
return nil, errorf("canonicalize: %v", err)
return nil, exc.WrapError("canonicalize", err)
}
if err := msg.SetRoot(root.ToPtr()); err != nil {
return nil, errorf("canonicalize: %v", err)
return nil, exc.WrapError("canonicalize", err)
}
if err := fillCanonicalStruct(root, s); err != nil {
return nil, annotatef(err, "canonicalize")
return nil, exc.WrapError("canonicalize", err)
}
return seg.Data(), nil
}
Expand All @@ -30,7 +35,7 @@ func canonicalPtr(dst *Segment, p Ptr) (Ptr, error) {
case structPtrType:
ss, err := NewStruct(dst, canonicalStructSize(p.Struct()))
if err != nil {
return Ptr{}, errorf("struct: %v", err)
return Ptr{}, exc.WrapError("struct", err)
}
if err := fillCanonicalStruct(ss, p.Struct()); err != nil {
return Ptr{}, err
Expand All @@ -55,14 +60,14 @@ func fillCanonicalStruct(dst, s Struct) error {
for i := uint16(0); i < dst.size.PointerCount; i++ {
p, err := s.Ptr(i)
if err != nil {
return annotatef(err, "struct pointer %d", i)
return exc.WrapError("struct pointer "+str.Utod(i), err)
}
cp, err := canonicalPtr(dst.seg, p)
if err != nil {
return annotatef(err, "struct pointer %d", i)
return exc.WrapError("struct pointer "+str.Utod(i), err)
}
if err := dst.SetPtr(i, cp); err != nil {
return annotatef(err, "struct pointer %d", i)
return exc.WrapError("struct pointer "+str.Utod(i), err)
}
}
return nil
Expand Down Expand Up @@ -98,7 +103,7 @@ func canonicalList(dst *Segment, l List) (List, error) {
sz := l.allocSize()
_, newAddr, err := alloc(dst, sz)
if err != nil {
return List{}, errorf("list: %v", err)
return List{}, exc.WrapError("list", err)
}
cl := List{
seg: dst,
Expand All @@ -115,19 +120,19 @@ func canonicalList(dst *Segment, l List) (List, error) {
if l.flags&isCompositeList == 0 {
cl, err := NewPointerList(dst, l.length)
if err != nil {
return List{}, errorf("list: %v", err)
return List{}, exc.WrapError("list", err)
}
for i := 0; i < l.Len(); i++ {
p, err := PointerList(l).At(i)
if err != nil {
return List{}, errorf("list element %d: %v", i, err)
return List{}, exc.WrapError("list element "+str.Itod(i), err)
}
cp, err := canonicalPtr(dst, p)
if err != nil {
return List{}, annotatef(err, "list element %d", i)
return List{}, exc.WrapError("list element "+str.Itod(i), err)
}
if err := cl.Set(i, cp); err != nil {
return List{}, errorf("list element %d: %v", i, err)
return List{}, exc.WrapError("list element "+str.Itod(i), err)
}
}
return List(cl), nil
Expand All @@ -146,11 +151,11 @@ func canonicalList(dst *Segment, l List) (List, error) {
}
cl, err := NewCompositeList(dst, elemSize, l.length)
if err != nil {
return List{}, errorf("list: %v", err)
return List{}, exc.WrapError("list", err)
}
for i := 0; i < cl.Len(); i++ {
if err := fillCanonicalStruct(cl.Struct(i), l.Struct(i)); err != nil {
return List{}, annotatef(err, "list element %d", i)
return List{}, exc.WrapError("list element "+str.Itod(i), err)
}
}
return cl, nil
Expand Down
18 changes: 10 additions & 8 deletions capability.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package capnp

import (
"context"
"errors"
"fmt"
"runtime"
"strconv"
"sync"

"capnproto.org/go/capnp/v3/exp/bufferpool"
"capnproto.org/go/capnp/v3/flowcontrol"
"capnproto.org/go/capnp/v3/internal/str"
"capnproto.org/go/capnp/v3/internal/syncutil"
)

Expand Down Expand Up @@ -94,12 +96,12 @@ type CapabilityID uint32

// String returns the ID in the format "capability X".
func (id CapabilityID) String() string {
return "capability " + fmtUdecimal(id)
return "capability " + str.Utod(id)
}

// GoString returns the ID as a Go expression.
func (id CapabilityID) GoString() string {
return "capnp.CapabilityID(" + fmtUdecimal(id) + ")"
return "capnp.CapabilityID(" + str.Utod(id) + ")"
}

// A Client is a reference to a Cap'n Proto capability.
Expand Down Expand Up @@ -313,10 +315,10 @@ func (c Client) SendCall(ctx context.Context, s Send) (*Answer, ReleaseFunc) {
h, _, released, finish := c.startCall()
defer finish()
if released {
return ErrorAnswer(s.Method, errorf("call on released client")), func() {}
return ErrorAnswer(s.Method, errors.New("call on released client")), func() {}
}
if h == nil {
return ErrorAnswer(s.Method, errorf("call on null client")), func() {}
return ErrorAnswer(s.Method, errors.New("call on null client")), func() {}
}

limiter := c.GetFlowLimiter()
Expand Down Expand Up @@ -391,11 +393,11 @@ func (c Client) RecvCall(ctx context.Context, r Recv) PipelineCaller {
h, _, released, finish := c.startCall()
defer finish()
if released {
r.Reject(errorf("call on released client"))
r.Reject(errors.New("call on released client"))
return nil
}
if h == nil {
r.Reject(errorf("call on null client"))
r.Reject(errors.New("call on null client"))
return nil
}
return h.Recv(ctx, r)
Expand Down Expand Up @@ -430,7 +432,7 @@ func (c Client) Resolve(ctx context.Context) error {
for {
h, released, resolved := c.peek()
if released {
return errorf("cannot resolve released client")
return errors.New("cannot resolve released client")
}

if resolved {
Expand Down Expand Up @@ -655,7 +657,7 @@ func finalizeClient(c *client) {
msg = "leaked client created by " + fname
} else {
msg = "leaked client created by " + fname + " on " +
c.creatorFile + ":" + fmtIdecimal(c.creatorLine)
c.creatorFile + ":" + str.Itod(c.creatorLine)
}
if c.creatorStack != "" {
msg += "\nCreation stack trace:\n" + c.creatorStack + "\n"
Expand Down
8 changes: 0 additions & 8 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,3 @@ func Disconnected(s string) error {
func IsDisconnected(e error) bool {
return exc.TypeOf(e) == exc.Disconnected
}

func errorf(format string, args ...any) error {
return capnperr.Failedf(format, args...)
}

func annotatef(err error, format string, args ...any) error {
return capnperr.Annotatef(err, format, args...)
}
17 changes: 6 additions & 11 deletions exc/exc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package exc

import (
"errors"
"fmt"
"strconv"
)

Expand Down Expand Up @@ -108,30 +107,26 @@ func (f Annotator) Failed(err error) *Exception {
return f.New(Failed, err)
}

func (f Annotator) Failedf(format string, args ...any) *Exception {
return f.Failed(fmt.Errorf(format, args...))
func (f Annotator) WrapFailed(msg string, err error) *Exception {
return f.New(Failed, WrapError(msg, err))
}

func (f Annotator) Disconnected(err error) *Exception {
return f.New(Disconnected, err)
}

func (f Annotator) Disconnectedf(format string, args ...any) *Exception {
return f.Disconnected(fmt.Errorf(format, args...))
func (f Annotator) WrapDisconnected(msg string, err error) *Exception {
return f.New(Disconnected, WrapError(msg, err))
}

func (f Annotator) Unimplemented(err error) *Exception {
return f.New(Unimplemented, err)
}

func (f Annotator) Unimplementedf(format string, args ...any) *Exception {
return f.Unimplemented(fmt.Errorf(format, args...))
func (f Annotator) WrapUnimplemented(msg string, err error) *Exception {
return f.New(Unimplemented, WrapError(msg, err))
}

func (f Annotator) Annotate(err error, msg string) *Exception {
return Annotate(string(f), msg, err)
}

func (f Annotator) Annotatef(err error, format string, args ...any) *Exception {
return f.Annotate(err, fmt.Sprintf(format, args...))
}
Loading