diff --git a/engine/graph/state.go b/engine/graph/state.go index 31c799c3..7ae37305 100644 --- a/engine/graph/state.go +++ b/engine/graph/state.go @@ -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 diff --git a/engine/sendrecv.go b/engine/sendrecv.go index 0d489cfb..92cb6d94 100644 --- a/engine/sendrecv.go +++ b/engine/sendrecv.go @@ -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() + } +}