Repository navigation
Expand file tree
/
Copy pathreflect.go
More file actions
54 lines (47 loc) · 1.46 KB
/
Copy pathreflect.go
File metadata and controls
54 lines (47 loc) · 1.46 KB
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
package apify
import (
"reflect"
"strings"
"sync"
)
// knownKeysCache memoizes the JSON key sets per struct type so the reflection cost is
// paid once per type rather than on every unmarshal.
var knownKeysCache sync.Map // map[reflect.Type]map[string]struct{}
// knownJSONKeys returns the set of JSON object keys that the struct type of v maps to its
// fields. Used by the models' UnmarshalJSON to decide which incoming keys are "extra"
// (unmodelled) and should be collected into the Extra map.
//
// The argument is a zero value of the model's alias type; only its type is inspected.
func knownJSONKeys(v any) map[string]struct{} {
t := reflect.TypeOf(v)
if cached, ok := knownKeysCache.Load(t); ok {
return cached.(map[string]struct{})
}
keys := make(map[string]struct{})
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
tag := field.Tag.Get("json")
if tag == "-" {
continue // e.g. the Extra field itself
}
name := strings.Split(tag, ",")[0]
if name == "" {
name = field.Name
}
keys[name] = struct{}{}
}
knownKeysCache.Store(t, keys)
return keys
}
// Terminal run/build statuses. A resource in any of these is finished and will not change.
var terminalStatuses = map[string]struct{}{
"SUCCEEDED": {},
"FAILED": {},
"ABORTED": {},
"TIMED-OUT": {},
}
// isTerminalStatus reports whether status is a terminal (finished) run/build status.
func isTerminalStatus(status string) bool {
_, ok := terminalStatuses[status]
return ok
}