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


Golang encoder.Encoder類代碼示例

本文整理匯總了Golang中github.com/martini-contrib/encoder.Encoder的典型用法代碼示例。如果您正苦於以下問題:Golang Encoder類的具體用法?Golang Encoder怎麽用?Golang Encoder使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


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

示例1: CreateGoal

func CreateGoal(enc encoder.Encoder, goal Goal) (int, []byte) {
	goalId, err := uuid.NewV4()
	if err != nil {
		return 500, encoder.Must(enc.Encode(""))
	}
	goal.Id = goalId.String()
	if goal.TargetType == "user" {
		user, ok := users[goal.Target]
		if !ok {
			return 400, encoder.Must(enc.Encode("Target does not exist"))
		}
		user.Goals[goal.Id] = &goal
	} else if goal.TargetType == "team" {
		team, ok := teams[goal.Target]
		if !ok {
			return 400, encoder.Must(enc.Encode("Target does not exist"))
		}
		team.Goals[goal.Id] = &goal
	} else {
		return 400, encoder.Must(enc.Encode("Unknown target type"))
	}

	goals[goal.Id] = &goal
	return 201, encoder.Must(enc.Encode(goal.Id))
}
開發者ID:FP7Collage,項目名稱:GoalSetting,代碼行數:25,代碼來源:goals.go

示例2: GetGoal

func GetGoal(enc encoder.Encoder, params martini.Params) (int, []byte) {
	goal, ok := goals[params["id"]]
	if ok {
		return 200, encoder.Must(enc.Encode(goal))
	}
	return 404, encoder.Must(enc.Encode())
}
開發者ID:FP7Collage,項目名稱:GoalSetting,代碼行數:7,代碼來源:goals.go

示例3: GetUsers

func GetUsers(dataStore DataStore, enc encoder.Encoder) (int, []byte) {
	users, err := dataStore.GetAllUsers()
	if err != nil {
		return http.StatusNoContent, encoder.Must(enc.Encode(NewError(ErrInternal, "Can't get users try again later")))
	}
	return http.StatusOK, encoder.Must(enc.Encode(users))
}
開發者ID:Blagomir,項目名稱:WhoIsInTheLab,代碼行數:7,代碼來源:registration.go

示例4: FetchStatus

func FetchStatus(s *Status, ms *mgo.Session, enc encoder.Encoder, r *http.Request) (int, []byte) {
	s.Log["MongoDB"] = NewLog(true, "")
	if err := ms.Ping(); err != nil {
		s.Log["MongoDB"] = NewLog(false, err.Error())
	}
	return http.StatusOK, encoder.Must(enc.Encode(s))
}
開發者ID:ranveerkunal,項目名稱:admin,代碼行數:7,代碼來源:admin.go

示例5: GetMac

func GetMac(res http.ResponseWriter, req *http.Request, enc encoder.Encoder) (int, []byte) {
	ip := strings.Split(req.RemoteAddr, ":")[0]
	mac, err := GetMacAddress("/proc/net/arp", ip)
	if err != nil {
		return http.StatusNotFound, encoder.Must(enc.Encode(NewError(ErrMacNotFound, err.Error())))
	}
	return http.StatusOK, encoder.Must(enc.Encode(map[string]string{"mac": mac}))
}
開發者ID:Blagomir,項目名稱:WhoIsInTheLab,代碼行數:8,代碼來源:registration.go

示例6: listLocations

func listLocations(enc encoder.Encoder, r *http.Request, db *mgo.Database) (int, []byte) {
	var locations []Location

	c := db.C("locations")
	err := c.Find(nil).All(&locations)
	if err != nil {
		return http.StatusInternalServerError, []byte("Impossible to retrieve the locations: " + err.Error())
	}

	return http.StatusOK, encoder.Must(enc.Encode(locations))
}
開發者ID:K-Phoen,項目名稱:BeThere,代碼行數:11,代碼來源:locations_api.go

示例7: GetServer

func GetServer(enc encoder.Encoder, db database.DB, parms martini.Params) (int, []byte) {
	fmt.Printf("begin get server\n")
	id, err := strconv.ParseInt(parms["id"], 10, 64)
	al := db.Get(id)
	if err != nil || al == nil {
		msg := fmt.Sprintf("the album with id %s does not exist", parms["id"])
		return http.StatusNotFound, encoder.Must(enc.Encode(
			NewError{errcode: 404, errmsg: msg}))
	}
	return http.StatusOK, encoder.Must(enc.Encode(al))
}
開發者ID:yc850k,項目名稱:haha,代碼行數:11,代碼來源:server.go

示例8: FindServer

func FindServer(enc encoder.Encoder, db database.DB, r *http.Request) (int, []byte) {
	fmt.Printf("begin find server\n")
	params := r.URL.Query()
	roomId, err := strconv.ParseInt(params.Get("room_id"), 10, 64)
	al := db.Find(roomId)
	if err != nil || al == nil {
		msg := fmt.Sprintf("server witch room_id %s does not exist", params.Get("id"))
		return http.StatusNotFound, encoder.Must(enc.Encode(
			NewError{errcode: 404, errmsg: msg}))
	}
	return http.StatusOK, encoder.Must(enc.Encode(al))
}
開發者ID:yc850k,項目名稱:haha,代碼行數:12,代碼來源:server.go

示例9: getLocation

func getLocation(params martini.Params, enc encoder.Encoder, db *mgo.Database) (int, []byte) {
	var location Location

	if !bson.IsObjectIdHex(params["id"]) {
		return http.StatusNotFound, []byte("Location with id \"" + params["id"] + "\" not found")
	}

	c := db.C("locations")
	err := c.FindId(bson.ObjectIdHex(params["id"])).One(&location)
	if err != nil {
		return http.StatusNotFound, []byte("Location with id \"" + params["id"] + "\" not found")
	}

	return http.StatusOK, encoder.Must(enc.Encode(location))
}
開發者ID:K-Phoen,項目名稱:BeThere,代碼行數:15,代碼來源:locations_api.go

示例10: CompleteGoal

func CompleteGoal(enc encoder.Encoder, params martini.Params, jsonParams GoalCompleteParams) (int, []byte) {
	goal, ok := goals[params["id"]]
	if !ok {
		return 404, encoder.Must(enc.Encode())
	}
	if _, ok := users[jsonParams.UserId]; !ok {
		return 400, encoder.Must(enc.Encode("User does not exist"))
	}
	goal.NumberOfCompletions--
	if goal.NumberOfCompletions > 0 {
		goal.State = "progress"
		return 200, encoder.Must(enc.Encode(goal.State)) //what should be returned here?
	}

	goal.State = "completed"
	return 200, encoder.Must(enc.Encode(goal.Reward))
}
開發者ID:FP7Collage,項目名稱:GoalSetting,代碼行數:17,代碼來源:goals.go

示例11: createLocation

func createLocation(newLocation NewLocationForm, enc encoder.Encoder, db *mgo.Database) (int, []byte) {
	// transform the form struct into a real Location
	location := Location{
		ID: bson.NewObjectId(),
		Coordinates: Coordinates{
			Lat:  newLocation.Lat,
			Long: newLocation.Long,
		},
		Name:        newLocation.Name,
		Description: newLocation.Description,
		Visited:     newLocation.Visited,
		CreatedAt:   time.Now(),
	}

	// and try to insert it
	c := db.C("locations")
	err := c.Insert(&location)

	if err != nil {
		return http.StatusInternalServerError, []byte("Impossible to insert the location: " + err.Error())
	}

	return http.StatusCreated, encoder.Must(enc.Encode(location))
}
開發者ID:K-Phoen,項目名稱:BeThere,代碼行數:24,代碼來源:locations_api.go

示例12: GetUser

func GetUser(dataStore DataStore, enc encoder.Encoder, params martini.Params) (int, []byte) {
	id, err := strconv.Atoi(params["id"])
	if err != nil {
		return http.StatusNotFound, encoder.Must(enc.Encode(NewError(ErrInternal, "Invalid id")))
	}

	user, dbErr := dataStore.GetUser(id)
	if dbErr != nil {
		return http.StatusNotFound, encoder.Must(enc.Encode(NewError(ErrUserNotFound, dbErr.Error())))
	}
	return http.StatusOK, encoder.Must(enc.Encode(user))
}
開發者ID:Blagomir,項目名稱:WhoIsInTheLab,代碼行數:12,代碼來源:registration.go

示例13: CreatePreset

func CreatePreset(
	params martini.Params, w http.ResponseWriter,
	r *http.Request, enc encoder.Encoder) (int, []byte) {
	bytes, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return http.StatusBadRequest,
			encoder.Must(enc.Encode(&Error{err.Error()}))
	}
	preset := &Preset{}
	err = json.Unmarshal(bytes, &preset)
	if err != nil {
		return http.StatusBadRequest,
			encoder.Must(enc.Encode(&Error{err.Error()}))
	}
	presets.Add(preset)
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	return http.StatusCreated, encoder.Must(enc.Encode(preset))
}
開發者ID:kevinjqiu,項目名稱:imposter,代碼行數:18,代碼來源:imposter.go

示例14: GetTeams

func GetTeams(enc encoder.Encoder) (int, []byte) {
	return 200, encoder.Must(enc.Encode(teams))
}
開發者ID:FP7Collage,項目名稱:GoalSetting,代碼行數:3,代碼來源:users.go

示例15: GetUsers

func GetUsers(enc encoder.Encoder) (int, []byte) {
	return 200, encoder.Must(enc.Encode(users))
}
開發者ID:FP7Collage,項目名稱:GoalSetting,代碼行數:3,代碼來源:users.go


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