当前位置: 首页>>代码示例>>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;未经允许,请勿转载。