File: unsupported_properties_error.go

package info (click to toggle)
golang-github-getkin-kin-openapi 0.1.0+git20181119.fa639d0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 480 kB
  • sloc: makefile: 2
file content (45 lines) | stat: -rw-r--r-- 1,218 bytes parent folder | download
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, "', '"))
}