summaryrefslogtreecommitdiffhomepage
path: root/tools/go_marshal/test/marshal_test.go
diff options
context:
space:
mode:
authorAyush Ranjan <ayushranjan@google.com>2021-02-08 18:03:29 -0800
committergVisor bot <gvisor-bot@google.com>2021-02-08 18:08:29 -0800
commitcfa4633c3d206aa2f9abdaac60d053162244ee6d (patch)
treecddf8d20bde9b55bee4d510876b77af92dc635a3 /tools/go_marshal/test/marshal_test.go
parente51f775cbb8db89d1dac6dcf584be5eef1f82d3c (diff)
[go-marshal] Add dynamic tag in go_marshal.
This makes it easier to implement dynamically sized types in go-marshal. You really only need to implement MarshalBytes, UnmarshalBytes and SizeBytes to implement the entire interface. By using the `dynamic` tag, the autogenerator will generate the rest of the methods for us. This change also simplifies how KernelIPTGetEntries implements Marshallable using the newly added utility. PiperOrigin-RevId: 356397114
Diffstat (limited to 'tools/go_marshal/test/marshal_test.go')
-rw-r--r--tools/go_marshal/test/marshal_test.go19
1 files changed, 19 insertions, 0 deletions
diff --git a/tools/go_marshal/test/marshal_test.go b/tools/go_marshal/test/marshal_test.go
index a00f9a684..b0091dc64 100644
--- a/tools/go_marshal/test/marshal_test.go
+++ b/tools/go_marshal/test/marshal_test.go
@@ -28,6 +28,7 @@ import (
"github.com/google/go-cmp/cmp"
"gvisor.dev/gvisor/pkg/marshal"
+ "gvisor.dev/gvisor/pkg/marshal/primitive"
"gvisor.dev/gvisor/pkg/syserror"
"gvisor.dev/gvisor/pkg/usermem"
"gvisor.dev/gvisor/tools/go_marshal/analysis"
@@ -513,3 +514,21 @@ func TestLimitedSliceMarshalling(t *testing.T) {
})
}
}
+
+func TestDynamicType(t *testing.T) {
+ t12 := test.Type12Dynamic{
+ X: 32,
+ Y: []primitive.Int64{5, 6, 7},
+ }
+
+ var m marshal.Marshallable
+ m = &t12 // Ensure that all methods were generated.
+ b := make([]byte, m.SizeBytes())
+ m.MarshalBytes(b)
+
+ var res test.Type12Dynamic
+ res.UnmarshalBytes(b)
+ if !reflect.DeepEqual(t12, res) {
+ t.Errorf("dynamic type is not same after marshalling and unmarshalling: before = %+v, after = %+v", t12, res)
+ }
+}