forked from nelsam/requests
/
unused.go
60 lines (53 loc) · 1.48 KB
/
unused.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
package requests
import (
"fmt"
"strings"
)
// UnusedFields is an error type for input values that were not used
// in a request.
type UnusedFields struct {
params map[string]interface{}
matched set
missing []string
}
// HasMissing returns whether or not this error knows about any input
// values that were not used in a request.
func (err *UnusedFields) HasMissing() bool {
return err.NumMissing() > 0
}
// NumMissing returns the number of input values that were not used in
// a request that this error knows about.
func (err *UnusedFields) NumMissing() int {
return len(err.params) - len(err.matched)
}
// parseMissing is used to find the input values that had no matching
// fields in a request.
func (err *UnusedFields) parseMissing() {
err.missing = make([]string, 0, err.NumMissing())
for param := range err.params {
hasMatch := false
for _, found := range err.matched {
if param == found {
hasMatch = true
break
}
}
if !hasMatch {
err.missing = append(err.missing, param)
}
}
}
// Fields returns the request names of the fields that had no
// corresponding struct fields in a request.
func (err *UnusedFields) Fields() []string {
if err.missing == nil {
err.parseMissing()
}
return err.missing
}
// Error returns an error message listing which fields could not be
// found in the target struct.
func (err *UnusedFields) Error() string {
return fmt.Sprintf("Request fields found with no matching struct fields: %s",
strings.Join(err.Fields(), ", "))
}