|
Packit Service |
3a6627 |
package jsoninfo
|
|
Packit Service |
3a6627 |
|
|
Packit Service |
3a6627 |
import (
|
|
Packit Service |
3a6627 |
"encoding/json"
|
|
Packit Service |
3a6627 |
"fmt"
|
|
Packit Service |
3a6627 |
"sort"
|
|
Packit Service |
3a6627 |
"strings"
|
|
Packit Service |
3a6627 |
)
|
|
Packit Service |
3a6627 |
|
|
Packit Service |
3a6627 |
// UnsupportedPropertiesError is a helper for extensions that want to refuse
|
|
Packit Service |
3a6627 |
// unsupported JSON object properties.
|
|
Packit Service |
3a6627 |
//
|
|
Packit Service |
3a6627 |
// It produces a helpful error message.
|
|
Packit Service |
3a6627 |
type UnsupportedPropertiesError struct {
|
|
Packit Service |
3a6627 |
Value interface{}
|
|
Packit Service |
3a6627 |
UnsupportedProperties map[string]json.RawMessage
|
|
Packit Service |
3a6627 |
}
|
|
Packit Service |
3a6627 |
|
|
Packit Service |
3a6627 |
func NewUnsupportedPropertiesError(v interface{}, m map[string]json.RawMessage) error {
|
|
Packit Service |
3a6627 |
return &UnsupportedPropertiesError{
|
|
Packit Service |
3a6627 |
Value: v,
|
|
Packit Service |
3a6627 |
UnsupportedProperties: m,
|
|
Packit Service |
3a6627 |
}
|
|
Packit Service |
3a6627 |
}
|
|
Packit Service |
3a6627 |
|
|
Packit Service |
3a6627 |
func (err *UnsupportedPropertiesError) Error() string {
|
|
Packit Service |
3a6627 |
m := err.UnsupportedProperties
|
|
Packit Service |
3a6627 |
typeInfo := GetTypeInfoForValue(err.Value)
|
|
Packit Service |
3a6627 |
if m == nil || typeInfo == nil {
|
|
Packit Service |
3a6627 |
return "Invalid UnsupportedPropertiesError"
|
|
Packit Service |
3a6627 |
}
|
|
Packit Service |
3a6627 |
keys := make([]string, 0, len(m))
|
|
Packit Service |
3a6627 |
for k := range m {
|
|
Packit Service |
3a6627 |
keys = append(keys, k)
|
|
Packit Service |
3a6627 |
}
|
|
Packit Service |
3a6627 |
sort.Strings(keys)
|
|
Packit Service |
3a6627 |
supported := typeInfo.FieldNames()
|
|
Packit Service |
3a6627 |
if len(supported) == 0 {
|
|
Packit Service |
3a6627 |
return fmt.Sprintf("Type '%T' doesn't take any properties. Unsupported properties: '%s'\n",
|
|
Packit Service |
3a6627 |
err.Value, strings.Join(keys, "', '"))
|
|
Packit Service |
3a6627 |
}
|
|
Packit Service |
3a6627 |
return fmt.Sprintf("Unsupported properties: '%s'\nSupported properties are: '%s'",
|
|
Packit Service |
3a6627 |
strings.Join(keys, "', '"),
|
|
Packit Service |
3a6627 |
strings.Join(supported, "', '"))
|
|
Packit Service |
3a6627 |
}
|