Skip to content

Commit

Permalink
Removed deprecated Read-related functions
Browse files Browse the repository at this point in the history
Removed deprecated Read-related functions as maintaining
the ReadAll function has become difficult.
  • Loading branch information
noborus committed Dec 23, 2024
1 parent 23be1cf commit 5c00767
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 160 deletions.
2 changes: 1 addition & 1 deletion oviewer/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func docHelper(t *testing.T, str string) *Document {
if err != nil {
t.Fatal(err)
}
if err := m.ReadAll(bytes.NewBufferString(str)); err != nil {
if err := m.ControlReader(bytes.NewBufferString(str), nil); err != nil {
t.Fatal(err)
}
for !m.BufEOF() {
Expand Down
4 changes: 2 additions & 2 deletions oviewer/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func ExampleNewOviewer() {
panic(err)
}
s := "Hello, World!"
if err := doc.ReadAll(bytes.NewBufferString(s)); err != nil {
if err := doc.ControlReader(bytes.NewBufferString(s), nil); err != nil {
panic(err)
}

Expand Down Expand Up @@ -65,7 +65,7 @@ func ExampleSearch() {
panic(err)
}
s := "Hello, World!"
if err := doc.ReadAll(bytes.NewBufferString(s)); err != nil {
if err := doc.ControlReader(bytes.NewBufferString(s), nil); err != nil {
panic(err)
}

Expand Down
70 changes: 0 additions & 70 deletions oviewer/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package oviewer

import (
"bufio"
"context"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -404,72 +403,3 @@ func (m *Document) close() error {
atomic.StoreInt32(&m.store.changed, 1)
return nil
}

// ReadFile reads file.
// If the file name is empty, read from standard input.
//
// Deprecated: Use ControlFile instead.
func (m *Document) ReadFile(fileName string) error {
r, err := m.openFileReader(fileName)
if err != nil {
return err
}
return m.ReadAll(r)
}

// ContinueReadAll continues to read even if it reaches EOF.
//
// Deprecated: Use ControlFile instead.
func (m *Document) ContinueReadAll(_ context.Context, r io.Reader) error {
return m.ReadAll(r)
}

// ReadReader reads reader.
// A wrapper for ReadAll.
//
// Deprecated: Use ControlReader instead.
func (m *Document) ReadReader(r io.Reader) error {
return m.ReadAll(r)
}

// ReadAll reads all from the reader.
// And store it in the lines of the Document.
// ReadAll needs to be notified on eofCh.
//
// Deprecated: Use ControlReader instead.
func (m *Document) ReadAll(r io.Reader) error {
reader := bufio.NewReader(r)
go func() {
if m.checkClose() {
return
}

if err := m.readAll(reader); err != nil {
if errors.Is(err, io.EOF) || errors.Is(err, io.ErrClosedPipe) || errors.Is(err, os.ErrClosed) {
m.store.offset = m.store.size
atomic.StoreInt32(&m.store.eof, 1)
return
}
log.Printf("error: %v\n", err)
return
}
}()
return nil
}

// readAll reads to the end.
// The read lines are stored in the lines of the Document.
func (m *Document) readAll(reader *bufio.Reader) error {
chunk := m.store.chunkForAdd(m.seekable, m.store.size)
start := len(chunk.lines)
for {
if err := m.store.readLines(chunk, reader, start, ChunkSize, true); err != nil {
return err
}
chunk = NewChunk(m.store.size)
m.store.mu.Lock()
m.store.chunks = append(m.store.chunks, chunk)
m.store.mu.Unlock()
start = 0
}
}
87 changes: 0 additions & 87 deletions oviewer/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,98 +3,11 @@ package oviewer
import (
"bufio"
"bytes"
"io"
"path/filepath"
"strings"
"sync/atomic"
"testing"
)

func TestDocument_ReadFile(t *testing.T) {
t.Parallel()
type args struct {
args string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "testNoFile",
args: args{
"noFile",
},
wantErr: true,
},
{
name: "testSTDIN",
args: args{
"",
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
m, err := NewDocument()
if err != nil {
t.Fatal(err)
}
if err := m.ReadFile(tt.args.args); (err != nil) != tt.wantErr {
t.Errorf("Document.ReadFile() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestDocument_ReadAll(t *testing.T) {
t.Parallel()
type args struct {
r io.Reader
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "test1",
args: args{
r: bytes.NewBufferString("foo\nbar\n"),
},
wantErr: false,
},
{
name: "testOverChunkSize",
args: args{
r: bytes.NewBufferString(strings.Repeat("a\n", ChunkSize+1)),
},
wantErr: false,
},
{
name: "testLongLine",
args: args{
r: bytes.NewBufferString(strings.Repeat("a", 4097)),
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
m, err := NewDocument()
if err != nil {
t.Fatal(err)
}
if err := m.ReadAll(tt.args.r); (err != nil) != tt.wantErr {
t.Errorf("Document.ReadAll() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestDocument_reset(t *testing.T) {
t.Parallel()
type fields struct {
Expand Down

0 comments on commit 5c00767

Please sign in to comment.