Exemplo n.º 1
0
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
}