MCPcopy Index your code
hub / github.com/oapi-codegen/oapi-codegen / GenerateInlinedSpec

Function GenerateInlinedSpec

pkg/codegen/inline.go:29–78  ·  view source on GitHub ↗

GenerateInlinedSpec generates a gzipped, base64 encoded JSON representation of the swagger definition, which we embed inside the generated code.

(t *template.Template, importMapping importMap, swagger *openapi3.T)

Source from the content-addressed store, hash-verified

27// GenerateInlinedSpec generates a gzipped, base64 encoded JSON representation of the
28// swagger definition, which we embed inside the generated code.
29func GenerateInlinedSpec(t *template.Template, importMapping importMap, swagger *openapi3.T) (string, error) {
30 // ensure that any external file references are embedded into the embedded spec
31 swagger.InternalizeRefs(context.Background(), nil)
32 // Marshal to json
33 encoded, err := swagger.MarshalJSON()
34 if err != nil {
35 return "", fmt.Errorf("error marshaling swagger: %w", err)
36 }
37
38 // flate
39 var buf bytes.Buffer
40 zw, err := flate.NewWriter(&buf, flate.BestCompression)
41 if err != nil {
42 return "", fmt.Errorf("new flate writer: %w", err)
43 }
44
45 if _, err := zw.Write(encoded); err != nil {
46 return "", fmt.Errorf("write flate: %w", err)
47 }
48
49 if err := zw.Close(); err != nil {
50 return "", fmt.Errorf("close flate writer: %w", err)
51 }
52
53 str := base64.StdEncoding.EncodeToString(buf.Bytes())
54
55 var parts []string
56 const width = 80
57
58 // Chop up the string into an array of strings.
59 for len(str) > width {
60 part := str[0:width]
61 parts = append(parts, part)
62 str = str[width:]
63 }
64 if len(str) > 0 {
65 parts = append(parts, str)
66 }
67
68 return GenerateTemplates(
69 []string{"inline.tmpl"},
70 t,
71 struct {
72 SpecParts []string
73 ImportMapping importMap
74 }{
75 SpecParts: parts,
76 ImportMapping: importMapping,
77 })
78}

Callers 1

GenerateFunction · 0.85

Calls 3

GenerateTemplatesFunction · 0.85
MarshalJSONMethod · 0.45
BytesMethod · 0.45

Tested by

no test coverage detected