File: keyvalues.go

package info (click to toggle)
golang-github-juju-utils 0.0~git20171220.f38c0b0-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,748 kB
  • sloc: makefile: 20
file content (41 lines) | stat: -rw-r--r-- 1,305 bytes parent folder | download | duplicates (2)
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
41
// Copyright 2014 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.

// The keyvalues package implements a set of functions for parsing key=value data,
// usually passed in as command-line parameters to juju subcommands, e.g.
// juju-set mongodb logging=true
package keyvalues

import (
	"fmt"
	"strings"
)

// DuplicateError signals that a duplicate key was encountered while parsing
// the input into a map.
type DuplicateError string

func (e DuplicateError) Error() string {
	return string(e)
}

// Parse parses the supplied string slice into a map mapping
// keys to values. Duplicate keys cause an error to be returned.
func Parse(src []string, allowEmptyValues bool) (map[string]string, error) {
	results := map[string]string{}
	for _, kv := range src {
		parts := strings.SplitN(kv, "=", 2)
		if len(parts) != 2 {
			return nil, fmt.Errorf(`expected "key=value", got %q`, kv)
		}
		key, value := strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1])
		if len(key) == 0 || (!allowEmptyValues && len(value) == 0) {
			return nil, fmt.Errorf(`expected "key=value", got "%s=%s"`, key, value)
		}
		if _, exists := results[key]; exists {
			return nil, DuplicateError(fmt.Sprintf("key %q specified more than once", key))
		}
		results[key] = value
	}
	return results, nil
}