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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
package _generated
import (
"encoding"
"fmt"
)
//go:generate msgp -v
// BinaryTestType implements encoding.BinaryMarshaler and encoding.BinaryUnmarshaler
type BinaryTestType struct {
Value string
}
func (t *BinaryTestType) MarshalBinary() ([]byte, error) {
return []byte(t.Value), nil
}
func (t *BinaryTestType) UnmarshalBinary(data []byte) error {
t.Value = string(data)
return nil
}
// Verify it implements the interfaces
var _ encoding.BinaryMarshaler = (*BinaryTestType)(nil)
var _ encoding.BinaryUnmarshaler = (*BinaryTestType)(nil)
//msgp:binmarshal BinaryTestType
// TextBinTestType implements encoding.TextMarshaler and encoding.TextUnmarshaler
type TextBinTestType struct {
Value string
}
func (t *TextBinTestType) MarshalText() ([]byte, error) {
return []byte(fmt.Sprintf("text:%s", t.Value)), nil
}
func (t *TextBinTestType) UnmarshalText(data []byte) error {
t.Value = string(data[5:]) // Remove "text:" prefix
return nil
}
var _ encoding.TextMarshaler = (*TextBinTestType)(nil)
var _ encoding.TextUnmarshaler = (*TextBinTestType)(nil)
//msgp:textmarshal TextBinTestType
// TextStringTestType for testing as:string option
type TextStringTestType struct {
Value string
}
func (t *TextStringTestType) MarshalText() ([]byte, error) {
return []byte(fmt.Sprintf("stringtext:%s", t.Value)), nil
}
func (t *TextStringTestType) UnmarshalText(data []byte) error {
t.Value = string(data[11:]) // Remove "stringtext:" prefix
return nil
}
var _ encoding.TextMarshaler = (*TextStringTestType)(nil)
var _ encoding.TextUnmarshaler = (*TextStringTestType)(nil)
//msgp:textmarshal as:string TextStringTestType
// TestStruct contains various combinations of marshaler types
type TestStruct struct {
// Direct values
BinaryValue BinaryTestType `msg:"bin_val"`
TextBinValue TextBinTestType `msg:"text_bin_val"`
TextStringValue TextStringTestType `msg:"text_str_val"`
// Pointers
BinaryPtr *BinaryTestType `msg:"bin_ptr"`
TextBinPtr *TextBinTestType `msg:"text_bin_ptr,omitempty"`
TextStringPtr *TextStringTestType `msg:"text_str_ptr,omitempty"`
// Slices
BinarySlice []BinaryTestType `msg:"bin_slice"`
TextBinSlice []TextBinTestType `msg:"text_bin_slice"`
TextStringSlice []TextStringTestType `msg:"text_str_slice"`
// Arrays
BinaryArray [3]BinaryTestType `msg:"bin_array"`
TextBinArray [2]TextBinTestType `msg:"text_bin_array"`
TextStringArray [4]TextStringTestType `msg:"text_str_array"`
// Maps with marshaler types as values
BinaryMap map[string]BinaryTestType `msg:"bin_map"`
TextBinMap map[string]TextBinTestType `msg:"text_bin_map"`
TextStringMap map[string]TextStringTestType `msg:"text_str_map"`
// Nested pointers and slices
NestedPtrSlice []*BinaryTestType `msg:"nested_ptr_slice"`
SliceOfArrays [][2]TextBinTestType `msg:"slice_of_arrays"`
MapOfSlices map[string][]BinaryTestType `msg:"map_of_slices"`
}
//msgp:binmarshal ErrorTestType
// ErrorTestType for testing error conditions
type ErrorTestType struct {
ShouldError bool
}
func (e *ErrorTestType) MarshalBinary() ([]byte, error) {
if e.ShouldError {
return nil, fmt.Errorf("intentional marshal error")
}
return []byte("ok"), nil
}
func (e *ErrorTestType) UnmarshalBinary(data []byte) error {
if string(data) == "error" {
return fmt.Errorf("intentional unmarshal error")
}
e.ShouldError = false
return nil
}
// Test types for as:string positioning flexibility
// TestTextMarshalerStringMiddle for testing as:string in middle of type list
type TestTextMarshalerStringMiddle struct {
Value string
}
func (t *TestTextMarshalerStringMiddle) MarshalText() ([]byte, error) {
return []byte("middle:" + t.Value), nil
}
func (t *TestTextMarshalerStringMiddle) UnmarshalText(text []byte) error {
t.Value = string(text)
return nil
}
// TestTextMarshalerStringEnd for testing as:string at end of type list
type TestTextMarshalerStringEnd struct {
Value string
}
func (t *TestTextMarshalerStringEnd) MarshalText() ([]byte, error) {
return []byte("end:" + t.Value), nil
}
func (t *TestTextMarshalerStringEnd) UnmarshalText(text []byte) error {
t.Value = string(text)
return nil
}
// TestTextAppenderStringPos for testing textappend with as:string positioning
type TestTextAppenderStringPos struct {
Value string
}
func (t *TestTextAppenderStringPos) AppendText(dst []byte) ([]byte, error) {
return append(dst, []byte("append:"+t.Value)...), nil
}
func (t *TestTextAppenderStringPos) UnmarshalText(text []byte) error {
t.Value = string(text)
return nil
}
// BinaryAppenderType implements encoding.BinaryAppender (Go 1.22+)
type BinaryAppenderType struct {
Value string
}
func (t *BinaryAppenderType) AppendBinary(dst []byte) ([]byte, error) {
return append(dst, []byte("binappend:"+t.Value)...), nil
}
func (t *BinaryAppenderType) UnmarshalBinary(data []byte) error {
t.Value = string(data)
return nil
}
// TextAppenderBinType implements encoding.TextAppender (stored as binary)
type TextAppenderBinType struct {
Value string
}
func (t *TextAppenderBinType) AppendText(dst []byte) ([]byte, error) {
return append(dst, []byte("textbin:"+t.Value)...), nil
}
func (t *TextAppenderBinType) UnmarshalText(text []byte) error {
t.Value = string(text)
return nil
}
// ErrorBinaryAppenderType for testing error conditions with BinaryAppender
type ErrorBinaryAppenderType struct {
ShouldError bool
Value string
}
func (e *ErrorBinaryAppenderType) AppendBinary(dst []byte) ([]byte, error) {
if e.ShouldError {
return nil, fmt.Errorf("intentional append binary error")
}
return append(dst, []byte("ok")...), nil
}
func (e *ErrorBinaryAppenderType) UnmarshalBinary(data []byte) error {
if string(data) == "error" {
return fmt.Errorf("intentional unmarshal binary error")
}
e.ShouldError = false
e.Value = string(data)
return nil
}
// ErrorTextAppenderType for testing error conditions with TextAppender
type ErrorTextAppenderType struct {
ShouldError bool
Value string
}
func (e *ErrorTextAppenderType) AppendText(dst []byte) ([]byte, error) {
if e.ShouldError {
return nil, fmt.Errorf("intentional append text error")
}
return append(dst, []byte("ok")...), nil
}
func (e *ErrorTextAppenderType) UnmarshalText(text []byte) error {
if string(text) == "error" {
return fmt.Errorf("intentional unmarshal text error")
}
e.ShouldError = false
e.Value = string(text)
return nil
}
//msgp:binappend BinaryAppenderType ErrorBinaryAppenderType
//msgp:textappend TextAppenderBinType ErrorTextAppenderType
//msgp:textmarshal TestTextMarshalerStringMiddle as:string TestTextMarshalerStringEnd
//msgp:textappend TestTextAppenderStringPos as:string
//msgp:binappend BinaryAppenderValue
// BinaryAppenderValue implements encoding.BinaryAppender (Go 1.22+)
type BinaryAppenderValue struct {
Value string `msg:"-"`
}
func (t BinaryAppenderValue) AppendBinary(dst []byte) ([]byte, error) {
return append(dst, []byte("binappend:"+t.Value)...), nil
}
func (t *BinaryAppenderValue) UnmarshalBinary(data []byte) error {
t.Value = string(data)
return nil
}
//msgp:textappend TestAppendTextString as:string
type TestAppendTextString struct {
Value string `msg:"-"`
}
func (t TestAppendTextString) AppendText(dst []byte) ([]byte, error) {
return append(dst, []byte("append:"+t.Value)...), nil
}
func (t *TestAppendTextString) UnmarshalText(text []byte) error {
t.Value = string(text)
return nil
}
//msgp:textappend TextAppenderBinValue
// TextAppenderBinValue implements encoding.TextAppender (stored as binary)
type TextAppenderBinValue struct {
Value string `msg:"-"`
}
func (t TextAppenderBinValue) AppendText(dst []byte) ([]byte, error) {
return append(dst, []byte("textbin:"+t.Value)...), nil
}
func (t *TextAppenderBinValue) UnmarshalText(text []byte) error {
t.Value = string(text)
return nil
}
|