-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtype.go
More file actions
167 lines (151 loc) · 3.16 KB
/
Copy pathtype.go
File metadata and controls
167 lines (151 loc) · 3.16 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package xgo
import (
"fmt"
"reflect"
"strings"
"text/tabwriter"
"unicode"
"github.com/ktye/iv/apl"
)
// New returns an initialization function for the given type.
func New(t reflect.Type) create {
return create{t}
}
type Value reflect.Value
// xgo values are copied by reference.
func (v Value) Copy() apl.Value {
return v
}
func (v Value) String(f apl.Format) string {
keys := v.Keys()
if keys == nil {
return fmt.Sprintf("xgo.Value (not a struct) %T", v)
}
var buf strings.Builder
tw := tabwriter.NewWriter(&buf, 1, 0, 1, ' ', 0)
for _, k := range keys {
val := v.At(k)
s := ""
if val == nil {
s = "?"
} else {
s = val.String(f)
}
fmt.Fprintf(tw, "%s:\t%s\n", k.String(f), s)
}
tw.Flush()
s := buf.String()
if len(s) > 0 && s[len(s)-1] == '\n' {
return s[:len(s)-1]
}
return s
}
// Keys returns the field names, if the value is a struct.
// It does not return the method names.
// It returns nil, if the Value is not a struct.
func (v Value) Keys() []apl.Value {
val := reflect.Value(v)
if val.Kind() == reflect.Ptr {
val = val.Elem()
}
if val.Kind() != reflect.Struct {
return nil
}
t := val.Type()
n := t.NumField()
res := make([]apl.Value, n)
for i := 0; i < n; i++ {
res[i] = apl.String(t.Field(i).Name)
}
return res
}
func (v Value) Methods() []string {
val := reflect.Value(v)
t := val.Type()
n := t.NumMethod()
res := make([]string, n)
for i := range res {
res[i] = lower(t.Method(i).Name)
}
return res
}
// Field returns the value of a field or a method with the given name.
func (v Value) At(key apl.Value) apl.Value {
name, ok := key.(apl.String)
if ok == false {
return nil
}
val := reflect.Value(v)
var zero reflect.Value
Name := upper(string(name))
m := val.MethodByName(Name)
if m != zero {
return Function{Name: Name, Fn: m}
}
if val.Kind() == reflect.Ptr {
val = val.Elem()
}
if val.Kind() != reflect.Struct {
return nil
}
sf := val.FieldByName(Name)
if sf == zero {
return nil
}
rv, err := Convert(sf)
if err != nil {
return nil
}
return rv
}
func (v Value) Set(key apl.Value, fv apl.Value) error {
field, ok := key.(apl.String)
if ok == false {
return fmt.Errorf("key must be a string")
}
val := reflect.Value(v).Elem()
if val.Kind() != reflect.Struct {
return fmt.Errorf("not a struct: cannot set field")
}
sf := val.FieldByName(string(field))
var zero reflect.Value
if sf == zero {
return fmt.Errorf("%v: field does not exist: %s", val.Type(), field)
}
sv, err := export(fv, sf.Type())
if err != nil {
return err
}
sf.Set(sv)
return nil
}
type create struct {
reflect.Type
}
func (t create) String(f apl.Format) string {
return fmt.Sprintf("new %v", t.Type)
}
func (t create) Copy() apl.Value {
return t
}
func (t create) Call(a *apl.Apl, L, R apl.Value) (apl.Value, error) {
v := reflect.New(t.Type)
return Value(v), nil
}
func upper(s string) string {
return firstrune(s, unicode.ToUpper)
}
func lower(s string) string {
return firstrune(s, unicode.ToLower)
}
func firstrune(s string, f func(r rune) rune) string {
var buf strings.Builder
for i, r := range s {
if i == 0 {
buf.WriteRune(f(r))
} else {
buf.WriteRune(r)
}
}
return buf.String()
}