Skip to content

Commit 06afd65

Browse files
author
Maksim Naumov
committed
More detailed error on Encoding and format code.
1 parent bd06a79 commit 06afd65

10 files changed

+175
-175
lines changed

Diff for: decoder.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
package php_session_decoder
22

33
import (
4-
"io"
54
"bytes"
5+
"io"
66
"strings"
7+
78
"github.com/yvasiyarov/php_session_decoder/php_serialize"
89
)
910

1011
type PhpDecoder struct {
11-
source *strings.Reader
12-
decoder *php_serialize.UnSerializer
12+
source *strings.Reader
13+
decoder *php_serialize.UnSerializer
1314
}
1415

1516
func NewPhpDecoder(phpSession string) *PhpDecoder {
1617
decoder := &PhpDecoder{
17-
source: strings.NewReader(phpSession),
18-
decoder: php_serialize.NewUnSerializer(""),
18+
source: strings.NewReader(phpSession),
19+
decoder: php_serialize.NewUnSerializer(""),
1920
}
2021
decoder.decoder.SetReader(decoder.source)
2122
return decoder
@@ -27,9 +28,9 @@ func (self *PhpDecoder) SetSerializedDecodeFunc(f php_serialize.SerializedDecode
2728

2829
func (self *PhpDecoder) Decode() (PhpSession, error) {
2930
var (
30-
name string
31-
err error
32-
value php_serialize.PhpValue
31+
name string
32+
err error
33+
value php_serialize.PhpValue
3334
)
3435
res := make(PhpSession)
3536

Diff for: decoder_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package php_session_decoder
22

33
import (
4-
"testing"
5-
"io/ioutil"
64
"encoding/json"
5+
"io/ioutil"
6+
"testing"
7+
78
"github.com/yvasiyarov/php_session_decoder/php_serialize"
89
)
910

@@ -222,7 +223,7 @@ func TestDecodeSerializableObjectValue(t *testing.T) {
222223
t.Errorf("Unable to convert %v to PhpArray\n", vv)
223224
} else if v1, ok1 := arrVal["item"]; !ok1 {
224225
t.Errorf("Array value decoded incorrectly, key `item` doest not exists\n")
225-
} else if itemObjValue, ok1 := v1.(*php_serialize.PhpObject); !ok1 {
226+
} else if itemObjValue, ok1 := v1.(*php_serialize.PhpObject); !ok1 {
226227
t.Errorf("Unable to convert %v to int\n", v1)
227228
} else if itemObjValue.GetClassName() != "AbcClass" {
228229
t.Errorf("Object name was decoded incorrectly: %#v\n", itemObjValue.GetClassName())
@@ -258,7 +259,7 @@ func TestDecodeSerializableObjectBar(t *testing.T) {
258259
f = func(s string) (php_serialize.PhpValue, error) {
259260
var (
260261
val map[string]string
261-
err error
262+
err error
262263
)
263264
err = json.Unmarshal([]byte(s), &val)
264265
return val, err

Diff for: encoder.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@ package php_session_decoder
22

33
import (
44
"bytes"
5+
"fmt"
6+
57
"github.com/yvasiyarov/php_session_decoder/php_serialize"
68
)
79

810
type PhpEncoder struct {
9-
data PhpSession
10-
encoder *php_serialize.Serializer
11+
data PhpSession
12+
encoder *php_serialize.Serializer
1113
}
1214

1315
func NewPhpEncoder(data PhpSession) *PhpEncoder {
1416
return &PhpEncoder{
15-
data: data,
16-
encoder: php_serialize.NewSerializer(),
17+
data: data,
18+
encoder: php_serialize.NewSerializer(),
1719
}
1820
}
1921

@@ -26,7 +28,7 @@ func (self *PhpEncoder) Encode() (string, error) {
2628
return "", nil
2729
}
2830
var (
29-
err error
31+
err error
3032
val string
3133
)
3234
buf := bytes.NewBuffer([]byte{})
@@ -35,6 +37,7 @@ func (self *PhpEncoder) Encode() (string, error) {
3537
buf.WriteString(k)
3638
buf.WriteRune(rune(SEPARATOR_VALUE_NAME))
3739
if val, err = self.encoder.Encode(v); err != nil {
40+
err = fmt.Errorf("php_session: error during encode value for %q: %v", k, err)
3841
break
3942
}
4043
buf.WriteString(val)

Diff for: encoder_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package php_session_decoder
22

33
import (
4+
"encoding/json"
45
"strings"
56
"testing"
7+
68
"github.com/yvasiyarov/php_session_decoder/php_serialize"
7-
"encoding/json"
89
)
910

1011
func TestEncodeBooleanValue(t *testing.T) {
@@ -73,8 +74,8 @@ func TestEncodeArrayValue(t *testing.T) {
7374
"arr": php_serialize.PhpArray{
7475
// Zero element
7576
//php_serialize.PhpValue(0): 5,
76-
0: 5,
77-
"test": true,
77+
0: 5,
78+
"test": true,
7879
"test2": nil,
7980
},
8081
}

Diff for: php_serialize/common.go

+29-29
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
package php_serialize
22

33
const (
4-
TOKEN_NULL = 'N'
5-
TOKEN_BOOL = 'b'
6-
TOKEN_INT = 'i'
7-
TOKEN_FLOAT = 'd'
8-
TOKEN_STRING = 's'
9-
TOKEN_ARRAY = 'a'
10-
TOKEN_OBJECT = 'O'
11-
TOKEN_OBJECT_SERIALIZED = 'C'
12-
13-
SEPARATOR_VALUE_TYPE = ':'
14-
SEPARATOR_VALUES = ';'
15-
16-
DELIMITER_STRING_LEFT = '"'
17-
DELIMITER_STRING_RIGHT = '"'
18-
DELIMITER_OBJECT_LEFT = '{'
19-
DELIMITER_OBJECT_RIGHT = '}'
20-
21-
FORMATTER_FLOAT = 'g'
22-
FORMATTER_PRECISION = 17
4+
TOKEN_NULL = 'N'
5+
TOKEN_BOOL = 'b'
6+
TOKEN_INT = 'i'
7+
TOKEN_FLOAT = 'd'
8+
TOKEN_STRING = 's'
9+
TOKEN_ARRAY = 'a'
10+
TOKEN_OBJECT = 'O'
11+
TOKEN_OBJECT_SERIALIZED = 'C'
12+
13+
SEPARATOR_VALUE_TYPE = ':'
14+
SEPARATOR_VALUES = ';'
15+
16+
DELIMITER_STRING_LEFT = '"'
17+
DELIMITER_STRING_RIGHT = '"'
18+
DELIMITER_OBJECT_LEFT = '{'
19+
DELIMITER_OBJECT_RIGHT = '}'
20+
21+
FORMATTER_FLOAT = 'g'
22+
FORMATTER_PRECISION = 17
2323
)
2424

2525
func NewPhpObject(className string) *PhpObject {
2626
return &PhpObject{
2727
className: className,
28-
members: PhpArray{},
28+
members: PhpArray{},
2929
}
3030
}
3131

@@ -38,8 +38,8 @@ type PhpValue interface{}
3838
type PhpArray map[PhpValue]PhpValue
3939

4040
type PhpObject struct {
41-
className string
42-
members PhpArray
41+
className string
42+
members PhpArray
4343
}
4444

4545
func (self *PhpObject) GetClassName() string {
@@ -61,22 +61,22 @@ func (self *PhpObject) SetMembers(members PhpArray) *PhpObject {
6161
}
6262

6363
func (self *PhpObject) GetPrivate(name string) (v PhpValue, ok bool) {
64-
v, ok = self.members["\x00" + self.className + "\x00" + name]
64+
v, ok = self.members["\x00"+self.className+"\x00"+name]
6565
return
6666
}
6767

6868
func (self *PhpObject) SetPrivate(name string, value PhpValue) *PhpObject {
69-
self.members["\x00" + self.className + "\x00" + name] = value
69+
self.members["\x00"+self.className+"\x00"+name] = value
7070
return self
7171
}
7272

7373
func (self *PhpObject) GetProtected(name string) (v PhpValue, ok bool) {
74-
v, ok = self.members["\x00*\x00" + name]
74+
v, ok = self.members["\x00*\x00"+name]
7575
return
7676
}
7777

7878
func (self *PhpObject) SetProtected(name string, value PhpValue) *PhpObject {
79-
self.members["\x00*\x00" + name] = value
79+
self.members["\x00*\x00"+name] = value
8080
return self
8181
}
8282

@@ -97,9 +97,9 @@ func NewPhpObjectSerialized(className string) *PhpObjectSerialized {
9797
}
9898

9999
type PhpObjectSerialized struct {
100-
className string
101-
data string
102-
value PhpValue
100+
className string
101+
data string
102+
value PhpValue
103103
}
104104

105105
func (self *PhpObjectSerialized) GetClassName() string {

Diff for: php_serialize/common_helper_test.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import "testing"
44

55
func TestPhpValueString(t *testing.T) {
66
var (
7-
val PhpValue = "string"
8-
expected string = "string"
7+
val PhpValue = "string"
8+
expected string = "string"
99
)
1010
if newVal := PhpValueString(val); newVal != expected {
1111
t.Errorf("Expected %q but got %q", expected, newVal)
@@ -14,8 +14,8 @@ func TestPhpValueString(t *testing.T) {
1414

1515
func TestPhpValueBool(t *testing.T) {
1616
var (
17-
val PhpValue = true
18-
expected bool = true
17+
val PhpValue = true
18+
expected bool = true
1919
)
2020
if newVal := PhpValueBool(val); newVal != expected {
2121
t.Errorf("Expected %t but got %t", expected, newVal)
@@ -24,8 +24,8 @@ func TestPhpValueBool(t *testing.T) {
2424

2525
func TestPhpValueInt(t *testing.T) {
2626
var (
27-
val PhpValue = 10
28-
expected int = 10
27+
val PhpValue = 10
28+
expected int = 10
2929
)
3030
if newVal := PhpValueInt(val); newVal != expected {
3131
t.Errorf("Expected %d but got %d", expected, newVal)
@@ -34,8 +34,8 @@ func TestPhpValueInt(t *testing.T) {
3434

3535
func TestPhpValueInt64(t *testing.T) {
3636
var (
37-
val PhpValue = int64(10)
38-
expected int64 = 10
37+
val PhpValue = int64(10)
38+
expected int64 = 10
3939
)
4040
if newVal := PhpValueInt64(val); newVal != expected {
4141
t.Errorf("Expected %d but got %d", expected, newVal)
@@ -44,8 +44,8 @@ func TestPhpValueInt64(t *testing.T) {
4444

4545
func TestPhpValueUInt(t *testing.T) {
4646
var (
47-
val PhpValue = uint(10)
48-
expected uint = 10
47+
val PhpValue = uint(10)
48+
expected uint = 10
4949
)
5050
if newVal := PhpValueUInt(val); newVal != expected {
5151
t.Errorf("Expected %d but got %d", expected, newVal)
@@ -54,8 +54,8 @@ func TestPhpValueUInt(t *testing.T) {
5454

5555
func TestPhpValueUInt64(t *testing.T) {
5656
var (
57-
val PhpValue = uint64(10)
58-
expected uint64 = 10
57+
val PhpValue = uint64(10)
58+
expected uint64 = 10
5959
)
6060
if newVal := PhpValueUInt64(val); newVal != expected {
6161
t.Errorf("Expected %d but got %d", expected, newVal)
@@ -64,8 +64,8 @@ func TestPhpValueUInt64(t *testing.T) {
6464

6565
func TestPhpValueFloat64(t *testing.T) {
6666
var (
67-
val PhpValue = float64(10.0)
68-
expected float64 = 10.0
67+
val PhpValue = float64(10.0)
68+
expected float64 = 10.0
6969
)
7070
if newVal := PhpValueFloat64(val); newVal != expected {
7171
t.Errorf("Expected %v but got %v", expected, newVal)

Diff for: php_serialize/serialize.go

+7-8
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ func Serialize(v PhpValue) (string, error) {
1212
}
1313

1414
type Serializer struct {
15-
lastErr error
16-
encodeFunc SerializedEncodeFunc
15+
lastErr error
16+
encodeFunc SerializedEncodeFunc
1717
}
1818

1919
func NewSerializer() *Serializer {
@@ -29,7 +29,7 @@ func (self *Serializer) Encode(v PhpValue) (string, error) {
2929

3030
switch t := v.(type) {
3131
default:
32-
self.saveError(fmt.Errorf("php_serialize: Unknown type %T", t))
32+
self.saveError(fmt.Errorf("php_serialize: Unknown type %T with value %#v", t, v))
3333
case nil:
3434
value = self.encodeNull()
3535
case bool:
@@ -62,7 +62,7 @@ func (self *Serializer) encodeBool(v PhpValue) string {
6262
}
6363

6464
func (self *Serializer) encodeNumber(v PhpValue) (res string) {
65-
var val string
65+
var val string
6666

6767
isFloat := false
6868

@@ -110,7 +110,7 @@ func (self *Serializer) encodeNumber(v PhpValue) (res string) {
110110
isFloat = true
111111
}
112112

113-
if (isFloat) {
113+
if isFloat {
114114
res = string(TOKEN_FLOAT)
115115
} else {
116116
res = string(TOKEN_INT)
@@ -135,8 +135,8 @@ func (self *Serializer) encodeString(v PhpValue, left, right rune, isFinal bool)
135135

136136
func (self *Serializer) encodeArray(v PhpValue, isFinal bool) (res string) {
137137
var (
138-
arrLen int
139-
data, s string
138+
arrLen int
139+
data, s string
140140
)
141141

142142
if isFinal {
@@ -180,7 +180,6 @@ func (self *Serializer) encodeSerialized(v PhpValue) (res string) {
180180
obj, _ := v.(*PhpObjectSerialized)
181181
res = string(TOKEN_OBJECT_SERIALIZED) + self.prepareClassName(obj.className)
182182

183-
184183
if self.encodeFunc == nil {
185184
serialized = obj.GetData()
186185
} else {

0 commit comments

Comments
 (0)