Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/river/internal/value/capsule.go
4096 views
1
package value
2
3
import (
4
"fmt"
5
)
6
7
// Capsule is a marker interface for Go values which forces a type to be
8
// represented as a River capsule. This is useful for types whose underlying
9
// value is not a capsule, such as:
10
//
11
// // Secret is a secret value. It would normally be a River string since the
12
// // underlying Go type is string, but it's a capsule since it implements
13
// // the Capsule interface.
14
// type Secret string
15
//
16
// func (s Secret) RiverCapsule() {}
17
//
18
// Extension interfaces are used to describe additional behaviors for Capsules.
19
// ConvertibleCapsule allows defining custom conversion rules to convert
20
// between other Go values.
21
type Capsule interface {
22
RiverCapsule()
23
}
24
25
// ErrNoConversion is returned by implementations of ConvertibleCapsule to
26
// denote that a custom conversion from or to a specific type is unavailable.
27
var ErrNoConversion = fmt.Errorf("no custom capsule conversion available")
28
29
// ConvertibleFromCapsule is a Capsule which supports custom conversion rules
30
// from any Go type which is not the same as the capsule type.
31
type ConvertibleFromCapsule interface {
32
Capsule
33
34
// ConvertFrom should modify the ConvertibleCapsule value based on the value
35
// of src.
36
//
37
// ConvertFrom should return ErrNoConversion if no conversion is available
38
// from src.
39
ConvertFrom(src interface{}) error
40
}
41
42
// ConvertibleIntoCapsule is a Capsule which supports custom conversion rules
43
// into any Go type which is not the same as the capsule type.
44
type ConvertibleIntoCapsule interface {
45
Capsule
46
47
// ConvertInto should convert its value and store it into dst. dst will be a
48
// pointer to a value which ConvertInto is expected to update.
49
//
50
// ConvertInto should return ErrNoConversion if no conversion into dst is
51
// available.
52
ConvertInto(dst interface{}) error
53
}
54
55