File: README.md

package info (click to toggle)
golang-github-araddon-gou 0.0~git20180509.7db4be5-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 160 kB
  • sloc: makefile: 2
file content (129 lines) | stat: -rw-r--r-- 3,408 bytes parent folder | download | duplicates (2)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
gou - Go Utilities
===========================

Go Utilities (logging, json)

JsonHelper
===============

A Go Json Helper, focused on Type coercion, and json path query.

```go
	package main
	import . "github.com/araddon/gou"
	import . "github.com/araddon/gou/goutest"
	import "testing"


	func TestJsonHelper() {

		var jsonData := []byte(`{
			"name":"aaron",
			"nullstring":null,
			"ints":[1,2,3,4],
			"int":1,
			"intstr":"1",
			"int64":1234567890,
			"MaxSize" : 1048576,
			"strings":["string1"],
			"stringscsv":"string1,string2",
			"nested":{
				"nest":"string2",
				"strings":["string1"],
				"int":2,
				"list":["value"],
				"nest2":{
					"test":"good"
				}
			},
			"nested2":[
				{"sub":2}
			],
			"period.name":"value"
		}`

		jh := NewJsonHelper(jsonData)

		// String method
		Assert(jh.String("name") == "aaron", t, "should get 'aaron' %s", jh.String("name"))
		// Int Method
		Assert(jh.Int("int") == 1, t, "get int ")
		// Selecting items from an array
		Assert(jh.Int("ints[0]") == 1, t, "get int from array %d", jh.Int("ints[0]"))
		Assert(jh.Int("ints[2]") == 3, t, "get int from array %d", jh.Int("ints[0]"))
		// Getting arrays
		Assert(len(jh.Ints("ints")) == 4, t, "get int array %v", jh.Ints("ints"))
		// Type coercion to Int64
		Assert(jh.Int64("int64") == 1234567890, t, "get int")
		Assert(jh.Int("nested.int") == 2, t, "get int")

		// Path based selection
		Assert(jh.String("nested.nest") == "string2", t, "should get string %s", jh.String("nested.nest"))
		Assert(jh.String("nested.nest2.test") == "good", t, "should get string %s", jh.String("nested.nest2.test"))
		Assert(jh.String("nested.list[0]") == "value", t, "get string from array")
		Assert(jh.Int("nested2[0].sub") == 2, t, "get int from obj in array %d", jh.Int("nested2[0].sub"))

		// casing?
		Assert(jh.Int("MaxSize") == 1048576, t, "get int, test capitalization? ")
		sl := jh.Strings("strings")
		Assert(len(sl) == 1 && sl[0] == "string1", t, "get strings ")
		sl = jh.Strings("stringscsv")
		Assert(len(sl) == 2 && sl[0] == "string1", t, "get strings ")

		// Safe gets
		i64, ok := jh.Int64Safe("int64")
		Assert(ok, t, "int64safe ok")
		Assert(i64 == 1234567890, t, "int64safe value")

		i, ok := jh.IntSafe("int")
		Assert(ok, t, "intsafe ok")
		Assert(i == 1, t, "intsafe value")

		l := jh.List("nested2")
		Assert(len(l) == 1, t, "get list")

		jhm := jh.Helpers("nested2")
		Assert(len(jhm) == 1, t, "get list of helpers")
		Assert(jhm[0].Int("sub") == 2, t, "Should get list of helpers")

		// Now lets test xpath type syntax
		Assert(jh.Int("/MaxSize") == 1048576, t, "get int, test capitalization? ")
		Assert(jh.String("/nested/nest") == "string2", t, "should get string %s", jh.String("/nested/nest"))
		Assert(jh.String("/nested/list[0]") == "value", t, "get string from array")
		// note this one has period in name
		Assert(jh.String("/period.name") == "value", t, "test period in name ")
	}

```

	
Logging
===============

Yet Another Go Logger, configureable logging.

```go
	package main
	import "github.com/araddon/gou"
	import "flag"

	var logLevel *string = flag.String("logging", "debug", "Which log level: [debug,info,warn,error,fatal]")

	func main() {

		flag.Parse()
		gou.SetupLogging(*logLevel)

		// logging methods
		gou.Debug("hello", thing, " more ", stuff)

		gou.Error("hello")

		gou.Errorf("hello %v", thing)
	}

```

License
===============
MIT License