engine: resources: Rename the value field in value resource
Having a different name makes it more obvious, and also leaves us open to add a string or bool field for more explicit typing.
This commit is contained in:
@@ -46,15 +46,15 @@ type ValueRes struct {
|
|||||||
|
|
||||||
init *engine.Init
|
init *engine.Init
|
||||||
|
|
||||||
// Value is an arbitrary value to store in this resource. It can also be
|
// Any is an arbitrary value to store in this resource. It can also be
|
||||||
// sent via send/recv and received by the same mechanism as well. The
|
// sent via send/recv and received by the same mechanism as well. The
|
||||||
// received value overwrites this value for the lifetime of the
|
// received value overwrites this value for the lifetime of the
|
||||||
// resource. It is interface{} because it can hold any type. It has
|
// resource. It is interface{} because it can hold any type. It has
|
||||||
// pointer because it is only set if an actual value exists.
|
// pointer because it is only set if an actual value exists.
|
||||||
Value *interface{} `lang:"value" yaml:"value"`
|
Any *interface{} `lang:"any" yaml:"any"`
|
||||||
|
|
||||||
cachedValue *interface{}
|
cachedAny *interface{}
|
||||||
isSet bool
|
isSet bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Default returns some sensible defaults for this resource.
|
// Default returns some sensible defaults for this resource.
|
||||||
@@ -64,7 +64,7 @@ func (obj *ValueRes) Default() engine.Res {
|
|||||||
// zero values of those types for those fields here... This will allow
|
// zero values of those types for those fields here... This will allow
|
||||||
// send/recv to not require an empty placeholder to type check.
|
// send/recv to not require an empty placeholder to type check.
|
||||||
return &ValueRes{
|
return &ValueRes{
|
||||||
Value: nil, // XXX: use the zero value of the actual chosen type
|
Any: nil, // XXX: use the zero value of the actual chosen type
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,19 +107,19 @@ func (obj *ValueRes) CheckApply(ctx context.Context, apply bool) (bool, error) {
|
|||||||
// notification of change. Therefore, it is important to process these
|
// notification of change. Therefore, it is important to process these
|
||||||
// promptly, if they must not be lost, such as for cache invalidation.
|
// promptly, if they must not be lost, such as for cache invalidation.
|
||||||
if !obj.isSet {
|
if !obj.isSet {
|
||||||
obj.cachedValue = obj.Value // store anything we have if any
|
obj.cachedAny = obj.Any // store anything we have if any
|
||||||
}
|
}
|
||||||
if val, exists := obj.init.Recv()["Value"]; exists && val.Changed {
|
if val, exists := obj.init.Recv()["Any"]; exists && val.Changed {
|
||||||
// if we received on Value, and it changed, invalidate the cache!
|
// if we received on Any, and it changed, invalidate the cache!
|
||||||
obj.init.Logf("CheckApply: received on `Value`")
|
obj.init.Logf("CheckApply: received on `Any`")
|
||||||
obj.isSet = true // we received something
|
obj.isSet = true // we received something
|
||||||
obj.cachedValue = obj.Value
|
obj.cachedAny = obj.Any
|
||||||
}
|
}
|
||||||
|
|
||||||
// send
|
// send
|
||||||
if obj.cachedValue != nil {
|
if obj.cachedAny != nil {
|
||||||
if err := obj.init.Send(&ValueSends{
|
if err := obj.init.Send(&ValueSends{
|
||||||
Value: obj.cachedValue,
|
Any: obj.cachedAny,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
@@ -136,8 +136,8 @@ func (obj *ValueRes) Cmp(r engine.Res) error {
|
|||||||
return fmt.Errorf("not a %s", obj.Kind())
|
return fmt.Errorf("not a %s", obj.Kind())
|
||||||
}
|
}
|
||||||
|
|
||||||
if !reflect.DeepEqual(obj.Value, res.Value) {
|
if !reflect.DeepEqual(obj.Any, res.Any) {
|
||||||
return fmt.Errorf("the Value differs")
|
return fmt.Errorf("the Any field differs")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -145,16 +145,16 @@ func (obj *ValueRes) Cmp(r engine.Res) error {
|
|||||||
|
|
||||||
// ValueSends is the struct of data which is sent after a successful Apply.
|
// ValueSends is the struct of data which is sent after a successful Apply.
|
||||||
type ValueSends struct {
|
type ValueSends struct {
|
||||||
// Value is the generated value being sent. It is interface{} because it
|
// Any is the generated value being sent. It is interface{} because it
|
||||||
// can hold any type. It has pointer because it is only set if an actual
|
// can hold any type. It has pointer because it is only set if an actual
|
||||||
// value is actually being sent.
|
// value is actually being sent.
|
||||||
Value *interface{} `lang:"value"`
|
Any *interface{} `lang:"any"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sends represents the default struct of values we can send using Send/Recv.
|
// Sends represents the default struct of values we can send using Send/Recv.
|
||||||
func (obj *ValueRes) Sends() interface{} {
|
func (obj *ValueRes) Sends() interface{} {
|
||||||
return &ValueSends{
|
return &ValueSends{
|
||||||
Value: nil,
|
Any: nil,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,15 @@
|
|||||||
value "hello1" {
|
value "hello1" {
|
||||||
#value => 42, # can be any type
|
#any => 42, # can be any type
|
||||||
value => "wow", # can be any type
|
any => "wow", # can be any type
|
||||||
}
|
}
|
||||||
value "hello2" {
|
value "hello2" {
|
||||||
value => "whatever", # TODO: remove the temporary placeholder here
|
any => "whatever", # TODO: remove the temporary placeholder here
|
||||||
#value => "", # XXX: remove any placeholder to see the bug when absent
|
#any => "", # XXX: remove any placeholder to see the bug when absent
|
||||||
}
|
}
|
||||||
|
|
||||||
test "test" {
|
test "test" {
|
||||||
#anotherstr => "", # get it from send/recv
|
#anotherstr => "", # get it from send/recv
|
||||||
}
|
}
|
||||||
|
|
||||||
Value["hello1"].value -> Value["hello2"].value
|
Value["hello1"].any -> Value["hello2"].any
|
||||||
Value["hello2"].value -> Test["test"].anotherstr
|
Value["hello2"].any -> Test["test"].anotherstr
|
||||||
|
|||||||
@@ -1,25 +1,25 @@
|
|||||||
-- main.mcl --
|
-- main.mcl --
|
||||||
value "hello1" {
|
value "hello1" {
|
||||||
#value => 42, # can be any type
|
#any => 42, # can be any type
|
||||||
value => "wow", # can be any type
|
any => "wow", # can be any type
|
||||||
}
|
}
|
||||||
value "hello2" {
|
value "hello2" {
|
||||||
value => "whatever", # TODO: remove the temporary placeholder here
|
any => "whatever", # TODO: remove the temporary placeholder here
|
||||||
#value => "", # XXX: remove any placeholder to see the bug when absent
|
#any => "", # XXX: remove any placeholder to see the bug when absent
|
||||||
}
|
}
|
||||||
|
|
||||||
test "test" {
|
test "test" {
|
||||||
#anotherstr => "", # get it from send/recv
|
#anotherstr => "", # get it from send/recv
|
||||||
}
|
}
|
||||||
|
|
||||||
Value["hello1"].value -> Value["hello2"].value
|
Value["hello1"].any -> Value["hello2"].any
|
||||||
Value["hello2"].value -> Test["test"].anotherstr
|
Value["hello2"].any -> Test["test"].anotherstr
|
||||||
-- OUTPUT --
|
-- OUTPUT --
|
||||||
Edge: value[hello1] -> value[hello2] # value[hello1] -> value[hello2]
|
Edge: value[hello1] -> value[hello2] # value[hello1] -> value[hello2]
|
||||||
Edge: value[hello2] -> test[test] # value[hello2] -> test[test]
|
Edge: value[hello2] -> test[test] # value[hello2] -> test[test]
|
||||||
Field: test[test].AnotherStr = "wow"
|
Field: test[test].AnotherStr = "wow"
|
||||||
Field: value[hello1].Value = "wow"
|
Field: value[hello1].Any = "wow"
|
||||||
Field: value[hello2].Value = "wow"
|
Field: value[hello2].Any = "wow"
|
||||||
Vertex: test[test]
|
Vertex: test[test]
|
||||||
Vertex: value[hello1]
|
Vertex: value[hello1]
|
||||||
Vertex: value[hello2]
|
Vertex: value[hello2]
|
||||||
|
|||||||
Reference in New Issue
Block a user