-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetter.go
58 lines (46 loc) · 1.13 KB
/
setter.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
package jaws
import "fmt"
type Setter[T comparable] interface {
Getter[T]
// JawsSet may return ErrValueUnchanged to indicate value was already set.
JawsSet(elem *Element, value T) (err error)
}
type setterReadOnly[T comparable] struct {
Getter[T]
}
func (setterReadOnly[T]) JawsSet(*Element, T) error {
return ErrValueNotSettable
}
func (s setterReadOnly[T]) JawsGetTag(*Request) any {
return s.Getter
}
func makeReadOnlySetter[T comparable](g Getter[T]) Setter[T] {
return setterReadOnly[T]{g}
}
type setterStatic[T comparable] struct {
v T
}
func (setterStatic[T]) JawsSet(*Element, T) error {
return ErrValueNotSettable
}
func (s setterStatic[T]) JawsGet(*Element) T {
return s.v
}
func (s setterStatic[T]) JawsGetTag(*Request) any {
return nil
}
func makeStaticSetter[T comparable](v T) Setter[T] {
return setterStatic[T]{v}
}
func makeSetter[T comparable](v any) Setter[T] {
switch v := v.(type) {
case Setter[T]:
return v
case Getter[T]:
return makeReadOnlySetter(v)
case T:
return makeStaticSetter(v)
}
var blank T
panic(fmt.Errorf("jaws.Setter[%T], jaws.Getter[%T] or %T not %T", blank, blank, blank, v))
}