From c15c7da42a1c7bc535cc16cca2b341526f8cf169 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Sun, 2 Sep 2018 23:57:42 +0200 Subject: [PATCH] resource: Remove some duplicate code --- resource/image.go | 3 ++- resource/readers.go | 53 ------------------------------------------- resource/resource.go | 11 +++++---- resource/transform.go | 7 +++--- 4 files changed, 12 insertions(+), 62 deletions(-) delete mode 100644 resource/readers.go diff --git a/resource/image.go b/resource/image.go index fd8aea376..8b1ba8209 100644 --- a/resource/image.go +++ b/resource/image.go @@ -24,6 +24,7 @@ import ( "github.com/mitchellh/mapstructure" + "github.com/gohugoio/hugo/common/hugio" "github.com/gohugoio/hugo/helpers" // Importing image codecs for image.DecodeConfig @@ -410,7 +411,7 @@ func (i *Image) initConfig() error { } var ( - f ReadSeekCloser + f hugio.ReadSeekCloser config image.Config ) diff --git a/resource/readers.go b/resource/readers.go deleted file mode 100644 index 56142c141..000000000 --- a/resource/readers.go +++ /dev/null @@ -1,53 +0,0 @@ -// 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 01e66078b..dbbbca3ad 100644 --- a/resource/resource.go +++ b/resource/resource.go @@ -28,6 +28,7 @@ import ( "github.com/gohugoio/hugo/output" "github.com/gohugoio/hugo/tpl" + "github.com/gohugoio/hugo/common/hugio" "github.com/gohugoio/hugo/common/loggers" jww "github.com/spf13/jwalterweatherman" @@ -137,12 +138,12 @@ type ContentResource interface { // OpenReadSeekeCloser allows setting some other way (than reading from a filesystem) // to open or create a ReadSeekCloser. -type OpenReadSeekCloser func() (ReadSeekCloser, error) +type OpenReadSeekCloser func() (hugio.ReadSeekCloser, error) // ReadSeekCloserResource is a Resource that supports loading its content. type ReadSeekCloserResource interface { Resource - ReadSeekCloser() (ReadSeekCloser, error) + ReadSeekCloser() (hugio.ReadSeekCloser, error) } // Resources represents a slice of resources, which can be a mix of different types. @@ -596,7 +597,7 @@ func (l *genericResource) Content() (interface{}, error) { return l.content, nil } -func (l *genericResource) ReadSeekCloser() (ReadSeekCloser, error) { +func (l *genericResource) ReadSeekCloser() (hugio.ReadSeekCloser, error) { if l.openReadSeekerCloser != nil { return l.openReadSeekerCloser() } @@ -623,7 +624,7 @@ func (l *genericResource) initHash() error { var err error l.hashInit.Do(func() { var hash string - var f ReadSeekCloser + var f hugio.ReadSeekCloser f, err = l.ReadSeekCloser() if err != nil { err = fmt.Errorf("failed to open source file: %s", err) @@ -645,7 +646,7 @@ func (l *genericResource) initHash() error { func (l *genericResource) initContent() error { var err error l.contentInit.Do(func() { - var r ReadSeekCloser + var r hugio.ReadSeekCloser r, err = l.ReadSeekCloser() if err != nil { return diff --git a/resource/transform.go b/resource/transform.go index c81748756..5c01c7129 100644 --- a/resource/transform.go +++ b/resource/transform.go @@ -20,6 +20,7 @@ import ( "strings" "github.com/gohugoio/hugo/common/errors" + "github.com/gohugoio/hugo/common/hugio" "github.com/gohugoio/hugo/helpers" "github.com/mitchellh/hashstructure" "github.com/spf13/afero" @@ -188,11 +189,11 @@ type transformedResource struct { Resource } -func (r *transformedResource) ReadSeekCloser() (ReadSeekCloser, error) { +func (r *transformedResource) ReadSeekCloser() (hugio.ReadSeekCloser, error) { if err := r.initContent(); err != nil { return nil, err } - return NewReadSeekerNoOpCloserFromString(r.content), nil + return hugio.NewReadSeekerNoOpCloserFromString(r.content), nil } func (r *transformedResource) transferTransformedValues(another *transformedResource) { @@ -478,7 +479,7 @@ func (r *transformedResource) initTransform(setContent bool) error { } // contentReadSeekerCloser returns a ReadSeekerCloser if possible for a given Resource. -func contentReadSeekerCloser(r Resource) (ReadSeekCloser, error) { +func contentReadSeekerCloser(r Resource) (hugio.ReadSeekCloser, error) { switch rr := r.(type) { case ReadSeekCloserResource: rc, err := rr.ReadSeekCloser()