initial commit
[govpp.git] / vendor / github.com / bennyscetbun / jsongo / print.go
1 package jsongo
2
3 import (
4         "fmt"
5         "regexp"
6         "strings"
7 )
8
9 //Thanks https://github.com/chuckpreslar/inflect for the UpperCamelCase
10
11 // Split's a string so that it can be converted to a different casing.
12 // Splits on underscores, hyphens, spaces and camel casing.
13 func split(str string) []string {
14         // FIXME: This isn't a perfect solution.
15         // ex. WEiRD CaSINg (Support for 13 year old developers)
16         return strings.Split(regexp.MustCompile(`-|_|([a-z])([A-Z])`).ReplaceAllString(strings.Trim(str, `-|_| `), `$1 $2`), ` `)
17 }
18
19 // UpperCamelCase converts a string to it's upper camel case version.
20 func UpperCamelCase(str string) string {
21         pieces := split(str)
22
23         for index, s := range pieces {
24                 pieces[index] = fmt.Sprintf(`%v%v`, strings.ToUpper(string(s[0])), strings.ToLower(s[1:]))
25         }
26
27         return strings.Join(pieces, ``)
28 }
29
30 func (that *JSONNode) printValue(indentlevel int, indentchar string) {
31         fmt.Printf(" %T ", that.Get())
32 }
33
34 func (that *JSONNode) printMap(indentlevel int, indentchar string) {
35         fmt.Printf(" struct {\n")
36         for key := range that.m {
37                 printfindent(indentlevel+1, indentchar, "%s", UpperCamelCase(key))
38                 that.m[key].print(indentlevel+1, indentchar)
39                 fmt.Printf(" `json:\"%s\"`\n", key)
40         }
41         printfindent(indentlevel, indentchar, "}")
42 }
43
44 func (that *JSONNode) printArray(indentlevel int, indentchar string) {
45         if len(that.a) == 0 {
46                 fmt.Printf(" []interface{} ")
47                 return
48         }
49         fmt.Printf(" [] ")
50         for key := range that.a {
51                 that.a[key].print(indentlevel+1, indentchar)
52                 break
53         }
54 }
55
56 //DebugProspect Print all the data the we ve got on a node and all it s children
57 func (that *JSONNode) print(indentlevel int, indentchar string) {
58         switch that.t {
59         case TypeValue:
60                 that.printValue(indentlevel, indentchar)
61         case TypeMap:
62                 that.printMap(indentlevel, indentchar)
63         case TypeArray:
64                 that.printArray(indentlevel, indentchar)
65         case TypeUndefined:
66                 printfindent(indentlevel, indentchar, "Is of Type: TypeUndefined\n")
67         }
68 }
69
70 //Print Print all the data the we ve got on a node and all it s children as a go struct :) (FOR DEV PURPOSE)
71 func (that *JSONNode) Print() {
72         that.print(0, "\t")
73         fmt.Printf("\n")
74 }