fork of https://github.com/sourcegraph/zoekt
1// Copyright 2016 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package zoekt // import "github.com/sourcegraph/zoekt"
16
17import (
18 "math/rand"
19 "reflect"
20
21 "google.golang.org/protobuf/types/known/durationpb"
22 "google.golang.org/protobuf/types/known/timestamppb"
23
24 webserverv1 "github.com/sourcegraph/zoekt/grpc/protos/zoekt/webserver/v1"
25)
26
27func FileMatchFromProto(p *webserverv1.FileMatch) FileMatch {
28 lineMatches := make([]LineMatch, len(p.GetLineMatches()))
29 for i, lineMatch := range p.GetLineMatches() {
30 lineMatches[i] = LineMatchFromProto(lineMatch)
31 }
32
33 chunkMatches := make([]ChunkMatch, len(p.GetChunkMatches()))
34 for i, chunkMatch := range p.GetChunkMatches() {
35 chunkMatches[i] = ChunkMatchFromProto(chunkMatch)
36 }
37
38 return FileMatch{
39 Score: p.GetScore(),
40 Debug: p.GetDebug(),
41 FileName: string(p.GetFileName()), // Note: 🚨Warning, this filename may be a non-UTF8 string.
42 Repository: p.GetRepository(),
43 Branches: p.GetBranches(),
44 LineMatches: lineMatches,
45 ChunkMatches: chunkMatches,
46 RepositoryID: p.GetRepositoryId(),
47 RepositoryPriority: p.GetRepositoryPriority(),
48 Content: p.GetContent(),
49 Checksum: p.GetChecksum(),
50 Language: p.GetLanguage(),
51 SubRepositoryName: p.GetSubRepositoryName(),
52 SubRepositoryPath: p.GetSubRepositoryPath(),
53 Version: p.GetVersion(),
54 }
55}
56
57func (m *FileMatch) ToProto() *webserverv1.FileMatch {
58 lineMatches := make([]*webserverv1.LineMatch, len(m.LineMatches))
59 for i, lm := range m.LineMatches {
60 lineMatches[i] = lm.ToProto()
61 }
62
63 chunkMatches := make([]*webserverv1.ChunkMatch, len(m.ChunkMatches))
64 for i, cm := range m.ChunkMatches {
65 chunkMatches[i] = cm.ToProto()
66 }
67
68 return &webserverv1.FileMatch{
69 Score: m.Score,
70 Debug: m.Debug,
71 FileName: []byte(m.FileName),
72 Repository: m.Repository,
73 Branches: m.Branches,
74 LineMatches: lineMatches,
75 ChunkMatches: chunkMatches,
76 RepositoryId: m.RepositoryID,
77 RepositoryPriority: m.RepositoryPriority,
78 Content: m.Content,
79 Checksum: m.Checksum,
80 Language: m.Language,
81 SubRepositoryName: m.SubRepositoryName,
82 SubRepositoryPath: m.SubRepositoryPath,
83 Version: m.Version,
84 }
85}
86
87func ChunkMatchFromProto(p *webserverv1.ChunkMatch) ChunkMatch {
88 ranges := make([]Range, len(p.GetRanges()))
89 for i, r := range p.GetRanges() {
90 ranges[i] = RangeFromProto(r)
91 }
92
93 symbols := make([]*Symbol, len(p.GetSymbolInfo()))
94 for i, r := range p.GetSymbolInfo() {
95 symbols[i] = SymbolFromProto(r)
96 }
97
98 return ChunkMatch{
99 Content: p.GetContent(),
100 ContentStart: LocationFromProto(p.GetContentStart()),
101 FileName: p.GetFileName(),
102 Ranges: ranges,
103 SymbolInfo: symbols,
104 Score: p.GetScore(),
105 BestLineMatch: p.GetBestLineMatch(),
106 DebugScore: p.GetDebugScore(),
107 }
108}
109
110func (cm *ChunkMatch) ToProto() *webserverv1.ChunkMatch {
111 ranges := make([]*webserverv1.Range, len(cm.Ranges))
112 for i, r := range cm.Ranges {
113 ranges[i] = r.ToProto()
114 }
115
116 symbolInfo := make([]*webserverv1.SymbolInfo, len(cm.SymbolInfo))
117 for i, si := range cm.SymbolInfo {
118 symbolInfo[i] = si.ToProto()
119 }
120
121 return &webserverv1.ChunkMatch{
122 Content: cm.Content,
123 ContentStart: cm.ContentStart.ToProto(),
124 FileName: cm.FileName,
125 Ranges: ranges,
126 SymbolInfo: symbolInfo,
127 Score: cm.Score,
128 BestLineMatch: cm.BestLineMatch,
129 DebugScore: cm.DebugScore,
130 }
131}
132
133func RangeFromProto(p *webserverv1.Range) Range {
134 return Range{
135 Start: LocationFromProto(p.GetStart()),
136 End: LocationFromProto(p.GetEnd()),
137 }
138}
139
140func (r *Range) ToProto() *webserverv1.Range {
141 return &webserverv1.Range{
142 Start: r.Start.ToProto(),
143 End: r.End.ToProto(),
144 }
145}
146
147func LocationFromProto(p *webserverv1.Location) Location {
148 return Location{
149 ByteOffset: p.GetByteOffset(),
150 LineNumber: p.GetLineNumber(),
151 Column: p.GetColumn(),
152 }
153}
154
155func (l *Location) ToProto() *webserverv1.Location {
156 return &webserverv1.Location{
157 ByteOffset: l.ByteOffset,
158 LineNumber: l.LineNumber,
159 Column: l.Column,
160 }
161}
162
163func LineMatchFromProto(p *webserverv1.LineMatch) LineMatch {
164 lineFragments := make([]LineFragmentMatch, len(p.GetLineFragments()))
165 for i, lineFragment := range p.GetLineFragments() {
166 lineFragments[i] = LineFragmentMatchFromProto(lineFragment)
167 }
168
169 return LineMatch{
170 Line: p.GetLine(),
171 LineStart: int(p.GetLineStart()),
172 LineEnd: int(p.GetLineEnd()),
173 LineNumber: int(p.GetLineNumber()),
174 Before: p.GetBefore(),
175 After: p.GetAfter(),
176 FileName: p.GetFileName(),
177 Score: p.GetScore(),
178 DebugScore: p.GetDebugScore(),
179 LineFragments: lineFragments,
180 }
181}
182
183func (lm *LineMatch) ToProto() *webserverv1.LineMatch {
184 fragments := make([]*webserverv1.LineFragmentMatch, len(lm.LineFragments))
185 for i, fragment := range lm.LineFragments {
186 fragments[i] = fragment.ToProto()
187 }
188
189 return &webserverv1.LineMatch{
190 Line: lm.Line,
191 LineStart: int64(lm.LineStart),
192 LineEnd: int64(lm.LineEnd),
193 LineNumber: int64(lm.LineNumber),
194 Before: lm.Before,
195 After: lm.After,
196 FileName: lm.FileName,
197 Score: lm.Score,
198 DebugScore: lm.DebugScore,
199 LineFragments: fragments,
200 }
201}
202
203func SymbolFromProto(p *webserverv1.SymbolInfo) *Symbol {
204 if p == nil {
205 return nil
206 }
207
208 return &Symbol{
209 Sym: p.GetSym(),
210 Kind: p.GetKind(),
211 Parent: p.GetParent(),
212 ParentKind: p.GetParentKind(),
213 }
214}
215
216func (s *Symbol) ToProto() *webserverv1.SymbolInfo {
217 if s == nil {
218 return nil
219 }
220
221 return &webserverv1.SymbolInfo{
222 Sym: s.Sym,
223 Kind: s.Kind,
224 Parent: s.Parent,
225 ParentKind: s.ParentKind,
226 }
227}
228
229func LineFragmentMatchFromProto(p *webserverv1.LineFragmentMatch) LineFragmentMatch {
230 return LineFragmentMatch{
231 LineOffset: int(p.GetLineOffset()),
232 Offset: p.GetOffset(),
233 MatchLength: int(p.GetMatchLength()),
234 SymbolInfo: SymbolFromProto(p.GetSymbolInfo()),
235 }
236}
237
238func (lfm *LineFragmentMatch) ToProto() *webserverv1.LineFragmentMatch {
239 return &webserverv1.LineFragmentMatch{
240 LineOffset: int64(lfm.LineOffset),
241 Offset: lfm.Offset,
242 MatchLength: int64(lfm.MatchLength),
243 SymbolInfo: lfm.SymbolInfo.ToProto(),
244 }
245}
246
247func FlushReasonFromProto(p webserverv1.FlushReason) FlushReason {
248 switch p {
249 case webserverv1.FlushReason_FLUSH_REASON_TIMER_EXPIRED:
250 return FlushReasonTimerExpired
251 case webserverv1.FlushReason_FLUSH_REASON_FINAL_FLUSH:
252 return FlushReasonFinalFlush
253 case webserverv1.FlushReason_FLUSH_REASON_MAX_SIZE:
254 return FlushReasonMaxSize
255 default:
256 return FlushReason(0)
257 }
258}
259
260func (fr FlushReason) ToProto() webserverv1.FlushReason {
261 switch fr {
262 case FlushReasonTimerExpired:
263 return webserverv1.FlushReason_FLUSH_REASON_TIMER_EXPIRED
264 case FlushReasonFinalFlush:
265 return webserverv1.FlushReason_FLUSH_REASON_FINAL_FLUSH
266 case FlushReasonMaxSize:
267 return webserverv1.FlushReason_FLUSH_REASON_MAX_SIZE
268 default:
269 return webserverv1.FlushReason_FLUSH_REASON_UNKNOWN_UNSPECIFIED
270 }
271}
272
273// Generate valid reasons for quickchecks
274func (fr FlushReason) Generate(rand *rand.Rand, size int) reflect.Value {
275 switch rand.Int() % 4 {
276 case 1:
277 return reflect.ValueOf(FlushReasonMaxSize)
278 case 2:
279 return reflect.ValueOf(FlushReasonFinalFlush)
280 case 3:
281 return reflect.ValueOf(FlushReasonTimerExpired)
282 default:
283 return reflect.ValueOf(FlushReason(0))
284 }
285}
286
287func StatsFromProto(p *webserverv1.Stats) Stats {
288 return Stats{
289 ContentBytesLoaded: p.GetContentBytesLoaded(),
290 IndexBytesLoaded: p.GetIndexBytesLoaded(),
291 Crashes: int(p.GetCrashes()),
292 Duration: p.GetDuration().AsDuration(),
293 FileCount: int(p.GetFileCount()),
294 ShardFilesConsidered: int(p.GetShardFilesConsidered()),
295 FilesConsidered: int(p.GetFilesConsidered()),
296 FilesLoaded: int(p.GetFilesLoaded()),
297 FilesSkipped: int(p.GetFilesSkipped()),
298 ShardsScanned: int(p.GetShardsScanned()),
299 ShardsSkipped: int(p.GetShardsSkipped()),
300 ShardsSkippedFilter: int(p.GetShardsSkippedFilter()),
301 MatchCount: int(p.GetMatchCount()),
302 NgramMatches: int(p.GetNgramMatches()),
303 NgramLookups: int(p.GetNgramLookups()),
304 Wait: p.GetWait().AsDuration(),
305 MatchTreeConstruction: p.GetMatchTreeConstruction().AsDuration(),
306 MatchTreeSearch: p.GetMatchTreeSearch().AsDuration(),
307 RegexpsConsidered: int(p.GetRegexpsConsidered()),
308 FlushReason: FlushReasonFromProto(p.GetFlushReason()),
309 }
310}
311
312func (s *Stats) ToProto() *webserverv1.Stats {
313 return &webserverv1.Stats{
314 ContentBytesLoaded: s.ContentBytesLoaded,
315 IndexBytesLoaded: s.IndexBytesLoaded,
316 Crashes: int64(s.Crashes),
317 Duration: durationpb.New(s.Duration),
318 FileCount: int64(s.FileCount),
319 ShardFilesConsidered: int64(s.ShardFilesConsidered),
320 FilesConsidered: int64(s.FilesConsidered),
321 FilesLoaded: int64(s.FilesLoaded),
322 FilesSkipped: int64(s.FilesSkipped),
323 ShardsScanned: int64(s.ShardsScanned),
324 ShardsSkipped: int64(s.ShardsSkipped),
325 ShardsSkippedFilter: int64(s.ShardsSkippedFilter),
326 MatchCount: int64(s.MatchCount),
327 NgramMatches: int64(s.NgramMatches),
328 NgramLookups: int64(s.NgramLookups),
329 Wait: durationpb.New(s.Wait),
330 MatchTreeConstruction: durationpb.New(s.MatchTreeConstruction),
331 MatchTreeSearch: durationpb.New(s.MatchTreeSearch),
332 RegexpsConsidered: int64(s.RegexpsConsidered),
333 FlushReason: s.FlushReason.ToProto(),
334 }
335}
336
337func ProgressFromProto(p *webserverv1.Progress) Progress {
338 return Progress{
339 Priority: p.GetPriority(),
340 MaxPendingPriority: p.GetMaxPendingPriority(),
341 }
342}
343
344func (p *Progress) ToProto() *webserverv1.Progress {
345 return &webserverv1.Progress{
346 Priority: p.Priority,
347 MaxPendingPriority: p.MaxPendingPriority,
348 }
349}
350
351func SearchResultFromStreamProto(p *webserverv1.StreamSearchResponse, repoURLs, lineFragments map[string]string) *SearchResult {
352 if p == nil {
353 return nil
354 }
355
356 return SearchResultFromProto(p.GetResponseChunk(), repoURLs, lineFragments)
357}
358
359func SearchResultFromProto(p *webserverv1.SearchResponse, repoURLs, lineFragments map[string]string) *SearchResult {
360 if p == nil {
361 return nil
362 }
363
364 files := make([]FileMatch, len(p.GetFiles()))
365 for i, file := range p.GetFiles() {
366 files[i] = FileMatchFromProto(file)
367 }
368
369 return &SearchResult{
370 Stats: StatsFromProto(p.GetStats()),
371 Progress: ProgressFromProto(p.GetProgress()),
372
373 Files: files,
374
375 RepoURLs: repoURLs,
376 LineFragments: lineFragments,
377 }
378}
379
380func (sr *SearchResult) ToProto() *webserverv1.SearchResponse {
381 if sr == nil {
382 return nil
383 }
384
385 files := make([]*webserverv1.FileMatch, len(sr.Files))
386 for i, file := range sr.Files {
387 files[i] = file.ToProto()
388 }
389
390 return &webserverv1.SearchResponse{
391 Stats: sr.Stats.ToProto(),
392 Progress: sr.Progress.ToProto(),
393
394 Files: files,
395 }
396}
397
398func (sr *SearchResult) ToStreamProto() *webserverv1.StreamSearchResponse {
399 if sr == nil {
400 return nil
401 }
402
403 return &webserverv1.StreamSearchResponse{ResponseChunk: sr.ToProto()}
404}
405
406func RepositoryBranchFromProto(p *webserverv1.RepositoryBranch) RepositoryBranch {
407 return RepositoryBranch{
408 Name: p.GetName(),
409 Version: p.GetVersion(),
410 }
411}
412
413func (r *RepositoryBranch) ToProto() *webserverv1.RepositoryBranch {
414 return &webserverv1.RepositoryBranch{
415 Name: r.Name,
416 Version: r.Version,
417 }
418}
419
420func RepositoryFromProto(p *webserverv1.Repository) Repository {
421 branches := make([]RepositoryBranch, len(p.GetBranches()))
422 for i, branch := range p.GetBranches() {
423 branches[i] = RepositoryBranchFromProto(branch)
424 }
425
426 subRepoMap := make(map[string]*Repository, len(p.GetSubRepoMap()))
427 for name, repo := range p.GetSubRepoMap() {
428 r := RepositoryFromProto(repo)
429 subRepoMap[name] = &r
430 }
431
432 fileTombstones := make(map[string]struct{}, len(p.GetFileTombstones()))
433 for _, file := range p.GetFileTombstones() {
434 fileTombstones[file] = struct{}{}
435 }
436
437 return Repository{
438 TenantID: int(p.GetTenantId()),
439 ID: p.GetId(),
440 Name: p.GetName(),
441 URL: p.GetUrl(),
442 Source: p.GetSource(),
443 Branches: branches,
444 SubRepoMap: subRepoMap,
445 CommitURLTemplate: p.GetCommitUrlTemplate(),
446 FileURLTemplate: p.GetFileUrlTemplate(),
447 LineFragmentTemplate: p.GetLineFragmentTemplate(),
448 priority: p.GetPriority(),
449 RawConfig: p.GetRawConfig(),
450 Rank: uint16(p.GetRank()),
451 IndexOptions: p.GetIndexOptions(),
452 HasSymbols: p.GetHasSymbols(),
453 Tombstone: p.GetTombstone(),
454 LatestCommitDate: p.GetLatestCommitDate().AsTime(),
455 FileTombstones: fileTombstones,
456 Metadata: p.GetMetadata(),
457 }
458}
459
460func (r *Repository) ToProto() *webserverv1.Repository {
461 if r == nil {
462 return nil
463 }
464
465 branches := make([]*webserverv1.RepositoryBranch, len(r.Branches))
466 for i, branch := range r.Branches {
467 branches[i] = branch.ToProto()
468 }
469
470 subRepoMap := make(map[string]*webserverv1.Repository, len(r.SubRepoMap))
471 for name, repo := range r.SubRepoMap {
472 subRepoMap[name] = repo.ToProto()
473 }
474
475 fileTombstones := make([]string, 0, len(r.FileTombstones))
476 for file := range r.FileTombstones {
477 fileTombstones = append(fileTombstones, file)
478 }
479
480 return &webserverv1.Repository{
481 TenantId: int64(r.TenantID),
482 Id: r.ID,
483 Name: r.Name,
484 Url: r.URL,
485 Source: r.Source,
486 Branches: branches,
487 SubRepoMap: subRepoMap,
488 CommitUrlTemplate: r.CommitURLTemplate,
489 FileUrlTemplate: r.FileURLTemplate,
490 LineFragmentTemplate: r.LineFragmentTemplate,
491 Priority: r.priority,
492 RawConfig: r.RawConfig,
493 Rank: uint32(r.Rank),
494 IndexOptions: r.IndexOptions,
495 HasSymbols: r.HasSymbols,
496 Tombstone: r.Tombstone,
497 LatestCommitDate: timestamppb.New(r.LatestCommitDate),
498 FileTombstones: fileTombstones,
499 Metadata: r.Metadata,
500 }
501}
502
503func IndexMetadataFromProto(p *webserverv1.IndexMetadata) IndexMetadata {
504 languageMap := make(map[string]uint16, len(p.GetLanguageMap()))
505 for language, id := range p.GetLanguageMap() {
506 languageMap[language] = uint16(id)
507 }
508
509 return IndexMetadata{
510 IndexFormatVersion: int(p.GetIndexFormatVersion()),
511 IndexFeatureVersion: int(p.GetIndexFeatureVersion()),
512 IndexMinReaderVersion: int(p.GetIndexMinReaderVersion()),
513 IndexTime: p.GetIndexTime().AsTime(),
514 PlainASCII: p.GetPlainAscii(),
515 LanguageMap: languageMap,
516 ZoektVersion: p.GetZoektVersion(),
517 ID: p.GetId(),
518 }
519}
520
521func (m *IndexMetadata) ToProto() *webserverv1.IndexMetadata {
522 if m == nil {
523 return nil
524 }
525
526 languageMap := make(map[string]uint32, len(m.LanguageMap))
527 for language, id := range m.LanguageMap {
528 languageMap[language] = uint32(id)
529 }
530
531 return &webserverv1.IndexMetadata{
532 IndexFormatVersion: int64(m.IndexFormatVersion),
533 IndexFeatureVersion: int64(m.IndexFeatureVersion),
534 IndexMinReaderVersion: int64(m.IndexMinReaderVersion),
535 IndexTime: timestamppb.New(m.IndexTime),
536 PlainAscii: m.PlainASCII,
537 LanguageMap: languageMap,
538 ZoektVersion: m.ZoektVersion,
539 Id: m.ID,
540 }
541}
542
543func RepoStatsFromProto(p *webserverv1.RepoStats) RepoStats {
544 return RepoStats{
545 Repos: int(p.GetRepos()),
546 Shards: int(p.GetShards()),
547 Documents: int(p.GetDocuments()),
548 IndexBytes: p.GetIndexBytes(),
549 ContentBytes: p.GetContentBytes(),
550 NewLinesCount: p.GetNewLinesCount(),
551 DefaultBranchNewLinesCount: p.GetDefaultBranchNewLinesCount(),
552 OtherBranchesNewLinesCount: p.GetOtherBranchesNewLinesCount(),
553 }
554}
555
556func (s *RepoStats) ToProto() *webserverv1.RepoStats {
557 return &webserverv1.RepoStats{
558 Repos: int64(s.Repos),
559 Shards: int64(s.Shards),
560 Documents: int64(s.Documents),
561 IndexBytes: s.IndexBytes,
562 ContentBytes: s.ContentBytes,
563 NewLinesCount: s.NewLinesCount,
564 DefaultBranchNewLinesCount: s.DefaultBranchNewLinesCount,
565 OtherBranchesNewLinesCount: s.OtherBranchesNewLinesCount,
566 }
567}
568
569func RepoListEntryFromProto(p *webserverv1.RepoListEntry) *RepoListEntry {
570 if p == nil {
571 return nil
572 }
573
574 return &RepoListEntry{
575 Repository: RepositoryFromProto(p.GetRepository()),
576 IndexMetadata: IndexMetadataFromProto(p.GetIndexMetadata()),
577 Stats: RepoStatsFromProto(p.GetStats()),
578 }
579}
580
581func (r *RepoListEntry) ToProto() *webserverv1.RepoListEntry {
582 if r == nil {
583 return nil
584 }
585
586 return &webserverv1.RepoListEntry{
587 Repository: r.Repository.ToProto(),
588 IndexMetadata: r.IndexMetadata.ToProto(),
589 Stats: r.Stats.ToProto(),
590 }
591}
592
593func MinimalRepoListEntryFromProto(p *webserverv1.MinimalRepoListEntry) MinimalRepoListEntry {
594 branches := make([]RepositoryBranch, len(p.GetBranches()))
595 for i, branch := range p.GetBranches() {
596 branches[i] = RepositoryBranchFromProto(branch)
597 }
598
599 return MinimalRepoListEntry{
600 HasSymbols: p.GetHasSymbols(),
601 Branches: branches,
602 IndexTimeUnix: p.GetIndexTimeUnix(),
603 }
604}
605
606func (m *MinimalRepoListEntry) ToProto() *webserverv1.MinimalRepoListEntry {
607 branches := make([]*webserverv1.RepositoryBranch, len(m.Branches))
608 for i, branch := range m.Branches {
609 branches[i] = branch.ToProto()
610 }
611 return &webserverv1.MinimalRepoListEntry{
612 HasSymbols: m.HasSymbols,
613 Branches: branches,
614 IndexTimeUnix: m.IndexTimeUnix,
615 }
616}
617
618func RepoListFromProto(p *webserverv1.ListResponse) *RepoList {
619 repos := make([]*RepoListEntry, len(p.GetRepos()))
620 for i, repo := range p.GetRepos() {
621 repos[i] = RepoListEntryFromProto(repo)
622 }
623
624 reposMap := make(map[uint32]MinimalRepoListEntry, len(p.GetReposMap()))
625 for id, mle := range p.GetReposMap() {
626 reposMap[id] = MinimalRepoListEntryFromProto(mle)
627 }
628
629 return &RepoList{
630 Repos: repos,
631 ReposMap: reposMap,
632 Crashes: int(p.GetCrashes()),
633 Stats: RepoStatsFromProto(p.GetStats()),
634 }
635}
636
637func (r *RepoList) ToProto() *webserverv1.ListResponse {
638 repos := make([]*webserverv1.RepoListEntry, len(r.Repos))
639 for i, repo := range r.Repos {
640 repos[i] = repo.ToProto()
641 }
642
643 reposMap := make(map[uint32]*webserverv1.MinimalRepoListEntry, len(r.ReposMap))
644 for id, repo := range r.ReposMap {
645 reposMap[id] = repo.ToProto()
646 }
647
648 return &webserverv1.ListResponse{
649 Repos: repos,
650 ReposMap: reposMap,
651 Crashes: int64(r.Crashes),
652 Stats: r.Stats.ToProto(),
653 }
654}
655
656func (l *ListOptions) ToProto() *webserverv1.ListOptions {
657 if l == nil {
658 return nil
659 }
660 var field webserverv1.ListOptions_RepoListField
661 switch l.Field {
662 case RepoListFieldRepos:
663 field = webserverv1.ListOptions_REPO_LIST_FIELD_REPOS
664 case RepoListFieldReposMap:
665 field = webserverv1.ListOptions_REPO_LIST_FIELD_REPOS_MAP
666 }
667
668 return &webserverv1.ListOptions{
669 Field: field,
670 }
671}
672
673func ListOptionsFromProto(p *webserverv1.ListOptions) *ListOptions {
674 if p == nil {
675 return nil
676 }
677 var field RepoListField
678 switch p.GetField() {
679 case webserverv1.ListOptions_REPO_LIST_FIELD_REPOS:
680 field = RepoListFieldRepos
681 case webserverv1.ListOptions_REPO_LIST_FIELD_REPOS_MAP:
682 field = RepoListFieldReposMap
683 }
684 return &ListOptions{
685 Field: field,
686 }
687}
688
689func SearchOptionsFromProto(p *webserverv1.SearchOptions) *SearchOptions {
690 if p == nil {
691 return nil
692 }
693
694 return &SearchOptions{
695 EstimateDocCount: p.GetEstimateDocCount(),
696 Whole: p.GetWhole(),
697 ShardMaxMatchCount: int(p.GetShardMaxMatchCount()),
698 TotalMaxMatchCount: int(p.GetTotalMaxMatchCount()),
699 ShardRepoMaxMatchCount: int(p.GetShardRepoMaxMatchCount()),
700 MaxWallTime: p.GetMaxWallTime().AsDuration(),
701 FlushWallTime: p.GetFlushWallTime().AsDuration(),
702 MaxDocDisplayCount: int(p.GetMaxDocDisplayCount()),
703 MaxMatchDisplayCount: int(p.GetMaxMatchDisplayCount()),
704 NumContextLines: int(p.GetNumContextLines()),
705 ChunkMatches: p.GetChunkMatches(),
706 Trace: p.GetTrace(),
707 DebugScore: p.GetDebugScore(),
708 UseBM25Scoring: p.GetUseBm25Scoring(),
709 }
710}
711
712func (s *SearchOptions) ToProto() *webserverv1.SearchOptions {
713 if s == nil {
714 return nil
715 }
716
717 return &webserverv1.SearchOptions{
718 EstimateDocCount: s.EstimateDocCount,
719 Whole: s.Whole,
720 ShardMaxMatchCount: int64(s.ShardMaxMatchCount),
721 TotalMaxMatchCount: int64(s.TotalMaxMatchCount),
722 ShardRepoMaxMatchCount: int64(s.ShardRepoMaxMatchCount),
723 MaxWallTime: durationpb.New(s.MaxWallTime),
724 FlushWallTime: durationpb.New(s.FlushWallTime),
725 MaxDocDisplayCount: int64(s.MaxDocDisplayCount),
726 MaxMatchDisplayCount: int64(s.MaxMatchDisplayCount),
727 NumContextLines: int64(s.NumContextLines),
728 ChunkMatches: s.ChunkMatches,
729 Trace: s.Trace,
730 DebugScore: s.DebugScore,
731 UseBm25Scoring: s.UseBM25Scoring,
732 }
733}