当前位置: 首页>>代码示例>>Golang>>正文


Golang data.Term类代码示例

本文整理汇总了Golang中rter/data.Term的典型用法代码示例。如果您正苦于以下问题:Golang Term类的具体用法?Golang Term怎么用?Golang Term使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Term类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: TestReadTerm

func TestReadTerm(t *testing.T) {
	readTerm := new(data.Term)

	testRead(t, "/taxonomy/"+term.Term, readTerm)

	readTerm.UpdateTime = term.UpdateTime

	structJSONCompare(t, term, readTerm)
}
开发者ID:falahhaprak,项目名称:rter,代码行数:9,代码来源:crud_test.go

示例2: TestSelectTerm

func TestSelectTerm(t *testing.T) {
	selectedTerm := new(data.Term)
	selectedTerm.Term = term.Term
	t.Log(selectedTerm.Term)
	err := Select(selectedTerm)

	if err != nil {
		t.Error(err)
	}

	t.Log(term.UpdateTime.UTC())
	t.Log(selectedTerm.UpdateTime.UTC())

	selectedTerm.UpdateTime = term.UpdateTime

	structJSONCompare(t, term, selectedTerm)
}
开发者ID:falahhaprak,项目名称:rter,代码行数:17,代码来源:storage_test.go

示例3: scanTerm

func scanTerm(term *data.Term, rows *sql.Rows) error {
	var updateTimeString string

	cols, err := rows.Columns()

	if err != nil {
		return err
	}

	if len(cols) < 5 {
		err = rows.Scan(
			&term.Term,
			&term.Automated,
			&term.Author,
			&updateTimeString,
		)
	} else {
		err = rows.Scan(
			&term.Term,
			&term.Automated,
			&term.Author,
			&updateTimeString,
			&term.Count,
		)
	}

	if err != nil {
		return err
	}

	// TODO: this is a hacky fix for null times
	if updateTimeString == "0000-00-00 00:00:00" {
		updateTimeString = "0001-01-01 00:00:00"
	}
	updateTime, err := time.Parse("2006-01-02 15:04:05", updateTimeString) // this assumes UTC as timezone

	if err != nil {
		log.Println("Term scanner failed to parse time.")
		return err
	}

	term.UpdateTime = updateTime

	return nil
}
开发者ID:jacobmil990,项目名称:rter,代码行数:45,代码来源:scanners.go

示例4: Delete

// Generic Delete handler
func Delete(w http.ResponseWriter, r *http.Request) {
	user, permissions := auth.Challenge(w, r, true)
	if user == nil || permissions < 1 {
		http.Error(w, "Please Login", http.StatusUnauthorized)
		return
	}

	w.Header().Set("Access-Control-Allow-Origin", "*")
	w.Header().Set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")

	vars := mux.Vars(r)

	var (
		val interface{} // Generic container for the deleted object
		err error
	)

	// Build a URI like representation of the datatype
	types := []string{vars["datatype"]}

	if childtype, ok := vars["childtype"]; ok {
		types = append(types, childtype)
	}

	// Switch based on that URI like representation and instantiate something in the generic container. Also infer the identifier from the vars and perform validation.
	switch strings.Join(types, "/") {
	case "items":
		item := new(data.Item)
		item.ID, err = strconv.ParseInt(vars["key"], 10, 64)

		val = item
	case "items/comments":
		comment := new(data.ItemComment)
		comment.ID, err = strconv.ParseInt(vars["childkey"], 10, 64)

		val = comment
	case "users":
		if vars["key"] != user.Username {
			http.Error(w, "Please don't delete other users", http.StatusUnauthorized)
			return
		}

		user := new(data.User)
		user.Username = vars["key"]

		val = user
	case "roles":
		role := new(data.Role)
		role.Title = vars["key"]

		val = role
	case "taxonomy":
		term := new(data.Term)
		term.Term = vars["key"]

		val = term
	default:
		http.NotFound(w, r)
		return
	}

	if err != nil {
		log.Println(err)
		http.Error(w, "Malformed key in URI", http.StatusBadRequest)
		return
	}

	// Perform the delete
	err = storage.Delete(val)

	if err == storage.ErrZeroAffected {
		http.Error(w, "No matches for query", http.StatusNotFound)
		return
	} else if err != nil {
		log.Println(err)
		http.Error(w, "Delete Database error, likely due to malformed request", http.StatusInternalServerError)
		return
	}

	// Confirm the delete
	w.WriteHeader(http.StatusNoContent)
}
开发者ID:cyc115,项目名称:rter,代码行数:83,代码来源:crud.go

示例5: Read

// Generic Read handler for reading single objects
func Read(w http.ResponseWriter, r *http.Request) {
	//No Auth for the moment

	w.Header().Set("Access-Control-Allow-Origin", "*")
	w.Header().Set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")

	vars := mux.Vars(r)

	var (
		val interface{} // Generic container for the read object
		err error
	)

	// Build a URI like representation of the datatype
	types := []string{vars["datatype"]}

	if childtype, ok := vars["childtype"]; ok {
		types = append(types, childtype)
	}

	// Switch based on that URI like representation and instantiate something in the generic container. Also infer the identifier from the vars and perform validation.
	switch strings.Join(types, "/") {
	case "items":
		item := new(data.Item)
		item.ID, err = strconv.ParseInt(vars["key"], 10, 64)

		val = item
	case "items/comments":
		comment := new(data.ItemComment)
		comment.ID, err = strconv.ParseInt(vars["childkey"], 10, 64)

		val = comment

	case "users":
		user := new(data.User)
		user.Username = vars["key"]

		val = user
	case "users/direction":
		direction := new(data.UserDirection)
		direction.Username = vars["key"]

		val = direction
	case "roles":
		role := new(data.Role)
		role.Title = vars["key"]

		val = role
	case "taxonomy":
		term := new(data.Term)
		term.Term = vars["key"]

		val = term
	case "taxonomy/ranking":
		ranking := new(data.TermRanking)
		ranking.Term = vars["key"]

		val = ranking
	default:
		http.NotFound(w, r)
		return
	}

	if err != nil {
		log.Println(err)
		http.Error(w, "Malformed key in URI", http.StatusBadRequest)
		return
	}

	// Perform the Select
	err = storage.Select(val)

	if err == storage.ErrZeroAffected {
		http.Error(w, "No matches for query", http.StatusNotFound)
		return
	} else if err != nil {
		log.Println(err)
		http.Error(w, "Select Database error, likely due to malformed request", http.StatusInternalServerError)
		return
	}

	// Important. Let's never send salt/hash out.
	switch v := val.(type) {
	case *data.User:
		v.Salt = ""
		v.Password = ""
	}

	w.Header().Set("Content-Type", "application/json") // Header are important when GZIP is enabled

	// Return the object we've selected from the database.
	encoder := json.NewEncoder(w)
	err = encoder.Encode(val)

	if err != nil {
		log.Println(err)
	}
}
开发者ID:cyc115,项目名称:rter,代码行数:99,代码来源:crud.go


注:本文中的rter/data.Term类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。