Search Apps Documentation Source Content File Folder Download Copy Actions Download

property_parser.gno

1.19 Kb · 55 lines
 1package blueprint
 2
 3import "net/url"
 4
 5// parsePropsCSV accepts comma-separated keys and comma-separated values.
 6// Each value must be URL-encoded so commas and other separators can be carried
 7// safely; this parser URL-decodes values before returning the map.
 8func parsePropsCSV(keys, values string) map[string]string {
 9	if keys == "" {
10		panic("properties keys must be not empty")
11	}
12
13	result := map[string]string{}
14	keyStart, valStart := 0, 0
15	keyIdx, valIdx := 0, 0
16
17	for {
18		for keyIdx < len(keys) && keys[keyIdx] != ',' {
19			keyIdx++
20		}
21		for valIdx < len(values) && values[valIdx] != ',' {
22			valIdx++
23		}
24
25		key := keys[keyStart:keyIdx]
26		if key == "" {
27			panic("properties key must be not empty")
28		}
29		if _, found := result[key]; found {
30			panic("properties key duplicated: " + key)
31		}
32
33		value, err := url.PathUnescape(values[valStart:valIdx])
34		if err != nil {
35			panic("properties value URL decode failed: " + key)
36		}
37		result[key] = value
38
39		keyEnd := keyIdx >= len(keys)
40		valEnd := valIdx >= len(values)
41		if keyEnd != valEnd {
42			panic("properties keys and values count mismatch")
43		}
44		if keyEnd {
45			break
46		}
47
48		keyIdx++
49		valIdx++
50		keyStart = keyIdx
51		valStart = valIdx
52	}
53
54	return result
55}