本文整理汇总了Golang中github.com/fern4lvarez/piladb/pila.Element.Decode方法的典型用法代码示例。如果您正苦于以下问题:Golang Element.Decode方法的具体用法?Golang Element.Decode怎么用?Golang Element.Decode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/fern4lvarez/piladb/pila.Element
的用法示例。
在下文中一共展示了Element.Decode方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: pushStackHandler
// pushStackHandler adds an element into a Stack and returns 200 and the element.
func (c *Conn) pushStackHandler(w http.ResponseWriter, r *http.Request, stack *pila.Stack) {
if r.Body == nil {
log.Println(r.Method, r.URL, http.StatusBadRequest,
"no element provided")
w.WriteHeader(http.StatusBadRequest)
return
}
var element pila.Element
err := element.Decode(r.Body)
if err != nil {
log.Println(r.Method, r.URL, http.StatusBadRequest,
"error on decoding element:", err)
w.WriteHeader(http.StatusBadRequest)
return
}
stack.Push(element.Value)
stack.Update(c.opDate)
log.Println(r.Method, r.URL, http.StatusOK, element.Value)
w.Header().Set("Content-Type", "application/json")
// Do not check error as we consider our element
// suitable for a JSON encoding.
b, _ := element.ToJSON()
w.Write(b)
}
示例2: configKeyHandler
// configKeyHandler handles a config value.
func (c *Conn) configKeyHandler(configKey string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
// we override the mux vars to be able to test
// an arbitrary configKey
if configKey != "" {
vars = map[string]string{
"key": configKey,
}
}
value := c.Config.Get(vars["key"])
if value == nil {
c.goneHandler(w, r, fmt.Sprintf("%s is not set", vars["key"]))
return
}
var element pila.Element
if r.Method == "GET" {
value := c.Config.Get(vars["key"])
element.Value = value
}
if r.Method == "POST" {
if r.Body == nil {
log.Println(r.Method, r.URL, http.StatusBadRequest,
"no element provided")
w.WriteHeader(http.StatusBadRequest)
return
}
err := element.Decode(r.Body)
if err != nil {
log.Println(r.Method, r.URL, http.StatusBadRequest,
"error on decoding element:", err)
w.WriteHeader(http.StatusBadRequest)
return
}
c.Config.Set(vars["key"], element.Value)
}
log.Println(r.Method, r.URL, http.StatusOK, element.Value)
w.Header().Set("Content-Type", "application/json")
b, err := element.ToJSON()
if err != nil {
log.Println(r.Method, r.URL, http.StatusBadRequest,
"error on decoding element:", err)
w.WriteHeader(http.StatusBadRequest)
return
}
w.Write(b)
})
}