diff options
Diffstat (limited to 'psr7/post.go')
-rw-r--r-- | psr7/post.go | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/psr7/post.go b/psr7/post.go new file mode 100644 index 00000000..30af7e3a --- /dev/null +++ b/psr7/post.go @@ -0,0 +1,45 @@ +package psr7 + +import "strings" + +type postData map[string]interface{} + +func (d postData) push(k string, v []string) { + if len(v) == 0 { + // doing nothing + return + } + + chunks := make([]string, 0) + for _, chunk := range strings.Split(k, "[") { + chunks = append(chunks, strings.Trim(chunk, "]")) + } + + d.pushChunk(chunks, v) +} + +func (d postData) pushChunk(k []string, v []string) { + if len(v) == 0 || v[0] == "" { + return + } + + head := k[0] + tail := k[1:] + if len(k) == 1 { + d[head] = v[0] + return + } + + // unnamed array + if len(tail) == 1 && tail[0] == "" { + d[head] = v + return + } + + if p, ok := d[head]; !ok { + d[head] = make(postData) + d[head].(postData).pushChunk(tail, v) + } else { + p.(postData).pushChunk(tail, v) + } +} |