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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
package handlers
import (
"encoding/json"
"reflect"
"syscall"
"time"
"github.com/containers/podman/v4/libpod/define"
"github.com/containers/podman/v4/pkg/util"
"github.com/gorilla/schema"
"github.com/sirupsen/logrus"
)
// NewAPIDecoder returns a configured schema.Decoder
func NewAPIDecoder() *schema.Decoder {
_ = ParseDateTime
d := schema.NewDecoder()
d.IgnoreUnknownKeys(true)
d.RegisterConverter(map[string][]string{}, convertURLValuesString)
d.RegisterConverter(time.Time{}, convertTimeString)
d.RegisterConverter(define.ContainerStatus(0), convertContainerStatusString)
var Signal syscall.Signal
d.RegisterConverter(Signal, convertSignal)
return d
}
// On client:
// v := map[string][]string{
// "dangling": {"true"},
// }
//
// payload, err := jsoniter.MarshalToString(v)
// if err != nil {
// panic(err)
// }
// payload = url.QueryEscape(payload)
func convertURLValuesString(query string) reflect.Value {
f := map[string][]string{}
err := json.Unmarshal([]byte(query), &f)
if err != nil {
logrus.Infof("convertURLValuesString: Failed to Unmarshal %s: %s", query, err.Error())
}
return reflect.ValueOf(f)
}
func convertContainerStatusString(query string) reflect.Value {
result, err := define.StringToContainerStatus(query)
if err != nil {
logrus.Infof("convertContainerStatusString: Failed to parse %s: %s", query, err.Error())
// We return nil here instead of result because reflect.ValueOf().IsValid() will be true
// in github.com/gorilla/schema's decoder, which means there's no parsing error
return reflect.ValueOf(nil)
}
return reflect.ValueOf(result)
}
// isZero() can be used to determine if parsing failed.
func convertTimeString(query string) reflect.Value {
var (
err error
t time.Time
)
for _, f := range []string{
time.UnixDate,
time.ANSIC,
time.RFC1123,
time.RFC1123Z,
time.RFC3339,
time.RFC3339Nano,
time.RFC822,
time.RFC822Z,
time.RFC850,
time.RubyDate,
time.Stamp,
time.StampMicro,
time.StampMilli,
time.StampNano,
} {
t, err = time.Parse(f, query)
if err == nil {
return reflect.ValueOf(t)
}
if _, isParseError := err.(*time.ParseError); isParseError {
// Try next format
continue
} else {
break
}
}
// We've exhausted all formats, or something bad happened
if err != nil {
logrus.Infof("convertTimeString: Failed to parse %s: %s", query, err.Error())
}
return reflect.ValueOf(time.Time{})
}
// ParseDateTime is a helper function to aid in parsing different Time/Date formats
// isZero() can be used to determine if parsing failed.
func ParseDateTime(query string) time.Time {
return convertTimeString(query).Interface().(time.Time)
}
func convertSignal(query string) reflect.Value {
signal, err := util.ParseSignal(query)
if err != nil {
logrus.Infof("convertSignal: Failed to parse %s: %s", query, err.Error())
}
return reflect.ValueOf(signal)
}
|