-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathparser.go
More file actions
405 lines (350 loc) · 12.2 KB
/
Copy pathparser.go
File metadata and controls
405 lines (350 loc) · 12.2 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// Copyright 2024 Google LLC
//
// Licensed 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 data
import (
"encoding/json"
"errors"
"io"
"strings"
"github.com/GoogleChrome/webstatus.dev/lib/gen/jsonschema/web_platform_dx__web_features_v3"
"github.com/GoogleChrome/webstatus.dev/lib/webdxfeaturetypes"
)
// V3Parser contains the logic to parse the JSON from the web-features Github Release.
type V3Parser struct{}
var ErrUnexpectedFormat = errors.New("unexpected format")
var ErrUnableToProcess = errors.New("unable to process the data")
// rawWebFeaturesJSONDataV3 is used to parse the source JSON.
// It holds the features as raw JSON messages to be processed individually.
type rawWebFeaturesJSONDataV3 struct {
Browsers web_platform_dx__web_features_v3.Browsers `json:"browsers"`
Groups map[string]web_platform_dx__web_features_v3.GroupData `json:"groups"`
Snapshots map[string]web_platform_dx__web_features_v3.SnapshotData `json:"snapshots"`
// TODO: When we move to v3, we will change Features to being json.RawMessage
Features json.RawMessage `json:"features"`
}
// featureKindPeek is a small helper struct to find the discriminator value in V3.
type featureKindPeek struct {
Kind string `json:"kind"`
}
// Parse expects the raw bytes for a map of string to
// https://github.com/web-platform-dx/web-features/blob/main/schemas/data.schema.json
// The string is the feature ID.
// It will consume the readcloser and close it.
func (p V3Parser) Parse(in io.ReadCloser) (*webdxfeaturetypes.ProcessedWebFeaturesData, error) {
defer in.Close()
var source rawWebFeaturesJSONDataV3
decoder := json.NewDecoder(in)
err := decoder.Decode(&source)
if err != nil {
return nil, errors.Join(ErrUnexpectedFormat, err)
}
err = validateRawWebFeaturesJSONDataV3(&source)
if err != nil {
return nil, errors.Join(ErrUnexpectedFormat, err)
}
processedData, err := postProcessV3(&source)
if err != nil {
return nil, errors.Join(ErrUnableToProcess, err)
}
return processedData, nil
}
func validateRawWebFeaturesJSONDataV3(data *rawWebFeaturesJSONDataV3) error {
return validateFeaturesV3(data.Features)
}
func validateFeaturesV3(data json.RawMessage) error {
if len(data) == 0 {
return errors.New("missing web features")
}
featureRawMessageMap := make(map[string]json.RawMessage)
if err := json.Unmarshal(data, &featureRawMessageMap); err != nil {
return err
}
if len(featureRawMessageMap) == 0 {
return errors.New("missing web features")
}
for id, rawFeature := range featureRawMessageMap {
if strings.TrimSpace(id) == "" {
return errors.New("empty web feature ID")
}
var peek featureKindPeek
if err := json.Unmarshal(rawFeature, &peek); err != nil {
return err
}
switch peek.Kind {
case string(web_platform_dx__web_features_v3.Feature):
if err := validateFeatureNameV3(rawFeature); err != nil {
return err
}
case string(web_platform_dx__web_features_v3.Moved), string(web_platform_dx__web_features_v3.Split):
default:
return errors.New("unknown web feature kind")
}
}
return nil
}
func validateFeatureNameV3(rawFeature json.RawMessage) error {
var value struct {
Name *string `json:"name"`
}
if err := json.Unmarshal(rawFeature, &value); err != nil {
return err
}
if value.Name == nil || strings.TrimSpace(*value.Name) == "" {
return errors.New("empty web feature name")
}
return nil
}
func postProcessV3(data *rawWebFeaturesJSONDataV3) (*webdxfeaturetypes.ProcessedWebFeaturesData, error) {
featureKinds, err := postProcessFeatureValueV3(data.Features)
if err != nil {
return nil, err
}
return &webdxfeaturetypes.ProcessedWebFeaturesData{
Browsers: postProcessBrowsersV3(data.Browsers),
Groups: postProcessGroupsV3(data.Groups),
Snapshots: postProcessSnapshotsV3(data.Snapshots),
Features: featureKinds,
}, nil
}
func postProcessBrowsersV3(value web_platform_dx__web_features_v3.Browsers) webdxfeaturetypes.Browsers {
return webdxfeaturetypes.Browsers{
Chrome: postProcessBrowserDataV3(value.Chrome),
ChromeAndroid: postProcessBrowserDataV3(value.ChromeAndroid),
Edge: postProcessBrowserDataV3(value.Edge),
Firefox: postProcessBrowserDataV3(value.Firefox),
FirefoxAndroid: postProcessBrowserDataV3(value.FirefoxAndroid),
Safari: postProcessBrowserDataV3(value.Safari),
SafariIos: postProcessBrowserDataV3(value.SafariIos),
}
}
func postProcessBrowserDataV3(value web_platform_dx__web_features_v3.BrowserData) webdxfeaturetypes.BrowserData {
var releases []webdxfeaturetypes.Release
if value.Releases != nil {
releases = make([]webdxfeaturetypes.Release, len(value.Releases))
for i, r := range value.Releases {
releases[i] = webdxfeaturetypes.Release{
Version: r.Version,
Date: r.Date,
}
}
}
return webdxfeaturetypes.BrowserData{Name: value.Name, Releases: releases}
}
func postProcessGroupsV3(
value map[string]web_platform_dx__web_features_v3.GroupData) map[string]webdxfeaturetypes.GroupData {
if value == nil {
return nil
}
groups := make(map[string]webdxfeaturetypes.GroupData, len(value))
for id, g := range value {
groups[id] = webdxfeaturetypes.GroupData{
Name: g.Name,
Parent: g.Parent,
}
}
return groups
}
func postProcessSnapshotsV3(
value map[string]web_platform_dx__web_features_v3.SnapshotData) map[string]webdxfeaturetypes.SnapshotData {
if value == nil {
return nil
}
snapshots := make(map[string]webdxfeaturetypes.SnapshotData, len(value))
for id, s := range value {
snapshots[id] = webdxfeaturetypes.SnapshotData{
Name: s.Name,
Spec: s.Spec,
}
}
return snapshots
}
func postProcessFeatureValueV3(data json.RawMessage) (*webdxfeaturetypes.FeatureKinds, error) {
featureKinds := webdxfeaturetypes.FeatureKinds{
Data: nil,
Moved: nil,
Split: nil,
}
featureRawMessageMap := make(map[string]json.RawMessage)
err := json.Unmarshal(data, &featureRawMessageMap)
if err != nil {
return nil, err
}
for id, rawFeature := range featureRawMessageMap {
// Peek inside the raw JSON to find the "kind"
var peek featureKindPeek
if err := json.Unmarshal(rawFeature, &peek); err != nil {
return nil, err
}
// Switch on the explicit "kind" to unmarshal into the correct type
switch peek.Kind {
case string(web_platform_dx__web_features_v3.Feature):
if featureKinds.Data == nil {
featureKinds.Data = make(map[string]webdxfeaturetypes.FeatureValue)
}
feature, err := processFeatureKind(rawFeature)
if err != nil {
return nil, err
}
featureKinds.Data[id] = *feature
case string(web_platform_dx__web_features_v3.Moved):
if featureKinds.Moved == nil {
featureKinds.Moved = make(map[string]webdxfeaturetypes.FeatureMovedData)
}
moved, err := processMovedKind(rawFeature)
if err != nil {
return nil, err
}
featureKinds.Moved[id] = *moved
case string(web_platform_dx__web_features_v3.Split):
if featureKinds.Split == nil {
featureKinds.Split = make(map[string]webdxfeaturetypes.FeatureSplitData)
}
split, err := processSplitKind(rawFeature)
if err != nil {
return nil, err
}
featureKinds.Split[id] = *split
default:
return nil, ErrUnexpectedFormat
}
}
return &featureKinds, nil
}
// processFeatureKind processes a feature of kind "feature".
func processFeatureKind(rawFeature json.RawMessage) (*webdxfeaturetypes.FeatureValue, error) {
var value web_platform_dx__web_features_v3.FeatureValue
if err := json.Unmarshal(rawFeature, &value); err != nil {
return nil, err
}
// Return an error because these values should be present. Quicktype just messes it up.
if value.Description == nil || value.DescriptionHTML == nil || value.Name == nil || value.Status == nil {
return nil, ErrUnexpectedFormat
}
if strings.TrimSpace(*value.Name) == "" {
return nil, ErrUnexpectedFormat
}
feature := &webdxfeaturetypes.FeatureValue{
Caniuse: value.Caniuse,
CompatFeatures: value.CompatFeatures,
Description: *value.Description,
DescriptionHTML: *value.DescriptionHTML,
Group: value.Group,
Name: *value.Name,
Snapshot: value.Snapshot,
Spec: value.Spec,
Status: postProcessStatusV3(*value.Status),
Discouraged: postProcessDiscouragedV3(value.Discouraged),
}
return feature, nil
}
// processMovedKind processes a feature of kind "moved".
func processMovedKind(rawFeature json.RawMessage) (*webdxfeaturetypes.FeatureMovedData, error) {
var value web_platform_dx__web_features_v3.FeatureValue
if err := json.Unmarshal(rawFeature, &value); err != nil {
return nil, err
}
// Return an error because these values should be present. Quicktype just messes it up.
if value.RedirectTarget == nil {
return nil, ErrUnexpectedFormat
}
moved := &webdxfeaturetypes.FeatureMovedData{
Kind: webdxfeaturetypes.FeatureMovedDataKind(value.Kind),
RedirectTarget: *value.RedirectTarget,
}
return moved, nil
}
// processSplitKind processes a feature of kind "split".
func processSplitKind(rawFeature json.RawMessage) (*webdxfeaturetypes.FeatureSplitData, error) {
var value web_platform_dx__web_features_v3.FeatureValue
if err := json.Unmarshal(rawFeature, &value); err != nil {
return nil, err
}
// Return an error because these values should be present. Quicktype just messes it up.
if value.RedirectTargets == nil {
return nil, ErrUnexpectedFormat
}
split := &webdxfeaturetypes.FeatureSplitData{
Kind: webdxfeaturetypes.FeatureSplitDataKind(value.Kind),
RedirectTargets: value.RedirectTargets,
}
return split, nil
}
func postProcessDiscouragedV3(
value *web_platform_dx__web_features_v3.Discouraged) *webdxfeaturetypes.Discouraged {
if value == nil {
return nil
}
return &webdxfeaturetypes.Discouraged{
AccordingTo: value.AccordingTo,
Alternatives: value.Alternatives,
}
}
func postProcessStatusV3(value web_platform_dx__web_features_v3.StatusHeadline) webdxfeaturetypes.Status {
return webdxfeaturetypes.Status{
Baseline: postProcessBaselineV3(value.Baseline),
BaselineHighDate: postProcessBaselineDates(value.BaselineHighDate),
BaselineLowDate: postProcessBaselineDates(value.BaselineLowDate),
Support: postProcessBaselineSupportV3(value.Support),
ByCompatKey: nil,
}
}
func postProcessBaselineDates(value *string) *string {
if value == nil {
return nil
}
*value = removeRangeSymbol(*value)
return value
}
func postProcessBaselineV3(
value *web_platform_dx__web_features_v3.BaselineUnion) *webdxfeaturetypes.BaselineUnion {
if value == nil {
return nil
}
var enum *webdxfeaturetypes.BaselineEnum
if value.Enum != nil {
switch *value.Enum {
case web_platform_dx__web_features_v3.High:
enum = new(webdxfeaturetypes.High)
case web_platform_dx__web_features_v3.Low:
enum = new(webdxfeaturetypes.Low)
}
}
return &webdxfeaturetypes.BaselineUnion{
Bool: value.Bool,
Enum: enum,
}
}
func postProcessBaselineSupportBrowser(value *string) *string {
if value == nil {
return nil
}
*value = removeRangeSymbol(*value)
return value
}
func postProcessBaselineSupportV3(
value web_platform_dx__web_features_v3.Support) webdxfeaturetypes.StatusSupport {
return webdxfeaturetypes.StatusSupport{
Chrome: postProcessBaselineSupportBrowser(value.Chrome),
ChromeAndroid: postProcessBaselineSupportBrowser(value.ChromeAndroid),
Edge: postProcessBaselineSupportBrowser(value.Edge),
Firefox: postProcessBaselineSupportBrowser(value.Firefox),
FirefoxAndroid: postProcessBaselineSupportBrowser(value.FirefoxAndroid),
Safari: postProcessBaselineSupportBrowser(value.Safari),
SafariIos: postProcessBaselineSupportBrowser(value.SafariIos),
}
}
// Removes web-features range character "≤" from the string.
func removeRangeSymbol(value string) string {
return strings.TrimPrefix(value, "≤")
}