2015-05-12 06:19:05 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/url"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
//ConvertToQueryValues converts the struct to url.Values
|
|
|
|
func ConvertToQueryValues(ifc interface{}) url.Values {
|
|
|
|
values := url.Values{}
|
|
|
|
SetQueryValues(ifc, &values)
|
|
|
|
return values
|
|
|
|
}
|
|
|
|
|
|
|
|
//SetQueryValues sets the struct to existing url.Values following ECS encoding rules
|
|
|
|
func SetQueryValues(ifc interface{}, values *url.Values) {
|
|
|
|
setQueryValues(ifc, values, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func setQueryValues(i interface{}, values *url.Values, prefix string) {
|
2015-10-25 03:01:15 +00:00
|
|
|
// add to support url.Values
|
|
|
|
mapValues, ok := i.(url.Values)
|
|
|
|
if ok {
|
|
|
|
for k, _ := range mapValues {
|
|
|
|
values.Set(k, mapValues.Get(k))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-05-12 06:19:05 +00:00
|
|
|
elem := reflect.ValueOf(i)
|
|
|
|
if elem.Kind() == reflect.Ptr {
|
|
|
|
elem = elem.Elem()
|
|
|
|
}
|
|
|
|
elemType := elem.Type()
|
|
|
|
for i := 0; i < elem.NumField(); i++ {
|
2016-10-26 01:37:58 +00:00
|
|
|
|
2015-05-12 06:19:05 +00:00
|
|
|
fieldName := elemType.Field(i).Name
|
2015-10-25 03:01:15 +00:00
|
|
|
anonymous := elemType.Field(i).Anonymous
|
2015-05-12 06:19:05 +00:00
|
|
|
field := elem.Field(i)
|
|
|
|
// TODO Use Tag for validation
|
|
|
|
// tag := typ.Field(i).Tag.Get("tagname")
|
|
|
|
kind := field.Kind()
|
|
|
|
if (kind == reflect.Ptr || kind == reflect.Array || kind == reflect.Slice || kind == reflect.Map || kind == reflect.Chan) && field.IsNil() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if kind == reflect.Ptr {
|
|
|
|
field = field.Elem()
|
2015-06-15 12:03:32 +00:00
|
|
|
kind = field.Kind()
|
2015-05-12 06:19:05 +00:00
|
|
|
}
|
|
|
|
var value string
|
2015-06-15 12:03:32 +00:00
|
|
|
//switch field.Interface().(type) {
|
|
|
|
switch kind {
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
2015-05-12 06:19:05 +00:00
|
|
|
i := field.Int()
|
|
|
|
if i != 0 {
|
|
|
|
value = strconv.FormatInt(i, 10)
|
|
|
|
}
|
2015-06-15 12:03:32 +00:00
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
2015-05-12 06:19:05 +00:00
|
|
|
i := field.Uint()
|
|
|
|
if i != 0 {
|
|
|
|
value = strconv.FormatUint(i, 10)
|
|
|
|
}
|
2015-06-15 12:03:32 +00:00
|
|
|
case reflect.Float32:
|
2015-05-12 06:19:05 +00:00
|
|
|
value = strconv.FormatFloat(field.Float(), 'f', 4, 32)
|
2015-06-15 12:03:32 +00:00
|
|
|
case reflect.Float64:
|
2015-05-12 06:19:05 +00:00
|
|
|
value = strconv.FormatFloat(field.Float(), 'f', 4, 64)
|
2015-06-15 12:03:32 +00:00
|
|
|
case reflect.Bool:
|
2015-05-12 06:19:05 +00:00
|
|
|
value = strconv.FormatBool(field.Bool())
|
2015-06-15 12:03:32 +00:00
|
|
|
case reflect.String:
|
2015-05-12 06:19:05 +00:00
|
|
|
value = field.String()
|
2015-10-25 03:01:15 +00:00
|
|
|
case reflect.Map:
|
|
|
|
ifc := field.Interface()
|
|
|
|
m := ifc.(map[string]string)
|
|
|
|
if m != nil {
|
|
|
|
j := 0
|
|
|
|
for k, v := range m {
|
|
|
|
j++
|
|
|
|
keyName := fmt.Sprintf("%s.%d.Key", fieldName, j)
|
|
|
|
values.Set(keyName, k)
|
|
|
|
valueName := fmt.Sprintf("%s.%d.Value", fieldName, j)
|
|
|
|
values.Set(valueName, v)
|
|
|
|
}
|
|
|
|
}
|
2015-06-15 12:03:32 +00:00
|
|
|
case reflect.Slice:
|
|
|
|
switch field.Type().Elem().Kind() {
|
|
|
|
case reflect.Uint8:
|
|
|
|
value = string(field.Bytes())
|
|
|
|
case reflect.String:
|
|
|
|
l := field.Len()
|
|
|
|
if l > 0 {
|
|
|
|
strArray := make([]string, l)
|
|
|
|
for i := 0; i < l; i++ {
|
|
|
|
strArray[i] = field.Index(i).String()
|
|
|
|
}
|
|
|
|
bytes, err := json.Marshal(strArray)
|
|
|
|
if err == nil {
|
|
|
|
value = string(bytes)
|
|
|
|
} else {
|
|
|
|
log.Printf("Failed to convert JSON: %v", err)
|
|
|
|
}
|
2015-05-12 06:19:05 +00:00
|
|
|
}
|
2015-06-15 12:03:32 +00:00
|
|
|
default:
|
2015-05-12 06:19:05 +00:00
|
|
|
l := field.Len()
|
|
|
|
for j := 0; j < l; j++ {
|
|
|
|
prefixName := fmt.Sprintf("%s.%d.", fieldName, (j + 1))
|
|
|
|
ifc := field.Index(j).Interface()
|
2015-10-25 03:01:15 +00:00
|
|
|
//log.Printf("%s : %v", prefixName, ifc)
|
2015-05-12 06:19:05 +00:00
|
|
|
if ifc != nil {
|
|
|
|
setQueryValues(ifc, values, prefixName)
|
|
|
|
}
|
|
|
|
}
|
2015-06-15 12:03:32 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
switch field.Interface().(type) {
|
|
|
|
case ISO6801Time:
|
|
|
|
t := field.Interface().(ISO6801Time)
|
|
|
|
value = t.String()
|
|
|
|
case time.Time:
|
|
|
|
t := field.Interface().(time.Time)
|
|
|
|
value = GetISO8601TimeStamp(t)
|
|
|
|
default:
|
2015-05-12 06:19:05 +00:00
|
|
|
ifc := field.Interface()
|
|
|
|
if ifc != nil {
|
2015-10-25 03:01:15 +00:00
|
|
|
if anonymous {
|
|
|
|
SetQueryValues(ifc, values)
|
|
|
|
} else {
|
|
|
|
prefixName := fieldName + "."
|
|
|
|
setQueryValues(ifc, values, prefixName)
|
|
|
|
}
|
2015-05-12 06:19:05 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if value != "" {
|
|
|
|
name := elemType.Field(i).Tag.Get("ArgName")
|
|
|
|
if name == "" {
|
|
|
|
name = fieldName
|
|
|
|
}
|
|
|
|
if prefix != "" {
|
|
|
|
name = prefix + name
|
|
|
|
}
|
|
|
|
values.Set(name, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|