-
Notifications
You must be signed in to change notification settings - Fork 53
/
options.go
57 lines (50 loc) · 1.41 KB
/
options.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
package v1
import (
"encoding/json"
"fmt"
"time"
"github.com/rancher/opni/pkg/util"
"google.golang.org/protobuf/types/known/structpb"
)
type DefaultUninstallOptions struct {
// If true, will permanently delete all stored data associated with this
// capability.
DeleteStoredData bool `json:"deleteStoredData,omitempty"`
// Delay the uninstall operation by this amount of time, during which the
// operation can be canceled without incurring any data loss.
// If deleteStoredData is false, this option may not have any effect.
InitialDelay Duration `json:"initialDelay,omitempty"`
}
type Duration time.Duration
func (d *Duration) UnmarshalJSON(b []byte) error {
var value any
if err := json.Unmarshal(b, &value); err != nil {
return err
}
switch value := value.(type) {
case string:
if parsed, err := time.ParseDuration(value); err != nil {
return err
} else {
*d = Duration(parsed)
}
case float64:
*d = Duration(time.Duration(value))
default:
return fmt.Errorf("invalid duration: %v", value)
}
return nil
}
func (uo DefaultUninstallOptions) ToStruct() *structpb.Struct {
m := make(map[string]any)
data := util.Must(json.Marshal(uo))
util.Must(json.Unmarshal(data, &m))
return util.Must(structpb.NewStruct(m))
}
func (uo *DefaultUninstallOptions) LoadFromStruct(s *structpb.Struct) error {
data, err := json.Marshal(s.AsMap())
if err != nil {
return err
}
return json.Unmarshal(data, uo)
}