-
Notifications
You must be signed in to change notification settings - Fork 16
/
schema.go
127 lines (108 loc) · 3.68 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
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
package generator
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"gopkg.in/yaml.v3"
)
type Schemas struct {
Versions []Version `yaml:"versions"`
}
type Version struct {
Name string `yaml:"Name"` // name of the version
Tag string `yaml:"Tag"` // git tag of the plotly version
URL string `yaml:"URL"` // url under which the plotly schema json file can be downloaded directly
Path string `yaml:"Path"` // path under which the schema file will be saved locally for future use
Generated string `yaml:"Generated"` // path for the generated package
Cdn string `yaml:"CDN"` // url for the cdn which should be included in the head of the generated html
}
// cleanJson makes sure that whitespaces and new line characters are removed from the downloaded json
func cleanJson(stream io.ReadCloser) (bytes.Buffer, error) {
var err error
var buf bytes.Buffer
// Read and decode JSON content
var jsonData interface{}
err = json.NewDecoder(stream).Decode(&jsonData)
if err != nil {
return buf, fmt.Errorf("could not decode json content of response")
}
// Re-encode JSON content with minimized whitespace and no new lines
encoder := json.NewEncoder(&buf)
encoder.SetIndent("", "")
err = encoder.Encode(jsonData)
if err != nil {
return buf, fmt.Errorf("could not remove whitespaces and new lines from json by encoding content")
}
// Remove newline character from the end of the encoded JSON
buf.Truncate(buf.Len() - 1)
return buf, nil
}
// DownloadSchema gets the schema file for a given version, and either saves the raw content,
// or prepares the file for the generator if prep is set to true
func DownloadSchema(version string, schemaURL string, outputpath string) (string, error) {
// Make GET request to download schema
response, err := http.Get(schemaURL)
if err != nil {
return "", err
}
if response.StatusCode != http.StatusOK {
return "", fmt.Errorf("could not schema file for version: %s\n[Status Code: %d (%s)]\nused url: %s\nlist the available tags first and recheck", version, response.StatusCode, response.Status, schemaURL)
}
defer response.Body.Close()
// clean the response
buf, err := cleanJson(response.Body)
if err != nil {
return "", fmt.Errorf("%s: from: %s", err, schemaURL)
}
// Create file to save the schema
err = os.MkdirAll(filepath.Dir(outputpath), 0755)
if err != nil {
return "", fmt.Errorf("could not create directory for plotly schema: %s", filepath.Dir(outputpath))
}
_, err = os.Stat(outputpath)
if err == nil {
fmt.Printf("WARN: overwriting: %s\n", outputpath)
}
file, err := os.Create(outputpath)
if err != nil {
return "", fmt.Errorf("could not create file for saving: %s", outputpath)
}
defer file.Close()
// Copy response body to file
fmt.Printf("Downloading %s -> %s\n", schemaURL, outputpath)
_, err = file.Write([]byte(`{"sha1":"11b662302a42aa0698df091a9974ac8f6e1a2292","modified":true,"schema":`))
if err != nil {
return "", fmt.Errorf("could not write json schema prepared for code generator: %s", outputpath)
}
// Write JSON content to file
_, err = io.Copy(file, &buf)
if err != nil {
return "", err
}
_, err = file.Write([]byte(`}`))
if err != nil {
return "", err
}
fmt.Printf("Schema downloaded and saved as: %s\n", outputpath)
return outputpath, nil
}
func ReadSchemas(schemapath string) []Version {
var err error
f, err := os.Open(schemapath)
if err != nil {
fmt.Printf("error opening schema yaml file: %s\n", err)
return nil
}
var schemas Schemas
decoder := yaml.NewDecoder(f)
err = decoder.Decode(&schemas)
if err != nil {
fmt.Printf("error decoding schema yaml file: %s\n", err)
return nil
}
return schemas.Versions
}