forked from elastic/apm-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinline_schemas.go
110 lines (97 loc) · 3.16 KB
/
inline_schemas.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
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package main
import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"regexp"
"strings"
)
const basePath = "./docs/spec/"
func main() {
schemaPaths := []struct {
path, schemaOut string
}{
{"errors/payload.json", "model/error/generated/schema/payload.go"},
{"transactions/payload.json", "model/transaction/generated/schema/payload.go"},
{"metrics/payload.json", "model/metric/generated/schema/payload.go"},
{"sourcemaps/payload.json", "model/sourcemap/generated/schema/payload.go"},
}
for _, schemaInfo := range schemaPaths {
file := filepath.Join(filepath.Dir(basePath), schemaInfo.path)
schemaBytes, err := ioutil.ReadFile(file)
if err != nil {
panic(err)
}
schema, err := replaceRef(filepath.Dir(file), string(schemaBytes))
if err != nil {
panic(err)
}
goScript := fmt.Sprintf("package schema\n\nconst PayloadSchema = `%s`\n", schema)
err = os.MkdirAll(path.Dir(schemaInfo.schemaOut), os.ModePerm)
if err != nil {
panic(err)
}
err = ioutil.WriteFile(schemaInfo.schemaOut, []byte(goScript), 0644)
if err != nil {
panic(err)
}
}
}
var re = regexp.MustCompile(`\"\$ref\": \"(.*?.json)\"`)
var findAndReplace = map[string]string{}
func replaceRef(currentDir string, schema string) (string, error) {
matches := re.FindAllStringSubmatch(schema, -1)
for _, match := range matches {
pattern := escapePattern(match[0])
if _, ok := findAndReplace[pattern]; !ok {
s, err := read(currentDir, match[1])
if err != nil {
panic(err)
}
findAndReplace[pattern] = trimSchemaPart(s)
}
re := regexp.MustCompile(pattern)
schema = re.ReplaceAllLiteralString(schema, findAndReplace[pattern])
}
return schema, nil
}
func read(currentRelativePath string, filePath string) (string, error) {
path := filepath.Join(currentRelativePath, filePath)
file, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
return replaceRef(filepath.Dir(path), string(file))
}
var reDollar = regexp.MustCompile(`\$`)
var reQuote = regexp.MustCompile(`\"`)
func escapePattern(pattern string) string {
pattern = reDollar.ReplaceAllLiteralString(pattern, `\$`)
return reQuote.ReplaceAllLiteralString(pattern, `\"`)
}
func trimSchemaPart(part string) string {
part = strings.Trim(part, "\n")
part = strings.Trim(part, "\b")
part = strings.TrimSuffix(part, "}")
part = strings.TrimPrefix(part, "{")
part = strings.Trim(part, "\n")
return strings.Trim(part, "\b")
}