-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
754 lines (652 loc) · 20 KB
/
types.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
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
package brave
import (
"fmt"
"regexp"
"strconv"
"strings"
"time"
anytime "github.com/ijt/go-anytime"
)
type ResultContainer[T any] struct {
Type string `json:"type"`
Results []T `json:"results"`
MutatedByGoggles bool `json:"mutated_by_goggles"`
}
type Query struct {
Original string `json:"original"`
ShowStrictWarning bool `json:"show_strict_warning"`
Altered string `json:"altered"`
Safesearch bool `json:"safesearch"`
IsNavigational bool `json:"is_navigational"`
IsGeolocal bool `json:"is_geolocal"`
LocalDecision string `json:"local_decision"`
LocalLocationsIdx int `json:"local_locations_idx"`
IsTrending bool `json:"is_trending"`
IsNewsBreaking bool `json:"is_news_breaking"`
AskForLocation bool `json:"ask_for_location"`
Language *Language `json:"language"`
SpellcheckOff bool `json:"spellcheck_off"`
Country string `json:"country"`
BadResults bool `json:"bad_results"`
ShouldFallback bool `json:"should_fallback"`
Lat string `json:"lat"`
Long string `json:"long"`
PostalCode string `json:"postal_code"`
City string `json:"city"`
State string `json:"state"`
HeaderCountry string `json:"header_country"`
MoreResultsAvailable bool `json:"more_results_available"`
CustomLocationLabel string `json:"custom_location_label"`
RedditCluster string `json:"reddit_cluster"`
SummaryKey string `json:"summary_key"`
}
type Language struct {
Main string `json:"main"`
}
type Mixed struct {
Type string `json:"type"`
Main []ResultReference
Top []ResultReference
Side []ResultReference
}
type ResultReference struct {
Type string `json:"type"`
Index int `json:"index"`
All bool `json:"all"`
}
type Result struct {
Title string `json:"title"`
URL string `json:"url"`
IsSourceLocal bool `json:"is_source_local"`
IsSourceBoth bool `json:"is_source_both"`
Description string `json:"description"`
PageAge *Timestamp `json:"page_age"`
PageFetched string `json:"page_fetched"`
Profile *Profile `json:"profile"`
Language string `json:"language"`
FamilyFriendly bool `json:"family_friendly"`
}
type Profile struct {
Name string `json:"name"`
LongName string `json:"long_name"`
URL string `json:"url"`
Image string `json:"img"`
}
type NewsResult struct {
Result
MetaURL MetaURL `json:"meta_url"`
Source string `json:"source"`
Breaking bool `json:"breaking"`
Thumbnail *Thumbnail `json:"thumbnail"`
Age *Timestamp `json:"age"`
}
type VideoResult struct {
Result
Type string `json:"type"`
Data *VideoData `json:"video"`
MetaURL MetaURL `json:"meta_url"`
Thumbnail *Thumbnail `json:"thumbnail"`
Age *Timestamp `json:"age"`
}
type VideoData struct {
Duration *Duration `json:"duration"`
Views VideoViews `json:"views"`
Creator string `json:"creator"`
Publisher string `json:"publisher"`
Thumbnail *Thumbnail `json:"thumbnail"`
}
type VideoViews int
func (v *VideoViews) UnmarshalJSON(in []byte) error {
str := string(in)
if strings.Contains(str, `"`) {
str = strings.ToLower(strings.Trim(str, `"`))
}
// this is so stupid.
if strings.HasSuffix(str, "k") {
str = strings.TrimSuffix(str, "k") + "000"
} else if strings.HasSuffix(str, "m") {
str = strings.TrimSuffix(str, "m") + "000000"
}
vv, err := strconv.Atoi(str)
if err != nil {
return err
}
*v = VideoViews(vv)
return nil
}
type MetaURL struct {
Scheme string `json:"scheme"`
NetLoc string `json:"netloc"`
Hostname string `json:"hostname"`
Favicon string `json:"favicon"`
Path string `json:"path"`
}
type Thumbnail struct {
Src string `json:"src"`
Height int `json:"height"`
Width int `json:"width"`
BackgroundColor string `json:"bg_color"`
Original string `json:"original"`
Logo bool `json:"logo"`
Duplicated bool `json:"duplicated"`
Theme string `json:"theme"`
}
type SearchResult struct {
Result
Type string `json:"type"`
DeepResults *DeepResult `json:"deep_results"`
Schemas any `json:"schemas"`
MetaURL MetaURL `json:"meta_url"`
Thumbnail *Thumbnail `json:"thumbnail"`
Age *Timestamp `json:"age"`
Language string `json:"language"`
ContentType string `json:"content_type"`
Subtype string `json:"subtype"`
Article *Article `json:"article"`
Book *Book `json:"book"`
Cluster []Result `json:"cluster"`
ClusterType string `json:"cluster_type"`
CreativeWork *CreativeWork `json:"creative_work"`
FAQ *FAQ `json:"faq"`
Location *LocationResult `json:"location"`
Movie *MovieData `json:"movie"`
MusicRecording *MusicRecording `json:"music_recording"`
ProductCluster []Product `json:"product_cluster"`
QA *QAPage `json:"qa"`
Rating *Rating `json:"rating"`
Recipe *Recipe `json:"recipe"`
Review *Review `json:"review"`
Software *Software `json:"software"`
Video *VideoData `json:"video"`
}
type ImageResult struct {
Type string `json:"type"`
Title string `json:"title"`
URL string `json:"url"`
Source string `json:"source"`
PageFetched *Timestamp `json:"page_fetched"`
Thumbnail *Thumbnail `json:"thumbnail"`
Properties *ImageProperties `json:"properties"`
MetaURL *MetaURL `json:"meta_url"`
}
type DeepResult struct {
News []NewsResult `json:"news"`
Buttons []ButtonResult `json:"buttons"`
Social []KnowledgeGraphProfile `json:"social"`
Videos []VideoResult `json:"videos"`
Images []Image `json:"images"`
}
type ButtonResult struct {
Type string `json:"type"`
Title string `json:"title"`
URL string `json:"url"`
}
type KnowledgeGraphProfile struct {
KnowledgeGraphEntity
URL string `json:"url"`
Description string `json:"description"`
}
type KnowledgeGraphEntity struct {
Title string `json:"title"`
Description string `json:"description"`
URL *URL `json:"url"`
Thumbnail *URL `json:"thumbnail"`
}
type URL struct {
Original string `json:"original"`
Display string `json:"display"`
Alternatives []string `json:"alternatives"`
Canonical string `json:"canonical"`
Mobile MobileURLItem `json:"mobile"`
}
type MobileURLItem struct {
Original string `json:"original"`
AMP string `json:"amp"`
Android string `json:"android"`
IOS string `json:"ios"`
}
type Image struct {
Thumbnail *Thumbnail `json:"thumbnail"`
URL string `json:"url"`
Properties *ImageProperties `json:"properties"`
Text string `json:"text"`
}
type ImageProperties struct {
URL string `json:"url"`
Resized string `json:"resized"`
Height int `json:"height"`
Width int `json:"width"`
Format string `json:"format"`
ContentSize string `json:"content_size"`
Placeholder string `json:"placeholder"`
}
type LocationResult struct {
Result
Type string `json:"type"`
ProviderURL string `json:"provider_url"`
Coordinates []float32 `json:"coordinates"`
ZoomLevel int `json:"zoom_level"`
Thumbnail *Thumbnail `json:"thumbnail"`
PostalAddress *PostalAddress `json:"postal_address"`
OpeningHours *OpeningHours `json:"opening_hours"`
Contact *Contact `json:"contact"`
PriceRange string `json:"price_range"`
Rating *Rating `json:"rating"`
Distance *Unit `json:"distance"`
Profiles []DataProvider `json:"profiles"`
Reviews *Reviews `json:"reviews"`
Pictures *PictureResults `json:"pictures"`
ServesCuisine []string `json:"serves_cuisine"`
Timezone string `json:"timezone"`
TimezoneOffset float32 `json:"timezone_offset"`
}
type PostalAddress struct {
Type string `json:"type"`
Country string `json:"country"`
PostalCode string `json:"postalCode"`
StreetAddress string `json:"streetAddress"`
AddressRegion string `json:"addressRegion"`
AddressLocality string `json:"addressLocality"`
DisplayAddress string `json:"displayAddress"`
}
type OpeningHours struct {
CurrentDay []DayOpeningHours `json:"current_day"`
Days [][]DayOpeningHours `json:"days"`
}
type DayOpeningHours struct {
AbbrName string `json:"abbr_name"`
FullName string `json:"full_name"`
Opens string `json:"opens"`
Closes string `json:"closes"`
}
type Contact struct {
Email string `json:"email"`
Telephone string `json:"telephone"`
}
type Rating struct {
RatingValue float32 `json:"ratingValue"`
BestRating float32 `json:"bestRating"`
ReviewCount int `json:"reviewCount"`
Profile *Profile `json:"profile"`
IsTripadvisor bool `json:"is_tripadvisor"`
}
type Recipe struct {
Title string `json:"title"`
Description string `json:"description"`
Thumbnail *Thumbnail `json:"thumbnail"`
URL string `json:"url"`
Domain string `json:"domain"`
Favicon string `json:"favicon"`
Time *Duration `json:"time"`
PrepTime *Duration `json:"prep_time"`
CookTime *Duration `json:"cook_time"`
Ingredients string `json:"ingredients"`
Instructions []HowTo `json:"instructions"`
Servings int `json:"servings"`
Calories int `json:"calories"`
Rating *Rating `json:"rating"`
RecipeCategory string `json:"recipeCategory"`
RecipeCuisine string `json:"recipeCuisine"`
Video *VideoData `json:"video"`
}
type HowTo struct {
Text string `json:"text"`
Name string `json:"name"`
URL string `json:"url"`
Image []string `json:"image"`
}
type DataProvider struct {
Type string `json:"type"`
Name string `json:"name"`
URL string `json:"url"`
LongName string `json:"long_name"`
Image string `json:"img"`
}
type Unit struct {
Value float32 `json:"value"`
Units string `json:"units"`
}
type Reviews struct {
Results []TripAdvisorReview `json:"results"`
ViewMoreURL string `json:"viewMoreUrl"`
ReviewsInForeignLanguage bool `json:"reviews_in_foreign_language"`
}
type TripAdvisorReview struct {
Title string `json:"title"`
Description string `json:"description"`
Date string `json:"date"`
Rating *Rating `json:"rating"`
Author *Person `json:"author"`
ReviewURL string `json:"review_url"`
Language string `json:"language"`
}
type Person struct {
Type string `json:"type"`
Name string `json:"name"`
URL string `json:"url"`
Thumbnail *Thumbnail `json:"thumbnail"`
}
type PictureResults struct {
Results []Thumbnail `json:"results"`
ViewMoreURL string `json:"viewMoreUrl"`
}
type MovieData struct {
Name string `json:"name"`
Description string `json:"description"`
URL string `json:"url"`
Thumbnail *Thumbnail `json:"thumbnail"`
Release string `json:"release"`
Directors []Person `json:"directors"`
Actors []Person `json:"actors"`
Rating *Rating `json:"rating"`
}
type FAQ struct {
Type string `json:"type"`
Results []QA `json:"results"`
}
type QA struct {
Question string `json:"question"`
Answer string `json:"answer"`
Title string `json:"title"`
URL string `json:"url"`
MetaURL MetaURL `json:"meta_url"`
}
type QAPage struct {
Question string `json:"question"`
Answer *Answer `json:"answer"`
}
type Answer struct {
Text string `json:"text"`
Author string `json:"author"`
UpvoteCount int `json:"upvoteCount"`
DownvoteCount int `json:"downvoteCount"`
}
type Book struct {
Title string `json:"title"`
Author []Person `json:"author"`
Date string `json:"date"`
Price *Price `json:"price"`
Pages Number `json:"pages"`
Publisher *Person `json:"publisher"`
Rating *Rating `json:"rating"`
}
type Price struct {
Price string `json:"price"`
PriceCurrency string `json:"price_currency"`
}
type Article struct {
Author []Person `json:"author"`
Date string `json:"date"`
Publisher *Organization `json:"publisher"`
Thumbnail *Thumbnail `json:"thumbnail"`
IsAccessibleForFree bool `json:"isAccessibleForFree"`
}
type Organization struct {
Type string `json:"type"`
Name string `json:"name"`
Thumbnail *Thumbnail `json:"thumbnail"`
}
type CreativeWork struct {
Name string `json:"name"`
Thumbnail *Thumbnail `json:"thumbnail"`
Rating *Rating `json:"rating"`
}
type MusicRecording struct {
Name string `json:"name"`
Thumbnail *Thumbnail `json:"thumbnail"`
Rating *Rating `json:"rating"`
}
type Review struct {
Type string `json:"type"`
Name string `json:"name"`
Thumbnail *Thumbnail `json:"thumbnail"`
Description string `json:"description"`
Rating *Rating `json:"rating"`
}
type Software struct {
Name string `json:"name"`
Author string `json:"author"`
Version string `json:"version"`
CodeRepository string `json:"codeRepository"`
Homepage string `json:"homepage"`
DatePublished string `json:"datePublisher"`
IsNPM bool `json:"is_npm"`
IsPyPi bool `json:"is_pypi"`
}
type Summarizer struct {
Type string `json:"type"`
Key string `json:"key"`
}
type SummaryMessage struct {
Type string `json:"type"`
Data string `json:"data"`
}
type SummaryEnrichments struct {
Raw string `json:"raw"`
Images []Image `json:"images"`
QA []SummaryAnswer `json:"qa"`
Entities []SummaryEntity `json:"entities"`
Context []SummaryContext `json:"context"`
}
type SummaryAnswer struct {
Answer string `json:"answer"`
Score float32 `json:"score"`
Highlight *TextLocation `json:"highlight"`
}
type SummaryEntity struct {
UUID string `json:"uuid"`
Name string `json:"name"`
URL string `json:"url"`
Text string `json:"text"`
Images []Image `json:"images"`
Highlight []TextLocation `json:"highlight"`
}
type SummaryContext struct {
Title string `json:"title"`
URL string `json:"url"`
MetaURL *MetaURL `json:"meta_url"`
}
type TextLocation struct {
Start Number `json:"start"`
End Number `json:"end"`
}
type SuggestResult struct {
Query string `json:"string"`
IsEntity bool `json:"is_entity"`
Title string `json:"title"`
Description string `json:"description"`
Image string `json:"img"`
}
type SpellcheckResultItem struct {
Query string `json:"query"`
}
type DiscussionResult struct {
SearchResult
Type string `json:"type"`
Data ForumData `json:"data"`
}
type ForumData struct {
ForumName string `json:"forum_name"`
NumAnswers int `json:"num_answers"`
Score string `json:"score"`
Question string `json:"question"`
TopComment string `json:"top_comment"`
}
type GraphInfoBox struct {
Result
Type string `json:"type"`
Position int `json:"position"`
Label string `json:"label"`
Category string `json:"category"`
LongDesc string `json:"long_desc"`
Thumbnail *Thumbnail `json:"thumbnail"`
Attributes []any `json:"attributes"`
Profiles []Profile `json:"profiles"`
WebsiteURL string `json:"website_url"`
AttributesShown int `json:"attributes_shown"`
Ratings []Rating `json:"ratings"`
Providers []DataProvider `json:"providers"`
Distance *Unit `json:"distance"`
Images []Thumbnail `json:"images"`
Movie *MovieData `json:"movie"`
}
type Product struct {
Type string `json:"type"`
Name string `json:"name"`
Price string `json:"price"`
Thumbnail *Thumbnail `json:"thumbnail"`
Description string `json:"description"`
Offers []Offer `json:"offers"`
Rating *Rating `json:"rating"`
}
type Offer struct {
URL string `json:"url"`
Price string `json:"price"`
PriceCurrency string `json:"priceCurrency"`
}
type ErrorResponse struct {
ID string `json:"id"`
Status int `json:"status"`
Code string `json:"code"`
Detail string `json:"detail"`
Meta ErrorMeta `json:"meta"`
RawQuery string `json:"raw_query"`
Time *Timestamp `json:"-"`
}
func (er ErrorResponse) Error() string {
return er.Detail
}
func (er ErrorResponse) Format(f fmt.State, c rune) {
switch c {
case 'v':
if f.Flag('+') {
meta := make([]string, 0, len(er.Meta.Errors))
for _, e := range er.Meta.Errors {
meta = append(meta,
fmt.Sprintf(
"(type [%s]; loc [%s]; input [%s]; msg [%s])",
e.Type, strings.Join(e.Loc, "."), e.Input, e.Message))
}
fmt.Fprintf(f, "error: %s (ID: %s; Status: %d; Code: %s RawQuery: %s); details: %s",
er.Detail, er.ID, er.Status, er.Code, strings.Join(meta, ", "), er.RawQuery)
} else {
fmt.Fprint(f, er.Detail)
}
case 's':
fmt.Fprint(f, er.Detail)
case 'q':
fmt.Fprintf(f, "%q", er.Detail)
default:
fmt.Fprint(f, er.Detail)
}
}
type ErrorMeta struct {
Component string `json:"component"`
Errors []ErrorMetaError `json:"errors"`
}
type ErrorMetaError struct {
Loc []string `json:"loc"`
Message string `json:"msg"`
Type string `json:"type"`
Context ErrorContext `json:"ctx"`
Input string `json:"input"`
}
type ErrorContext struct {
EnumValues []string `json:"enum_values"`
}
type Duration time.Duration
func (d *Duration) Duration() *time.Duration {
if d == nil {
return nil
}
tt := time.Duration(*d)
return &tt
}
func (d *Duration) UnmarshalJSON(in []byte) error {
str := string(in)
if !strings.Contains(str, `"`) {
return nil
}
str = strings.Trim(string(in), `"`)
matches := durationRegex.FindAllString(str, -1)
if l := len(matches); l < 3 {
for l < 3 {
matches = append([]string{"00"}, matches...)
l++
}
}
dur, err := time.ParseDuration(fmt.Sprintf("%sh%sm%ss", matches[0], matches[1], matches[2]))
if err != nil {
return err
}
*d = Duration(dur)
return nil
}
type Timestamp time.Time
func (t *Timestamp) Time() *time.Time {
if t == nil {
return nil
}
tt := time.Time(*t)
return &tt
}
func (t *Timestamp) UnmarshalJSON(in []byte) (err error) {
var res time.Time
str := string(in)
if strings.Contains(str, `"`) {
str = strings.Trim(string(in), `"`)
var err error
for _, fmt := range timeFormats {
res, err = time.Parse(fmt, str)
if err == nil {
err = nil
break
}
}
if err != nil {
var err2 error
res, err2 = anytime.Parse(str, time.Now())
if err2 != nil && strings.Contains(str, "second") {
matches := durationRegex.FindAllString(str, 1)
if len(matches) == 0 {
return nil
}
seconds, _ := strconv.Atoi(matches[0])
if seconds == 0 {
res = time.Now()
} else {
res = time.Now().Add(-time.Duration(seconds) * time.Second)
}
} else if err2 != nil {
return nil
}
}
} else {
i, err := strconv.Atoi(str)
if err != nil {
return err
}
res = time.Unix(int64(i), 0)
}
*t = Timestamp(res)
return nil
}
type Number int
func (n *Number) UnmarshalJSON(b []byte) error {
str := string(b)
str = strings.Trim(str, `"`)
num, err := strconv.Atoi(str)
if err != nil {
return nil
}
*n = Number(num)
return nil
}
var (
timeFormats = []string{
time.RFC3339,
"2006-01-02T15:04:05",
"January 2, 2006",
}
durationRegex = regexp.MustCompile(`(\d{1,2})`)
)