-
Notifications
You must be signed in to change notification settings - Fork 2
/
step_command_cache.go
70 lines (57 loc) · 1.48 KB
/
step_command_cache.go
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
package pipeline
import (
"encoding/json"
"fmt"
"github.com/buildkite/go-pipeline/ordered"
)
var _ interface {
json.Marshaler
ordered.Unmarshaler
} = (*Cache)(nil)
var (
errUnsupportedCacheType = fmt.Errorf("unsupported type for cache")
)
// Cache models the cache settings for a given step
type Cache struct {
Disabled bool `yaml:",omitempty"`
Name string `yaml:"name,omitempty"`
Paths []string `yaml:"paths,omitempty"`
Size string `yaml:"size,omitempty"`
RemainingFields map[string]any `yaml:",inline"`
}
// MarshalJSON marshals the step to JSON. Special handling is needed because
// yaml.v3 has "inline" but encoding/json has no concept of it.
func (c *Cache) MarshalJSON() ([]byte, error) {
if c.Disabled {
return json.Marshal(false)
}
return inlineFriendlyMarshalJSON(c)
}
// UnmarshalOrdered unmarshals from the following types:
// - string: a single path
// - []string: multiple paths
// - ordered.Map: a map containing paths, among potentially other things
func (c *Cache) UnmarshalOrdered(o any) error {
switch v := o.(type) {
case bool:
if !v {
c.Disabled = true
}
case string:
c.Paths = []string{v}
case []any:
s := make([]string, 0, len(v))
if err := ordered.Unmarshal(v, &s); err != nil {
return err
}
c.Paths = s
case *ordered.MapSA:
type wrappedCache Cache
if err := ordered.Unmarshal(o, (*wrappedCache)(c)); err != nil {
return err
}
default:
return fmt.Errorf("%w: %T", errUnsupportedCacheType, v)
}
return nil
}