forked from elastic/go-elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.go
193 lines (167 loc) · 4.97 KB
/
model.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Licensed to Elasticsearch B.V. under one or more agreements.
// Elasticsearch B.V. licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
package genexamples
import (
"bufio"
"fmt"
"regexp"
"sort"
"strings"
)
func init() {
sort.Strings(EnabledFiles)
}
// EnabledFiles contains a list of files where documentation should be generated.
//
var EnabledFiles = []string{
"aggregations/bucket/datehistogram-aggregation.asciidoc",
"aggregations/bucket/filter-aggregation.asciidoc",
"aggregations/bucket/terms-aggregation.asciidoc",
"aggregations/metrics/valuecount-aggregation.asciidoc",
"api-conventions.asciidoc",
"cat/indices.asciidoc",
"cluster/health.asciidoc",
"docs/bulk.asciidoc",
"docs/delete-by-query.asciidoc",
"docs/delete.asciidoc",
"docs/get.asciidoc",
"docs/index_.asciidoc",
"docs/reindex.asciidoc",
"docs/update-by-query.asciidoc",
"docs/update.asciidoc",
"getting-started.asciidoc",
"indices/aliases.asciidoc",
"indices/create-index.asciidoc",
"indices/delete-index.asciidoc",
"indices/get-index.asciidoc",
"indices/get-mapping.asciidoc",
"indices/put-mapping.asciidoc",
"indices/templates.asciidoc",
"indices/update-settings.asciidoc",
"mapping.asciidoc",
"mapping/fields/id-field.asciidoc",
"mapping/params/fielddata.asciidoc",
"mapping/params/format.asciidoc",
"mapping/params/multi-fields.asciidoc",
"mapping/types/array.asciidoc",
"mapping/types/date.asciidoc",
"mapping/types/keyword.asciidoc",
"mapping/types/nested.asciidoc",
"mapping/types/numeric.asciidoc",
"query-dsl.asciidoc",
"query-dsl/bool-query.asciidoc",
"query-dsl/exists-query.asciidoc",
"query-dsl/function-score-query.asciidoc",
"query-dsl/match-all-query.asciidoc",
"query-dsl/match-phrase-query.asciidoc",
"query-dsl/match-query.asciidoc",
"query-dsl/multi-match-query.asciidoc",
"query-dsl/nested-query.asciidoc",
"query-dsl/query-string-query.asciidoc",
"query-dsl/query_filter_context.asciidoc",
"query-dsl/range-query.asciidoc",
"query-dsl/regexp-query.asciidoc",
"query-dsl/term-query.asciidoc",
"query-dsl/terms-query.asciidoc",
"query-dsl/wildcard-query.asciidoc",
"search.asciidoc",
"search/count.asciidoc",
"search/request-body.asciidoc",
"search/request/from-size.asciidoc",
"search/request/scroll.asciidoc",
"search/request/sort.asciidoc",
"search/search.asciidoc",
"search/suggesters.asciidoc",
"setup/install/check-running.asciidoc",
"setup/logging-config.asciidoc",
}
var (
reHTTPMethod = regexp.MustCompile(`^HEAD|GET|PUT|DELETE|POST`)
reAnnotation = regexp.MustCompile(`^(?P<line>.*)\s*\<\d\>\s*$`)
reComment = regexp.MustCompile(`^#\s?`)
)
// Example represents the code example in documentation.
//
// See: https://github.com/elastic/built-docs/blob/master/raw/en/elasticsearch/reference/master/alternatives_report.json
//
type Example struct {
SourceLocation struct {
File string
Line int
} `json:"source_location"`
Digest string
Source string
}
// IsEnabled returns true when the example should be processed.
//
func (e Example) IsEnabled() bool {
// TODO(karmi): Use "filepatch.Match()" to support glob patterns
index := sort.SearchStrings(EnabledFiles, e.SourceLocation.File)
if index > len(EnabledFiles)-1 || EnabledFiles[index] != e.SourceLocation.File {
return false
}
return true
}
// IsExecutable returns true when the example contains a request.
//
func (e Example) IsExecutable() bool {
return reHTTPMethod.MatchString(e.Source)
}
// IsTranslated returns true when the example can be converted to Go source code.
//
func (e Example) IsTranslated() bool {
return Translator{Example: e}.IsTranslated()
}
// ID returns example identifier.
//
func (e Example) ID() string {
return fmt.Sprintf("%s:%d", e.SourceLocation.File, e.SourceLocation.Line)
}
// Chapter returns the example chapter.
//
func (e Example) Chapter() string {
r := strings.NewReplacer("/", "_", "-", "_", ".asciidoc", "")
return r.Replace(e.SourceLocation.File)
}
// GithubURL returns a link for the example source.
//
func (e Example) GithubURL() string {
return fmt.Sprintf("https://github.com/elastic/elasticsearch/blob/master/docs/reference/%s#L%d", e.SourceLocation.File, e.SourceLocation.Line)
}
// Commands returns the list of commands from source.
//
func (e Example) Commands() ([]string, error) {
var (
buf strings.Builder
list []string
scan = bufio.NewScanner(strings.NewReader(e.Source))
)
for scan.Scan() {
line := scan.Text()
if reComment.MatchString(line) {
continue
}
line = reAnnotation.ReplaceAllString(line, "$1")
if reHTTPMethod.MatchString(line) {
if buf.Len() > 0 {
list = append(list, buf.String())
}
buf.Reset()
}
buf.WriteString(line)
buf.WriteString("\n")
}
if err := scan.Err(); err != nil {
return list, err
}
if buf.Len() > 0 {
list = append(list, buf.String())
}
return list, nil
}
// Translated returns the code translated from Console to Go.
//
func (e Example) Translated() (string, error) {
return Translator{Example: e}.Translate()
}