diff options
author | Wolfy-J <[email protected]> | 2018-05-31 15:19:54 +0300 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2018-05-31 15:19:54 +0300 |
commit | 91a081e3ec43302ca1df8d436e48c2a14d7c76b9 (patch) | |
tree | 7c644b8bccf04f1a8dd5c07314665152dbea6d63 /psr7/post.go | |
parent | 48f4f7a39a2336be24cc74b4116c02cc941dbd9a (diff) |
psr7 support
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) + } +} |