Skip to content

Commit 0c928f9

Browse files
committed
Initial commit
1 parent a52dc83 commit 0c928f9

File tree

5 files changed

+212
-3
lines changed

5 files changed

+212
-3
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,8 @@
1010

1111
# Output of the go coverage tool, specifically when used with LiteIDE
1212
*.out
13+
14+
# editors
15+
.idea
16+
.DS_Store
17+
.vscode

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2019 Go Lang | REST Services
3+
Copyright (c) 2019 Roshan Gade
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,39 @@
1-
# parser
2-
Use to parse data, as well as read file and parse data. Commonly use for config file read, etc
1+
# Data parser
2+
Use to parse data. As well as, it reads file and parse data. Commonly use to read config file, messages etc in application.
3+
4+
## JSON
5+
6+
### How to use?
7+
- Can load data in bytes using .Load(data)
8+
- Can load data from file using .LoadFile(path)
9+
````
10+
var config parser.JSON
11+
12+
_ = config.LoadFile("examples/config.json")
13+
````
14+
15+
#### Available methods
16+
- Get
17+
- GetString
18+
- GetInt
19+
- GetFloat
20+
- GetBool
21+
- GetTime
22+
- GetDuration
23+
24+
Can get data from inner object/array too. Use dot(.) to join keys.
25+
26+
#### Example
27+
````
28+
// JSON
29+
{
30+
"test": 1,
31+
"outer": [{
32+
"inner": "Hello World",
33+
"bool": false
34+
}]
35+
}
36+
37+
// Get
38+
config.GetString("outer.0.inner")
39+
````

json.go

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/*!
2+
* go-rs/parser
3+
* Copyright(c) 2019 Roshan Gade
4+
* MIT Licensed
5+
*/
6+
package parser
7+
8+
import (
9+
"encoding/json"
10+
"errors"
11+
"io/ioutil"
12+
"path/filepath"
13+
"strconv"
14+
"strings"
15+
"time"
16+
)
17+
18+
type JSON struct {
19+
data interface{}
20+
}
21+
22+
var (
23+
invalidJSON = errors.New("JSON is invalid")
24+
)
25+
26+
// Prefer absolute filepath
27+
func (c *JSON) Load(data []byte) (err error) {
28+
29+
if !json.Valid(data) {
30+
err = invalidJSON
31+
return
32+
}
33+
34+
err = json.Unmarshal(data, &c.data)
35+
if err != nil {
36+
return
37+
}
38+
39+
return
40+
}
41+
42+
// Prefer absolute filepath
43+
func (c *JSON) LoadFile(path string) (err error) {
44+
path, err = filepath.Abs(path)
45+
if err != nil {
46+
return
47+
}
48+
49+
data, err := ioutil.ReadFile(path)
50+
if err != nil {
51+
return
52+
}
53+
54+
return c.Load(data)
55+
56+
}
57+
58+
func typeIdentifier(data interface{}) (arr []interface{}, obj map[string]interface{}) {
59+
switch data.(interface{}).(type) {
60+
case []interface{}:
61+
arr = data.([]interface{})
62+
case map[string]interface{}:
63+
obj = data.(map[string]interface{})
64+
}
65+
return
66+
}
67+
68+
func (c *JSON) Get(key string) (val interface{}, exists bool) {
69+
keys := strings.Split(key, ".")
70+
lastIndex := int64(len(keys) - 1)
71+
72+
var arr []interface{}
73+
var obj map[string]interface{}
74+
75+
if c.data == nil {
76+
return
77+
}
78+
arr, obj = typeIdentifier(c.data)
79+
80+
for _, v := range keys[:lastIndex] {
81+
if arr == nil && obj == nil {
82+
break
83+
}
84+
85+
if arr != nil {
86+
i, err := strconv.ParseInt(v, 10, 64)
87+
if err != nil {
88+
return
89+
}
90+
if i < 0 || i >= int64(len(arr)) {
91+
return
92+
}
93+
arr, obj = typeIdentifier(arr[i])
94+
continue
95+
}
96+
97+
if obj != nil {
98+
arr, obj = typeIdentifier(obj[v])
99+
continue
100+
}
101+
}
102+
103+
if arr != nil {
104+
i, err := strconv.ParseInt(keys[lastIndex], 10, 64)
105+
if err != nil {
106+
return
107+
}
108+
if i < 0 || i >= int64(len(arr)) {
109+
return
110+
}
111+
val = arr[i]
112+
} else if obj != nil {
113+
val = obj[keys[lastIndex]]
114+
}
115+
116+
exists = val != nil
117+
return
118+
}
119+
120+
func (c *JSON) GetString(key string) (s string) {
121+
val, ok := c.Get(key)
122+
if ok {
123+
s, _ = val.(string)
124+
}
125+
return
126+
}
127+
128+
func (c *JSON) GetInt(key string) (i int) {
129+
val, ok := c.Get(key)
130+
if ok {
131+
i, _ = val.(int)
132+
}
133+
return
134+
}
135+
136+
func (c *JSON) GetFloat(key string) (f float64) {
137+
val, ok := c.Get(key)
138+
if ok {
139+
f, _ = val.(float64)
140+
}
141+
return
142+
}
143+
144+
func (c *JSON) GetBool(key string) (b bool) {
145+
val, ok := c.Get(key)
146+
if ok {
147+
b = val.(bool)
148+
}
149+
return
150+
}
151+
152+
func (c *JSON) GetTime(key string) (t time.Time) {
153+
val, ok := c.Get(key)
154+
if ok {
155+
t, _ = val.(time.Time)
156+
}
157+
return
158+
}
159+
160+
func (c *JSON) GetDuration(key string) (d time.Duration) {
161+
val, ok := c.Get(key)
162+
if ok {
163+
d, _ = val.(time.Duration)
164+
}
165+
return
166+
}

version.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.0.1-beta.1

0 commit comments

Comments
 (0)