1
- #! /bin/bash -eux
2
- set -o pipefail
3
-
4
- types=()
5
- types+=(" Callback" )
6
- types+=(" Example" )
7
- types+=(" Header" )
8
- types+=(" Link" )
9
- types+=(" Parameter" )
10
- types+=(" RequestBody" )
11
- types+=(" Response" )
12
- types+=(" Schema" )
13
- types+=(" SecurityScheme" )
14
-
15
- cat << EOF
16
- package openapi3
1
+ // Code generated by go generate; DO NOT EDIT.
2
+ package {{ .Package }}
17
3
18
4
import (
19
5
"context"
@@ -24,44 +10,38 @@ import (
24
10
"github.com/go-openapi/jsonpointer"
25
11
"github.com/perimeterx/marshmallow"
26
12
)
27
- EOF
28
-
29
- for type in " ${types[@]} " ; do
30
- cat << EOF
31
-
32
- // ${type} Ref represents either a ${type} or a \$ ref to a ${type} .
13
+ {{ range $type := .Types }}
14
+ // {{ $type }}Ref represents either a {{ $type }} or a $ref to a {{ $type }}.
33
15
// When serializing and both fields are set, Ref is preferred over Value.
34
- type ${ type} Ref struct {
16
+ type {{ $ type } }Ref struct {
35
17
Ref string
36
- Value *${ type}
18
+ Value *{{ $ type } }
37
19
extra []string
38
20
}
39
21
40
- var _ jsonpointer.JSONPointable = (*${ type} Ref)(nil)
22
+ var _ jsonpointer.JSONPointable = (*{{ $ type } }Ref)(nil)
41
23
42
- func (x *${ type} Ref) isEmpty() bool { return x == nil || x.Ref == "" && x.Value == nil }
24
+ func (x *{{ $ type } }Ref) isEmpty() bool { return x == nil || x.Ref == "" && x.Value == nil }
43
25
44
- // MarshalYAML returns the YAML encoding of ${ type} Ref.
45
- func (x ${ type} Ref) MarshalYAML() (interface{}, error) {
26
+ // MarshalYAML returns the YAML encoding of {{ $ type } }Ref.
27
+ func (x {{ $ type } }Ref) MarshalYAML() (interface{}, error) {
46
28
if ref := x.Ref; ref != "" {
47
29
return &Ref{Ref: ref}, nil
48
30
}
49
31
return x.Value.MarshalYAML()
50
32
}
51
33
52
- // MarshalJSON returns the JSON encoding of ${ type} Ref.
53
- func (x ${ type} Ref) MarshalJSON() ([]byte, error) {
34
+ // MarshalJSON returns the JSON encoding of {{ $ type } }Ref.
35
+ func (x {{ $ type } }Ref) MarshalJSON() ([]byte, error) {
54
36
y, err := x.MarshalYAML()
55
37
if err != nil {
56
38
return nil, err
57
39
}
58
40
return json.Marshal(y)
59
- EOF
60
- cat << EOF
61
41
}
62
42
63
- // UnmarshalJSON sets ${ type} Ref to a copy of data.
64
- func (x *${ type} Ref) UnmarshalJSON(data []byte) error {
43
+ // UnmarshalJSON sets {{ $ type } }Ref to a copy of data.
44
+ func (x *{{ $ type } }Ref) UnmarshalJSON(data []byte) error {
65
45
var refOnly Ref
66
46
if extra, err := marshmallow.Unmarshal(data, &refOnly, marshmallow.WithExcludeKnownFieldsFromMap(true)); err == nil && refOnly.Ref != "" {
67
47
x.Ref = refOnly.Ref
@@ -77,8 +57,8 @@ func (x *${type}Ref) UnmarshalJSON(data []byte) error {
77
57
return json.Unmarshal(data, &x.Value)
78
58
}
79
59
80
- // Validate returns an error if ${ type} Ref does not comply with the OpenAPI spec.
81
- func (x *${ type} Ref) Validate(ctx context.Context, opts ...ValidationOption) error {
60
+ // Validate returns an error if {{ $ type } }Ref does not comply with the OpenAPI spec.
61
+ func (x *{{ $ type } }Ref) Validate(ctx context.Context, opts ...ValidationOption) error {
82
62
ctx = WithValidationOptions(ctx, opts...)
83
63
if extra := x.extra; len(extra) != 0 {
84
64
extras := make([]string, 0, len(extra))
@@ -102,13 +82,11 @@ func (x *${type}Ref) Validate(ctx context.Context, opts ...ValidationOption) err
102
82
}
103
83
104
84
// JSONLookup implements https://pkg.go.dev/github.com/go-openapi/jsonpointer#JSONPointable
105
- func (x *${ type} Ref) JSONLookup(token string) (interface{}, error) {
106
- if token == "\ $ ref" {
85
+ func (x *{{ $ type } }Ref) JSONLookup(token string) (interface{}, error) {
86
+ if token == "$ref" {
107
87
return x.Ref, nil
108
88
}
109
89
ptr, _, err := jsonpointer.GetForToken(x.Value, token)
110
90
return ptr, err
111
91
}
112
- EOF
113
-
114
- done
92
+ {{ end -}}
0 commit comments