Skip to content

Commit

Permalink
engine: Transform the send/recv init functions into helpers
Browse files Browse the repository at this point in the history
Since we'll want to use them elsewhere, we should make these helper
functions. It also makes the code look a lot neater. Unfortunately, it
adds a bit more indirection, but this isn't a critical flaw here.
  • Loading branch information
purpleidea committed May 4, 2021
1 parent cdc5ca8 commit 4ccd622
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
21 changes: 2 additions & 19 deletions engine/graph/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,26 +169,9 @@ func (obj *State) Init() error {
}
return res.Refresh()
},
Send: func(st interface{}) error {
res, ok := obj.Vertex.(engine.SendableRes)
if !ok {
panic("res does not support the Sendable trait")
}
// XXX: type check this
//expected := res.Sends()
//if err := XXX_TYPE_CHECK(expected, st); err != nil {
// return err
//}

return res.Send(st) // send the struct
},
Recv: func() map[string]*engine.Send { // TODO: change this API?
res, ok := obj.Vertex.(engine.RecvableRes)
if !ok {
panic("res does not support the Recvable trait")
}
return res.Recv()
},
Send: engine.GenerateSendFunc(res),
Recv: engine.GenerateRecvFunc(res),

// FIXME: pass in a safe, limited query func instead?
// TODO: not implemented, use FilteredGraph
Expand Down
39 changes: 37 additions & 2 deletions engine/sendrecv.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ type SendableRes interface {
Sends() interface{}

// Send is used in CheckApply to send the desired data. It returns an
// error if the data is malformed or doesn't type check.
// error if the data is malformed or doesn't type check. You should use
// the GenerateSendFunc helper function to build this function for use
// in the resource internal state handle.
Send(st interface{}) error

// Sent returns the most recently sent data. This is used by the engine.
Expand All @@ -54,7 +56,8 @@ type RecvableRes interface {

// Recv is used by the resource to get information on changes. This data
// can be used to invalidate caches, restart watches, or it can be
// ignored entirely.
// ignored entirely. You should use the GenerateRecvFunc helper function
// to build this function for use in the resource internal state handle.
Recv() map[string]*Send
}

Expand All @@ -65,3 +68,35 @@ type Send struct {

Changed bool // set to true if this key was updated, read only!
}

// GenerateSendFunc generates the Send function using the resource of our
// choice for use in the resource internal state handle.
func GenerateSendFunc(res Res) func(interface{}) error {
return func(st interface{}) error {
//fmt.Printf("from: %+v\n", res)
//fmt.Printf("send: %+v\n", st)
r, ok := res.(SendableRes)
if !ok {
panic("res does not support the Sendable trait")
}
// XXX: type check this
//expected := r.Sends()
//if err := XXX_TYPE_CHECK(expected, st); err != nil {
// return err
//}

return r.Send(st) // send the struct
}
}

// GenerateRecvFunc generates the Recv function using the resource of our
// choice for use in the resource internal state handle.
func GenerateRecvFunc(res Res) func() map[string]*Send {
return func() map[string]*Send { // TODO: change this API?
r, ok := res.(RecvableRes)
if !ok {
panic("res does not support the Recvable trait")
}
return r.Recv()
}
}

0 comments on commit 4ccd622

Please sign in to comment.