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:
@@ -169,26 +169,9 @@ func (obj *State) Init() error {
|
|||||||
}
|
}
|
||||||
return res.Refresh()
|
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
|
Send: engine.GenerateSendFunc(res),
|
||||||
},
|
Recv: engine.GenerateRecvFunc(res),
|
||||||
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()
|
|
||||||
},
|
|
||||||
|
|
||||||
// FIXME: pass in a safe, limited query func instead?
|
// FIXME: pass in a safe, limited query func instead?
|
||||||
// TODO: not implemented, use FilteredGraph
|
// TODO: not implemented, use FilteredGraph
|
||||||
|
|||||||
@@ -33,7 +33,9 @@ type SendableRes interface {
|
|||||||
Sends() interface{}
|
Sends() interface{}
|
||||||
|
|
||||||
// Send is used in CheckApply to send the desired data. It returns an
|
// 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
|
Send(st interface{}) error
|
||||||
|
|
||||||
// Sent returns the most recently sent data. This is used by the engine.
|
// 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
|
// 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
|
// 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
|
Recv() map[string]*Send
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,3 +68,35 @@ type Send struct {
|
|||||||
|
|
||||||
Changed bool // set to true if this key was updated, read only!
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user