forked from Ompluscator/dynamic-struct
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter_field.go
53 lines (46 loc) · 1.07 KB
/
writer_field.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
package dynamicstruct
import (
"errors"
"reflect"
)
type scalarImpl struct {
field reflect.StructField
value reflect.Value
}
func (s *scalarImpl) Set(value any) (err error) {
defer func() {
er := recover()
if er != nil {
err = errors.New(er.(string))
}
}()
s.value.Set(reflect.ValueOf(value))
return
}
func (s *scalarImpl) Get() (any, bool) {
return s.value.Interface(), true
}
// can set struct.substruct field value
func (s *scalarImpl) linkSet(_ []string, value any) error {
return s.Set(value)
}
// can set struct.substruct field value
func (s *scalarImpl) linkGet(_ []string) (any, bool) {
return s.Get()
}
// can set struct.substruct field value
func (s *scalarImpl) LinkSet(_ string, _ any) error {
return errors.New("can not LinkSet")
}
func (s *scalarImpl) LinkGet(_ string) (any, bool) {
return nil, false
}
func (s *scalarImpl) Type() reflect.Type {
return s.field.Type
}
func (s *scalarImpl) linkTyp(names []string) (reflect.Type, bool) {
return s.Type(), true
}
func (s *scalarImpl) LinkTyp(name string) (reflect.Type, bool) {
return s.Type(), true
}