merge parseParameter

This commit is contained in:
Adrien Marquès 2019-11-19 16:14:50 +01:00
parent 1f9c3c02c1
commit c40fb03aa7
Signed by: xdrm-brackets
GPG Key ID: D75243CA236D825E
2 changed files with 87 additions and 88 deletions

View File

@ -1,88 +0,0 @@
package reqdata
import (
"encoding/json"
"fmt"
"reflect"
)
// parseParameter parses http GET/POST data
// - []string
// - size = 1 : return json of first element
// - size > 1 : return array of json elements
// - string : return json if valid, else return raw string
func parseParameter(data interface{}) interface{} {
dtype := reflect.TypeOf(data)
dvalue := reflect.ValueOf(data)
switch dtype.Kind() {
/* (1) []string -> recursive */
case reflect.Slice:
// 1. Return nothing if empty
if dvalue.Len() == 0 {
return nil
}
// 2. only return first element if alone
if dvalue.Len() == 1 {
element := dvalue.Index(0)
if element.Kind() != reflect.String {
return nil
}
return parseParameter(element.String())
}
// 3. Return all elements if more than 1
result := make([]interface{}, dvalue.Len())
for i, l := 0, dvalue.Len(); i < l; i++ {
element := dvalue.Index(i)
// ignore non-string
if element.Kind() != reflect.String {
continue
}
result[i] = parseParameter(element.String())
}
return result
/* (2) string -> parse */
case reflect.String:
// build json wrapper
wrapper := fmt.Sprintf("{\"wrapped\":%s}", dvalue.String())
// try to parse as json
var result interface{}
err := json.Unmarshal([]byte(wrapper), &result)
// return if success
if err == nil {
mapval, ok := result.(map[string]interface{})
if !ok {
return dvalue.String()
}
wrapped, ok := mapval["wrapped"]
if !ok {
return dvalue.String()
}
return wrapped
}
// else return as string
return dvalue.String()
}
/* (3) NIL if unknown type */
return dvalue
}

View File

@ -1,5 +1,11 @@
package reqdata package reqdata
import (
"encoding/json"
"fmt"
"reflect"
)
// Parameter represents an http request parameter // Parameter represents an http request parameter
// that can be of type URL, GET, or FORM (multipart, json, urlencoded) // that can be of type URL, GET, or FORM (multipart, json, urlencoded)
type Parameter struct { type Parameter struct {
@ -27,3 +33,84 @@ func (i *Parameter) Parse() {
i.Value = parseParameter(i.Value) i.Value = parseParameter(i.Value)
} }
// parseParameter parses http GET/POST data
// - []string
// - size = 1 : return json of first element
// - size > 1 : return array of json elements
// - string : return json if valid, else return raw string
func parseParameter(data interface{}) interface{} {
dtype := reflect.TypeOf(data)
dvalue := reflect.ValueOf(data)
switch dtype.Kind() {
/* (1) []string -> recursive */
case reflect.Slice:
// 1. Return nothing if empty
if dvalue.Len() == 0 {
return nil
}
// 2. only return first element if alone
if dvalue.Len() == 1 {
element := dvalue.Index(0)
if element.Kind() != reflect.String {
return nil
}
return parseParameter(element.String())
}
// 3. Return all elements if more than 1
result := make([]interface{}, dvalue.Len())
for i, l := 0, dvalue.Len(); i < l; i++ {
element := dvalue.Index(i)
// ignore non-string
if element.Kind() != reflect.String {
continue
}
result[i] = parseParameter(element.String())
}
return result
/* (2) string -> parse */
case reflect.String:
// build json wrapper
wrapper := fmt.Sprintf("{\"wrapped\":%s}", dvalue.String())
// try to parse as json
var result interface{}
err := json.Unmarshal([]byte(wrapper), &result)
// return if success
if err == nil {
mapval, ok := result.(map[string]interface{})
if !ok {
return dvalue.String()
}
wrapped, ok := mapval["wrapped"]
if !ok {
return dvalue.String()
}
return wrapped
}
// else return as string
return dvalue.String()
}
/* (3) NIL if unknown type */
return dvalue
}