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