本文整理汇总了Golang中github.com/jeffail/gabs.Container.Search方法的典型用法代码示例。如果您正苦于以下问题:Golang Container.Search方法的具体用法?Golang Container.Search怎么用?Golang Container.Search使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/jeffail/gabs.Container
的用法示例。
在下文中一共展示了Container.Search方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: testGetMultStatementOfGroup
func testGetMultStatementOfGroup(t *testing.T, group *gabs.Container) {
db := initDatabase(t)
defer db.Close()
mart := initHandler(db)
ids := []string{
uuid.NewV4().String(),
uuid.NewV4().String(),
uuid.NewV4().String(),
uuid.NewV4().String(),
}
stmt, err := gabs.ParseJSON([]byte(statetmentAgentBoilerplate))
fatalIfError(t, err)
stmtSlice := []*gabs.Container{
appendAccount(t, stmt),
appendMbox(t, stmt),
appendMboxSHA1(t, stmt),
appendOpenID(t, stmt),
}
for i := 0; i < len(ids); i++ {
putStatement(t, mart, stmtSlice[i].String(), ids[i])
var s interface{}
err = json.Unmarshal([]byte(stmtSlice[i].Search("actor").String()), &s)
fatalIfError(t, err)
err = group.ArrayAppendP(s, "actor.member")
fatalIfError(t, err)
}
// construct query
v := &url.Values{}
//t.Log(group.Search("actor").String())
v.Add("agent", group.Search("actor").String())
resp := getStatement(t, mart, v)
//t.Log(string(resp))
respstmt, err := gabs.ParseJSON(resp)
fatalIfError(t, err)
cnt, err := respstmt.ArrayCount("statements")
fatalIfError(t, err)
if cnt != len(ids) {
t.Fatalf("Expected %d statements in response; got %d", len(ids), cnt)
}
children, err := respstmt.S("statements").Children()
fatalIfError(t, err)
for idx, stm := range children {
if id, ok := stm.Search("id").Data().(string); !ok || id != ids[idx] {
t.Fatalf("Got invalid order of statement array")
}
}
}
示例2: testGetMultStatementOfActor
func testGetMultStatementOfActor(t *testing.T, stmt *gabs.Container) {
db := initDatabase(t)
defer db.Close()
mart := initHandler(db)
id1 := uuid.NewV4().String()
id2 := uuid.NewV4().String()
putStatement(t, mart, stmt.String(), id1)
putStatement(t, mart, stmt.String(), id2)
// construct query
v := &url.Values{}
v.Add("agent", stmt.Search("actor").String())
respstmt, err := gabs.ParseJSON(getStatement(t, mart, v))
fatalIfError(t, err)
cnt, err := respstmt.ArrayCount("statements")
fatalIfError(t, err)
if cnt != 2 {
t.Fatalf("Expected 2 statements in response; got %d", cnt)
}
s0, err := respstmt.ArrayElement(0, "statements")
fatalIfError(t, err)
if id, ok := s0.Search("id").Data().(string); !ok || id != id1 {
t.Fatalf("Got invalid order of statement array")
}
s1, err := respstmt.ArrayElement(1, "statements")
fatalIfError(t, err)
if id, ok := s1.Search("id").Data().(string); !ok || id != id2 {
t.Fatalf("Got invalid order of statement array")
}
}
示例3: search
func search(data *gabs.Container, filter tFilter, i int) (*gabs.Container, string) {
var ret string //returned value
filter.Path = filter.Path[i:]
//fmt.Println("I:::", i, filter.Path, reflect.TypeOf(data.Data()).String())
//fmt.Println(data)
// fmt.Println(data.Search(filter.Key).String())
if len(filter.Value) > 0 {
if data.Search(filter.Key).Data() == filter.Value {
ret = data.Search(filter.Ret).Data().(string)
if len(filter.Match) > 0 {
if regexp.MustCompile(filter.Match).MatchString(ret) {
if len(filter.Replace) > 0 {
ret = regexp.MustCompile(filter.Replace).ReplaceAllString(ret, "")
}
return data, ret
} else {
ret = ""
}
} else {
if len(filter.Replace) > 0 {
ret = regexp.MustCompile(filter.Replace).ReplaceAllString(ret, "")
}
return data, ret
}
}
}
for j, str := range filter.Path {
if data.Data() != nil {
if reflect.TypeOf(data.Data()).String() == "[]interface {}" {
children, err := data.Children()
if err != nil {
fmt.Println(err.Error())
}
for _, child := range children {
data, ret = search(child, filter, j)
if len(ret) > 0 {
return data, ret
}
}
} else {
data = data.Search(str)
}
}
// fmt.Println(str, data)
}
return data, ret
}