File: slice.go

package info (click to toggle)
golang-github-containers-common 0.64.2%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,528 kB
  • sloc: makefile: 130; sh: 102
file content (101 lines) | stat: -rw-r--r-- 2,954 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package attributedstring

import (
	"bytes"
	"fmt"

	"github.com/BurntSushi/toml"
)

// Slice allows for extending a TOML string array with custom
// attributes that control how the array is marshaled into a Go string.
//
// Specifically, an Slice can be configured to avoid it being
// overridden by a subsequent unmarshal sequence.  When the `append` attribute
// is specified, the array will be appended instead (e.g., `array=["9",
// {append=true}]`).
type Slice struct { // A "mixed-type array" in TOML.
	// Note that the fields below _must_ be exported.  Otherwise the TOML
	// encoder would fail during type reflection.
	Values     []string
	Attributes struct { // Using a struct allows for adding more attributes in the future.
		Append *bool // Nil if not set by the user
	}
}

// NewSlice creates a new slice with the specified values.
func NewSlice(values []string) Slice {
	return Slice{Values: values}
}

// Get returns the Slice values or an empty string slice.
func (a *Slice) Get() []string {
	if a.Values == nil {
		return []string{}
	}
	return a.Values
}

// Set overrides the values of the Slice.
func (a *Slice) Set(values []string) {
	a.Values = values
}

// UnmarshalTOML is the custom unmarshal method for Slice.
func (a *Slice) UnmarshalTOML(data any) error {
	iFaceSlice, ok := data.([]any)
	if !ok {
		return fmt.Errorf("unable to cast to interface array: %v", data)
	}

	var loadedStrings []string
	for _, x := range iFaceSlice { // Iterate over each item in the slice.
		switch val := x.(type) {
		case string: // Strings are directly appended to the slice.
			loadedStrings = append(loadedStrings, val)
		case map[string]any: // The attribute struct is represented as a map.
			for k, v := range val { // Iterate over all _supported_ keys.
				switch k {
				case "append":
					boolVal, ok := v.(bool)
					if !ok {
						return fmt.Errorf("unable to cast append to bool: %v", k)
					}
					a.Attributes.Append = &boolVal
				default: // Unsupported map key.
					return fmt.Errorf("unsupported key %q in map: %v", k, val)
				}
			}
		default: // Unsupported item.
			return fmt.Errorf("unsupported item in attributed string slice: %v", x)
		}
	}

	if a.Attributes.Append != nil && *a.Attributes.Append { // If _explicitly_ configured, append the loaded slice.
		a.Values = append(a.Values, loadedStrings...)
	} else { // Default: override the existing Slice.
		a.Values = loadedStrings
	}
	return nil
}

// MarshalTOML is the custom marshal method for Slice.
func (a *Slice) MarshalTOML() ([]byte, error) {
	iFaceSlice := make([]any, 0, len(a.Values))

	for _, x := range a.Values {
		iFaceSlice = append(iFaceSlice, x)
	}

	if a.Attributes.Append != nil {
		attributes := map[string]any{"append": *a.Attributes.Append}
		iFaceSlice = append(iFaceSlice, attributes)
	}

	buf := new(bytes.Buffer)
	enc := toml.NewEncoder(buf)
	if err := enc.Encode(iFaceSlice); err != nil {
		return nil, err
	}
	return buf.Bytes(), nil
}