this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add internal function to unwrap LineReaders

When applying, we need to copy all data after the last fragment line.
This function provides a way to get back the possibly-buffered io.Reader
that backs the LineReader.

+26 -6
+26 -6
gitdiff/io.go
··· 2 2 3 3 import ( 4 4 "bufio" 5 + "fmt" 5 6 "io" 6 7 ) 7 8 ··· 16 17 ReadString(delim byte) (string, error) 17 18 } 18 19 20 + type readStringReader interface { 21 + io.Reader 22 + StringReader 23 + } 24 + 19 25 // LineReader is the interface that wraps the ReadLine method. 20 26 // 21 27 // ReadLine reads the next full line in the input, returing the the data ··· 31 37 ReadLine() (string, int, error) 32 38 } 33 39 34 - // NewLineReader returns a LineReader for a reader starting at a specific line 35 - // using the newline character, \n, as a line separator. If r is a 36 - // StringReader, it is used directly. Otherwise, it is wrapped in a way that 37 - // may read extra data from the underlying input. 40 + // NewLineReader returns a LineReader starting at a specific line and using the 41 + // newline character, \n, as a line separator. If r is a StringReader, it is 42 + // used directly. Otherwise, it is wrapped in a way that may read extra data 43 + // from the underlying input. 38 44 func NewLineReader(r io.Reader, lineno int) LineReader { 39 - sr, ok := r.(StringReader) 45 + sr, ok := r.(readStringReader) 40 46 if !ok { 41 47 sr = bufio.NewReader(r) 42 48 } ··· 44 50 } 45 51 46 52 type lineReader struct { 47 - r StringReader 53 + r readStringReader 48 54 n int 49 55 } 50 56 ··· 56 62 } 57 63 return 58 64 } 65 + 66 + // unwrapLineReader returns a plain io.Reader that was converted to a 67 + // LineReader by wrapping or casting. It should only be called from functions 68 + // that accept an io.Reader as an argument and then convert it. 69 + func unwrapLineReader(lr LineReader) io.Reader { 70 + switch r := lr.(type) { 71 + case io.Reader: 72 + return r 73 + case *lineReader: 74 + return r.r 75 + default: 76 + panic(fmt.Sprintf("%T does not implement io.Reader and is not a gitdiff wrapper", lr)) 77 + } 78 + }