-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresource.go
73 lines (66 loc) · 1.57 KB
/
resource.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
71
72
73
package scy
import (
"fmt"
"github.com/viant/afs/storage"
"reflect"
"time"
)
// Resource represents a secret config
type Resource struct {
Name string `json:",omitempty" yaml:"Name"`
URL string `json:",omitempty" yaml:"URL"`
Key string `json:",omitempty" yaml:"Key"` //encryption key
MaxRetry int `json:",omitempty" yaml:"MaxRetry"`
TimeoutMs int `json:",omitempty" yaml:"TimeoutMs"`
Fallback *Resource `json:",omitempty" yaml:"Fallback"`
Options []storage.Option `json:"-" yaml:"-"`
Data []byte `json:",omitempty" yaml:"Data"`
target reflect.Type
}
func (r *Resource) Timeout() time.Duration {
return time.Duration(r.TimeoutMs) * time.Millisecond
}
func (r *Resource) Init() {
if r.MaxRetry == 0 {
r.MaxRetry = 3
}
if r.TimeoutMs == 0 {
r.TimeoutMs = 5000
}
}
// SetTarget sets target type
func (r *Resource) SetTarget(t reflect.Type) {
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
r.target = t
}
// Validate checks if resource if valid
func (r *Resource) Validate() error {
if r == nil {
return fmt.Errorf("resource was empty")
}
if r.URL == "" {
return fmt.Errorf("url was empty")
}
return nil
}
// NewResource creates a resource
func NewResource(target interface{}, URL, Key string) *Resource {
result := &Resource{
URL: URL,
Key: Key,
}
if target == nil {
return result
}
switch v := target.(type) {
case string:
result.Name = v
case reflect.Type:
result.SetTarget(v)
default:
result.SetTarget(reflect.TypeOf(v))
}
return result
}