github.com/kovansky/hugo@v0.92.3-0.20220224232819-63076e4ff19f/tpl/openapi/openapi3/openapi3.go (about)

     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  	"io/ioutil"
    18  
    19  	gyaml "github.com/ghodss/yaml"
    20  
    21  	"github.com/pkg/errors"
    22  
    23  	kopenapi3 "github.com/getkin/kin-openapi/openapi3"
    24  	"github.com/gohugoio/hugo/cache/namedmemcache"
    25  	"github.com/gohugoio/hugo/deps"
    26  	"github.com/gohugoio/hugo/parser/metadecoders"
    27  	"github.com/gohugoio/hugo/resources/resource"
    28  )
    29  
    30  // New returns a new instance of the openapi3-namespaced template functions.
    31  func New(deps *deps.Deps) *Namespace {
    32  	// TODO(bep) consolidate when merging that "other branch" -- but be aware of the keys.
    33  	cache := namedmemcache.New()
    34  	deps.BuildStartListeners.Add(
    35  		func() {
    36  			cache.Clear()
    37  		})
    38  
    39  	return &Namespace{
    40  		cache: cache,
    41  		deps:  deps,
    42  	}
    43  }
    44  
    45  // Namespace provides template functions for the "openapi3".
    46  type Namespace struct {
    47  	cache *namedmemcache.Cache
    48  	deps  *deps.Deps
    49  }
    50  
    51  func (ns *Namespace) Unmarshal(r resource.UnmarshableResource) (*kopenapi3.T, error) {
    52  	key := r.Key()
    53  	if key == "" {
    54  		return nil, errors.New("no Key set in Resource")
    55  	}
    56  
    57  	v, err := ns.cache.GetOrCreate(key, func() (interface{}, error) {
    58  		f := metadecoders.FormatFromMediaType(r.MediaType())
    59  		if f == "" {
    60  			return nil, errors.Errorf("MIME %q not supported", r.MediaType())
    61  		}
    62  
    63  		reader, err := r.ReadSeekCloser()
    64  		if err != nil {
    65  			return nil, err
    66  		}
    67  		defer reader.Close()
    68  
    69  		b, err := ioutil.ReadAll(reader)
    70  		if err != nil {
    71  			return nil, err
    72  		}
    73  
    74  		s := &kopenapi3.T{}
    75  		switch f {
    76  		case metadecoders.YAML:
    77  			err = gyaml.Unmarshal(b, s)
    78  		default:
    79  			err = metadecoders.Default.UnmarshalTo(b, f, s)
    80  		}
    81  		if err != nil {
    82  			return nil, err
    83  		}
    84  
    85  		err = kopenapi3.NewLoader().ResolveRefsIn(s, nil)
    86  
    87  		return s, err
    88  	})
    89  	if err != nil {
    90  		return nil, err
    91  	}
    92  
    93  	return v.(*kopenapi3.T), nil
    94  }