當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Request.ToCSV方法代碼示例

本文整理匯總了Golang中net/http.Request.ToCSV方法的典型用法代碼示例。如果您正苦於以下問題:Golang Request.ToCSV方法的具體用法?Golang Request.ToCSV怎麽用?Golang Request.ToCSV使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在net/http.Request的用法示例。


在下文中一共展示了Request.ToCSV方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: reportWriter

func reportWriter(c appengine.Context, w http.ResponseWriter, r *http.Request, s, e time.Time, opt ReportOptions, rep string, format string) {
	var rows []ReportRow
	var meta ReportMetadata
	var err error
	switch rep {
	case "classb":
		rows, meta, err = classbReport(c, s, e, opt)
	case "adsbclassb":
		rows, meta, err = adsbClassbReport(c, s, e, opt)
	case "discrep":
		rows, meta, err = discrepReport(c, s, e, opt)
	case "serfr1":
		rows, meta, err = serfr1Report(c, s, e, opt)
	case "brixx1":
		rows, meta, err = brixx1Report(c, s, e, opt)
	case "serfr1complaints":
		rows, meta, err = serfr1ComplaintsReport(c, s, e, opt)
	case "skimmer":
		rows, meta, err = skimmerReport(c, s, e, opt)
	case "brixxviolations":
		rows, meta, err = brixxViolationReport(c, s, e, opt)
	case "serfr1at":
		rows, meta, err = serfr1AtReport(c, s, e, opt)
	}
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	// Should do something better for this
	filename := date.NowInPdt().AddDate(0, 0, -1).Format(rep + "-20060102.csv")
	outFunc := func(csvWriter *csv.Writer) error {
		csvWriter.Write(rows[0].ToCSVHeaders())
		for _, r := range rows {
			if err := csvWriter.Write(r.ToCSV()); err != nil {
				return err
			}
		}
		csvWriter.Flush()
		return nil
	}

	if format == "csv" {
		w.Header().Set("Content-Type", "application/csv")
		w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", filename))
		csvWriter := csv.NewWriter(w)
		if err := outFunc(csvWriter); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

	} else if format == "gcs" {
		newCtx := newappengine.NewContext(r)
		handle, err := gcs.OpenRW(newCtx, "serfr0-reports", filename, "text/plain") //?"application/csv")
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		csvWriter := csv.NewWriter(handle.IOWriter())
		if err := outFunc(csvWriter); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		if err := handle.Close(); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		w.Header().Set("Content-Type", "text/plain")
		w.Write([]byte(fmt.Sprintf("OK!\nGCS file '%s' written to bucket", filename)))

	} else {
		var params = map[string]interface{}{
			"Start":    s,
			"End":      e,
			"Metadata": meta,
			"Options":  opt,
		}

		// Is there not a more elegant way to do this kind of thing ?
		switch rep {
		case "classb":
			out := []CBRow{}
			for _, r := range rows {
				out = append(out, r.(CBRow))
			}
			params["Rows"] = out
		case "adsbclassb":
			out := []ACBRow{}
			for _, r := range rows {
				out = append(out, r.(ACBRow))
			}
			params["Rows"] = out
		case "serfr1", "brixx1", "discrep":
			out := []SERFR1Row{}
			for _, r := range rows {
				out = append(out, r.(SERFR1Row))
			}
			params["Rows"] = out
			rep = "serfr1"
		case "serfr1complaints":
//.........這裏部分代碼省略.........
開發者ID:hugoh,項目名稱:complaints,代碼行數:101,代碼來源:reports.go


注:本文中的net/http.Request.ToCSV方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。