summaryrefslogtreecommitdiff
path: root/pkg/api/handlers/decoder.go
diff options
context:
space:
mode:
authorOpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com>2020-01-24 06:56:55 -0800
committerGitHub <noreply@github.com>2020-01-24 06:56:55 -0800
commit81e59a742b46d41848c8c213e155fbc9ecc4e5f8 (patch)
treebb0fade91f9d35015b10316cf362f46e03a4a035 /pkg/api/handlers/decoder.go
parent5bad873c4cd9fab9112e1d84ba376d47073cc8bb (diff)
parent9634e7eef77abbba2584b8e78daf9c76cfdcedd9 (diff)
downloadpodman-81e59a742b46d41848c8c213e155fbc9ecc4e5f8.tar.gz
podman-81e59a742b46d41848c8c213e155fbc9ecc4e5f8.tar.bz2
podman-81e59a742b46d41848c8c213e155fbc9ecc4e5f8.zip
Merge pull request #4958 from jwhonce/wip/filters
[CI:DOCS] Add query parameter converters for complex types
Diffstat (limited to 'pkg/api/handlers/decoder.go')
-rw-r--r--pkg/api/handlers/decoder.go50
1 files changed, 50 insertions, 0 deletions
diff --git a/pkg/api/handlers/decoder.go b/pkg/api/handlers/decoder.go
new file mode 100644
index 000000000..d87409394
--- /dev/null
+++ b/pkg/api/handlers/decoder.go
@@ -0,0 +1,50 @@
+package handlers
+
+import (
+ "encoding/json"
+ "reflect"
+ "time"
+
+ "github.com/gorilla/schema"
+ "github.com/sirupsen/logrus"
+)
+
+// NewAPIDecoder returns a configured schema.Decoder
+func NewAPIDecoder() *schema.Decoder {
+ d := schema.NewDecoder()
+ d.IgnoreUnknownKeys(true)
+ d.RegisterConverter(map[string][]string{}, convertUrlValuesString)
+ d.RegisterConverter(time.Time{}, convertTimeString)
+ 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 convertTimeString(query string) reflect.Value {
+ t, err := time.Parse(time.RFC3339, query)
+ if err != nil {
+ logrus.Infof("convertTimeString: Failed to Unmarshal %s: %s", query, err.Error())
+
+ return reflect.ValueOf(time.Time{})
+ }
+ return reflect.ValueOf(t)
+}