diff options
author | Bjørn Erik Pedersen <[email protected]> | 2018-07-12 21:06:48 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2018-07-12 21:32:52 +0200 |
commit | 47d38628ec0f4e72ff17661f13456b2a1511fe13 (patch) | |
tree | b3fe897e0a1181ea758213e06d8c7799ef55b5f3 | |
parent | 0024dcfe3e016c67046de06d1dac5e7f5235f9e1 (diff) | |
download | hugo-47d38628ec0f4e72ff17661f13456b2a1511fe13.tar.gz hugo-47d38628ec0f4e72ff17661f13456b2a1511fe13.zip |
resource: Clean up the in-memory Resource reader usage
Turns out `strings.Reader` implements both `io.Reader` and `io.Seeker`, so we don't need anything special.
Updates #4936
-rw-r--r-- | Gopkg.lock | 8 | ||||
-rw-r--r-- | resource/create/create.go | 16 | ||||
-rw-r--r-- | resource/readers.go | 53 | ||||
-rw-r--r-- | resource/resource.go | 9 | ||||
-rw-r--r-- | resource/transform.go | 16 |
5 files changed, 56 insertions, 46 deletions
diff --git a/Gopkg.lock b/Gopkg.lock index b24355f26..e61def16c 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -143,12 +143,6 @@ [[projects]] branch = "master" - name = "github.com/dsnet/golib" - packages = ["memfile"] - revision = "1ea1667757804fdcccc5a1810e09aba618885ac2" - -[[projects]] - branch = "master" name = "github.com/eknkc/amber" packages = [ ".", @@ -525,6 +519,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "993acf46c3937e5cebef4f39a2733150aad7cbf96913a017e56cd03c8241a56e" + inputs-digest = "9990a6372a365db28e5c7f5bf96aaa6ea72ed217790ff3380e7b6f3c1cd19009" solver-name = "gps-cdcl" solver-version = 1 diff --git a/resource/create/create.go b/resource/create/create.go index 1c7894232..e9d7b6de4 100644 --- a/resource/create/create.go +++ b/resource/create/create.go @@ -16,12 +16,10 @@ package create import ( - "io" "path/filepath" "github.com/spf13/afero" - "github.com/dsnet/golib/memfile" "github.com/gohugoio/hugo/resource" ) @@ -36,15 +34,6 @@ func New(rs *resource.Spec) *Client { return &Client{rs: rs} } -type memFileCloser struct { - *memfile.File - io.Closer -} - -func (m *memFileCloser) Close() error { - return nil -} - // Get creates a new Resource by opening the given filename in the given filesystem. func (c *Client) Get(fs afero.Fs, filename string) (resource.Resource, error) { filename = filepath.Clean(filename) @@ -53,7 +42,6 @@ func (c *Client) Get(fs afero.Fs, filename string) (resource.Resource, error) { resource.ResourceSourceDescriptor{ LazyPublish: true, SourceFilename: filename}) - }) } @@ -66,9 +54,7 @@ func (c *Client) FromString(targetPath, content string) (resource.Resource, erro resource.ResourceSourceDescriptor{ LazyPublish: true, OpenReadSeekCloser: func() (resource.ReadSeekCloser, error) { - return &memFileCloser{ - File: memfile.New([]byte(content)), - }, nil + return resource.NewReadSeekerNoOpCloserFromString(content), nil }, RelTargetFilename: filepath.Clean(targetPath)}) diff --git a/resource/readers.go b/resource/readers.go new file mode 100644 index 000000000..56142c141 --- /dev/null +++ b/resource/readers.go @@ -0,0 +1,53 @@ +// Copyright 2018 The Hugo Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package resource + +import ( + "io" + "strings" +) + +// ReadSeeker wraps io.Reader and io.Seeker. +type ReadSeeker interface { + io.Reader + io.Seeker +} + +// ReadSeekCloser is implemented by afero.File. We use this as the common type for +// content in Resource objects, even for strings. +type ReadSeekCloser interface { + ReadSeeker + io.Closer +} + +// ReadSeekerNoOpCloser implements ReadSeekCloser by doing nothing in Close. +type ReadSeekerNoOpCloser struct { + ReadSeeker +} + +// Close does nothing. +func (r ReadSeekerNoOpCloser) Close() error { + return nil +} + +// NewReadSeekerNoOpCloser creates a new ReadSeekerNoOpCloser with the given ReadSeeker. +func NewReadSeekerNoOpCloser(r ReadSeeker) ReadSeekerNoOpCloser { + return ReadSeekerNoOpCloser{r} +} + +// NewReadSeekerNoOpCloserFromString uses strings.NewReader to create a new ReadSeekerNoOpCloser +// from the given string. +func NewReadSeekerNoOpCloserFromString(content string) ReadSeekerNoOpCloser { + return ReadSeekerNoOpCloser{strings.NewReader(content)} +} diff --git a/resource/resource.go b/resource/resource.go index a7a9cb878..718d4d303 100644 --- a/resource/resource.go +++ b/resource/resource.go @@ -16,7 +16,6 @@ package resource import ( "errors" "fmt" - "io" "io/ioutil" "mime" "os" @@ -134,14 +133,6 @@ type ContentResource interface { Content() (interface{}, error) } -// ReadSeekCloser is implemented by afero.File. We use this as the common type for -// content in Resource objects, even for strings. -type ReadSeekCloser interface { - io.Reader - io.Seeker - io.Closer -} - // OpenReadSeekeCloser allows setting some other way (than reading from a filesystem) // to open or create a ReadSeekCloser. type OpenReadSeekCloser func() (ReadSeekCloser, error) diff --git a/resource/transform.go b/resource/transform.go index 93ace2ba0..5ba5ec821 100644 --- a/resource/transform.go +++ b/resource/transform.go @@ -188,25 +188,11 @@ type transformedResource struct { Resource } -type readSeeker struct { - io.Reader - io.Seeker - io.Closer -} - -func (r *readSeeker) Close() error { - return nil -} - -func (r *readSeeker) Seek(offset int64, whence int) (int64, error) { - panic("Seek is not supported by this io.Reader") -} - func (r *transformedResource) ReadSeekCloser() (ReadSeekCloser, error) { if err := r.initContent(); err != nil { return nil, err } - return &readSeeker{Reader: strings.NewReader(r.content)}, nil + return NewReadSeekerNoOpCloserFromString(r.content), nil } func (r *transformedResource) transferTransformedValues(another *transformedResource) { |