-
Notifications
You must be signed in to change notification settings - Fork 0
/
primitives.go
96 lines (86 loc) · 2.24 KB
/
primitives.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
package sep
import "strings"
import "errors"
func ListParser(data string) (output []string, err error) {
if data == "" {
return
}
if data[0] != '[' {
return output, errors.New("I couldn't find the opening tag o.o")
}
if data[len(data) - 1] != ']' {
return output, errors.New("I couldn't find the closing tag o.o")
}
if data == "[]" {
return
}
var startIndex int
var itemLen int = 0
for i := 1;i < len(data);i++ {
if data[i] == ',' || data[i] == ']' {
if itemLen != 0 {
output = append(output,data[startIndex + 1:i])
}
startIndex = i
} else {
itemLen++
}
}
//fmt.Println(output)
return
}
func MapParser(data string) (output map[string]string, err error) {
output = make(map[string]string)
if data == "" {
return
}
if data[0] != '[' && data[0] != '{' {
return output, errors.New("I couldn't find the opening tag o.o")
}
if data[len(data) - 1] != ']' && data[len(data) - 1] != '}' {
return output, errors.New("I couldn't find the closing tag o.o")
}
if data == "[]" || data == "{}" {
return
}
if data[0] == '[' && data[len(data) - 1] != ']' {
return output, errors.New("the opening and closing tags don't match x.x")
}
if data[0] == '{' && data[len(data) - 1] != '}' {
return output, errors.New("the opening and closing tags don't match x.x")
}
data = NormalizeMapString(data)
var elements []string
var startIndex int
var itemLen int = 0
for i := 1;i < len(data);i++ {
if data[i] == ',' || data[i] == '}' {
if itemLen != 0 {
elements = append(elements,data[startIndex + 1:i])
}
startIndex = i
} else {
itemLen++
}
}
//fmt.Println(elements)
for _, element := range elements {
if strings.Index(element,":") == -1 {
return output, errors.New("I couldn't find the : seperator between an element name and an element value o.o")
}
fields := strings.SplitN(element,":",2)
if fields[0] == "" {
return output, errors.New("You can't have a blank name for a field x.x")
}
if fields[1] == "" {
return output, errors.New("You can't have an empty field x.x")
}
_, exists := output[fields[0]]
if exists {
return output, errors.New("You can't have two fields with the same name")
}
output[fields[0]] = fields[1]
}
// fmt.Printf("%+v\n", output)
return
}