Search Apps Documentation Source Content File Folder Download Copy Actions Download

property_parser.gno

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