-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.go
135 lines (108 loc) · 2.61 KB
/
map.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
package main
import (
"errors"
"io/ioutil"
"os"
jsonyaml "github.com/ghodss/yaml"
"github.com/xeipuuv/gojsonschema"
"gopkg.in/yaml.v2"
)
// Link refers to another Stage by their identifier
type Link struct {
LinkTo string `yaml:"link-to"`
CitedInURL string `yaml:"cited-in-url"`
stage *Stage
}
// Stage represents the individual stages on the map
type Stage struct {
DisplayName string `yaml:"display-name"`
DefinitionURL string `yaml:"definition-url"`
Requires []Link `yaml:"requires"`
id string
path string
errors []error
}
// JourneyMap contains the complete definition of stages on the map
type JourneyMap struct {
stages []*Stage
}
var stageSchemaLoader gojsonschema.JSONLoader
func init() {
stageSchemaLoader = gojsonschema.NewReferenceLoader("file://./schema/node.json")
}
func loadStageFile(filename string) ([]byte, error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
data, err = jsonyaml.YAMLToJSON(data)
if err != nil {
return nil, err
}
return data, nil
}
func validateStage(content []byte) []error {
var errs []error
documentLoader := gojsonschema.NewBytesLoader(content)
validationResult, err := gojsonschema.Validate(stageSchemaLoader, documentLoader)
if err != nil {
errs = append(errs, err)
} else {
for _, desc := range validationResult.Errors() {
errs = append(errs, errors.New(desc.String()))
}
}
return errs
}
func load(path string) *Stage {
stage := Stage{path: path, id: pathToID(path)}
content, err := loadStageFile(path)
if err != nil {
stage.errors = append(stage.errors, err)
return &stage
}
schemaErrors := validateStage(content)
stage.errors = append(stage.errors, schemaErrors...)
err = yaml.Unmarshal(content, &stage)
if err != nil {
stage.errors = append(stage.errors, err)
}
return &stage
}
func resolveStage(m *JourneyMap, id string) *Stage {
var ref *Stage
for i := range m.stages {
if m.stages[i].id == id {
ref = m.stages[i]
break
}
}
if ref == nil {
ref = &Stage{id: "error", DisplayName: "Error"}
}
return ref
}
func resolveLinks(m *JourneyMap) {
for _, s := range m.stages {
for lid := range s.Requires {
link := &s.Requires[lid]
link.stage = resolveStage(m, link.LinkTo)
}
}
}
// LoadMap builds the entire journey from the YAML data files
func LoadMap() (bool, JourneyMap) {
m := JourneyMap{}
valid := true
walk(func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
stage := load(path)
valid = valid && len(stage.errors) == 0
m.stages = append(m.stages, stage)
return nil
})
resolveLinks(&m)
return valid, m
}