File: helpers.go

package info (click to toggle)
golang-github-tombuildsstuff-giovanni 0.20.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 15,908 kB
  • sloc: makefile: 3
file content (40 lines) | stat: -rw-r--r-- 1,057 bytes parent folder | download | duplicates (4)
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
package filesystems

import (
	"fmt"
	"strings"
)

func buildProperties(input map[string]string) string {
	// properties has to be a comma-separated key-value pair
	properties := make([]string, 0)

	for k, v := range input {
		properties = append(properties, fmt.Sprintf("%s=%s", k, v))
	}

	return strings.Join(properties, ",")
}

func parseProperties(input string) (*map[string]string, error) {
	properties := make(map[string]string)
	if input == "" {
		return &properties, nil
	}

	// properties is a comma-separated list of key-value pairs
	splitProperties := strings.Split(input, ",")
	for _, propertyRaw := range splitProperties {
		// because these are base64-encoded they're likely to end in at least one =
		// as such we can't string split on that -_-
		position := strings.Index(propertyRaw, "=")
		if position < 0 {
			return nil, fmt.Errorf("Expected there to be an equals in the key value pair: %q", propertyRaw)
		}

		key := propertyRaw[0:position]
		value := propertyRaw[position+1:]
		properties[key] = value
	}
	return &properties, nil
}