This repository has been archived by the owner on Mar 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
schema.go
102 lines (88 loc) · 2.47 KB
/
schema.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
package schema
import (
"fmt"
ipld "github.com/ipfs/go-ipld-format"
"github.com/textileio/go-textile/pb"
)
// ErrFileValidationFailed indicates dag schema validation failed
var ErrFileValidationFailed = fmt.Errorf("file failed schema validation")
// ErrEmptySchema indicates a schema is empty
var ErrEmptySchema = fmt.Errorf("schema does not create any files")
// ErrLinkOrderNotSolvable
var ErrLinkOrderNotSolvable = fmt.Errorf("link order is not solvable")
// ErrSchemaInvalidMill indicates a schema has an invalid mill entry
var ErrSchemaInvalidMill = fmt.Errorf("schema contains an invalid mill")
// ErrMissingJsonSchema indicates json schema is missing
var ErrMissingJsonSchema = fmt.Errorf("json mill requires a json schema")
// ErrBadJsonSchema indicates json schema is invalid
var ErrBadJsonSchema = fmt.Errorf("json schema is not valid")
// FileTag indicates the link should "use" the input file as source
const FileTag = ":file"
// SingleFileTag is a magic key indicating that a directory is actually a single file
const SingleFileTag = ":single"
// ValidateMill is false if mill is not one of the built in tags
func ValidateMill(mill string) bool {
switch mill {
case
"/schema",
"/blob",
"/image/resize",
"/image/exif",
"/json":
return true
}
return false
}
// LinkByName finds a link w/ one of the given names in the provided list
func LinkByName(links []*ipld.Link, names []string) *ipld.Link {
for _, l := range links {
for _, n := range names {
if l.Name == n {
return l
}
}
}
return nil
}
// Steps returns link steps in the order they should be processed
func Steps(links map[string]*pb.Link) ([]pb.Step, error) {
var steps []pb.Step
run := links
i := 0
for {
if i > len(links) {
return nil, ErrLinkOrderNotSolvable
}
next := orderLinks(run, &steps)
if len(next) == 0 {
break
}
run = next
i++
}
return steps, nil
}
// orderLinks attempts to place all links in steps, returning any unused
// whose source is not yet in steps
func orderLinks(links map[string]*pb.Link, steps *[]pb.Step) map[string]*pb.Link {
unused := make(map[string]*pb.Link)
for name, link := range links {
if link.Use == FileTag {
*steps = append([]pb.Step{{Name: name, Link: link}}, *steps...)
} else {
useAt := -1
for i, s := range *steps {
if link.Use == s.Name {
useAt = i
break
}
}
if useAt >= 0 {
*steps = append(*steps, pb.Step{Name: name, Link: link})
} else {
unused[name] = link
}
}
}
return unused
}