forked from grailbio/reflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compat.go
58 lines (51 loc) · 1.43 KB
/
compat.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
// 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 (
"github.com/grailbio/reflow"
"github.com/grailbio/reflow/types"
"github.com/grailbio/reflow/values"
)
func fileToFileset(file values.File) reflow.Fileset {
return reflow.Fileset{
Map: map[string]reflow.File{
".": reflow.File(file),
},
}
}
func dirToFileset(dir values.Dir) reflow.Fileset {
fs := reflow.Fileset{Map: map[string]reflow.File{}}
for k, file := range dir {
fs.Map[k] = reflow.File(file)
}
return fs
}
func coerceToFileset(t *types.T, v values.T) reflow.Fileset {
switch t.Kind {
case types.FileKind:
return fileToFileset(v.(values.File))
case types.DirKind:
return dirToFileset(v.(values.Dir))
case types.ListKind:
list := v.(values.List)
fs := reflow.Fileset{List: make([]reflow.Fileset, len(list))}
for i := range list {
fs.List[i] = coerceToFileset(t.Elem, list[i])
}
return fs
default:
panic("invalid input type")
}
}
var coerceFlowToFilesetDigest = reflow.Digester.FromString("grail.com/reflow/syntax.coerceFlowToFileset")
func coerceFlowToFileset(t *types.T, f *reflow.Flow) *reflow.Flow {
return &reflow.Flow{
Op: reflow.OpCoerce,
Deps: []*reflow.Flow{f},
FlowDigest: coerceFlowToFilesetDigest,
Coerce: func(v values.T) (values.T, error) {
return coerceToFileset(t, v), nil
},
}
}