forked from getkin/kin-openapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unsupported_properties_error.go
45 lines (41 loc) · 1.19 KB
/
unsupported_properties_error.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
package jsoninfo
import (
"encoding/json"
"fmt"
"sort"
"strings"
)
// UnsupportedPropertiesError is a helper for extensions that want to refuse
// unsupported JSON object properties.
//
// It produces a helpful error message.
type UnsupportedPropertiesError struct {
Value interface{}
UnsupportedProperties map[string]json.RawMessage
}
func NewUnsupportedPropertiesError(v interface{}, m map[string]json.RawMessage) error {
return &UnsupportedPropertiesError{
Value: v,
UnsupportedProperties: m,
}
}
func (err *UnsupportedPropertiesError) Error() string {
m := err.UnsupportedProperties
typeInfo := GetTypeInfoForValue(err.Value)
if m == nil || typeInfo == nil {
return "Invalid UnsupportedPropertiesError"
}
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
supported := typeInfo.FieldNames()
if len(supported) == 0 {
return fmt.Sprintf("Type '%T' doesn't take any properties. Unsupported properties: '%s'\n",
err.Value, strings.Join(keys, "', '"))
}
return fmt.Sprintf("Unsupported properties: '%s'\nSupported properties are: '%s'",
strings.Join(keys, "', '"),
strings.Join(supported, "', '"))
}