rsapi/commands.go
func Normalize(payload APIParams, name string, v interface{}) (APIParams, error) {
	var matches = nameRegex.FindStringSubmatch(name)
	if len(matches) == 0 {
		return nil, nil
	}
	var k = matches[1]
	if len(k) == 0 {
		return nil, nil
	}
	var after = matches[2]
	if after == "" {
		payload[k] = v
	} else if after == "[" {
		payload[name] = v
	} else if after == "[]" {
		if _, ok := payload[k]; !ok {
			payload[k] = []interface{}{}
		}
		a, ok := payload[k].([]interface{})
		if !ok {
			return nil, fmt.Errorf("expected array for param '%s'", k)
		}
		payload[k] = append(a, v)
	} else {
		matches = childRegexp.FindStringSubmatch(after)
		if len(matches) == 0 {
			matches = childRegexp2.FindStringSubmatch(after)
		}
		if len(matches) > 0 {
			var childKey = matches[1]
			if _, ok := payload[k]; !ok {
				payload[k] = []interface{}{}
			}
			var array []interface{}
			if a, ok := payload[k].([]interface{}); ok {
				array = a
			} else {
				return nil, fmt.Errorf("expected array for param '%s'", k)
			}
			var handled bool
			if len(array) > 0 {
				if last, ok := array[len(array)-1].(APIParams); ok {
					if _, ok := last[childKey]; !ok {
						handled = true
						Normalize(last, childKey, v)
					}
				}
			}
			if !handled {
				var p, err = Normalize(APIParams{}, childKey, v)
				if err != nil {
					return nil, err
				}
				payload[k] = append(array, p)
			}
		} else {
			if payload[k] == nil {
				payload[k] = APIParams{}
			}
			if _, ok := payload[k].(APIParams); !ok {
				return nil, fmt.Errorf("expected map for param '%s'", k)
			}
			var p, err = Normalize(payload[k].(APIParams), after, v)
			if err != nil {
				return nil, err
			}
			payload[k] = p
		}
	}
	return payload, nil
}
func Normalize(payload APIParams, name string, v interface{}) (APIParams, error) {
	var matches = nameRegex.FindStringSubmatch(name)
	if len(matches) == 0 {
		return nil, nil
	}
	var k = matches[1]
	if len(k) == 0 {
		return nil, nil
	}
	var after = matches[2]
	if after == "" {
		payload[k] = v
	} else if after == "[" {
		payload[name] = v
	} else if after == "[]" {
		if _, ok := payload[k]; !ok {
			payload[k] = []interface{}{}
		}
		a, ok := payload[k].([]interface{})
		if !ok {
			return nil, fmt.Errorf("expected array for param '%s'", k)
		}
		payload[k] = append(a, v)
	} else {
		matches = childRegexp.FindStringSubmatch(after)
		if len(matches) == 0 {
			matches = childRegexp2.FindStringSubmatch(after)
		}
		if len(matches) > 0 {
			var childKey = matches[1]
			if _, ok := payload[k]; !ok {
				payload[k] = []interface{}{}
			}
			var array []interface{}
			if a, ok := payload[k].([]interface{}); ok {
				array = a
			} else {
				return nil, fmt.Errorf("expected array for param '%s'", k)
			}
			var handled bool
			if len(array) > 0 {
				if last, ok := array[len(array)-1].(APIParams); ok {
					if _, ok := last[childKey]; !ok {
						handled = true
						Normalize(last, childKey, v)
					}
				}
			}
			if !handled {
				var p, err = Normalize(APIParams{}, childKey, v)
				if err != nil {
					return nil, err
				}
				payload[k] = append(array, p)
			}
		} else {
			if payload[k] == nil {
				payload[k] = APIParams{}
			}
			if _, ok := payload[k].(APIParams); !ok {
				return nil, fmt.Errorf("expected map for param '%s'", k)
			}
			var p, err = Normalize(payload[k].(APIParams), after, v)
			if err != nil {
				return nil, err
			}
			payload[k] = p
		}
	}
	return payload, nil
}
// Normalize recursively traverses a query string encoded name and builds up the corresponding
// structure.
// "a[b]" produces a map[string]interface{} with key "b"
// "a[]" produces a []interface{}