aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/bennyscetbun/jsongo/print.go
blob: 68699186b4559a7f7c8e5e54d7d0762891e41bd3 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package jsongo

import (
	"fmt"
	"regexp"
	"strings"
)

//Thanks https://github.com/chuckpreslar/inflect for the UpperCamelCase

// Split's a string so that it can be converted to a different casing.
// Splits on underscores, hyphens, spaces and camel casing.
func split(str string) []string {
	// FIXME: This isn't a perfect solution.
	// ex. WEiRD CaSINg (Support for 13 year old developers)
	return strings.Split(regexp.MustCompile(`-|_|([a-z])([A-Z])`).ReplaceAllString(strings.Trim(str, `-|_| `), `$1 $2`), ` `)
}

// UpperCamelCase converts a string to it's upper camel case version.
func UpperCamelCase(str string) string {
	pieces := split(str)

	for index, s := range pieces {
		pieces[index] = fmt.Sprintf(`%v%v`, strings.ToUpper(string(s[0])), strings.ToLower(s[1:]))
	}

	return strings.Join(pieces, ``)
}

func (that *JSONNode) printValue(indentlevel int, indentchar string) {
	fmt.Printf(" %T ", that.Get())
}

func (that *JSONNode) printMap(indentlevel int, indentchar string) {
	fmt.Printf(" struct {\n")
	for key := range that.m {
		printfindent(indentlevel+1, indentchar, "%s", UpperCamelCase(key))
		that.m[key].print(indentlevel+1, indentchar)
		fmt.Printf(" `json:\"%s\"`\n", key)
	}
	printfindent(indentlevel, indentchar, "}")
}

func (that *JSONNode) printArray(indentlevel int, indentchar string) {
	if len(that.a) == 0 {
		fmt.Printf(" []interface{} ")
		return
	}
	fmt.Printf(" [] ")
	for key := range that.a {
		that.a[key].print(indentlevel+1, indentchar)
		break
	}
}

//DebugProspect Print all the data the we ve got on a node and all it s children
func (that *JSONNode) print(indentlevel int, indentchar string) {
	switch that.t {
	case TypeValue:
		that.printValue(indentlevel, indentchar)
	case TypeMap:
		that.printMap(indentlevel, indentchar)
	case TypeArray:
		that.printArray(indentlevel, indentchar)
	case TypeUndefined:
		printfindent(indentlevel, indentchar, "Is of Type: TypeUndefined\n")
	}
}

//Print Print all the data the we ve got on a node and all it s children as a go struct :) (FOR DEV PURPOSE)
func (that *JSONNode) Print() {
	that.print(0, "\t")
	fmt.Printf("\n")
}