-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
Testee.Query
to access map easily
- Loading branch information
Showing
6 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
mquery | ||
=== | ||
|
||
```go | ||
import mquery | ||
|
||
var m = map[string]any{ | ||
"foo": "bar", | ||
"hoge": map[string]any{ | ||
"name": "otiai10", | ||
}, | ||
"fuga": map[int]map[string]any{ | ||
0: {"greet": "Hello"}, | ||
1: {"greet": "こんにちは"}, | ||
}, | ||
"langs": []string{"Go", "JavaScript", "English"}, | ||
"baz": nil, | ||
"required": false, | ||
} | ||
|
||
func main() { | ||
fmt.Println( | ||
Query(m, "foo"), // "bar" | ||
Query(m, "hoge.name"), // "otiai10" | ||
Query(m, "fuga.0.greet"), // "Hello" | ||
Query(m, "langs.2"), // "English" | ||
Query(m, "required"), // false | ||
Query(m, "baz.biz"), // nil | ||
) | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package mquery | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
var m = map[string]any{ | ||
"foo": "bar", | ||
"hoge": map[string]any{ | ||
"name": "otiai10", | ||
}, | ||
"fuga": map[int]map[string]any{ | ||
0: {"greet": "Hello"}, | ||
1: {"greet": "こんにちは"}, | ||
}, | ||
"langs": []string{"Go", "JavaScript", "English"}, | ||
"baz": nil, | ||
"required": false, | ||
} | ||
|
||
func TestQuery(t *testing.T) { | ||
// Expect(t, Query(m, "foo")).ToBe("bar") | ||
// Expect(t, Query(m, "hoge.name")).ToBe("otiai10") | ||
// Expect(t, Query(m, "fuga.1.greet")).ToBe("こんにちは") | ||
// Expect(t, Query(m, "langs.0")).ToBe("Go") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package mquery | ||
|
||
var a = map[string]any{ | ||
"foo": "bar", | ||
"hoge": map[string]any{ | ||
"name": "otiai10", | ||
}, | ||
"fuga": map[int]map[string]any{ | ||
0: {"greet": "Hello"}, | ||
1: {"greet": "こんにちは"}, | ||
}, | ||
"langs": []string{"Go", "JavaScript", "English"}, | ||
"baz": nil, | ||
"required": false, | ||
} | ||
|
||
func ExampleQuery() { | ||
// Query(m, "foo") => "bar" | ||
// Query(m, "hoge.name") => "otiai10" | ||
// Query(m, "fuga.1.greet") => "こんにちは" | ||
// Query(m, "langs.0") => "Go" | ||
// Query(m, "baz") => nil | ||
// Query(m, "required") => false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package mquery | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
func Query(m any, q string) any { | ||
return query(m, strings.Split(q, ".")) | ||
} | ||
|
||
func query(m any, qs []string) any { | ||
t := reflect.TypeOf(m) | ||
switch t.Kind() { | ||
case reflect.Map: | ||
return queryMap(m, t, qs) | ||
case reflect.Slice: | ||
return querySlice(m, t, qs) | ||
default: | ||
return m | ||
} | ||
} | ||
|
||
func queryMap(m any, t reflect.Type, qs []string) any { | ||
if len(qs) == 0 { | ||
return m | ||
} | ||
switch t.Key().Kind() { | ||
case reflect.String: | ||
next := reflect.ValueOf(m).MapIndex(reflect.ValueOf(qs[0])).Interface() | ||
return query(next, qs[1:]) | ||
case reflect.Int: | ||
i, err := strconv.Atoi(qs[0]) | ||
if err != nil { | ||
return fmt.Errorf("cannot access map with keyword: %s: %v", qs[0], err) | ||
} | ||
next := reflect.ValueOf(m).MapIndex(reflect.ValueOf(i)).Interface() | ||
return query(next, qs[1:]) | ||
} | ||
return nil | ||
} | ||
|
||
func querySlice(m any, t reflect.Type, qs []string) any { | ||
if len(qs) == 0 { | ||
return m | ||
} | ||
v := reflect.ValueOf(m) | ||
if v.Len() == 0 { | ||
return nil | ||
} | ||
i, err := strconv.Atoi(qs[0]) | ||
if err != nil { | ||
return fmt.Errorf("cannot access slice with keyword: %s: %v", qs[0], err) | ||
} | ||
if v.Len() <= i { | ||
return nil | ||
} | ||
next := v.Index(i).Interface() | ||
return query(next, qs[1:]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters