Skip to content

Commit

Permalink
Merge pull request #285 from capnproto/bugfix/re-export-cap-methods
Browse files Browse the repository at this point in the history
Add client methods to interfaceClient template
  • Loading branch information
zenhack authored Aug 5, 2022
2 parents 02af063 + eed93fb commit b987519
Show file tree
Hide file tree
Showing 6 changed files with 390 additions and 11 deletions.
9 changes: 5 additions & 4 deletions capnpc-go/capnpc-go.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ import (

// Non-stdlib import paths.
const (
capnpImport = "capnproto.org/go/capnp/v3"
textImport = capnpImport + "/encoding/text"
schemasImport = capnpImport + "/schemas"
serverImport = capnpImport + "/server"
capnpImport = "capnproto.org/go/capnp/v3"
textImport = capnpImport + "/encoding/text"
schemasImport = capnpImport + "/schemas"
serverImport = capnpImport + "/server"
flowcontrolImport = capnpImport + "/flowcontrol"
)

// genoptions are parameters that control code generation.
Expand Down
10 changes: 10 additions & 0 deletions capnpc-go/fileparts.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ func (i *imports) init() {
i.reserve(importSpec{path: schemasImport, name: "schemas"})
i.reserve(importSpec{path: serverImport, name: "server"})
i.reserve(importSpec{path: textImport, name: "text"})
i.reserve(importSpec{path: flowcontrolImport, name: "fc"})

i.reserve(importSpec{path: "fmt", name: "fmt"})
i.reserve(importSpec{path: "context", name: "context"})
i.reserve(importSpec{path: "math", name: "math"})
i.reserve(importSpec{path: "strconv", name: "strconv"})
Expand All @@ -89,6 +91,14 @@ func (i *imports) Text() string {
return i.add(importSpec{path: textImport, name: "text"})
}

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
47 changes: 47 additions & 0 deletions capnpc-go/templates/interfaceClient
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,36 @@ func (c {{$.Node.Name}}) {{.Name|title}}(ctx {{$.G.Imports.Context}}.Context, pa
}
{{end}}

// String returns a string that identifies this capability for debugging
// purposes. Its format should not be depended on: in particular, it
// 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))
}

// AddRef creates a new Client that refers to the same capability as c.
// If c is nil or has resolved to null, then AddRef returns nil.
func (c {{$.Node.Name}}) AddRef() {{$.Node.Name}} {
return {{$.Node.Name}}(capnp.Client(c).AddRef())
}

// Release releases a capability reference. If this is the last
// reference to the capability, then the underlying resources associated
// with the capability will be released.
//
// Release will panic if c has already been released, but not if c is
// nil or resolved to null.
func (c {{$.Node.Name}}) Release() {
capnp.Client(c).Release()
}

// Resolve blocks until the capability is fully resolved or the Context
// expires.
func (c {{$.Node.Name}}) Resolve(ctx context.Context) error {
return capnp.Client(c).Resolve(ctx)
}

func (c {{$.Node.Name}}) EncodeAsPtr(seg *capnp.Segment) capnp.Ptr {
return capnp.Client(c).EncodeAsPtr(seg)
}
Expand All @@ -37,6 +59,31 @@ func ({{$.Node.Name}}) DecodeFromPtr(p capnp.Ptr) {{$.Node.Name}} {
return {{$.Node.Name}}(capnp.Client{}.DecodeFromPtr(p))
}

// IsValid reports whether c is a valid reference to a capability.
// A reference is invalid if it is nil, has resolved to null, or has
// been released.
func (c {{$.Node.Name}}) IsValid() bool {
return capnp.Client(c).IsValid()
}

// IsSame reports whether c and other refer to a capability created by the
// same call to NewClient. This can return false negatives if c or other
// are not fully resolved: use Resolve if this is an issue. If either
// c or other are released, then IsSame panics.
func (c {{$.Node.Name}}) IsSame(other {{$.Node.Name}}) bool {
return capnp.Client(c).IsSame(capnp.Client(other))
}

// Update the flowcontrol.FlowLimiter used to manage flow control for
// this client. This affects all future calls, but not calls already
// waiting to send. Passing nil sets the value to flowcontrol.NopLimiter,
// which is also the default.
func (c {{$.Node.Name}}) SetFlowLimiter(lim {{.G.Imports.FlowControl}}.FlowLimiter) {
capnp.Client(c).SetFlowLimiter(lim)
}

// Get the current flowcontrol.FlowLimiter used to manage flow control
// for this client.
func (c {{$.Node.Name}}) GetFlowLimiter() {{.G.Imports.FlowControl}}.FlowLimiter {
return capnp.Client(c).GetFlowLimiter()
}
143 changes: 140 additions & 3 deletions internal/aircraftlib/aircraft.capnp.go

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

Loading

0 comments on commit b987519

Please sign in to comment.