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