/
v0module.go
149 lines (134 loc) · 3.46 KB
/
v0module.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
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
// Copyright 2017 GRAIL, Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package syntax
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/grailbio/reflow/lang"
"github.com/grailbio/reflow/types"
"github.com/grailbio/reflow/values"
)
// V0module implements a module bridge to "v0" reflow scripts. The
// module's parameters correspond with the script's parameters, and
// it produces a value exporting toplevel identifiers.
type v0module struct {
params map[string]string
path string
typ *types.T
}
// Eager returns true.
func (m *v0module) Eager() bool { return true }
func (m *v0module) new(args []string) *lang.Program {
prog := &lang.Program{
File: m.path,
Args: args,
// Typechecking happens before the v0module is constructed,
// so we can safely discard messages here.
Errors: ioutil.Discard,
}
f, err := os.Open(m.path)
if err != nil {
panic(fmt.Sprintf("failed to open file %s: %v", m.path, err))
}
defer f.Close()
if err := prog.ParseAndTypecheck(f); err != nil {
panic(err)
}
return prog
}
func (m *v0module) Make(sess *Session, params *values.Env) (values.T, error) {
var args []string
if v := params.Value("args"); v != nil {
l := v.(values.List)
args = make([]string, len(l))
for i := range l {
args[i] = l[i].(string)
}
}
prog := m.new(args)
flags := prog.Flags()
var flagerr error
flags.VisitAll(func(f *flag.Flag) {
if err := f.Value.Set(params.Value(f.Name).(string)); err != nil && flagerr == nil {
flagerr = err
}
})
if err := flagerr; err != nil {
return nil, err
}
return prog.ModuleValue()
}
func (m *v0module) Flags(sess *Session, env *values.Env) (*flag.FlagSet, error) {
return nil, errors.New("flags not supported for v0 modules")
}
func (m *v0module) FlagEnv(*flag.FlagSet, *values.Env, *types.Env) error {
return errors.New("flags not supported for v0 modules")
}
func (m *v0module) ParamErr(env *types.Env) error {
params := make(map[string]bool)
for k := range m.params {
params[k] = true
}
for id, t := range env.Symbols() {
switch id {
case "args":
if t.Kind != types.ListKind || t.Elem.Kind != types.StringKind {
return fmt.Errorf("invalid type %s for parameter %s (type [string])", t, id)
}
default:
if !params[id] {
return fmt.Errorf("module has no parameter %s", id)
}
if t.Kind != types.StringKind {
return fmt.Errorf("invalid type %s for parameter %s (type string)", t, id)
}
}
delete(params, id)
}
if len(params) > 0 {
var missing []string
for id := range params {
missing = append(missing, id)
}
if len(missing) == 1 {
return fmt.Errorf("missing module parameter %s", missing[0])
}
return fmt.Errorf("missing module parameters %s", strings.Join(missing, ", "))
}
return nil
}
func (m *v0module) Params() []Param {
var params []Param
for ident, doc := range m.params {
p := Param{
Ident: ident,
Doc: doc,
Required: true,
Type: types.String,
}
params = append(params, p)
}
return append(params, Param{
Ident: "args",
Doc: "Reflow v0 arguments",
Required: false,
Type: types.List(types.String),
})
}
func (m *v0module) Doc(ident string) string {
return ""
}
func (m *v0module) Type(*types.Env) *types.T {
return m.typ
}
func (*v0module) InjectArgs(*Session, []string) error {
panic("syntax.v0module: invalid call to InjectArgs")
}
func (*v0module) InjectedArgs() []string {
return nil
}