engine: Transform the send/recv init functions into helpers

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.
This commit is contained in:
James Shubin
2020-04-15 10:04:06 -04:00
parent cdc5ca8854
commit 58998f9cab
2 changed files with 39 additions and 21 deletions

View File

@@ -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

View File

@@ -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.
@@ -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
}
@@ -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()
}
}