/
wrapped_string.go
146 lines (118 loc) · 3.82 KB
/
wrapped_string.go
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
package decoder
import (
"reflect"
"strconv"
"unsafe"
"github.com/trim21/go-phpserialize/internal/errors"
"github.com/trim21/go-phpserialize/internal/runtime"
)
type stringWrappedDecoder interface {
DecodeString(ctx *RuntimeContext, bytes []byte, topCursor int64, p unsafe.Pointer) error
}
type wrappedStringDecoder struct {
typ *runtime.Type
dec stringWrappedDecoder
stringDecoder *stringDecoder
structName string
fieldName string
isPtrType bool
}
func newWrappedStringDecoder(typ *runtime.Type, dec Decoder, structName, fieldName string) (Decoder, error) {
var innerDec stringWrappedDecoder
switch v := dec.(type) {
case *boolDecoder:
innerDec = newStringBoolDecoder(structName, fieldName)
case *floatDecoder:
innerDec = newStringFloatDecoder(structName, fieldName, v)
case *uintDecoder:
innerDec = newStringUintDecoder(structName, fieldName, v)
case *intDecoder:
innerDec = newStringIntDecoder(structName, fieldName, v)
case *ptrDecoder:
err := v.wrapString()
return dec, err
default:
return nil, &errors.UnsupportedTypeError{Type: runtime.RType2Type(typ)}
}
return &wrappedStringDecoder{
typ: typ,
dec: innerDec,
stringDecoder: newStringDecoder(structName, fieldName),
structName: structName,
fieldName: fieldName,
isPtrType: typ.Kind() == reflect.Ptr,
}, nil
}
func (d *wrappedStringDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe.Pointer) (int64, error) {
bytes, c, err := d.stringDecoder.decodeByte(ctx.Buf, cursor)
if err != nil {
return 0, err
}
if bytes == nil {
if d.isPtrType {
*(*unsafe.Pointer)(p) = nil
}
return c, nil
}
if err := d.dec.DecodeString(ctx, bytes, cursor, p); err != nil {
return 0, err
}
return c, nil
}
func newStringBoolDecoder(structName, fieldName string) *stringBoolDecoder {
return &stringBoolDecoder{}
}
var _ stringWrappedDecoder = (*stringBoolDecoder)(nil)
type stringBoolDecoder struct {
}
func (s stringBoolDecoder) DecodeString(ctx *RuntimeContext, bytes []byte, topCursor int64, p unsafe.Pointer) error {
str := *(*string)(unsafe.Pointer(&bytes))
value, err := strconv.ParseBool(str)
if err != nil {
return errors.ErrSyntax(err.Error(), topCursor)
}
if value {
**(**bool)(unsafe.Pointer(&p)) = true
} else {
**(**bool)(unsafe.Pointer(&p)) = false
}
return nil
}
var _ stringWrappedDecoder = (*stringFloatDecoder)(nil)
func newStringFloatDecoder(structName, fieldName string, dec *floatDecoder) *stringFloatDecoder {
return &stringFloatDecoder{floatDecoder: dec}
}
type stringFloatDecoder struct {
floatDecoder *floatDecoder
}
func (d stringFloatDecoder) DecodeString(ctx *RuntimeContext, bytes []byte, topCursor int64, p unsafe.Pointer) error {
_, err := d.floatDecoder.processBytes(bytes, topCursor, p)
return err
}
var _ stringWrappedDecoder = (*stringUintDecoder)(nil)
func newStringUintDecoder(structName, fieldName string, dec *uintDecoder) *stringUintDecoder {
return &stringUintDecoder{uintDecoder: dec}
}
type stringUintDecoder struct {
uintDecoder *uintDecoder
}
func (d stringUintDecoder) DecodeString(ctx *RuntimeContext, bytes []byte, topCursor int64, p unsafe.Pointer) error {
_, err := d.uintDecoder.processBytes(bytes, topCursor, p)
return err
}
var _ stringWrappedDecoder = (*stringIntDecoder)(nil)
func newStringIntDecoder(structName string, fieldName string, decoder *intDecoder) *stringIntDecoder {
return &stringIntDecoder{intDecoder: decoder}
}
type stringIntDecoder struct {
intDecoder *intDecoder
}
func (d *stringIntDecoder) DecodeString(ctx *RuntimeContext, bytes []byte, topCursor int64, p unsafe.Pointer) error {
_, err := d.intDecoder.processBytes(bytes, topCursor, p)
return err
}
func (d *ptrDecoder) wrapString() error {
var err error
d.dec, err = newWrappedStringDecoder(d.typ, d.dec, d.structName, d.fieldName)
return err
}