Skip to content

Commit 36d0568

Browse files
committed
Update README.md
1 parent 8101d39 commit 36d0568

File tree

1 file changed

+28
-144
lines changed

1 file changed

+28
-144
lines changed

README.md

Lines changed: 28 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -1,163 +1,47 @@
1-
# go-ognl
1+
# go_ognl
22

33
关键字符`.``#`,遇到都会截断到下一层(`#`和特殊,会将当前层展开,例如一个struct,会将所有字段对应的值展开)
44

55
---
6-
1. 直接使用 `go_ognl.Get(obj,path)` 获取需要的值,返回对象提供 `Effective()` 判断是否有有效值, `Value()`返回解析后的对象,如果使用`#`将会返回`[]interface{}{}`,`Values()`则直接回返回一个`[]interface{}{}`,同时也提供`.Get(path)`链式调用;
7-
2. 使用`go_ognl.Parse(obj)` 保存为临时,可以在此基础上多次调用 `.Get(path)`
86

7+
1. 直接使用 `go_ognl.Get(obj,path)` 获取需要的值,返回对象提供 `Effective()` 判断是否有有效值, `Value()`
8+
返回解析后的对象,如果使用`#`将会返回`[]interface{}{}`,`Values()`则直接回返回一个`[]interface{}{}`
9+
,同时也提供`.Get(path)`链式调用;
10+
2. 使用`go_ognl.Parse(obj)` 保存为临时,可以在此基础上多次调用 `.Get(path)`
911

12+
## download
13+
```shell
14+
go get github.com/songzhibin97/go-ognl
15+
```
1016

1117
## demo
18+
1219
```go
13-
package go_ognl
20+
package main
1421

1522
import (
16-
"github.com/stretchr/testify/assert"
17-
"testing"
23+
"fmt"
24+
"github.com/songzhibin97/go-ognl"
1825
)
1926

20-
type Mock struct {
21-
Name string
22-
Age int
23-
Hash1 map[string]interface{}
24-
Hash2 map[int]interface{}
25-
List []*Mock
26-
Array [3]*Mock
27-
lName string
28-
lAge int
29-
lHash1 map[string]interface{}
30-
lHash2 map[int]interface{}
31-
lList []*Mock
32-
lArray [3]*Mock
27+
type Test struct {
28+
First string
29+
Middle *Test
30+
last int
3331
}
3432

35-
func TestGet(t *testing.T) {
36-
var (
37-
t2 = &Mock{
38-
Name: "t2",
39-
Age: 2,
40-
}
41-
hash1 = map[string]interface{}{
42-
"string1": "string",
43-
"int1": 1,
44-
"t2": t2,
45-
}
46-
t3 = &Mock{
47-
Name: "t3",
48-
Age: 3,
49-
}
50-
t4 = &Mock{
51-
Name: "t4",
52-
Age: 4,
53-
Hash1: map[string]interface{}{},
54-
}
55-
hash2 = map[int]interface{}{
56-
2: t2,
57-
3: t3,
58-
4: t4,
59-
}
60-
list = []*Mock{t2, t3, t4}
61-
array = [3]*Mock{t2, t3, t4}
62-
t1 = &Mock{
63-
Name: "t1",
64-
lName: "lt1",
65-
Age: 1,
66-
lAge: 11,
67-
Hash1: hash1,
68-
lHash1: hash1,
69-
Hash2: hash2,
70-
lHash2: hash2,
71-
List: list,
72-
lList: list,
73-
Array: array,
74-
lArray: array,
75-
}
76-
)
77-
hash1["t1"] = t1
78-
test := []struct {
79-
query string
80-
value interface{}
81-
effective bool
82-
}{
83-
{"Name", "t1", true},
84-
{"Age", 1, true},
85-
{"Hash1.string1", "string", true},
86-
{"Hash1.int1", 1, true},
87-
{"Hash1.t2.Name", "t2", true},
88-
{"Hash2.2.Name", "t2", true},
89-
{"List.1.Name", "t3", true},
90-
{"List.0.Name", "t2", true},
91-
{"Array.0.Name", "t2", true},
92-
{"hash2.0", nil, false},
93-
{"Hash1.t1.Hash1.t1.Hash1.t1.Name", "t1", true},
94-
{"lName", "lt1", true},
95-
{"lAge", 11, true},
96-
{"lHash1.string1", "string", true},
97-
{"lHash1.int1", 1, true},
98-
{"lHash1.t2.Name", "t2", true},
99-
{"lHash2.2.Name", "t2", true},
100-
{"lList.1.Name", "t3", true},
101-
{"lList.0.Name", "t2", true},
102-
{"lArray.0.Name", "t2", true},
103-
{"lhash2.0", nil, false},
104-
{"lHash1.t1.Hash1.t1.lHash1.t1.Name", "t1", true},
105-
106-
{"#", []interface{}{"t1", 1, hash1, hash2, list, array, "lt1", 11, hash1, hash2, list, array}, true},
107-
{"#string1#", []interface{}{"string", "string"}, true},
108-
{"List#1.Name", []interface{}{"t3", "t3", "t3", "t3"}, true},
109-
}
110-
for index, v := range test {
111-
vv := Get(t1, v.query)
112-
if !assert.Equal(t, v.effective, vv.Effective()) {
113-
t.Errorf("effective fault expected:%t, got:%t", v.effective, vv.Effective())
114-
return
115-
}
116-
if !assert.Equal(t, v.value, vv.Value()) {
117-
t.Errorf("no equal index:%d, query:%s, expected:%v, got:%v", index, v.query, v.value, vv.Value())
118-
}
119-
}
120-
121-
p := Parse(t1)
122-
for index, v := range test {
123-
vv := p.Get(v.query)
124-
if !assert.Equal(t, v.effective, vv.Effective()) {
125-
t.Errorf("effective fault expected:%t, got:%t", v.effective, vv.Effective())
126-
return
127-
}
128-
if !assert.Equal(t, v.value, vv.Value()) {
129-
t.Errorf("no equal index:%d, query:%s, expected:%v, got:%v", index, v.query, v.value, vv.Value())
130-
}
131-
}
132-
133-
test = []struct {
134-
query string
135-
value interface{}
136-
effective bool
137-
}{
138-
{"0", t2, true},
139-
{"1", t3, true},
140-
{"2", t4, true},
141-
{"3", nil, false},
142-
{"0.Name", "t2", true},
143-
{"0.Age", 2, true},
144-
{"1.Name", "t3", true},
145-
{"1.Age", 3, true},
146-
{"2.Name", "t4", true},
147-
{"2.Age", 4, true},
148-
{"#", []interface{}{t2, t3, t4}, true},
149-
}
150-
g1 := Get(t1, "List")
151-
for index, v := range test {
152-
vv := g1.Get(v.query)
153-
if !assert.Equal(t, v.effective, vv.Effective()) {
154-
t.Errorf("effective fault expected:%t, got:%t", v.effective, vv.Effective())
155-
return
156-
}
157-
if !assert.Equal(t, v.value, vv.Value()) {
158-
t.Errorf("no equal index:%d, query:%s, expected:%v, got:%v", index, v.query, v.value, vv.Value())
159-
}
33+
func main() {
34+
t1 := &Test{
35+
First: "first",
36+
last: 7,
16037
}
38+
t1.Middle = t1
39+
fmt.Println(ognl.Get(t1, "First").Value()) // first
40+
fmt.Println(ognl.Get(t1, "last").Value()) // 7
41+
fmt.Println(ognl.Get(t1, "Middle.Middle.Middle.last").Value()) // 7
42+
fmt.Println(ognl.Get(t1, "#").Value()) // []interface{}{"first",t1,7}
43+
fmt.Println(ognl.Get(t1, "##").Value()) // []interface{}{"first","first",t1,7,7}
44+
fmt.Println(ognl.Get(t1, "##").Values()) // []interface{}{"first","first",t1,7,7}
16145
}
16246

16347
```

0 commit comments

Comments
 (0)