-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase.go
70 lines (61 loc) · 1.54 KB
/
database.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
package ortfodb
import (
"errors"
jsoniter "github.com/json-iterator/go"
)
func LoadDatabase(at string, skipValidation bool) (database Database, err error) {
json := jsoniter.ConfigFastest
content, err := readFileBytes(at)
if err != nil {
return
}
if !skipValidation {
validated, validationErrors, err := validateWithJSONSchema(string(content), DatabaseJSONSchema())
if err != nil {
return database, err
}
if !validated {
DisplayValidationErrors(validationErrors, "database JSON")
err = errors.New("database JSON is invalid")
return database, err
}
}
err = json.Unmarshal(content, &database)
return
}
func (db Database) FindMedia(mediaEmbed Media, workID string) (found bool, media Media) {
w, ok := db.FindWork(workID)
if !ok {
return false, Media{}
}
for _, wsl := range w.Content {
for _, b := range wsl.Blocks {
if b.Type == "media" && b.RelativeSource == mediaEmbed.RelativeSource {
return true, b.Media
}
}
}
return false, Media{}
}
func (db Database) FindWork(idOrAlias string) (work Work, found bool) {
work, found = db[idOrAlias]
if !found {
for _, w := range db {
for _, alias := range w.Metadata.Aliases {
if alias == idOrAlias {
return w, true
}
}
}
}
return
}
// FirstParagraph returns the first paragraph content block of the given work in the given language
func (work Work) FirstParagraph(lang string) (found bool, paragraph ContentBlock) {
for _, block := range work.Content[lang].Blocks {
if block.Type == "paragraph" {
return true, block
}
}
return
}