From 094709e105d48547bf5297adc0ad0c777678b0a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Sun, 23 Dec 2018 16:43:04 +0100 Subject: [PATCH] tpl/transform: Simplify transform.Unmarshal func See #5428 --- go.sum | 1 + tpl/transform/unmarshal.go | 46 ++++++++++----------------------- tpl/transform/unmarshal_test.go | 16 ++++++------ 3 files changed, 23 insertions(+), 40 deletions(-) diff --git a/go.sum b/go.sum index f7cfa6da96d..ea33a1abbff 100644 --- a/go.sum +++ b/go.sum @@ -72,6 +72,7 @@ github.com/magefile/mage v1.4.0 h1:RI7B1CgnPAuu2O9lWszwya61RLmfL0KCdo+QyyI/Bhk= github.com/magefile/mage v1.4.0/go.mod h1:IUDi13rsHje59lecXokTfGX0QIzO45uVPlXnJYsXepA= github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= +github.com/markbates/inflect v0.0.0-20171215194931-a12c3aec81a6 h1:LZhVjIISSbj8qLf2qDPP0D8z0uvOWAW5C85ly5mJW6c= github.com/markbates/inflect v0.0.0-20171215194931-a12c3aec81a6/go.mod h1:oTeZL2KHA7CUX6X+fovmK9OvIOFuqu0TwdQrZjLTh88= github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2/go.mod h1:0KeJpeMD6o+O4hW7qJOT7vyQPKrWmj26uf5wMc/IiIs= github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs= diff --git a/tpl/transform/unmarshal.go b/tpl/transform/unmarshal.go index d83cafd3ab2..e0574eb0408 100644 --- a/tpl/transform/unmarshal.go +++ b/tpl/transform/unmarshal.go @@ -17,7 +17,6 @@ import ( "io/ioutil" "strings" - "github.com/gohugoio/hugo/common/hugio" "github.com/mitchellh/mapstructure" "github.com/gohugoio/hugo/helpers" @@ -30,7 +29,7 @@ import ( // Unmarshal unmarshals the data given, which can be either a string // or a Resource. Supported formats are JSON, TOML, YAML, and CSV. -// You can optional provide an Options object as the first argument. +// You can optionally provide an options map as the first argument. func (ns *Namespace) Unmarshal(args ...interface{}) (interface{}, error) { if len(args) < 1 || len(args) > 2 { return nil, errors.New("unmarshal takes 1 or 2 arguments") @@ -54,32 +53,13 @@ func (ns *Namespace) Unmarshal(args ...interface{}) (interface{}, error) { if err != nil { return nil, errors.WithMessage(err, "failed to decode options") } - } - // All the relevant Resource types implements ReadSeekCloserResource, - // which should be the most effective way to get the content. - if r, ok := data.(resource.ReadSeekCloserResource); ok { - var key string - var reader hugio.ReadSeekCloser - - if k, ok := r.(resource.Identifier); ok { - key = k.Key() - } + if r, ok := data.(unmarshableResource); ok { + key := r.Key() if key == "" { - reader, err := r.ReadSeekCloser() - if err != nil { - return nil, err - } - defer reader.Close() - - key, err = helpers.MD5FromReader(reader) - if err != nil { - return nil, err - } - - reader.Seek(0, 0) + return nil, errors.New("no Key set in Resource") } return ns.cache.GetOrCreate(key, func() (interface{}, error) { @@ -88,14 +68,11 @@ func (ns *Namespace) Unmarshal(args ...interface{}) (interface{}, error) { return nil, errors.Errorf("MIME %q not supported", r.MediaType()) } - if reader == nil { - var err error - reader, err = r.ReadSeekCloser() - if err != nil { - return nil, err - } - defer reader.Close() + reader, err := r.ReadSeekCloser() + if err != nil { + return nil, err } + defer reader.Close() b, err := ioutil.ReadAll(reader) if err != nil { @@ -104,7 +81,6 @@ func (ns *Namespace) Unmarshal(args ...interface{}) (interface{}, error) { return decoder.Unmarshal(b, f) }) - } dataStr, err := cast.ToStringE(data) @@ -124,6 +100,12 @@ func (ns *Namespace) Unmarshal(args ...interface{}) (interface{}, error) { }) } +// All the relevant resources implements this interface. +type unmarshableResource interface { + resource.ReadSeekCloserResource + resource.Identifier +} + func decodeDecoder(m map[string]interface{}) (metadecoders.Decoder, error) { opts := metadecoders.Default diff --git a/tpl/transform/unmarshal_test.go b/tpl/transform/unmarshal_test.go index 00424c6936a..b1ce30b1f26 100644 --- a/tpl/transform/unmarshal_test.go +++ b/tpl/transform/unmarshal_test.go @@ -102,23 +102,23 @@ func TestUnmarshal(t *testing.T) { {`slogan = "Hugo Rocks!"`, nil, func(m map[string]interface{}) { assertSlogan(m) }}, - {testContentResource{content: `slogan: "Hugo Rocks!"`, mime: media.YAMLType}, nil, func(m map[string]interface{}) { + {testContentResource{key: "r1", content: `slogan: "Hugo Rocks!"`, mime: media.YAMLType}, nil, func(m map[string]interface{}) { assertSlogan(m) }}, - {testContentResource{content: `{ "slogan": "Hugo Rocks!" }`, mime: media.JSONType}, nil, func(m map[string]interface{}) { + {testContentResource{key: "r1", content: `{ "slogan": "Hugo Rocks!" }`, mime: media.JSONType}, nil, func(m map[string]interface{}) { assertSlogan(m) }}, - {testContentResource{content: `slogan = "Hugo Rocks!"`, mime: media.TOMLType}, nil, func(m map[string]interface{}) { + {testContentResource{key: "r1", content: `slogan = "Hugo Rocks!"`, mime: media.TOMLType}, nil, func(m map[string]interface{}) { assertSlogan(m) }}, - {testContentResource{content: `1997,Ford,E350,"ac, abs, moon",3000.00 + {testContentResource{key: "r1", content: `1997,Ford,E350,"ac, abs, moon",3000.00 1999,Chevy,"Venture ""Extended Edition""","",4900.00`, mime: media.CSVType}, nil, func(r [][]string) { assert.Equal(2, len(r)) first := r[0] assert.Equal(5, len(first)) assert.Equal("Ford", first[1]) }}, - {testContentResource{content: `a;b;c`, mime: media.CSVType}, map[string]interface{}{"comma": ";"}, func(r [][]string) { + {testContentResource{key: "r1", content: `a;b;c`, mime: media.CSVType}, map[string]interface{}{"comma": ";"}, func(r [][]string) { assert.Equal(r, [][]string{[]string{"a", "b", "c"}}) }}, @@ -130,7 +130,7 @@ func TestUnmarshal(t *testing.T) { assert.Equal(r, [][]string{[]string{"a", "b", "c"}}) }}, - {testContentResource{content: ` + {testContentResource{key: "r1", content: ` % This is a comment a;b;c`, mime: media.CSVType}, map[string]interface{}{"CommA": ";", "Comment": "%"}, func(r [][]string) { assert.Equal(r, [][]string{[]string{"a", "b", "c"}}) @@ -138,8 +138,8 @@ a;b;c`, mime: media.CSVType}, map[string]interface{}{"CommA": ";", "Comment": "% }}, // errors {"thisisnotavaliddataformat", nil, false}, - {testContentResource{content: `invalid&toml"`, mime: media.TOMLType}, nil, false}, - {testContentResource{content: `unsupported: MIME"`, mime: media.CalendarType}, nil, false}, + {testContentResource{key: "r1", content: `invalid&toml"`, mime: media.TOMLType}, nil, false}, + {testContentResource{key: "r1", content: `unsupported: MIME"`, mime: media.CalendarType}, nil, false}, {"thisisnotavaliddataformat", nil, false}, {`{ notjson }`, nil, false}, {tstNoStringer{}, nil, false},