This is an automated email from the ASF dual-hosted git repository.

sjwiesman pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink-statefun.git


The following commit(s) were added to refs/heads/master by this push:
     new dc243bd  [hotfix][go] harden NewCell creation checks
dc243bd is described below

commit dc243bddf331a049f256cadcb148a74a80a1aa62
Author: sjwiesman <[email protected]>
AuthorDate: Wed Aug 18 08:34:58 2021 -0500

    [hotfix][go] harden NewCell creation checks
---
 statefun-sdk-go/v3/pkg/statefun/internal/cell.go   |  2 +-
 .../v3/pkg/statefun/internal/cell_test.go          | 33 ++++++++++++++++++++++
 2 files changed, 34 insertions(+), 1 deletion(-)

diff --git a/statefun-sdk-go/v3/pkg/statefun/internal/cell.go 
b/statefun-sdk-go/v3/pkg/statefun/internal/cell.go
index f3479c8..16f7e42 100644
--- a/statefun-sdk-go/v3/pkg/statefun/internal/cell.go
+++ b/statefun-sdk-go/v3/pkg/statefun/internal/cell.go
@@ -43,7 +43,7 @@ func NewCell(state *protocol.ToFunction_PersistedValue, 
typeTypeName string) *Ce
                typeTypeName: typeTypeName,
        }
 
-       if state.StateValue != nil {
+       if state.StateValue != nil && state.StateValue.HasValue {
                c.hasValue = true
                c.buf = state.StateValue.Value
        }
diff --git a/statefun-sdk-go/v3/pkg/statefun/internal/cell_test.go 
b/statefun-sdk-go/v3/pkg/statefun/internal/cell_test.go
index 198c74a..d59096e 100644
--- a/statefun-sdk-go/v3/pkg/statefun/internal/cell_test.go
+++ b/statefun-sdk-go/v3/pkg/statefun/internal/cell_test.go
@@ -23,6 +23,39 @@ import (
        "testing"
 )
 
+func TestNewCell(t *testing.T) {
+       cell := NewCell(&protocol.ToFunction_PersistedValue{
+               StateName: "state",
+       }, "typename")
+
+       assert.False(t, cell.HasValue(), "cell should not have a value")
+       assert.Equal(t, cell.typeTypeName, "typename")
+
+       cell = NewCell(&protocol.ToFunction_PersistedValue{
+               StateName: "state",
+               StateValue: &protocol.TypedValue{
+                       Typename: "blah",
+                       HasValue: false,
+                       Value:    []byte{1, 1, 1, 1},
+               },
+       }, "typename")
+
+       assert.False(t, cell.HasValue(), "cell should not have a value")
+       assert.Equal(t, cell.typeTypeName, "typename")
+
+       cell = NewCell(&protocol.ToFunction_PersistedValue{
+               StateName: "state",
+               StateValue: &protocol.TypedValue{
+                       Typename: "blah",
+                       HasValue: true,
+                       Value:    []byte{1, 1, 1, 1},
+               },
+       }, "typename")
+
+       assert.True(t, cell.HasValue(), "cell should have a value")
+       assert.Equal(t, cell.typeTypeName, "typename")
+}
+
 func TestCellReadWrite(t *testing.T) {
        cell := NewCell(&protocol.ToFunction_PersistedValue{
                StateName: "state",

Reply via email to