From 47d38628ec0f4e72ff17661f13456b2a1511fe13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Thu, 12 Jul 2018 21:06:48 +0200 Subject: [PATCH] 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 --- Gopkg.lock | 8 +----- resource/create/create.go | 16 +----------- resource/readers.go | 53 +++++++++++++++++++++++++++++++++++++++ resource/resource.go | 9 ------- resource/transform.go | 16 +----------- 5 files changed, 56 insertions(+), 46 deletions(-) create mode 100644 resource/readers.go diff --git a/Gopkg.lock b/Gopkg.lock index b24355f26..e61def16c 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -141,12 +141,6 @@ revision = "487489b64fb796de2e55f4e8a4ad1e145f80e957" version = "v1.1.6" -[[projects]] - branch = "master" - name = "github.com/dsnet/golib" - packages = ["memfile"] - revision = "1ea1667757804fdcccc5a1810e09aba618885ac2" - [[projects]] branch = "master" name = "github.com/eknkc/amber" @@ -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) {