spiegel_podman/pkg/api/handlers/decoder.go
Jhon Honce 9634e7eef7 Add query parameter converters for complex types
* Add converter for URL query parameters of type map[string][]string
* Add converter for URL query parameters of type time.Time
* Added function to allocate and configure schema.Decoder for API use
* Updated API handlers to leverage new converters, and correct handler
  code for filter type

An encoding example for a client using filters:

  v := map[string][]string{
      "dangling": {"true"},
  }
  payload, err := jsoniter.MarshalToString(v)
  if err != nil {
    panic(err)
  }
  payload = "?filters=" + url.QueryEscape(payload)

Signed-off-by: Jhon Honce <jhonce@redhat.com>
2020-01-23 16:32:00 -07:00

50 lines
1.1 KiB
Go

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)
}