From 4a556cb8bf068c6937dd6591c63bd19d0beb4fb0 Mon Sep 17 00:00:00 2001 From: Janusz Marcinkiewicz Date: Thu, 27 Jun 2024 13:05:01 +0200 Subject: [PATCH] Add `any` support Signed-off-by: Janusz Marcinkiewicz --- gen/elem.go | 1 + msgp/read.go | 2 +- tinygotest/testdata/roundtrip/main.go | 43 +++++++++++++++++---------- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/gen/elem.go b/gen/elem.go index b8b821ab..bfed70b5 100644 --- a/gen/elem.go +++ b/gen/elem.go @@ -119,6 +119,7 @@ var primitives = map[string]Primitive{ "int64": Int64, "bool": Bool, "interface{}": Intf, + "any": Intf, "time.Time": Time, "time.Duration": Duration, "msgp.Extension": Ext, diff --git a/msgp/read.go b/msgp/read.go index e6d72f17..82501278 100644 --- a/msgp/read.go +++ b/msgp/read.go @@ -1266,7 +1266,7 @@ func (m *Reader) ReadTime() (t time.Time, err error) { return } -// ReadIntf reads out the next object as a raw interface{}. +// ReadIntf reads out the next object as a raw interface{}/any. // Arrays are decoded as []interface{}, and maps are decoded // as map[string]interface{}. Integers are decoded as int64 // and unsigned integers are decoded as uint64. diff --git a/tinygotest/testdata/roundtrip/main.go b/tinygotest/testdata/roundtrip/main.go index 21f616d7..b786d378 100644 --- a/tinygotest/testdata/roundtrip/main.go +++ b/tinygotest/testdata/roundtrip/main.go @@ -2,6 +2,7 @@ package main import ( "bytes" + "fmt" "github.com/tinylib/msgp/msgp" ) @@ -19,21 +20,23 @@ type EmbeddedStruct struct { // Example provides a decent variety of types and features and // lets us test for basic functionality in TinyGo. type Example struct { - Int64 int64 `msg:"int64"` - Uint64 uint64 `msg:"uint64"` - Int32 int32 `msg:"int32"` - Uint32 uint32 `msg:"uint32"` - Int16 int32 `msg:"int16"` - Uint16 uint32 `msg:"uint16"` - Int8 int32 `msg:"int8"` - Byte byte `msg:"byte"` - Float64 float64 `msg:"float64"` - Float32 float32 `msg:"float32"` - String string `msg:"string"` - ByteSlice []byte `msg:"byte_slice"` - StringSlice []string `msg:"string_slice"` - IntArray [2]int `msg:"int_array"` - SomeStruct SomeStruct `msg:"some_struct"` + Interface interface{} `msg:"interface"` + Any any `msg:"any"` + Int64 int64 `msg:"int64"` + Uint64 uint64 `msg:"uint64"` + Int32 int32 `msg:"int32"` + Uint32 uint32 `msg:"uint32"` + Int16 int32 `msg:"int16"` + Uint16 uint32 `msg:"uint16"` + Int8 int32 `msg:"int8"` + Byte byte `msg:"byte"` + Float64 float64 `msg:"float64"` + Float32 float32 `msg:"float32"` + String string `msg:"string"` + ByteSlice []byte `msg:"byte_slice"` + StringSlice []string `msg:"string_slice"` + IntArray [2]int `msg:"int_array"` + SomeStruct SomeStruct `msg:"some_struct"` EmbeddedStruct @@ -44,6 +47,8 @@ type Example struct { // Setup populuates the struct with test data func (e *Example) Setup() { + e.Interface = 10 + e.Any = "any" e.Int64 = 10 e.Uint64 = 11 e.Int32 = 12 @@ -68,6 +73,14 @@ func (e *Example) Setup() { } func (e *Example) Eq(e2 *Example) bool { + fmt.Println(e.Interface, e2.Interface) + fmt.Println(e.Any, e2.Any) + if e.Interface.(int) != e2.Interface.(int) { + return false + } + if e.Any.(string) != e2.Any.(string) { + return false + } if e.Int64 != e2.Int64 { return false }