Skip to content

Commit bd06a79

Browse files
author
Maksim Naumov
committed
Some helper functions
1 parent c36c2e7 commit bd06a79

File tree

2 files changed

+173
-0
lines changed

2 files changed

+173
-0
lines changed

Diff for: php_serialize/common_helper.go

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package php_serialize
2+
3+
import (
4+
"strconv"
5+
)
6+
7+
func PhpValueString(p PhpValue) (res string) {
8+
res, _ = p.(string)
9+
return
10+
}
11+
12+
func PhpValueBool(p PhpValue) (res bool) {
13+
switch p.(type) {
14+
case bool:
15+
res, _ = p.(bool)
16+
case string:
17+
str, _ := p.(string)
18+
res, _ = strconv.ParseBool(str)
19+
}
20+
return
21+
}
22+
23+
func PhpValueInt(p PhpValue) (res int) {
24+
switch p.(type) {
25+
case int:
26+
res, _ = p.(int)
27+
case int8:
28+
intVal, _ := p.(int8)
29+
res = int(intVal)
30+
case int16:
31+
intVal, _ := p.(int16)
32+
res = int(intVal)
33+
case int32:
34+
intVal, _ := p.(int32)
35+
res = int(intVal)
36+
case int64:
37+
intVal, _ := p.(int64)
38+
res = int(intVal)
39+
case uint:
40+
intVal, _ := p.(uint)
41+
res = int(intVal)
42+
case uint8:
43+
intVal, _ := p.(uint8)
44+
res = int(intVal)
45+
case uint16:
46+
intVal, _ := p.(uint16)
47+
res = int(intVal)
48+
case uint32:
49+
intVal, _ := p.(uint32)
50+
res = int(intVal)
51+
case uint64:
52+
intVal, _ := p.(uint64)
53+
res = int(intVal)
54+
case string:
55+
str, _ := p.(string)
56+
res, _ = strconv.Atoi(str)
57+
}
58+
return
59+
}
60+
61+
func PhpValueInt64(p PhpValue) (res int64) {
62+
switch p.(type) {
63+
case int64:
64+
res = p.(int64)
65+
default:
66+
res = int64(PhpValueInt(p))
67+
}
68+
return
69+
}
70+
71+
func PhpValueUInt(p PhpValue) (res uint) {
72+
switch p.(type) {
73+
case uint:
74+
res = p.(uint)
75+
default:
76+
res = uint(PhpValueInt(p))
77+
}
78+
return
79+
}
80+
81+
func PhpValueUInt64(p PhpValue) (res uint64) {
82+
switch p.(type) {
83+
case uint64:
84+
res = p.(uint64)
85+
default:
86+
res = uint64(PhpValueInt(p))
87+
}
88+
return
89+
}
90+
91+
func PhpValueFloat64(p PhpValue) (res float64) {
92+
switch p.(type) {
93+
case float64:
94+
res, _ = p.(float64)
95+
case string:
96+
str, _ := p.(string)
97+
res, _ = strconv.ParseFloat(str, 64)
98+
}
99+
return
100+
}

Diff for: php_serialize/common_helper_test.go

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package php_serialize
2+
3+
import "testing"
4+
5+
func TestPhpValueString(t *testing.T) {
6+
var (
7+
val PhpValue = "string"
8+
expected string = "string"
9+
)
10+
if newVal := PhpValueString(val); newVal != expected {
11+
t.Errorf("Expected %q but got %q", expected, newVal)
12+
}
13+
}
14+
15+
func TestPhpValueBool(t *testing.T) {
16+
var (
17+
val PhpValue = true
18+
expected bool = true
19+
)
20+
if newVal := PhpValueBool(val); newVal != expected {
21+
t.Errorf("Expected %t but got %t", expected, newVal)
22+
}
23+
}
24+
25+
func TestPhpValueInt(t *testing.T) {
26+
var (
27+
val PhpValue = 10
28+
expected int = 10
29+
)
30+
if newVal := PhpValueInt(val); newVal != expected {
31+
t.Errorf("Expected %d but got %d", expected, newVal)
32+
}
33+
}
34+
35+
func TestPhpValueInt64(t *testing.T) {
36+
var (
37+
val PhpValue = int64(10)
38+
expected int64 = 10
39+
)
40+
if newVal := PhpValueInt64(val); newVal != expected {
41+
t.Errorf("Expected %d but got %d", expected, newVal)
42+
}
43+
}
44+
45+
func TestPhpValueUInt(t *testing.T) {
46+
var (
47+
val PhpValue = uint(10)
48+
expected uint = 10
49+
)
50+
if newVal := PhpValueUInt(val); newVal != expected {
51+
t.Errorf("Expected %d but got %d", expected, newVal)
52+
}
53+
}
54+
55+
func TestPhpValueUInt64(t *testing.T) {
56+
var (
57+
val PhpValue = uint64(10)
58+
expected uint64 = 10
59+
)
60+
if newVal := PhpValueUInt64(val); newVal != expected {
61+
t.Errorf("Expected %d but got %d", expected, newVal)
62+
}
63+
}
64+
65+
func TestPhpValueFloat64(t *testing.T) {
66+
var (
67+
val PhpValue = float64(10.0)
68+
expected float64 = 10.0
69+
)
70+
if newVal := PhpValueFloat64(val); newVal != expected {
71+
t.Errorf("Expected %v but got %v", expected, newVal)
72+
}
73+
}

0 commit comments

Comments
 (0)