forked from charmbracelet/huh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
accessor.go
44 lines (36 loc) · 885 Bytes
/
accessor.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
package huh
// Accessor give read/write access to field values.
type Accessor[T any] interface {
Get() T
Set(value T)
}
// EmbeddedAccessor is a basic accessor, acting as the default one for fields.
type EmbeddedAccessor[T any] struct {
value T
}
// Get gets the value.
func (a *EmbeddedAccessor[T]) Get() T {
return a.value
}
// Set sets the value.
func (a *EmbeddedAccessor[T]) Set(value T) {
a.value = value
}
// PointerAccessor allows field value to be exposed as a pointed variable.
type PointerAccessor[T any] struct {
value *T
}
// NewPointerAccessor returns a new pointer accessor.
func NewPointerAccessor[T any](value *T) *PointerAccessor[T] {
return &PointerAccessor[T]{
value: value,
}
}
// Get gets the value.
func (a *PointerAccessor[T]) Get() T {
return *a.value
}
// Set sets the value.
func (a *PointerAccessor[T]) Set(value T) {
*a.value = value
}