-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathroutine.go
98 lines (76 loc) · 2.36 KB
/
routine.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
package main
import (
"encoding/json"
"fmt"
"github.com/xeipuuv/gojsonschema"
"io/ioutil"
"strings"
"time"
)
func processFile(inputPath string, ch chan<- ShortResult) {
start := time.Now()
buffer, err := ioutil.ReadFile(inputPath)
if err != nil {
ch <- ShortResult{fmt.Sprintf("can't read input file: %v\n", err), 1}
return
}
err = preflightAsset(&buffer, inputPath)
if err != nil {
ch <- ShortResult{fmt.Sprintf("input failed preflight check: %v\n", err), 1}
return
}
result := processBytes(buffer)
//output filename
bare := strings.Replace(strings.ToLower(inputPath), ".json", "", -1)
ext := ".png"
outputPath := strings.Join([]string{bare, ext}, "")
if result.Code > 0 {
fmt.Printf("%s\n", result.Message)
ch <- ShortResult{fmt.Sprintf("%s: %s\n", outputPath, result.Message), 1}
return
}
//save to file
result.Context.SavePNG(outputPath)
secs := time.Since(start).Seconds()
ch <- ShortResult{fmt.Sprintf("%s: %.2fs", outputPath, secs), 0}
return
}
func processBytes(bytes []byte) Result {
start := time.Now()
i18n, msg := getI18n()
if msg != "" {
return Result{fmt.Sprintf("Can't read i18n table: %s\n", msg), 1, nil}
}
//staticTimelineSchemaJsonBytes func taken from bindata.go; if function name doesn't match, go get -u helps
schemaBytes, err := staticTimelineSchemaJsonBytes()
if err != nil {
return Result{fmt.Sprintf("Can't read schema file: %v\n", err), 1, nil}
}
schemaLoader := gojsonschema.NewStringLoader(string(schemaBytes))
documentLoader := gojsonschema.NewStringLoader(string(bytes))
result, err := gojsonschema.Validate(schemaLoader, documentLoader)
if err != nil {
return Result{fmt.Sprintf("Can't validate JSON: %s\n", err.Error()), 1, nil}
}
if !result.Valid() {
fmt.Printf("Invalid JSON:\n")
for _, desc := range result.Errors() {
fmt.Printf("- %s\n", desc)
}
return Result{fmt.Sprintf("Invalid JSON: %s\n", result.Errors()[0]), 1, nil}
}
var data Data
if err := json.Unmarshal(bytes, &data); err != nil {
return Result{fmt.Sprintf("JSON unmarshaling failed: %s\n", err), 1, nil}
}
enrichData(&data)
errNo, errString := validateData(&data)
if errNo > 0 {
return Result{fmt.Sprintf(errString), 1, nil}
}
ctx := drawScene(&data, i18n)
w := ctx.Width()
h := ctx.Height()
secs := time.Since(start).Seconds()
return Result{fmt.Sprintf("Image dimensions %d✕%d: %.2fs", w, h, secs), 0, ctx}
}