README.md
1gou - Go Utilities
2===========================
3
4Go Utilities (logging, json)
5
6JsonHelper
7===============
8
9A Go Json Helper, focused on Type coercion, and json path query.
10
11```go
12 package main
13 import . "github.com/araddon/gou"
14 import . "github.com/araddon/gou/goutest"
15 import "testing"
16
17
18 func TestJsonHelper() {
19
20 var jsonData := []byte(`{
21 "name":"aaron",
22 "nullstring":null,
23 "ints":[1,2,3,4],
24 "int":1,
25 "intstr":"1",
26 "int64":1234567890,
27 "MaxSize" : 1048576,
28 "strings":["string1"],
29 "stringscsv":"string1,string2",
30 "nested":{
31 "nest":"string2",
32 "strings":["string1"],
33 "int":2,
34 "list":["value"],
35 "nest2":{
36 "test":"good"
37 }
38 },
39 "nested2":[
40 {"sub":2}
41 ],
42 "period.name":"value"
43 }`
44
45 jh := NewJsonHelper(jsonData)
46
47 // String method
48 Assert(jh.String("name") == "aaron", t, "should get 'aaron' %s", jh.String("name"))
49 // Int Method
50 Assert(jh.Int("int") == 1, t, "get int ")
51 // Selecting items from an array
52 Assert(jh.Int("ints[0]") == 1, t, "get int from array %d", jh.Int("ints[0]"))
53 Assert(jh.Int("ints[2]") == 3, t, "get int from array %d", jh.Int("ints[0]"))
54 // Getting arrays
55 Assert(len(jh.Ints("ints")) == 4, t, "get int array %v", jh.Ints("ints"))
56 // Type coercion to Int64
57 Assert(jh.Int64("int64") == 1234567890, t, "get int")
58 Assert(jh.Int("nested.int") == 2, t, "get int")
59
60 // Path based selection
61 Assert(jh.String("nested.nest") == "string2", t, "should get string %s", jh.String("nested.nest"))
62 Assert(jh.String("nested.nest2.test") == "good", t, "should get string %s", jh.String("nested.nest2.test"))
63 Assert(jh.String("nested.list[0]") == "value", t, "get string from array")
64 Assert(jh.Int("nested2[0].sub") == 2, t, "get int from obj in array %d", jh.Int("nested2[0].sub"))
65
66 // casing?
67 Assert(jh.Int("MaxSize") == 1048576, t, "get int, test capitalization? ")
68 sl := jh.Strings("strings")
69 Assert(len(sl) == 1 && sl[0] == "string1", t, "get strings ")
70 sl = jh.Strings("stringscsv")
71 Assert(len(sl) == 2 && sl[0] == "string1", t, "get strings ")
72
73 // Safe gets
74 i64, ok := jh.Int64Safe("int64")
75 Assert(ok, t, "int64safe ok")
76 Assert(i64 == 1234567890, t, "int64safe value")
77
78 i, ok := jh.IntSafe("int")
79 Assert(ok, t, "intsafe ok")
80 Assert(i == 1, t, "intsafe value")
81
82 l := jh.List("nested2")
83 Assert(len(l) == 1, t, "get list")
84
85 jhm := jh.Helpers("nested2")
86 Assert(len(jhm) == 1, t, "get list of helpers")
87 Assert(jhm[0].Int("sub") == 2, t, "Should get list of helpers")
88
89 // Now lets test xpath type syntax
90 Assert(jh.Int("/MaxSize") == 1048576, t, "get int, test capitalization? ")
91 Assert(jh.String("/nested/nest") == "string2", t, "should get string %s", jh.String("/nested/nest"))
92 Assert(jh.String("/nested/list[0]") == "value", t, "get string from array")
93 // note this one has period in name
94 Assert(jh.String("/period.name") == "value", t, "test period in name ")
95 }
96
97```
98
99
100Logging
101===============
102
103Yet Another Go Logger, configureable logging.
104
105```go
106 package main
107 import "github.com/araddon/gou"
108 import "flag"
109
110 var logLevel *string = flag.String("logging", "debug", "Which log level: [debug,info,warn,error,fatal]")
111
112 func main() {
113
114 flag.Parse()
115 gou.SetupLogging(*logLevel)
116
117 // logging methods
118 gou.Debug("hello", thing, " more ", stuff)
119
120 gou.Error("hello")
121
122 gou.Errorf("hello %v", thing)
123 }
124
125```
126
127License
128===============
129MIT License
130