-
Notifications
You must be signed in to change notification settings - Fork 34
/
decode_data.go
105 lines (89 loc) · 2.76 KB
/
decode_data.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// OpenRDAP
// Copyright 2017 Tom Harwood
// MIT License, see the LICENSE file.
package rdap
// DecodeData stores a snapshot of all fields in an RDAP object (in raw
// interface{} form), at the time of decoding. This allows the values of unknown
// fields to be retrieved.
//
// DecodeData also stores minor warnings/errors generated during decoding.
//
// DecodeData appears in each RDAP struct (e.g. rdap.Domain{}), and is populated
// during decoding. For manually constructed RDAP structs (d :=
// &rdap.Domain{Handle: "x"}...), DecodeData is not relevant and can be ignored.
//
// The snapshot values are entirely independent from other fields, and thus are
// not synchronised in any way.
type DecodeData struct {
isKnown map[string]bool
values map[string]interface{}
overrideKnownValue map[string]bool
notes map[string][]string
}
// TODO (temporary, using for spew output)
func (r DecodeData) String() string {
result := "["
for name, notes := range r.notes {
for _, note := range notes {
result += "\n !!!" + name + ": " + note
}
}
result += "\n"
return result
}
// Notes returns a list of minor warnings/errors encountered while decoding the
// field |name|.
//
// |name| is the RDAP field name (not the Go field name), so "port43", not
// "Port43". For a full list of decoded field names, use Fields().
//
// The warnings/errors returned look like: "invalid JSON type, expecting float".
func (r DecodeData) Notes(name string) []string {
if notes, ok := r.notes[name]; ok {
return notes
}
return nil
}
//func (r DecodeData) OverrideValue(key string, value interface{}) {
// r.values[key] = value
// r.overrideKnownValue[key] = true
//}
// Value returns the value of the field |name| as an interface{}.
//
// |name| is the RDAP field name (not the Go field name), so "port43", not
// "Port43". For a full list of decoded field names, use Fields().
func (r DecodeData) Value(name string) interface{} {
if v, ok := r.values[name]; ok {
return v
}
return nil
}
// Fields returns a list of all RDAP field names decoded.
//
// This includes both known/unknown fields.
//
// The names returned are the RDAP field names (not the Go field names), so
// "port43", not "Port43".
func (r DecodeData) Fields() []string {
var fields []string
for f := range r.values {
fields = append(fields, f)
}
return fields
}
// UnknownFields returns a list of unknown RDAP fields decoded.
func (r DecodeData) UnknownFields() []string {
var fields []string
for f := range r.values {
if _, isKnown := r.isKnown[f]; !isKnown {
fields = append(fields, f)
}
}
return fields
}
func (r *DecodeData) init() {
r.isKnown = map[string]bool{}
r.values = map[string]interface{}{}
r.overrideKnownValue = map[string]bool{}
r.notes = map[string][]string{}
}