Blame vendor/github.com/deepmap/oapi-codegen/pkg/codegen/inline.go

Packit Service 3a6627
// Copyright 2019 DeepMap, Inc.
Packit Service 3a6627
//
Packit Service 3a6627
// Licensed under the Apache License, Version 2.0 (the "License");
Packit Service 3a6627
// you may not use this file except in compliance with the License.
Packit Service 3a6627
// You may obtain a copy of the License at
Packit Service 3a6627
//
Packit Service 3a6627
// http://www.apache.org/licenses/LICENSE-2.0
Packit Service 3a6627
//
Packit Service 3a6627
// Unless required by applicable law or agreed to in writing, software
Packit Service 3a6627
// distributed under the License is distributed on an "AS IS" BASIS,
Packit Service 3a6627
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit Service 3a6627
// See the License for the specific language governing permissions and
Packit Service 3a6627
// limitations under the License.
Packit Service 3a6627
package codegen
Packit Service 3a6627
Packit Service 3a6627
import (
Packit Service 3a6627
	"bufio"
Packit Service 3a6627
	"bytes"
Packit Service 3a6627
	"compress/gzip"
Packit Service 3a6627
	"encoding/base64"
Packit Service 3a6627
	"fmt"
Packit Service 3a6627
	"text/template"
Packit Service 3a6627
Packit Service 3a6627
	"github.com/getkin/kin-openapi/openapi3"
Packit Service 3a6627
)
Packit Service 3a6627
Packit Service 3a6627
// This generates a gzipped, base64 encoded JSON representation of the
Packit Service 3a6627
// swagger definition, which we embed inside the generated code.
Packit Service 3a6627
func GenerateInlinedSpec(t *template.Template, swagger *openapi3.Swagger) (string, error) {
Packit Service 3a6627
	// Marshal to json
Packit Service 3a6627
	encoded, err := swagger.MarshalJSON()
Packit Service 3a6627
	if err != nil {
Packit Service 3a6627
		return "", fmt.Errorf("error marshaling swagger: %s", err)
Packit Service 3a6627
	}
Packit Service 3a6627
Packit Service 3a6627
	// gzip
Packit Service 3a6627
	var buf bytes.Buffer
Packit Service 3a6627
	zw, err := gzip.NewWriterLevel(&buf, gzip.BestCompression)
Packit Service 3a6627
	if err != nil {
Packit Service 3a6627
		return "", fmt.Errorf("error creating gzip compressor: %s", err)
Packit Service 3a6627
	}
Packit Service 3a6627
	_, err = zw.Write(encoded)
Packit Service 3a6627
	if err != nil {
Packit Service 3a6627
		return "", fmt.Errorf("error gzipping swagger file: %s", err)
Packit Service 3a6627
	}
Packit Service 3a6627
	err = zw.Close()
Packit Service 3a6627
	if err != nil {
Packit Service 3a6627
		return "", fmt.Errorf("error gzipping swagger file: %s", err)
Packit Service 3a6627
	}
Packit Service 3a6627
	str := base64.StdEncoding.EncodeToString(buf.Bytes())
Packit Service 3a6627
Packit Service 3a6627
	var parts []string
Packit Service 3a6627
	const width = 80
Packit Service 3a6627
Packit Service 3a6627
	// Chop up the string into an array of strings.
Packit Service 3a6627
	for len(str) > width {
Packit Service 3a6627
		part := str[0:width]
Packit Service 3a6627
		parts = append(parts, part)
Packit Service 3a6627
		str = str[width:]
Packit Service 3a6627
	}
Packit Service 3a6627
	if len(str) > 0 {
Packit Service 3a6627
		parts = append(parts, str)
Packit Service 3a6627
	}
Packit Service 3a6627
Packit Service 3a6627
	// Generate inline code.
Packit Service 3a6627
	buf.Reset()
Packit Service 3a6627
	w := bufio.NewWriter(&buf)
Packit Service 3a6627
	err = t.ExecuteTemplate(w, "inline.tmpl", parts)
Packit Service 3a6627
	if err != nil {
Packit Service 3a6627
		return "", fmt.Errorf("error generating inlined spec: %s", err)
Packit Service 3a6627
	}
Packit Service 3a6627
	err = w.Flush()
Packit Service 3a6627
	if err != nil {
Packit Service 3a6627
		return "", fmt.Errorf("error flushing output buffer for inlined spec: %s", err)
Packit Service 3a6627
	}
Packit Service 3a6627
	return buf.String(), nil
Packit Service 3a6627
}