Skip to content

Commit

Permalink
Merge pull request capnproto#335 from capnproto/enhancement/any-list
Browse files Browse the repository at this point in the history
Add code generation for AnyList
  • Loading branch information
lthibault authored Nov 14, 2022
2 parents c279606 + 4fdd2a0 commit 072e16a
Show file tree
Hide file tree
Showing 19 changed files with 445 additions and 565 deletions.
32 changes: 23 additions & 9 deletions answer.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,12 @@ func (ans *Answer) Struct() (Struct, error) {
return ans.f.Struct()
}

// List waits until the answer is resolved and returns the list
// this answer represents.
func (ans *Answer) List() (List, error) {
return ans.f.List()
}

// Client returns the answer as a client. If the answer's originating
// call has not completed, then calls will be queued until the original
// call's completion. The client reference is borrowed: the caller
Expand Down Expand Up @@ -419,15 +425,29 @@ func (f *Future) Done() <-chan struct{} {
return f.promise.resolved
}

// Struct waits until the answer is resolved and returns the struct
// Ptr waits until the answer is resolved and returns the pointer
// this future represents.
func (f *Future) Struct() (Struct, error) {
func (f *Future) Ptr() (Ptr, error) {
p := f.promise
<-p.resolved
p.mu.Lock()
r := p.resolution()
p.mu.Unlock()
return r.strct(f.transform())
return r.ptr(f.transform())
}

// Struct waits until the answer is resolved and returns the struct
// this answer represents.
func (f *Future) Struct() (Struct, error) {
p, err := f.Ptr()
return p.Struct(), err
}

// List waits until the answer is resolved and returns the list
// this answer represents.
func (f *Future) List() (List, error) {
p, err := f.Ptr()
return p.List(), err
}

// Client returns the future as a client. If the answer's originating
Expand Down Expand Up @@ -589,12 +609,6 @@ func (r resolution) ptr(transform []PipelineOp) (Ptr, error) {
return p, nil
}

// strct obtains a Struct by applying a transform.
func (r resolution) strct(transform []PipelineOp) (Struct, error) {
p, err := r.ptr(transform)
return p.Struct(), err
}

// client obtains a Client by applying a transform.
func (r resolution) client(transform []PipelineOp) Client {
p, err := r.ptr(transform)
Expand Down
7 changes: 5 additions & 2 deletions capnpc-go/any_pointer.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ func (s structAnyPointerRenderStrategy) StructParams() any {
}

func (s structAnyPointerRenderStrategy) ListParams() any {
return s.PtrParams() // TODO(soon): implement AnyList
return structAnyListFieldParams{
structFieldParams: s.Params,
Default: s.Default,
}
}

func (s structAnyPointerRenderStrategy) CapabilityParams() any {
Expand All @@ -81,7 +84,7 @@ func (s promiseAnyPointerRenderStrategy) StructParams() any {
}

func (s promiseAnyPointerRenderStrategy) ListParams() any {
return s.PtrParams() // TODO(soon): implement AnyList
return promiseFieldAnyListParams(s)
}

func (s promiseAnyPointerRenderStrategy) CapabilityParams() any {
Expand Down
2 changes: 2 additions & 0 deletions capnpc-go/templateparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type (
structPointerFieldParams structObjectFieldParams
structStructFieldParams structObjectFieldParams
structAnyStructFieldParams structObjectFieldParams
structAnyListFieldParams structObjectFieldParams
)

type structBoolFieldParams struct {
Expand Down Expand Up @@ -161,6 +162,7 @@ type promiseFieldParams struct {
type (
promiseFieldAnyPointerParams promiseFieldParams
promiseCapabilityFieldParams promiseFieldParams
promiseFieldAnyListParams promiseFieldParams
)

type promiseFieldInterfaceParams struct {
Expand Down
7 changes: 3 additions & 4 deletions capnpc-go/templates/promise
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// {{.Node.Name}}_Future is a wrapper for a {{.Node.Name}} promised by a client call.
type {{.Node.Name}}_Future struct { *capnp.Future }

func (p {{.Node.Name}}_Future) Struct() ({{.Node.Name}}, error) {
s, err := p.Future.Struct()
return {{.Node.Name}}(s), err
func (f {{.Node.Name}}_Future) Struct() ({{.Node.Name}}, error) {
p, err := f.Future.Ptr()
return {{.Node.Name}}(p.Struct()), err
}

3 changes: 3 additions & 0 deletions capnpc-go/templates/promiseFieldAnyList
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
func (p {{ .Node.Name }}_Future) {{ .Field.Name|title }}() *capnp.Future {
return p.Future.Field({{ .Field.Slot.Offset }}, nil)
}
1 change: 0 additions & 1 deletion capnpc-go/templates/promiseFieldStruct
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ func (p {{.Node.Name}}_Future) {{.Field.Name|title}}() {{.G.RemoteNodeName .Stru
return {{.G.RemoteNodeName .Struct .Node}}_Future{Future: p.Future.Field(
{{- .Field.Slot.Offset}}, {{if .Default.IsValid}}{{.Default}}{{else}}nil{{end}})}
}

19 changes: 19 additions & 0 deletions capnpc-go/templates/structAnyListField
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
func (s {{.Node.Name}}) {{.Field.Name|title}}() (capnp.List, error) {
{{template "_checktag" . -}}
p, err := capnp.Struct(s).Ptr({{.Field.Slot.Offset}})
{{if .Default.IsValid -}}
if err != nil {
return capnp.List{}, err
}
return p.ListDefault({{.Default}})
{{- else -}}
return p.List(), err
{{- end}}
}

{{template "_hasfield" .}}

func (s {{.Node.Name}}) Set{{.Field.Name|title}}(v capnp.List) error {
{{template "_settag" . -}}
return capnp.Struct(s).SetPtr({{.Field.Slot.Offset}}, v.ToPtr())
}
1 change: 0 additions & 1 deletion capnpc-go/templates/structListField
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,3 @@ func (s {{.Node.Name}}) New{{.Field.Name|title}}(n int32) ({{.FieldType}}, error
err = capnp.Struct(s).SetPtr({{.Field.Slot.Offset}}, l.ToPtr())
return l, err
}

Loading

0 comments on commit 072e16a

Please sign in to comment.