-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.go
105 lines (97 loc) · 2.07 KB
/
utils.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
package goworld
import (
"encoding/gob"
"fmt"
"strconv"
)
func init() {
gob.Register(NewAcpErr(""))
}
func portNo(port int) string {
return fmt.Sprintf(":%d", port)
}
type AcpErr struct {
Err string
}
func (err *AcpErr) Error() string {
return err.Err
}
func NewAcpErr(msg string) *AcpErr {
return &AcpErr{msg}
}
func ParseStringParam(value, dataType string) (result interface{}, err error) {
switch dataType {
case "boolean":
return strconv.ParseBool(value)
case "unsigned_byte":
if result, err = strconv.ParseUint(value, 10, 8); err != nil {
break
}
result = result.(uint8)
return
case "signed_byte":
if result, err = strconv.ParseInt(value, 10, 8); err != nil {
break
}
result = result.(int8)
return
case "unsigned_short":
if result, err = strconv.ParseUint(value, 10, 16); err != nil {
break
}
result = result.(uint16)
return
case "signed_short":
if result, err = strconv.ParseInt(value, 10, 16); err != nil {
break
}
result = result.(int16)
return
case "unsigned_int":
if result, err = strconv.ParseUint(value, 10, 32); err != nil {
break
}
result = result.(uint32)
return
case "signed_int":
if result, err = strconv.ParseInt(value, 10, 32); err != nil {
break
}
result = result.(uint32)
return
case "unsigned_long":
if result, err = strconv.ParseUint(value, 10, 64); err != nil {
break
}
result = result.(uint64)
return
case "signed_long":
if result, err = strconv.ParseInt(value, 10, 64); err != nil {
break
}
result = result.(uint64)
return
case "short_float":
if result, err = strconv.ParseFloat(value, 32); err != nil {
break
}
result = result.(float32)
return
case "float":
if result, err = strconv.ParseFloat(value, 64); err != nil {
break
}
result = result.(float64)
return
case "chars":
result = value
return
default:
err = NewAcpErr(fmt.Sprintf("Unsuported data type '%s' in ParssStringParam", dataType))
return
}
if err != nil {
err = NewAcpErr(fmt.Sprintf("Error parsing string parameter '%s' to data type '%s' ", value, dataType))
}
return
}