summaryrefslogtreecommitdiff
path: root/psr7/post.go
blob: 30af7e3a679f3f386ed80f5e3d5a2b87e70beb2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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)
	}
}