hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

openapi3.go (2318B)

    1 // Copyright 2020 The Hugo Authors. All rights reserved.
    2 //
    3 // Licensed under the Apache License, Version 2.0 (the "License");
    4 // you may not use this file except in compliance with the License.
    5 // You may obtain a copy of the License at
    6 // http://www.apache.org/licenses/LICENSE-2.0
    7 //
    8 // Unless required by applicable law or agreed to in writing, software
    9 // distributed under the License is distributed on an "AS IS" BASIS,
   10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   11 // See the License for the specific language governing permissions and
   12 // limitations under the License.
   13 
   14 package openapi3
   15 
   16 import (
   17 	"fmt"
   18 	"io/ioutil"
   19 
   20 	gyaml "github.com/ghodss/yaml"
   21 
   22 	"errors"
   23 
   24 	kopenapi3 "github.com/getkin/kin-openapi/openapi3"
   25 	"github.com/gohugoio/hugo/cache/namedmemcache"
   26 	"github.com/gohugoio/hugo/deps"
   27 	"github.com/gohugoio/hugo/parser/metadecoders"
   28 	"github.com/gohugoio/hugo/resources/resource"
   29 )
   30 
   31 // New returns a new instance of the openapi3-namespaced template functions.
   32 func New(deps *deps.Deps) *Namespace {
   33 	// TODO(bep) consolidate when merging that "other branch" -- but be aware of the keys.
   34 	cache := namedmemcache.New()
   35 	deps.BuildStartListeners.Add(
   36 		func() {
   37 			cache.Clear()
   38 		})
   39 
   40 	return &Namespace{
   41 		cache: cache,
   42 		deps:  deps,
   43 	}
   44 }
   45 
   46 // Namespace provides template functions for the "openapi3".
   47 type Namespace struct {
   48 	cache *namedmemcache.Cache
   49 	deps  *deps.Deps
   50 }
   51 
   52 func (ns *Namespace) Unmarshal(r resource.UnmarshableResource) (*kopenapi3.T, error) {
   53 	key := r.Key()
   54 	if key == "" {
   55 		return nil, errors.New("no Key set in Resource")
   56 	}
   57 
   58 	v, err := ns.cache.GetOrCreate(key, func() (any, error) {
   59 		f := metadecoders.FormatFromMediaType(r.MediaType())
   60 		if f == "" {
   61 			return nil, fmt.Errorf("MIME %q not supported", r.MediaType())
   62 		}
   63 
   64 		reader, err := r.ReadSeekCloser()
   65 		if err != nil {
   66 			return nil, err
   67 		}
   68 		defer reader.Close()
   69 
   70 		b, err := ioutil.ReadAll(reader)
   71 		if err != nil {
   72 			return nil, err
   73 		}
   74 
   75 		s := &kopenapi3.T{}
   76 		switch f {
   77 		case metadecoders.YAML:
   78 			err = gyaml.Unmarshal(b, s)
   79 		default:
   80 			err = metadecoders.Default.UnmarshalTo(b, f, s)
   81 		}
   82 		if err != nil {
   83 			return nil, err
   84 		}
   85 
   86 		err = kopenapi3.NewLoader().ResolveRefsIn(s, nil)
   87 
   88 		return s, err
   89 	})
   90 	if err != nil {
   91 		return nil, err
   92 	}
   93 
   94 	return v.(*kopenapi3.T), nil
   95 }