resource: Remove some duplicate code

This commit is contained in:
Bjørn Erik Pedersen 2018-09-02 23:57:42 +02:00
parent bcbe57c6e9
commit c15c7da42a
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F
4 changed files with 12 additions and 62 deletions

View file

@ -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
)

View file

@ -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)}
}

View file

@ -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

View file

@ -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()