2016-03-23 20:53:09 +01:00
|
|
|
// Copyright 2014 Alvaro J. Genial. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package form
|
|
|
|
|
|
|
|
import (
|
2016-05-04 17:48:12 +02:00
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2016-03-23 20:53:09 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type node map[string]interface{}
|
|
|
|
|
|
|
|
func (n node) Values() url.Values {
|
2016-05-04 17:48:12 +02:00
|
|
|
vs := url.Values{}
|
|
|
|
n.merge("", &vs)
|
|
|
|
return vs
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n node) merge(p string, vs *url.Values) {
|
2016-05-04 17:48:12 +02:00
|
|
|
for k, x := range n {
|
|
|
|
switch y := x.(type) {
|
|
|
|
case string:
|
|
|
|
vs.Add(p+escape(k), y)
|
|
|
|
case node:
|
|
|
|
y.merge(p+escape(k)+".", vs)
|
|
|
|
default:
|
|
|
|
panic("value is neither string nor node")
|
|
|
|
}
|
|
|
|
}
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Add tests for implicit indexing.
|
|
|
|
func parseValues(vs url.Values, canIndexFirstLevelOrdinally bool) node {
|
2016-05-04 17:48:12 +02:00
|
|
|
// NOTE: Because of the flattening of potentially multiple strings to one key, implicit indexing works:
|
|
|
|
// i. At the first level; e.g. Foo.Bar=A&Foo.Bar=B becomes 0.Foo.Bar=A&1.Foo.Bar=B
|
|
|
|
// ii. At the last level; e.g. Foo.Bar._=A&Foo.Bar._=B becomes Foo.Bar.0=A&Foo.Bar.1=B
|
|
|
|
// TODO: At in-between levels; e.g. Foo._.Bar=A&Foo._.Bar=B becomes Foo.0.Bar=A&Foo.1.Bar=B
|
|
|
|
// (This last one requires that there only be one placeholder in order for it to be unambiguous.)
|
|
|
|
|
|
|
|
m := map[string]string{}
|
|
|
|
for k, ss := range vs {
|
|
|
|
indexLastLevelOrdinally := strings.HasSuffix(k, "."+implicitKey)
|
|
|
|
|
|
|
|
for i, s := range ss {
|
|
|
|
if canIndexFirstLevelOrdinally {
|
|
|
|
k = strconv.Itoa(i) + "." + k
|
|
|
|
} else if indexLastLevelOrdinally {
|
|
|
|
k = strings.TrimSuffix(k, implicitKey) + strconv.Itoa(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
m[k] = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
n := node{}
|
|
|
|
for k, s := range m {
|
|
|
|
n = n.split(k, s)
|
|
|
|
}
|
|
|
|
return n
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func splitPath(path string) (k, rest string) {
|
2016-05-04 17:48:12 +02:00
|
|
|
esc := false
|
|
|
|
for i, r := range path {
|
|
|
|
switch {
|
|
|
|
case !esc && r == '\\':
|
|
|
|
esc = true
|
|
|
|
case !esc && r == '.':
|
|
|
|
return unescape(path[:i]), path[i+1:]
|
|
|
|
default:
|
|
|
|
esc = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return unescape(path), ""
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n node) split(path, s string) node {
|
2016-05-04 17:48:12 +02:00
|
|
|
k, rest := splitPath(path)
|
|
|
|
if rest == "" {
|
|
|
|
return add(n, k, s)
|
|
|
|
}
|
|
|
|
if _, ok := n[k]; !ok {
|
|
|
|
n[k] = node{}
|
|
|
|
}
|
|
|
|
|
|
|
|
c := getNode(n[k])
|
|
|
|
n[k] = c.split(rest, s)
|
|
|
|
return n
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func add(n node, k, s string) node {
|
2016-05-04 17:48:12 +02:00
|
|
|
if n == nil {
|
|
|
|
return node{k: s}
|
|
|
|
}
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-05-04 17:48:12 +02:00
|
|
|
if _, ok := n[k]; ok {
|
|
|
|
panic("key " + k + " already set")
|
|
|
|
}
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-05-04 17:48:12 +02:00
|
|
|
n[k] = s
|
|
|
|
return n
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func isEmpty(x interface{}) bool {
|
2016-05-04 17:48:12 +02:00
|
|
|
switch y := x.(type) {
|
|
|
|
case string:
|
|
|
|
return y == ""
|
|
|
|
case node:
|
|
|
|
if s, ok := y[""].(string); ok {
|
|
|
|
return s == ""
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
panic("value is neither string nor node")
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func getNode(x interface{}) node {
|
2016-05-04 17:48:12 +02:00
|
|
|
switch y := x.(type) {
|
|
|
|
case string:
|
|
|
|
return node{"": y}
|
|
|
|
case node:
|
|
|
|
return y
|
|
|
|
}
|
|
|
|
panic("value is neither string nor node")
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func getString(x interface{}) string {
|
2016-05-04 17:48:12 +02:00
|
|
|
switch y := x.(type) {
|
|
|
|
case string:
|
|
|
|
return y
|
|
|
|
case node:
|
|
|
|
if s, ok := y[""].(string); ok {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
panic("value is neither string nor node")
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func escape(s string) string {
|
2016-05-04 17:48:12 +02:00
|
|
|
return strings.Replace(strings.Replace(s, `\`, `\\`, -1), `.`, `\.`, -1)
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func unescape(s string) string {
|
2016-05-04 17:48:12 +02:00
|
|
|
return strings.Replace(strings.Replace(s, `\.`, `.`, -1), `\\`, `\`, -1)
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|