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


Golang PermaNode.FromMap方法代碼示例

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


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

示例1: handleOpen

func handleOpen(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	userid, sessionid, err := getSession(c, r)
	if err != nil {
		sendError(w, r, "No session cookie")
		return
	}
	// Read the request body
	jreq, err := ioutil.ReadAll(r.Body)
	if err != nil {
		http.Error(w, "Error reading request body", http.StatusInternalServerError)
		return
	}
	r.Body.Close()
	// Parse request
	var req openCloseRequest
	err = json.Unmarshal(jreq, &req)
	if err != nil {
		sendError(w, r, "Malformed JSON")
		return
	}
	// Load the channel infos
	var ch channelStruct
	if err = datastore.Get(c, datastore.NewKey("channel", userid+"/"+sessionid, 0, nil), &ch); err != nil {
		sendError(w, r, "Unknown channel: "+userid+"/"+sessionid)
		return
	}
	// Check
	if len(ch.OpenPermas) >= 10 {
		sendError(w, r, "Too many open channels")
		return
	}
	is_open := false
	for _, p := range ch.OpenPermas {
		if p == req.Perma {
			is_open = true
			break
		}
	}
	var perma grapher.PermaNode
	if !is_open {
		// Update channel infos
		ch.OpenPermas = append(ch.OpenPermas, req.Perma)
		_, err = datastore.Put(c, datastore.NewKey("channel", userid+"/"+sessionid, 0, nil), &ch)
		if err != nil {
			sendError(w, r, "Internal server error")
			return
		}
		// Repeat all blobs from this document.
		s := newStore(c)
		g := grapher.NewGrapher(userid, schema, s, s, nil)
		s.SetGrapher(g)
		ch := newChannelAPI(c, s, userid, sessionid, true, g)
		perma, err = g.Repeat(req.Perma, req.From)
		if err != nil {
			sendError(w, r, "Failed opening")
			return
		}
		fmt.Fprintf(w, `{"ok":true, "blobs":[%v]}`, strings.Join(ch.messageBuffer, ","))
	} else {
		fmt.Fprint(w, `{"ok":true, "blobs":[]}`)
	}

	if req.MarkAsRead {
		if perma == nil {
			s := newStore(c)
			data, err := s.GetPermaNode(req.Perma)
			if err != nil {
				log.Printf("Err: Failed reading permanode")
				return
			}
			perma = grapher.NewPermaNode(nil)
			perma.FromMap(req.Perma, data)
		}
		markAsRead(c, userid, perma.BlobRef(), perma.SequenceNumber()-1)
	}
}
開發者ID:AaronO,項目名稱:lightwave,代碼行數:77,代碼來源:server.go


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