Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	gen/elem.go
  • Loading branch information
birneee committed Mar 2, 2024
2 parents 18d0794 + 6d6f813 commit 8e6189e
Show file tree
Hide file tree
Showing 24 changed files with 706 additions and 138 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
test:
strategy:
matrix:
go-version: [1.14.x, 1.18.x, 1.19.x]
go-version: [1.18.x, 1.19.x, 1.20.x, 1.21.x]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
timeout-minutes: 10
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
linters:
strategy:
matrix:
go-version: [1.19.x]
go-version: [1.21.x]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
timeout-minutes: 10
Expand All @@ -35,5 +35,5 @@ jobs:
- name: lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.50
version: v1.55.1
args: --print-resources-usage --timeout=10m --verbose
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ ci: prepare
if [ `arch` == 'x86_64' ]; then \
sudo apt-get -y -q update; \
sudo apt-get -y -q install build-essential; \
wget -q https://github.com/tinygo-org/tinygo/releases/download/v0.26.0/tinygo_0.26.0_amd64.deb; \
sudo dpkg -i tinygo_0.26.0_amd64.deb; \
wget -q https://github.com/tinygo-org/tinygo/releases/download/v0.30.0/tinygo_0.30.0_amd64.deb; \
sudo dpkg -i tinygo_0.30.0_amd64.deb; \
export PATH=$$PATH:/usr/local/tinygo/bin; \
fi
go test -v ./... ./_generated
57 changes: 57 additions & 0 deletions _generated/allownil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package _generated

import (
"bytes"
"reflect"
"testing"

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

func TestAllownil(t *testing.T) {
tt := &NamedStructAN{
A: []string{},
B: nil,
}
var buf bytes.Buffer

err := msgp.Encode(&buf, tt)
if err != nil {
t.Fatal(err)
}
in := buf.Bytes()

for _, tnew := range []*NamedStructAN{{}, {A: []string{}}, {B: []string{}}} {
err = msgp.Decode(bytes.NewBuffer(in), tnew)
if err != nil {
t.Error(err)
}

if !reflect.DeepEqual(tt, tnew) {
t.Logf("in: %#v", tt)
t.Logf("out: %#v", tnew)
t.Fatal("objects not equal")
}
}

in, err = tt.MarshalMsg(nil)
if err != nil {
t.Fatal(err)
}
for _, tanother := range []*NamedStructAN{{}, {A: []string{}}, {B: []string{}}} {
var left []byte
left, err = tanother.UnmarshalMsg(in)
if err != nil {
t.Error(err)
}
if len(left) > 0 {
t.Errorf("%d bytes left", len(left))
}

if !reflect.DeepEqual(tt, tanother) {
t.Logf("in: %#v", tt)
t.Logf("out: %#v", tanother)
t.Fatal("objects not equal")
}
}
}
45 changes: 26 additions & 19 deletions _generated/def.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,31 @@ type TestType struct {
ValueA string `msg:"value_a"`
ValueB []byte `msg:"value_b"`
} `msg:"object"`
Child *TestType `msg:"child"`
Time time.Time `msg:"time"`
Any interface{} `msg:"any"`
Appended msgp.Raw `msg:"appended"`
Num msgp.Number `msg:"num"`
Byte byte
Rune rune
RunePtr *rune
RunePtrPtr **rune
RuneSlice []rune
Slice1 []string
Slice2 []string
SlicePtr *[]string
Child *TestType `msg:"child"`
Time time.Time `msg:"time"`
Any interface{} `msg:"any"`
Appended msgp.Raw `msg:"appended"`
Num msgp.Number `msg:"num"`
Byte byte
Rune rune
RunePtr *rune
RunePtrPtr **rune
RuneSlice []rune
Slice1 []string
Slice2 []string
SlicePtr *[]string
MapStringEmpty map[string]struct{}
MapStringEmpty2 map[string]EmptyStruct
}

//msgp:tuple Object
type Object struct {
ObjectNo string `msg:"objno"`
Slice1 []string `msg:"slice1"`
Slice2 []string `msg:"slice2"`
MapMap map[string]map[string]string
ObjectNo string `msg:"objno"`
Slice1 []string `msg:"slice1"`
Slice2 []string `msg:"slice2"`
MapMap map[string]map[string]string
MapStringEmpty map[string]struct{}
MapStringEmpty2 map[string]EmptyStruct
}

//msgp:tuple TestBench
Expand Down Expand Up @@ -266,8 +270,11 @@ type NonMsgStructTags struct {
B string `json:"b"`
C []string `json:"c"`
VeryNested []struct {
A []string `json:"a"`
B []string `msg:"bbbb" xml:"-"`
A []string `json:"a"`
B []string `msg:"bbbb" xml:"-"`
C map[string]struct{} `msg:"cccc"`
}
}
}

type EmptyStruct struct{}
21 changes: 16 additions & 5 deletions _generated/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ func (a *TestType) Equal(b *TestType) bool {
if !bytes.Equal(aa, ab) {
return false
}
if len(a.MapStringEmpty) == 0 && len(b.MapStringEmpty) == 0 {
a.MapStringEmpty = nil
b.MapStringEmpty = nil
}
if len(a.MapStringEmpty2) == 0 && len(b.MapStringEmpty2) == 0 {
a.MapStringEmpty2 = nil
b.MapStringEmpty2 = nil
}

a.Time, b.Time = time.Time{}, time.Time{}
aa, ab = nil, nil
ok := reflect.DeepEqual(a, b)
Expand Down Expand Up @@ -97,9 +106,11 @@ func Test1EncodeDecode(t *testing.T) {
ValueA: "here's the first inner value",
ValueB: []byte("here's the second inner value"),
},
Child: nil,
Time: time.Now(),
Appended: msgp.Raw([]byte{}), // 'nil'
Child: nil,
Time: time.Now(),
Appended: msgp.Raw([]byte{}), // 'nil'
MapStringEmpty: map[string]struct{}{"Key": {}, "Key2": {}},
MapStringEmpty2: map[string]EmptyStruct{"Key3": {}, "Key4": {}},
}

var buf bytes.Buffer
Expand All @@ -117,8 +128,8 @@ func Test1EncodeDecode(t *testing.T) {
}

if !tt.Equal(tnew) {
t.Logf("in: %v", tt)
t.Logf("out: %v", tnew)
t.Logf("in: %#v", tt)
t.Logf("out: %#v", tnew)
t.Fatal("objects not equal")
}

Expand Down
10 changes: 0 additions & 10 deletions _generated/issue94.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,6 @@ import (

//go:generate msgp

// Issue 94: shims were not propogated recursively,
// which caused shims that weren't at the top level
// to be silently ignored.
//
// The following line will generate an error after
// the code is generated if the generated code doesn't
// have the right identifier in it.

//go:generate ./search.sh $GOFILE timetostr

//msgp:shim time.Time as:string using:timetostr/strtotime
type T struct {
T time.Time
Expand Down
25 changes: 25 additions & 0 deletions _generated/issue94_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package _generated

import (
"bytes"
"os"
"testing"
)

// Issue 94: shims were not propogated recursively,
// which caused shims that weren't at the top level
// to be silently ignored.
//
// The following line will generate an error after
// the code is generated if the generated code doesn't
// have the right identifier in it.
func TestIssue94(t *testing.T) {
b, err := os.ReadFile("issue94_gen.go")
if err != nil {
t.Fatal(err)
}
const want = "timetostr"
if !bytes.Contains(b, []byte(want)) {
t.Errorf("generated code did not contain %q", want)
}
}
86 changes: 86 additions & 0 deletions _generated/omitzero.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package _generated

import "time"

//go:generate msgp

// check some specific cases for omitzero

type OmitZero0 struct {
AStruct OmitZeroA `msg:"astruct,omitempty"` // leave this one omitempty
BStruct OmitZeroA `msg:"bstruct,omitzero"` // and compare to this
AStructPtr *OmitZeroA `msg:"astructptr,omitempty"` // a pointer case omitempty
BStructPtr *OmitZeroA `msg:"bstructptr,omitzero"` // a pointer case omitzero
AExt OmitZeroExt `msg:"aext,omitzero"` // external type case
AExtPtr *OmitZeroExtPtr `msg:"aextptr,omitzero"` // external type pointer case

// more
APtrNamedStr *NamedStringOZ `msg:"aptrnamedstr,omitzero"`
ANamedStruct NamedStructOZ `msg:"anamedstruct,omitzero"`
APtrNamedStruct *NamedStructOZ `msg:"aptrnamedstruct,omitzero"`
EmbeddableStruct `msg:",flatten,omitzero"` // embed flat
EmbeddableStructOZ `msg:"embeddablestruct2,omitzero"` // embed non-flat
ATime time.Time `msg:"atime,omitzero"`

OmitZeroTuple OmitZeroTuple `msg:"ozt"` // the inside of a tuple should ignore both omitempty and omitzero
}

type OmitZeroA struct {
A string `msg:"a,omitempty"`
B NamedStringOZ `msg:"b,omitzero"`
C NamedStringOZ `msg:"c,omitzero"`
}

func (o *OmitZeroA) IsZero() bool {
if o == nil {
return true
}
return *o == (OmitZeroA{})
}

type NamedStructOZ struct {
A string `msg:"a,omitempty"`
B string `msg:"b,omitempty"`
}

func (ns *NamedStructOZ) IsZero() bool {
if ns == nil {
return true
}
return *ns == (NamedStructOZ{})
}

type NamedStringOZ string

func (ns *NamedStringOZ) IsZero() bool {
if ns == nil {
return true
}
return *ns == ""
}

type EmbeddableStructOZ struct {
SomeEmbed string `msg:"someembed2,omitempty"`
}

func (es EmbeddableStructOZ) IsZero() bool { return es == (EmbeddableStructOZ{}) }

type EmbeddableStructOZ2 struct {
SomeEmbed2 string `msg:"someembed2,omitempty"`
}

func (es EmbeddableStructOZ2) IsZero() bool { return es == (EmbeddableStructOZ2{}) }

//msgp:tuple OmitZeroTuple

// OmitZeroTuple is flagged for tuple output, it should ignore all omitempty and omitzero functionality
// since it's fundamentally incompatible.
type OmitZeroTuple struct {
FieldA string `msg:"fielda,omitempty"`
FieldB NamedStringOZ `msg:"fieldb,omitzero"`
FieldC NamedStringOZ `msg:"fieldc,omitzero"`
}

type OmitZero1 struct {
T1 OmitZeroTuple `msg:"t1"`
}
Loading

0 comments on commit 8e6189e

Please sign in to comment.