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:
James Shubin
2023-11-18 15:15:33 -05:00
parent 88b414b9a3
commit d022d7f09e
3 changed files with 31 additions and 31 deletions

View File

@@ -46,14 +46,14 @@ type ValueRes struct {
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
// received value overwrites this value for the lifetime of the
// resource. It is interface{} because it can hold any type. It has
// 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
}
@@ -64,7 +64,7 @@ func (obj *ValueRes) Default() engine.Res {
// zero values of those types for those fields here... This will allow
// send/recv to not require an empty placeholder to type check.
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
// promptly, if they must not be lost, such as for cache invalidation.
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 we received on Value, and it changed, invalidate the cache!
obj.init.Logf("CheckApply: received on `Value`")
if val, exists := obj.init.Recv()["Any"]; exists && val.Changed {
// if we received on Any, and it changed, invalidate the cache!
obj.init.Logf("CheckApply: received on `Any`")
obj.isSet = true // we received something
obj.cachedValue = obj.Value
obj.cachedAny = obj.Any
}
// send
if obj.cachedValue != nil {
if obj.cachedAny != nil {
if err := obj.init.Send(&ValueSends{
Value: obj.cachedValue,
Any: obj.cachedAny,
}); err != nil {
return false, err
}
@@ -136,8 +136,8 @@ func (obj *ValueRes) Cmp(r engine.Res) error {
return fmt.Errorf("not a %s", obj.Kind())
}
if !reflect.DeepEqual(obj.Value, res.Value) {
return fmt.Errorf("the Value differs")
if !reflect.DeepEqual(obj.Any, res.Any) {
return fmt.Errorf("the Any field differs")
}
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.
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
// 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.
func (obj *ValueRes) Sends() interface{} {
return &ValueSends{
Value: nil,
Any: nil,
}
}

View File

@@ -1,15 +1,15 @@
value "hello1" {
#value => 42, # can be any type
value => "wow", # can be any type
#any => 42, # can be any type
any => "wow", # can be any type
}
value "hello2" {
value => "whatever", # TODO: remove the temporary placeholder here
#value => "", # XXX: remove any placeholder to see the bug when absent
any => "whatever", # TODO: remove the temporary placeholder here
#any => "", # XXX: remove any placeholder to see the bug when absent
}
test "test" {
#anotherstr => "", # get it from send/recv
}
Value["hello1"].value -> Value["hello2"].value
Value["hello2"].value -> Test["test"].anotherstr
Value["hello1"].any -> Value["hello2"].any
Value["hello2"].any -> Test["test"].anotherstr

View File

@@ -1,25 +1,25 @@
-- main.mcl --
value "hello1" {
#value => 42, # can be any type
value => "wow", # can be any type
#any => 42, # can be any type
any => "wow", # can be any type
}
value "hello2" {
value => "whatever", # TODO: remove the temporary placeholder here
#value => "", # XXX: remove any placeholder to see the bug when absent
any => "whatever", # TODO: remove the temporary placeholder here
#any => "", # XXX: remove any placeholder to see the bug when absent
}
test "test" {
#anotherstr => "", # get it from send/recv
}
Value["hello1"].value -> Value["hello2"].value
Value["hello2"].value -> Test["test"].anotherstr
Value["hello1"].any -> Value["hello2"].any
Value["hello2"].any -> Test["test"].anotherstr
-- OUTPUT --
Edge: value[hello1] -> value[hello2] # value[hello1] -> value[hello2]
Edge: value[hello2] -> test[test] # value[hello2] -> test[test]
Field: test[test].AnotherStr = "wow"
Field: value[hello1].Value = "wow"
Field: value[hello2].Value = "wow"
Field: value[hello1].Any = "wow"
Field: value[hello2].Any = "wow"
Vertex: test[test]
Vertex: value[hello1]
Vertex: value[hello2]