Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions _generated/def.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,8 @@ type Flob struct {
}

type Numberwang int8

//msgp:ignore ExternalString
type ExternalString string
type ExternalArr [4]byte
type ExternalInt int
35 changes: 35 additions & 0 deletions _generated/map_autoshim.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package _generated

//go:generate msgp -v

//msgp:maps autoshim

//msgp:replace ExternalString with:string
//msgp:replace ExternalInt with:int

type MyMapKeyStruct3 struct {
MapString map[string]int `msg:",allownil"`
MapString3 map[ExternalString]int `msg:",allownil"`
MapString2 map[ExternalInt]int `msg:",allownil"`
MapFloat32 map[float32]int `msg:",allownil"`
MapFloat64 map[float64]int `msg:",allownil"`
MapUint map[uint]int `msg:",allownil"`
MapUint8 map[uint8]int `msg:",allownil"`
MapUint16 map[uint16]int `msg:",allownil"`
MapUint32 map[uint32]int `msg:",allownil"`
MapUint64 map[uint64]int `msg:",allownil"`
MapByte map[byte]int `msg:",allownil"`
MapInt map[int]int `msg:",allownil"`
MapInt8 map[int8]int `msg:",allownil"`
MapInt16 map[int16]int `msg:",allownil"`
MapInt32 map[int32]int `msg:",allownil"`
MapInt64 map[int64]int `msg:",allownil"`
MapBool map[bool]int `msg:",allownil"`
MapMapInt map[int]map[int]int `msg:",allownil"`

// Maps with unsupported base types.
MapArray map[[4]byte]int `msg:",allownil"` // Ignored
MapArray4 map[[4]uint32]int `msg:",allownil"` // Ignored
MapComplex64 map[complex64]int `msg:",allownil"` // Ignored
MapComplex128 map[complex128]int `msg:",allownil"` // Ignored
}
141 changes: 141 additions & 0 deletions _generated/map_autoshim_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package _generated

import (
"bytes"
"math"
"math/rand/v2"
"reflect"
"strconv"
"testing"

"github.com/tinylib/msgp/msgp"
)

func TestAutoShimKeys(t *testing.T) {
rng := rand.New(rand.NewPCG(0, 0))

// Generate a bunch of random maps
for i := range 500 {
var test MyMapKeyStruct3
if i != 0 {
// Don't add anything to the first object
test.MapString = make(map[string]int)
test.MapString2 = make(map[ExternalInt]int)
test.MapString3 = make(map[ExternalString]int)
test.MapFloat32 = make(map[float32]int)
test.MapFloat64 = make(map[float64]int)
test.MapUint = make(map[uint]int)
test.MapUint8 = make(map[uint8]int)
test.MapUint16 = make(map[uint16]int)
test.MapUint32 = make(map[uint32]int)
test.MapUint64 = make(map[uint64]int)
test.MapByte = make(map[byte]int)
test.MapInt = make(map[int]int)
test.MapInt8 = make(map[int8]int)
test.MapInt16 = make(map[int16]int)
test.MapInt32 = make(map[int32]int)
test.MapInt64 = make(map[int64]int)
test.MapBool = make(map[bool]int)
test.MapMapInt = make(map[int]map[int]int)

for range rng.IntN(50) {
test.MapString[string(strconv.Itoa(rng.IntN(math.MaxInt32)))] = rng.IntN(100)
}
for range rng.IntN(50) {
test.MapString3[ExternalString(strconv.Itoa(rng.IntN(math.MaxInt32)))] = rng.IntN(100)
}
for range rng.IntN(50) {
test.MapString2[ExternalInt(rng.IntN(math.MaxInt32))] = rng.IntN(100)
}
for range rng.IntN(50) {
test.MapFloat32[float32(rng.Float32())] = rng.IntN(100)
}
for range rng.IntN(50) {
test.MapFloat64[rng.Float64()] = rng.IntN(100)
}
for range rng.IntN(50) {
test.MapUint[uint(rng.Uint64())] = rng.IntN(100)
}
for range rng.IntN(50) {
test.MapUint8[uint8(rng.Uint64())] = rng.IntN(100)
}
for range rng.IntN(50) {
test.MapUint16[uint16(rng.Uint64())] = rng.IntN(100)
}
for range rng.IntN(50) {
test.MapUint32[rng.Uint32()] = rng.IntN(100)
}
for range rng.IntN(50) {
test.MapUint64[rng.Uint64()] = rng.IntN(100)
}
for range rng.IntN(50) {
test.MapByte[byte(uint8(rng.Uint64()))] = rng.IntN(100)
}
for range rng.IntN(50) {
test.MapInt[rng.IntN(math.MaxInt32)] = rng.IntN(100)
}
for range rng.IntN(50) {
test.MapInt8[int8(rng.IntN(int(math.MaxInt8)+1))] = rng.IntN(100)
}
for range rng.IntN(50) {
test.MapInt16[int16(rng.IntN(int(math.MaxInt16)+1))] = rng.IntN(100)
}
for range rng.IntN(50) {
test.MapInt32[int32(rng.IntN(int(math.MaxInt32)))] = rng.IntN(100)
}
for range rng.IntN(50) {
// Use only non-negative values to stay within IntN capabilities
test.MapInt64[int64(rng.IntN(int(math.MaxInt32)))] = rng.IntN(100)
}
if rng.IntN(2) == 0 {
test.MapBool[true] = rng.IntN(100)
}
if rng.IntN(2) == 0 {
test.MapBool[false] = rng.IntN(100)
}

for range rng.IntN(50) {
dst := make(map[int]int, 50)
test.MapMapInt[rng.Int()] = dst
for range rng.IntN(50) {
dst[rng.Int()] = rng.IntN(100)
}
}
}
var encoded [][]byte
b, err := test.MarshalMsg(nil)
if err != nil {
t.Fatal(err)
}
encoded = append(encoded, b)
var buf bytes.Buffer
en := msgp.NewWriter(&buf)
err = test.EncodeMsg(en)
if err != nil {
t.Fatal(err)
}
err = en.Flush()
if err != nil {
t.Fatal(err)
}
encoded = append(encoded, buf.Bytes())
for _, enc := range encoded {
var decoded, decoded2 MyMapKeyStruct3
_, err = decoded.UnmarshalMsg(enc)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(&decoded, &test) {
t.Errorf("decoded != test")
}
dec := msgp.NewReader(bytes.NewReader(enc))
err = decoded2.DecodeMsg(dec)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(&decoded2, &test) {
t.Errorf("decoded2 != test")
}
}
}
}
81 changes: 81 additions & 0 deletions _generated/map_bin_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package _generated

import (
"encoding/hex"
"time"
)

//go:generate msgp -unexported -v

//msgp:maps binkeys

type ArrayMapKey [4]byte

//msgp:replace ExternalArr with:[4]byte

//msgp:replace ExternalString with:string

type mapKeyBytes2 [8]byte

//msgp:shim mapKeyBytes2 as:string using:hexEncode2/hexDecode2 witherr:false

type mapKeyShimmed time.Duration

//msgp:shim mapKeyShimmed as:[]byte using:durEncode/durDecode witherr:true

type MyStringType string

type MyMapKeyStruct2 struct {
MapString map[string]int `msg:",allownil"`
MapString2 map[MyStringType]int `msg:",allownil"`
MapString3 map[ExternalString]int `msg:",allownil"`
MapString4 map[mapKeyBytes2]int `msg:",allownil"`
MapFloat32 map[float32]int `msg:",allownil"`
MapFloat64 map[float64]int `msg:",allownil"`
MapComplex64 map[complex64]int `msg:",allownil"`
MapComplex128 map[complex128]int `msg:",allownil"`
MapUint map[uint]int `msg:",allownil"`
MapUint8 map[uint8]int `msg:",allownil"`
MapUint16 map[uint16]int `msg:",allownil"`
MapUint32 map[uint32]int `msg:",allownil"`
MapUint64 map[uint64]int `msg:",allownil"`
MapByte map[byte]int `msg:",allownil"`
MapInt map[int]int `msg:",allownil"`
MapInt8 map[int8]int `msg:",allownil"`
MapInt16 map[int16]int `msg:",allownil"`
MapInt32 map[int32]int `msg:",allownil"`
MapInt64 map[int64]int `msg:",allownil"`
MapBool map[bool]int `msg:",allownil"`
MapMapInt map[int]map[int]int `msg:",allownil"`

// Maps with array keys
MapArray map[[4]byte]int `msg:",allownil"`
MapArray2 map[ArrayMapKey]int `msg:",allownil"`
MapArray3 map[ExternalArr]int `msg:",allownil"`
MapArray4 map[[4]uint32]int `msg:",allownil"`

// Maps with shimmed types
MapDuration map[mapKeyShimmed]int `msg:",allownil"`
}

func hexEncode2(b mapKeyBytes2) string {
return hex.EncodeToString(b[:])
}

func hexDecode2(s string) mapKeyBytes2 {
var b [8]byte
_, err := hex.Decode(b[:], []byte(s))
if err != nil {
panic(err)
}
return b
}

func durEncode(v mapKeyShimmed) []byte {
return []byte(time.Duration(v).String())
}

func durDecode(b []byte) (mapKeyShimmed, error) {
v, err := time.ParseDuration(string(b))
return mapKeyShimmed(v), err
}
Loading
Loading