/
parser_enum.go
114 lines (106 loc) · 2.65 KB
/
parser_enum.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package jsonschema
import (
"encoding/json"
"github.com/go-faster/errors"
"github.com/go-faster/jx"
"golang.org/x/exp/slices"
"github.com/ogen-go/ogen/internal/xslices"
)
func inferJSONType(v json.RawMessage) (string, error) {
d := jx.DecodeBytes(v)
switch tt := d.Next(); tt {
case jx.String:
return "string", nil
case jx.Number:
return "number", nil
case jx.Bool:
return "bool", nil
case jx.Null:
return "", errors.Errorf("cannot infer type from %q", v)
default:
return "", errors.Errorf("invalid value %q", v)
}
}
func parseEnumValues(s *Schema, rawValues []json.RawMessage) ([]any, error) {
var values []any
for _, raw := range rawValues {
val, err := parseJSONValue(s, raw)
if err != nil {
return nil, errors.Wrapf(err, "parse value %q", raw)
}
values = append(values, val)
}
return values, nil
}
func parseJSONValue(root *Schema, v json.RawMessage) (any, error) {
var parse func(s *Schema, d *jx.Decoder) (any, error)
parse = func(s *Schema, d *jx.Decoder) (any, error) {
switch next := d.Next(); next {
case jx.Null:
return nil, d.Null()
case jx.String:
return d.Str()
case jx.Number:
n, err := d.Num()
if err != nil {
return nil, err
}
if n.IsInt() {
return n.Int64()
}
return n.Float64()
case jx.Bool:
return d.Bool()
case jx.Array:
var arr []any
if err := d.Arr(func(d *jx.Decoder) error {
var item *Schema
if s != nil {
item = s.Item
}
v, err := parse(item, d)
if err != nil {
return errors.Wrap(err, "array item")
}
arr = append(arr, v)
return nil
}); err != nil {
return nil, err
}
return arr, nil
case jx.Object:
obj := map[string]any{}
if err := d.ObjBytes(func(d *jx.Decoder, key []byte) error {
v, err := parse(nil, d)
if err != nil {
return errors.Wrapf(err, "property %q", key)
}
obj[string(key)] = v
return nil
}); err != nil {
return nil, err
}
return obj, nil
default:
return nil, errors.Errorf("unexpected type: %q", next)
}
}
return parse(root, jx.DecodeBytes(v))
}
// See https://github.com/OAI/OpenAPI-Specification/blob/main/proposals/2019-10-31-Clarify-Nullable.md#if-a-schema-specifies-nullable-true-and-enum-1-2-3-does-that-schema-allow-null-values-see-1900.
func handleNullableEnum(s *Schema) {
// Workaround: handle nullable enums correctly.
//
// Notice that nullable enum requires `null` in value list.
//
// Check that enum contains `null` value.
s.Nullable = s.Nullable || slices.ContainsFunc(s.Enum, func(v any) bool {
return v == nil
})
// Filter all `null`s.
if s.Nullable {
s.Enum = xslices.Filter(s.Enum, func(v any) bool {
return v != nil
})
}
}