コード例 #1
0
ファイル: writer.go プロジェクト: imc-trading/peekaboo
func writeJSON(w http.ResponseWriter, r *http.Request, data interface{}, cache interface{}) {
	if strings.ToLower(r.URL.Query().Get("envelope")) == "true" {
		e := map[string]interface{}{
			"status": http.StatusOK,
			"data":   data,
			"error":  []string{},
			"cache":  cache,
		}

		filter := r.URL.Query().Get("filter")
		if filter != "" {
			d, err := dquery.Filter(filter, e)
			if err != nil {
				writeJSONError(w, r, nil, err.Error(), http.StatusBadRequest)
				return
			}

			w.Header().Set("Content-Type", "application/json; charset=utf-8")
			w.Header().Set("Access-Control-Allow-Origin", "*")
			w.WriteHeader(http.StatusOK)
			writeMIME(w, r, d)
			return
		}

		w.Header().Set("Content-Type", "application/json; charset=utf-8")
		w.Header().Set("Access-Control-Allow-Origin", "*")
		w.WriteHeader(http.StatusOK)
		writeMIME(w, r, e)
	} else {

		filter := r.URL.Query().Get("filter")
		if filter != "" {
			d, err := dquery.Filter(filter, data)
			if err != nil {
				writeJSONError(w, r, nil, err.Error(), http.StatusBadRequest)
				return
			}

			w.Header().Set("Content-Type", "application/json; charset=utf-8")
			w.Header().Set("Access-Control-Allow-Origin", "*")
			w.WriteHeader(http.StatusOK)
			writeMIME(w, r, d)
			return
		}

		w.Header().Set("Content-Type", "application/json; charset=utf-8")
		w.Header().Set("Access-Control-Allow-Origin", "*")
		w.WriteHeader(http.StatusOK)
		writeMIME(w, r, data)
	}
}
コード例 #2
0
ファイル: test.go プロジェクト: imc-trading/peekaboo
func main() {
	d := map[string]interface{}{
		"Cats": []string{
			"Leopard",
			"Lion",
			"Puma",
		},
		"Creatures": map[string]interface{}{
			"Evil": []string{
				"Dracula",
				"Dragon",
			},
			"Good": []string{
				"Unicorns",
				"Cats",
			},
		},
	}

	/*
		j, err := dquery.FilterJSON(".Cats", d)
		if err != nil {
			log.Fatal(err)
		}
		fmt.Println(string(j))
	*/
	r2, err2 := dquery.Filter(".", d)
	if err2 != nil {
		log.Fatal(err2)
	}
	fmt.Println(r2)
	/*
		r4, err4 := dquery.FilterJSON(".", d)
		if err4 != nil {
			log.Fatal(err4)
		}
		fmt.Println(string(r4))

		r3, err3 := dquery.FilterJSON(".Cats.[5]", d)
		if err3 != nil {
			log.Fatal(err3)
		}
		fmt.Println(string(r3))
	*/
}