/
unsupported_properties_error.go
42 lines (38 loc) · 1.1 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
package jsoninfo
import (
"encoding/json"
"fmt"
"sort"
)
// 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 fmt.Sprintf("invalid %T", *err)
}
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: %+v",
err.Value, keys)
}
return fmt.Sprintf("unsupported properties: %+v (supported properties are: %+v)", keys, supported)
}