-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathvar.go
More file actions
132 lines (120 loc) · 2.72 KB
/
Copy pathvar.go
File metadata and controls
132 lines (120 loc) · 2.72 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
package io
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"reflect"
"strings"
"text/tabwriter"
"github.com/ktye/iv/apl"
)
// Varfs exports variables as a file system.
type varfs struct {
*apl.Apl
}
func (v varfs) FileSystem(root string) (FileSystem, error) {
if v.Apl == nil {
return nil, fmt.Errorf("varfs: apl is not connected")
}
if root != "/" {
return nil, fmt.Errorf("varfs can only be registerd with root file: var:///")
}
return v, nil
}
func (v varfs) String() string {
return "var:///"
}
func (v varfs) Write(name string) (rc io.WriteCloser, err error) {
defer func() {
if err != nil {
err = &os.PathError{
Op: "write",
Path: name,
Err: err,
}
}
}()
if strings.HasSuffix(name, "/") {
return nil, fmt.Errorf("varfs: cannot write to directory")
}
// Package variables are immutable.
if strings.ContainsRune(name, '→') {
return nil, fmt.Errorf("varfs: cannot update package variable")
}
x := v.Apl.Lookup(name)
if x == nil {
return nil, fmt.Errorf("varfs: variable does not exist")
}
if vr, ok := x.(apl.VarReader); ok {
var b bytes.Buffer
return varWriter{Buffer: &b, a: v.Apl, v: vr, name: name}, nil
}
return nil, fmt.Errorf("varfs: type is not assignable: %T", x)
}
type varWriter struct {
*bytes.Buffer
a *apl.Apl
v apl.VarReader
name string
}
func (vw varWriter) Close() error {
t := reflect.TypeOf(vw.v)
v, err := vw.v.ReadFrom(vw.a, vw.Buffer)
if err != nil {
return err
}
if nt := reflect.TypeOf(v); nt != t {
return fmt.Errorf("%T ReadFrom returns a wrong type: %T", t, nt)
}
return vw.a.Assign(vw.name, v)
}
func (v varfs) Open(name, mpt string) (io.ReadCloser, error) {
list := func() (io.ReadCloser, error) {
pkg := strings.TrimSuffix(name, "/")
l, err := v.Apl.Vars(pkg)
if err != nil {
return nil, err
}
if pkg != "" {
pkg += "→"
}
var buf bytes.Buffer
tw := tabwriter.NewWriter(&buf, 1, 0, 1, ' ', 0)
for i := range l {
varname := pkg + l[i]
info := ""
if strings.HasSuffix(varname, "/") == false {
x := v.Apl.Lookup(varname)
if x == nil {
info = "?"
} else {
info = reflect.TypeOf(x).String()
if ar, ok := x.(apl.Array); ok {
info += fmt.Sprintf(" %v", ar.Shape())
}
if s, ok := x.(apl.String); ok {
info += fmt.Sprintf(" %d", len(s))
}
}
}
fmt.Fprintf(tw, "%s\t%s\n", mpt+varname, info)
}
tw.Flush()
return ioutil.NopCloser(&buf), nil
}
if name == "" || strings.HasSuffix(name, "/") {
return list()
}
// Print a variable.
x := v.Apl.Lookup(name)
if x != nil {
return ioutil.NopCloser(strings.NewReader(x.String(v.Apl.Format))), nil
}
return nil, &os.PathError{
Op: "open",
Path: name,
Err: fmt.Errorf("variable does not exist"),
}
}