go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/logdog/common/renderer/source.go (about) 1 // Copyright 2015 The LUCI Authors. 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 15 package renderer 16 17 import ( 18 "io" 19 20 "go.chromium.org/luci/logdog/api/logpb" 21 ) 22 23 // Source returns successive LogEntry records for processing. 24 type Source interface { 25 // NextLogEntry returns the next successive LogEntry record to render, or an 26 // error if it could not be retrieved. 27 // 28 // If there are no more log entries in the stream, NextLogEntry should return 29 // io.EOF as an error. 30 // 31 // It is valid to return a LogEntry and an error (including io.EOF) at the 32 // same time. The LogEntry will be processed before the error. 33 NextLogEntry() (*logpb.LogEntry, error) 34 } 35 36 // StaticSource is a Source that returns successive log entries from a slice 37 // of log entries. The slice is mutated during processing. 38 type StaticSource []*logpb.LogEntry 39 40 var _ Source = (*StaticSource)(nil) 41 42 // NextLogEntry implements Source. 43 func (s *StaticSource) NextLogEntry() (le *logpb.LogEntry, err error) { 44 // Pop the next entry, mutating s. 45 if len(*s) > 0 { 46 le, *s = (*s)[0], (*s)[1:] 47 } 48 if len(*s) == 0 { 49 err = io.EOF 50 } 51 return 52 }