11package sep
22
3- import "strings"
43import "strconv"
54import "errors"
65
@@ -10,133 +9,6 @@ type Datastore interface {
109DeleteVar (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-
14012func ResolveVariable (data string , server_data Datastore ) (result string , err error ) {
14113if len (data ) < 3 {
14214return "" , errors .New ("variable name too short x.x" )
0 commit comments