Compare commits

..

No commits in common. "22947db2b6ae17c6069b466d05533a3407e23aec" and "e2e3799bc37284472a6020b528bf5a346dd070a0" have entirely different histories.

1 changed files with 15 additions and 42 deletions

View File

@ -4,19 +4,8 @@ import (
"encoding/json"
"fmt"
"reflect"
"git.xdrm.io/go/aicra/internal/cerr"
)
// ErrUnknownType is returned when encountering an unknown type
const ErrUnknownType = cerr.Error("unknown type")
// ErrInvalidJSON is returned when json parse failed
const ErrInvalidJSON = cerr.Error("invalid json")
// ErrInvalidRootType is returned when json is a map
const ErrInvalidRootType = cerr.Error("invalid json root type")
// Parameter represents an http request parameter
// that can be of type URL, GET, or FORM (multipart, json, urlencoded)
type Parameter struct {
@ -33,23 +22,16 @@ type Parameter struct {
}
// Parse parameter (json-like) if not already done
func (i *Parameter) Parse() error {
func (i *Parameter) Parse() {
/* (1) Stop if already parsed or nil*/
if i.Parsed || i.Value == nil {
return nil
return
}
/* (2) Try to parse value */
parsed, err := parseParameter(i.Value)
if err != nil {
return err
}
i.Value = parseParameter(i.Value)
i.Parsed = true
i.Value = parsed
return nil
}
// parseParameter parses http GET/POST data
@ -57,7 +39,7 @@ func (i *Parameter) Parse() error {
// - 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{}, error) {
func parseParameter(data interface{}) interface{} {
dtype := reflect.TypeOf(data)
dvalue := reflect.ValueOf(data)
@ -68,21 +50,17 @@ func parseParameter(data interface{}) (interface{}, error) {
// 1. Return nothing if empty
if dvalue.Len() == 0 {
return data, nil
return nil
}
// 2. only return first element if alone
if dvalue.Len() == 1 {
element := dvalue.Index(0)
// try to parse if a string (containing json)
if element.Kind() == reflect.String {
return parseParameter(element.String())
if element.Kind() != reflect.String {
return nil
}
// already typed
return data, nil
return parseParameter(element.String())
}
@ -94,17 +72,12 @@ func parseParameter(data interface{}) (interface{}, error) {
// ignore non-string
if element.Kind() != reflect.String {
result[i] = nil
continue
}
parsed, err := parseParameter(element.String())
if err != nil {
return data, err
}
result[i] = parsed
result[i] = parseParameter(element.String())
}
return result, nil
return result
/* (2) string -> parse */
case reflect.String:
@ -121,23 +94,23 @@ func parseParameter(data interface{}) (interface{}, error) {
mapval, ok := result.(map[string]interface{})
if !ok {
return dvalue.String(), ErrInvalidRootType
return dvalue.String()
}
wrapped, ok := mapval["wrapped"]
if !ok {
return dvalue.String(), ErrInvalidJSON
return dvalue.String()
}
return wrapped, nil
return wrapped
}
// else return as string
return dvalue.String(), nil
return dvalue.String()
}
/* (3) NIL if unknown type */
return dvalue, ErrUnknownType
return dvalue
}