Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/river/types.go
4095 views
1
package river
2
3
import "github.com/grafana/agent/pkg/river/internal/value"
4
5
// Our types in this file are re-implementations of interfaces from
6
// value.Capsule. They are *not* defined as type aliases, since pkg.go.dev
7
// would show the type alias instead of the contents of that type (which IMO is
8
// a frustrating user experience).
9
//
10
// The types below must be kept in sync with the internal package, and the
11
// checks below ensure they're compatible.
12
var (
13
_ value.Unmarshaler = (Unmarshaler)(nil)
14
_ value.Capsule = (Capsule)(nil)
15
_ value.ConvertibleFromCapsule = (ConvertibleFromCapsule)(nil)
16
_ value.ConvertibleIntoCapsule = (ConvertibleIntoCapsule)(nil)
17
)
18
19
// The Unmarshaler interface allows a type to hook into River decoding and
20
// decode into another type or provide pre-decoding logic.
21
type Unmarshaler interface {
22
// UnmarshalRiver is invoked when decoding a River value into a Go value. f
23
// should be called with a pointer to a value to decode into. UnmarshalRiver
24
// will not be called on types which are squashed into the parent struct
25
// using `river:",squash"`.
26
UnmarshalRiver(f func(v interface{}) error) error
27
}
28
29
// Capsule is an interface marker which tells River that a type should always
30
// be treated as a "capsule type" instead of the default type River would
31
// assign.
32
//
33
// Capsule types are useful for passing around arbitrary Go values in River
34
// expressions and for declaring new synthetic types with custom conversion
35
// rules.
36
//
37
// By default, only two capsule values of the same underlying Go type are
38
// compatible. Types which implement ConvertibleFromCapsule or
39
// ConvertibleToCapsule can provide custom logic for conversions from and to
40
// other types.
41
type Capsule interface {
42
// RiverCapsule marks the type as a Capsule. RiverCapsule is never invoked by
43
// River.
44
RiverCapsule()
45
}
46
47
// ErrNoConversion is returned by implementations of ConvertibleFromCapsule and
48
// ConvertibleToCapsule when a conversion with a specific type is unavailable.
49
//
50
// Returning this error causes River to fall back to default conversion rules.
51
var ErrNoConversion = value.ErrNoConversion
52
53
// ConvertibleFromCapsule is a Capsule which supports custom conversion from
54
// any Go type which is not the same as the capsule type.
55
type ConvertibleFromCapsule interface {
56
Capsule
57
58
// ConvertFrom updates the ConvertibleFromCapsule value based on the value of
59
// src. src may be any Go value, not just other capsules.
60
//
61
// ConvertFrom should return ErrNoConversion if no conversion is available
62
// from src. Other errors are treated as a River decoding error.
63
ConvertFrom(src interface{}) error
64
}
65
66
// ConvertibleIntoCapsule is a Capsule which supports custom conversion into
67
// any Go type which is not the same as the capsule type.
68
type ConvertibleIntoCapsule interface {
69
Capsule
70
71
// ConvertInto should convert its value and store it into dst. dst will be a
72
// pointer to a Go value of any type.
73
//
74
// ConvertInto should return ErrNoConversion if no conversion into dst is
75
// available. Other errors are treated as a River decoding error.
76
ConvertInto(dst interface{}) error
77
}
78
79