File: omitzero_ext.go

package info (click to toggle)
golang-github-tinylib-msgp 1.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 836 kB
  • sloc: makefile: 47
file content (114 lines) | stat: -rw-r--r-- 2,500 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package _generated

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

// this has "external" types that will show up
// as generic IDENT during code generation

type OmitZeroExt struct {
	a int // custom type
}

// IsZero will return true if a is not positive
func (o OmitZeroExt) IsZero() bool { return o.a <= 0 }

// EncodeMsg implements msgp.Encodable
func (o OmitZeroExt) EncodeMsg(en *msgp.Writer) (err error) {
	if o.a > 0 {
		return en.WriteInt(o.a)
	}
	return en.WriteNil()
}

// DecodeMsg implements msgp.Decodable
func (o *OmitZeroExt) DecodeMsg(dc *msgp.Reader) (err error) {
	if dc.IsNil() {
		err = dc.ReadNil()
		if err != nil {
			return
		}
		o.a = 0
		return
	}
	o.a, err = dc.ReadInt()
	return err
}

// MarshalMsg implements msgp.Marshaler
func (o OmitZeroExt) MarshalMsg(b []byte) (ret []byte, err error) {
	ret = msgp.Require(b, o.Msgsize())
	if o.a > 0 {
		return msgp.AppendInt(ret, o.a), nil
	}
	return msgp.AppendNil(ret), nil
}

// UnmarshalMsg implements msgp.Unmarshaler
func (o *OmitZeroExt) UnmarshalMsg(bts []byte) (ret []byte, err error) {
	if msgp.IsNil(bts) {
		bts, err = msgp.ReadNilBytes(bts)
		return bts, err
	}
	o.a, bts, err = msgp.ReadIntBytes(bts)
	return bts, err
}

// Msgsize implements msgp.Msgsizer
func (o OmitZeroExt) Msgsize() (s int) {
	return msgp.IntSize
}

type OmitZeroExtPtr struct {
	a int // custom type
}

// IsZero will return true if a is nil or not positive
func (o *OmitZeroExtPtr) IsZero() bool { return o == nil || o.a <= 0 }

// EncodeMsg implements msgp.Encodable
func (o *OmitZeroExtPtr) EncodeMsg(en *msgp.Writer) (err error) {
	if o.a > 0 {
		return en.WriteInt(o.a)
	}
	return en.WriteNil()
}

// DecodeMsg implements msgp.Decodable
func (o *OmitZeroExtPtr) DecodeMsg(dc *msgp.Reader) (err error) {
	if dc.IsNil() {
		err = dc.ReadNil()
		if err != nil {
			return
		}
		o.a = 0
		return
	}
	o.a, err = dc.ReadInt()
	return err
}

// MarshalMsg implements msgp.Marshaler
func (o *OmitZeroExtPtr) MarshalMsg(b []byte) (ret []byte, err error) {
	ret = msgp.Require(b, o.Msgsize())
	if o.a > 0 {
		return msgp.AppendInt(ret, o.a), nil
	}
	return msgp.AppendNil(ret), nil
}

// UnmarshalMsg implements msgp.Unmarshaler
func (o *OmitZeroExtPtr) UnmarshalMsg(bts []byte) (ret []byte, err error) {
	if msgp.IsNil(bts) {
		bts, err = msgp.ReadNilBytes(bts)
		return bts, err
	}
	o.a, bts, err = msgp.ReadIntBytes(bts)
	return bts, err
}

// Msgsize implements msgp.Msgsizer
func (o *OmitZeroExtPtr) Msgsize() (s int) {
	return msgp.IntSize
}