From 32916f9a6f65d638a4141680b31257dbf7b6337e Mon Sep 17 00:00:00 2001 From: James Shubin Date: Fri, 19 Jan 2024 18:52:31 -0500 Subject: [PATCH] engine: resources: Receive keys should match mcl, not golang The capitalization of these keys was wrong and they weren't getting seen. Add a test as well. --- docs/resource-guide.md | 6 ++-- engine/resources/file.go | 4 +-- engine/resources/kv.go | 4 +-- engine/resources/print.go | 4 +-- engine/resources/test.go | 32 +++++++++++++++---- engine/resources/test_test.go | 1 + engine/resources/value.go | 4 +-- .../TestAstFunc3/sendrecv-simple0.txtar | 27 ++++++++++++++++ 8 files changed, 65 insertions(+), 17 deletions(-) create mode 100644 lang/interpret_test/TestAstFunc3/sendrecv-simple0.txtar diff --git a/docs/resource-guide.md b/docs/resource-guide.md index e67a6526..445bc20d 100644 --- a/docs/resource-guide.md +++ b/docs/resource-guide.md @@ -678,10 +678,10 @@ receiving one. This can _only_ be done inside of the `CheckApply` function! ```golang // inside CheckApply, probably near the top -if val, exists := obj.init.Recv()["SomeKey"]; exists { - obj.init.Logf("the SomeKey param was sent to us from: %s.%s", val.Res, val.Key) +if val, exists := obj.init.Recv()["some_key"]; exists { + obj.init.Logf("the some_key param was sent to us from: %s.%s", val.Res, val.Key) if val.Changed { - obj.init.Logf("the SomeKey param was just updated!") + obj.init.Logf("the some_key param was just updated!") // you may want to invalidate some local cache } } diff --git a/engine/resources/file.go b/engine/resources/file.go index 1b1437cd..0dab5966 100644 --- a/engine/resources/file.go +++ b/engine/resources/file.go @@ -1226,9 +1226,9 @@ func (obj *FileRes) CheckApply(ctx context.Context, apply bool) (bool, error) { // might not have a new value to copy, and therefore we won't see this // notification of change. Therefore, it is important to process these // promptly, if they must not be lost, such as for cache invalidation. - if val, exists := obj.init.Recv()["Content"]; exists && val.Changed { + if val, exists := obj.init.Recv()["content"]; exists && val.Changed { // if we received on Content, and it changed, invalidate the cache! - obj.init.Logf("contentCheckApply: invalidating sha256sum of `Content`") + obj.init.Logf("contentCheckApply: invalidating sha256sum of `content`") obj.sha256sum = "" // invalidate!! } diff --git a/engine/resources/kv.go b/engine/resources/kv.go index 340c1bfe..87883e41 100644 --- a/engine/resources/kv.go +++ b/engine/resources/kv.go @@ -282,9 +282,9 @@ func (obj *KVRes) CheckApply(ctx context.Context, apply bool) (bool, error) { } }() - if val, exists := obj.init.Recv()["Value"]; exists && val.Changed { + if val, exists := obj.init.Recv()["value"]; exists && val.Changed { // if we received on Value, and it changed, wooo, nothing to do. - obj.init.Logf("CheckApply: `Value` was updated!") + obj.init.Logf("CheckApply: `value` was updated!") } value, exists, err := obj.kvGet(ctx, obj.getKey()) diff --git a/engine/resources/print.go b/engine/resources/print.go index ba84662c..0afca747 100644 --- a/engine/resources/print.go +++ b/engine/resources/print.go @@ -85,9 +85,9 @@ func (obj *PrintRes) Watch(ctx context.Context) error { // CheckApply method for Print resource. Does nothing, returns happy! func (obj *PrintRes) CheckApply(ctx context.Context, apply bool) (bool, error) { obj.init.Logf("CheckApply: %t", apply) - if val, exists := obj.init.Recv()["Msg"]; exists && val.Changed { + if val, exists := obj.init.Recv()["msg"]; exists && val.Changed { // if we received on Msg, and it changed, log message - obj.init.Logf("CheckApply: Received `Msg` of: %s", obj.Msg) + obj.init.Logf("CheckApply: Received `msg` of: %s", obj.Msg) } var refresh = obj.init.Refresh() diff --git a/engine/resources/test.go b/engine/resources/test.go index 30b92cd1..902566bc 100644 --- a/engine/resources/test.go +++ b/engine/resources/test.go @@ -91,12 +91,13 @@ type TestRes struct { // Func1 passes the value 42 to the input and returns a string. Func1 func(int) string `lang:"func1" yaml:"func1"` - ValidateBool bool `lang:"validatebool" yaml:"validate_bool"` // set to true to cause a validate error - ValidateError string `lang:"validateerror" yaml:"validate_error"` // set to cause a validate error - AlwaysGroup bool `lang:"alwaysgroup" yaml:"always_group"` // set to true to cause auto grouping - CompareFail bool `lang:"comparefail" yaml:"compare_fail"` // will compare fail? - SendValue string `lang:"sendvalue" yaml:"send_value"` // what value should we send? - OnlyShow []string `lang:"onlyshow" yaml:"only_show"` // what values do we show? + ValidateBool bool `lang:"validatebool" yaml:"validate_bool"` // set to true to cause a validate error + ValidateError string `lang:"validateerror" yaml:"validate_error"` // set to cause a validate error + AlwaysGroup bool `lang:"alwaysgroup" yaml:"always_group"` // set to true to cause auto grouping + CompareFail bool `lang:"comparefail" yaml:"compare_fail"` // will compare fail? + SendValue string `lang:"sendvalue" yaml:"send_value"` // what value should we send? + ExpectRecv *[]string `lang:"expectrecv" yaml:"expect_recv"` // what keys should we expect from send/recv? + OnlyShow []string `lang:"onlyshow" yaml:"only_show"` // what values do we show? // TODO: add more fun properties! @@ -146,14 +147,20 @@ func (obj *TestRes) Watch(ctx context.Context) error { // CheckApply method for Test resource. Does nothing, returns happy! func (obj *TestRes) CheckApply(ctx context.Context, apply bool) (bool, error) { + expectRecv := []string{} for key, val := range obj.init.Recv() { obj.init.Logf("CheckApply: Received `%s`, changed: %t", key, val.Changed) + expectRecv = append(expectRecv, key) } if obj.init.Refresh() { obj.init.Logf("Received a notification!") } + if obj.ExpectRecv != nil && len(*obj.ExpectRecv) != len(expectRecv) { + return false, fmt.Errorf("the received keys differ from expected, got: %+v", expectRecv) + } + fakeLogf := func(format string, v ...interface{}) { key := format[0:strings.LastIndex(format, ":")] if len(obj.OnlyShow) == 0 || util.StrInList(key, obj.OnlyShow) { @@ -372,6 +379,19 @@ func (obj *TestRes) Cmp(r engine.Res) error { if obj.SendValue != res.SendValue { return fmt.Errorf("the SendValue differs") } + if (obj.ExpectRecv == nil) != (res.ExpectRecv == nil) { // xor + return fmt.Errorf("the ExpectRecv differs") + } + if obj.ExpectRecv != nil && res.ExpectRecv != nil { + if len(*obj.ExpectRecv) != len(*res.ExpectRecv) { + return fmt.Errorf("the length of ExpectRecv differs") + } + for i, x := range *obj.ExpectRecv { + if x != (*res.ExpectRecv)[i] { + return fmt.Errorf("the item at ExpectRecv index %d differs", i) + } + } + } if len(obj.OnlyShow) != len(res.OnlyShow) { return fmt.Errorf("the length of OnlyShow differs") } diff --git a/engine/resources/test_test.go b/engine/resources/test_test.go index 9b7969c5..bffaebfe 100644 --- a/engine/resources/test_test.go +++ b/engine/resources/test_test.go @@ -168,6 +168,7 @@ func TestLowerStructFieldNameToFieldName2(t *testing.T) { "alwaysgroup": "AlwaysGroup", "comparefail": "CompareFail", "sendvalue": "SendValue", + "expectrecv": "ExpectRecv", "onlyshow": "OnlyShow", "comment": "Comment", diff --git a/engine/resources/value.go b/engine/resources/value.go index 9848edba..cf584fae 100644 --- a/engine/resources/value.go +++ b/engine/resources/value.go @@ -126,9 +126,9 @@ func (obj *ValueRes) CheckApply(ctx context.Context, apply bool) (bool, error) { received := false different := false checkOK := false - if val, exists := obj.init.Recv()["Any"]; exists && val.Changed { + 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.init.Logf("CheckApply: received on `any`") obj.isSet = true // we received something obj.cachedAny = obj.Any received = true // we'll always need to send below when we recv diff --git a/lang/interpret_test/TestAstFunc3/sendrecv-simple0.txtar b/lang/interpret_test/TestAstFunc3/sendrecv-simple0.txtar new file mode 100644 index 00000000..9165bbf0 --- /dev/null +++ b/lang/interpret_test/TestAstFunc3/sendrecv-simple0.txtar @@ -0,0 +1,27 @@ +-- main.mcl -- +# send/recv of value1.any into test.msg works! +value "value1" { + any => "i am value1", +} +test "test1" { + sendvalue => "hello from test", + expectrecv => ["anotherstr",], # expecting to recv on these keys! + + Meta:autogroup => false, +} +print "print1" { + #msg => "", # gets value from send_value above +} +Value["value1"].any -> Test["test1"].anotherstr +Test["test1"].hello -> Print["print1"].msg +-- OUTPUT -- +Edge: test[test1] -> print[print1] # test[test1] -> print[print1] +Edge: value[value1] -> test[test1] # value[value1] -> test[test1] +Field: print[print1].Msg = "hello from test" +Field: test[test1].AnotherStr = "i am value1" +Field: test[test1].ExpectRecv = ["anotherstr"] +Field: test[test1].SendValue = "hello from test" +Field: value[value1].Any = "i am value1" +Vertex: print[print1] +Vertex: test[test1] +Vertex: value[value1]