Skip to content

Commit

Permalink
storage: avoid io.NopCloser; it was relatively recently introduced.
Browse files Browse the repository at this point in the history
(I think our policy is roughly "support the last two significant go
versions", and by that, we might technically be allowed to use this,
but, eh; there's minimal cost to being conservative today.)
  • Loading branch information
warpfork committed Nov 11, 2021
1 parent 7a48106 commit b161ca7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
8 changes: 5 additions & 3 deletions storage/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func GetStream(ctx context.Context, store ReadableStorage, key string) (io.ReadC
}
// Fallback to basic.
blob, err := store.Get(ctx, key)
return io.NopCloser(bytes.NewReader(blob)), err
return noopCloser{bytes.NewReader(blob)}, err
}

// PutStream returns an io.Writer and a WriteCommitter callback.
Expand Down Expand Up @@ -112,9 +112,11 @@ func Peek(ctx context.Context, store ReadableStorage, key string) ([]byte, io.Cl
}
// Fallback to basic.
bs, err := store.Get(ctx, key)
return bs, noopCloser{}, err
return bs, noopCloser{nil}, err
}

type noopCloser struct{}
type noopCloser struct {
io.Reader
}

func (noopCloser) Close() error { return nil }
8 changes: 5 additions & 3 deletions storage/memstore/memstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (store *Store) GetStream(ctx context.Context, key string) (io.ReadCloser, e
if !exists {
return nil, fmt.Errorf("404") // FIXME this needs a standard error type
}
return io.NopCloser(bytes.NewReader(content)), nil
return noopCloser{bytes.NewReader(content)}, nil
}

// Peek implements go-ipld-prime/storage.PeekableStorage.Peek.
Expand All @@ -93,9 +93,11 @@ func (store *Store) Peek(ctx context.Context, key string) ([]byte, io.Closer, er
if !exists {
return nil, nil, fmt.Errorf("404") // FIXME this needs a standard error type
}
return content, noopCloser{}, nil
return content, noopCloser{nil}, nil
}

type noopCloser struct{}
type noopCloser struct {
io.Reader
}

func (noopCloser) Close() error { return nil }

0 comments on commit b161ca7

Please sign in to comment.