Skip to content

Commit 7102c0c

Browse files
authored
refactor: use go's embedding feature for embedding json schema (#413)
1 parent be707c1 commit 7102c0c

File tree

4 files changed

+1510
-1529
lines changed

4 files changed

+1510
-1529
lines changed

file/codegen/main.go

+1-23
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
11
package main
22

33
import (
4-
"bytes"
54
"encoding/json"
65
"io/ioutil"
76
"log"
87
"reflect"
9-
"text/template"
108

119
"github.com/alecthomas/jsonschema"
1210
"github.com/kong/deck/file"
1311
"github.com/kong/go-kong/kong"
1412
)
1513

16-
const templateContent = `// Code generated by go generate; DO NOT EDIT.
17-
package file
18-
19-
const contentSchema = ` + "`{{.Schema}}`\n"
20-
21-
type templateData struct {
22-
Schema string
23-
}
24-
2514
var (
2615
// routes and services
2716
anyOfNameOrID = []*jsonschema.Type{
@@ -124,18 +113,7 @@ func main() {
124113
log.Fatalln(err)
125114
}
126115

127-
tmpl := template.New("codegen")
128-
tmpl, err = tmpl.Parse(templateContent)
129-
if err != nil {
130-
log.Fatalln(err)
131-
}
132-
133-
var buffer bytes.Buffer
134-
err = tmpl.Execute(&buffer, templateData{string(jsonSchema)})
135-
if err != nil {
136-
log.Fatalln(err)
137-
}
138-
err = ioutil.WriteFile("schema.go", buffer.Bytes(), 0o644)
116+
err = ioutil.WriteFile("kong_json_schema.json", jsonSchema, 0o644)
139117
if err != nil {
140118
log.Fatalln(err)
141119
}

0 commit comments

Comments
 (0)