Skip to content

Commit 966f655

Browse files
committed
Reorganised things a little.
1 parent 099f658 commit 966f655

File tree

3 files changed

+134
-128
lines changed

3 files changed

+134
-128
lines changed

primitives.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package sep
2+
3+
import "strings"
4+
import "errors"
5+
6+
func ListParser(data string) (output []string, err error) {
7+
if data == "" {
8+
return
9+
}
10+
if data[0] != '[' {
11+
return output, errors.New("I couldn't find the opening tag o.o")
12+
}
13+
if data[len(data) - 1] != ']' {
14+
return output, errors.New("I couldn't find the closing tag o.o")
15+
}
16+
if data == "[]" {
17+
return
18+
}
19+
20+
var startIndex int
21+
var itemLen int = 0
22+
for i := 1;i < len(data);i++ {
23+
if data[i] == ',' || data[i] == ']' {
24+
if itemLen != 0 {
25+
output = append(output,data[startIndex + 1:i])
26+
}
27+
startIndex = i
28+
} else {
29+
itemLen++
30+
}
31+
}
32+
//fmt.Println(output)
33+
return
34+
}
35+
36+
func MapParser(data string) (output map[string]string, err error) {
37+
output = make(map[string]string)
38+
if data == "" {
39+
return
40+
}
41+
if data[0] != '[' && data[0] != '{' {
42+
return output, errors.New("I couldn't find the opening tag o.o")
43+
}
44+
if data[len(data) - 1] != ']' && data[len(data) - 1] != '}' {
45+
return output, errors.New("I couldn't find the closing tag o.o")
46+
}
47+
if data == "[]" || data == "{}" {
48+
return
49+
}
50+
51+
if data[0] == '[' && data[len(data) - 1] != ']' {
52+
return output, errors.New("the opening and closing tags don't match x.x")
53+
}
54+
if data[0] == '{' && data[len(data) - 1] != '}' {
55+
return output, errors.New("the opening and closing tags don't match x.x")
56+
}
57+
58+
data = NormalizeMapString(data)
59+
60+
var elements []string
61+
var startIndex int
62+
var itemLen int = 0
63+
for i := 1;i < len(data);i++ {
64+
if data[i] == ',' || data[i] == '}' {
65+
if itemLen != 0 {
66+
elements = append(elements,data[startIndex + 1:i])
67+
}
68+
startIndex = i
69+
} else {
70+
itemLen++
71+
}
72+
}
73+
//fmt.Println(elements)
74+
75+
for _, element := range elements {
76+
if strings.Index(element,":") == -1 {
77+
return output, errors.New("I couldn't find the : seperator between an element name and an element value o.o")
78+
}
79+
80+
fields := strings.SplitN(element,":",2)
81+
if fields[0] == "" {
82+
return output, errors.New("You can't have a blank name for a field x.x")
83+
}
84+
if fields[1] == "" {
85+
return output, errors.New("You can't have an empty field x.x")
86+
}
87+
88+
_, exists := output[fields[0]]
89+
if exists {
90+
return output, errors.New("You can't have two fields with the same name")
91+
}
92+
output[fields[0]] = fields[1]
93+
}
94+
// fmt.Printf("%+v\n", output)
95+
return
96+
}

sep.go

Lines changed: 0 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package sep
22

3-
import "strings"
43
import "strconv"
54
import "errors"
65

@@ -10,133 +9,6 @@ type Datastore interface {
109
DeleteVar(name string) error
1110
}
1211

13-
func ListParser(data string) (output []string, err error) {
14-
if data == "" {
15-
return
16-
}
17-
if data[0] != '[' {
18-
return output, errors.New("I couldn't find the opening tag o.o")
19-
}
20-
if data[len(data) - 1] != ']' {
21-
return output, errors.New("I couldn't find the closing tag o.o")
22-
}
23-
if data == "[]" {
24-
return
25-
}
26-
27-
var startIndex int
28-
var itemLen int = 0
29-
for i := 1;i < len(data);i++ {
30-
if data[i] == ',' || data[i] == ']' {
31-
if itemLen != 0 {
32-
output = append(output,data[startIndex + 1:i])
33-
}
34-
startIndex = i
35-
} else {
36-
itemLen++
37-
}
38-
}
39-
//fmt.Println(output)
40-
return
41-
}
42-
43-
func MapParser(data string) (output map[string]string, err error) {
44-
output = make(map[string]string)
45-
if data == "" {
46-
return
47-
}
48-
if data[0] != '[' && data[0] != '{' {
49-
return output, errors.New("I couldn't find the opening tag o.o")
50-
}
51-
if data[len(data) - 1] != ']' && data[len(data) - 1] != '}' {
52-
return output, errors.New("I couldn't find the closing tag o.o")
53-
}
54-
if data == "[]" || data == "{}" {
55-
return
56-
}
57-
58-
if data[0] == '[' && data[len(data) - 1] != ']' {
59-
return output, errors.New("the opening and closing tags don't match x.x")
60-
}
61-
if data[0] == '{' && data[len(data) - 1] != '}' {
62-
return output, errors.New("the opening and closing tags don't match x.x")
63-
}
64-
65-
data = NormalizeMapString(data)
66-
67-
var elements []string
68-
var startIndex int
69-
var itemLen int = 0
70-
for i := 1;i < len(data);i++ {
71-
if data[i] == ',' || data[i] == '}' {
72-
if itemLen != 0 {
73-
elements = append(elements,data[startIndex + 1:i])
74-
}
75-
startIndex = i
76-
} else {
77-
itemLen++
78-
}
79-
}
80-
//fmt.Println(elements)
81-
82-
for _, element := range elements {
83-
if strings.Index(element,":") == -1 {
84-
return output, errors.New("I couldn't find the : seperator between an element name and an element value o.o")
85-
}
86-
87-
fields := strings.SplitN(element,":",2)
88-
if fields[0] == "" {
89-
return output, errors.New("You can't have a blank name for a field x.x")
90-
}
91-
if fields[1] == "" {
92-
return output, errors.New("You can't have an empty field x.x")
93-
}
94-
95-
_, exists := output[fields[0]]
96-
if exists {
97-
return output, errors.New("You can't have two fields with the same name")
98-
}
99-
output[fields[0]] = fields[1]
100-
}
101-
// fmt.Printf("%+v\n", output)
102-
return
103-
}
104-
105-
func DetectType(data string) string {
106-
if len(data) == 0 {
107-
return "string"
108-
}
109-
if data[0] == '[' {
110-
return "list"
111-
}
112-
if data[0] == '{' {
113-
return "map"
114-
}
115-
if data[0] == '*' {
116-
return "variable"
117-
}
118-
_, err := strconv.Atoi(data)
119-
if err == nil {
120-
return "int"
121-
}
122-
return "string"
123-
}
124-
125-
// Normalize the [s and ]s into {s and }s for maps
126-
func NormalizeMapString(data string) string {
127-
if len(data) < 3 {
128-
return "{}"
129-
}
130-
if data == "[]" {
131-
return "{}"
132-
}
133-
if data[0] == '[' && data[len(data) - 1] == ']' {
134-
data = data[1:len(data) - 1]
135-
data = "{" + data + "}"
136-
}
137-
return data
138-
}
139-
14012
func ResolveVariable(data string, server_data Datastore) (result string, err error) {
14113
if len(data) < 3 {
14214
return "", errors.New("variable name too short x.x")

utils.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package sep
2+
3+
import "strconv"
4+
5+
func DetectType(data string) string {
6+
if len(data) == 0 {
7+
return "string"
8+
}
9+
if data[0] == '[' {
10+
return "list"
11+
}
12+
if data[0] == '{' {
13+
return "map"
14+
}
15+
if data[0] == '*' {
16+
return "variable"
17+
}
18+
_, err := strconv.Atoi(data)
19+
if err == nil {
20+
return "int"
21+
}
22+
return "string"
23+
}
24+
25+
// Normalize the [s and ]s into {s and }s for maps
26+
func NormalizeMapString(data string) string {
27+
if len(data) < 3 {
28+
return "{}"
29+
}
30+
if data == "[]" {
31+
return "{}"
32+
}
33+
if data[0] == '[' && data[len(data) - 1] == ']' {
34+
data = data[1:len(data) - 1]
35+
data = "{" + data + "}"
36+
}
37+
return data
38+
}

0 commit comments

Comments
 (0)