• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang pila.Element类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



在下文中一共展示了Element类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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)
}
开发者ID:fern4lvarez,项目名称:piladb,代码行数:29,代码来源:conn.go


示例2: TestPeekStackHandler

func TestPeekStackHandler(t *testing.T) {
	s := pila.NewStack("stack", time.Now().UTC())

	db := pila.NewDatabase("db")
	_ = db.AddStack(s)

	p := pila.NewPila()
	_ = p.AddDatabase(db)

	conn := NewConn()
	conn.Pila = p

	element := pila.Element{Value: "test-element"}
	expectedElementJSON, _ := element.ToJSON()

	s.Push(element.Value)

	request, err := http.NewRequest("GET",
		fmt.Sprintf("/databases/%s/stacks/%s?peek",
			db.ID.String(),
			s.ID.String()),
		nil)
	if err != nil {
		t.Fatal(err)
	}
	request.Header.Set("Content-Type", "application/json")

	response := httptest.NewRecorder()

	conn.peekStackHandler(response, request, s)

	if peekElement := db.Stacks[s.ID].Peek(); peekElement != element.Value {
		t.Errorf("peek element is %v, expected %v", peekElement, element.Value)
	}

	if contentType := response.Header().Get("Content-Type"); contentType != "application/json" {
		t.Errorf("Content-Type is %v, expected %v", contentType, "application/json")
	}

	if response.Code != http.StatusOK {
		t.Errorf("response code is %v, expected %v", response.Code, http.StatusOK)
	}

	elementJSON, err := ioutil.ReadAll(response.Body)
	if err != nil {
		t.Fatal(err)
	}

	if string(elementJSON) != string(expectedElementJSON) {
		t.Errorf("peek element is %v, expected %v", string(elementJSON), string(expectedElementJSON))
	}
}
开发者ID:fern4lvarez,项目名称:piladb,代码行数:52,代码来源:conn_test.go


示例3: peekStackHandler

// peekStackHandler returns the peek of the Stack without modifying it.
func (c *Conn) peekStackHandler(w http.ResponseWriter, r *http.Request, stack *pila.Stack) {
	var element pila.Element
	element.Value = stack.Peek()
	stack.Read(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)
}
开发者ID:fern4lvarez,项目名称:piladb,代码行数:14,代码来源:conn.go


示例4: popStackHandler

// popStackHandler extracts the peek element of a Stack, returns 200 and returns it.
func (c *Conn) popStackHandler(w http.ResponseWriter, r *http.Request, stack *pila.Stack) {
	value, ok := stack.Pop()
	if !ok {
		log.Println(r.Method, r.URL, http.StatusNoContent)
		w.WriteHeader(http.StatusNoContent)
		return
	}
	stack.Update(c.opDate)

	element := pila.Element{Value: value}

	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)
}
开发者ID:fern4lvarez,项目名称:piladb,代码行数:20,代码来源:conn.go


示例5: 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)
	})
}
开发者ID:fern4lvarez,项目名称:piladb,代码行数:54,代码来源:config.go


示例6: TestStackHandler_DELETE

func TestStackHandler_DELETE(t *testing.T) {
	element := pila.Element{Value: "test-element"}
	expectedElementJSON, _ := element.ToJSON()

	s := pila.NewStack("stack", time.Now().UTC())

	db := pila.NewDatabase("db")
	_ = db.AddStack(s)

	p := pila.NewPila()
	_ = p.AddDatabase(db)

	conn := NewConn()
	conn.Pila = p

	s.Push(element.Value)

	inputOutput := []struct {
		input struct {
			database, stack, op string
		}
		output struct {
			response []byte
			code     int
		}
	}{
		{struct {
			database, stack, op string
		}{db.ID.String(), s.ID.String(), ""},
			struct {
				response []byte
				code     int
			}{expectedElementJSON, http.StatusOK},
		},
		{struct {
			database, stack, op string
		}{db.Name, s.Name, ""},
			struct {
				response []byte
				code     int
			}{expectedElementJSON, http.StatusOK},
		},
		{struct {
			database, stack, op string
		}{db.Name, s.Name, "flush"},
			struct {
				response []byte
				code     int
			}{nil, http.StatusOK},
		},
		{struct {
			database, stack, op string
		}{db.Name, s.Name, "full"},
			struct {
				response []byte
				code     int
			}{nil, http.StatusNoContent},
		},
	}

	for _, io := range inputOutput {
		request, err := http.NewRequest("DELETE",
			fmt.Sprintf("/databases/%s/stacks/%s?%s",
				io.input.database,
				io.input.stack,
				io.input.op),
			nil)
		if err != nil {
			t.Fatal(err)
		}

		response := httptest.NewRecorder()

		params := map[string]string{
			"database_id": io.input.database,
			"stack_id":    io.input.stack,
		}

		stackHandle := conn.stackHandler(&params)
		stackHandle.ServeHTTP(response, request)

		if response.Code != io.output.code {
			t.Errorf("on op %s response code is %v, expected %v", io.input.op, response.Code, io.output.code)
		}

		responseJSON, err := ioutil.ReadAll(response.Body)
		if err != nil {
			t.Fatal(err)
		}

		if io.input.op == "flush" {
			s.Update(conn.opDate)
			stackStatus := s.Status()

			expectedStackStatusJSON, err := stackStatus.ToJSON()
			if err != nil {
				t.Fatal(err)
			}
			if string(responseJSON) != string(expectedStackStatusJSON) {
				t.Errorf("on op %s response is %s, expected %s", io.input.op, string(responseJSON), string(expectedStackStatusJSON))
//.........这里部分代码省略.........
开发者ID:fern4lvarez,项目名称:piladb,代码行数:101,代码来源:conn_test.go


示例7: TestStackHandler_POST

func TestStackHandler_POST(t *testing.T) {
	s := pila.NewStack("stack", time.Now().UTC())

	db := pila.NewDatabase("db")
	_ = db.AddStack(s)

	p := pila.NewPila()
	_ = p.AddDatabase(db)

	conn := NewConn()
	conn.Pila = p
	conn.opDate = time.Now().UTC()

	element := pila.Element{Value: "test-element"}
	expectedElementJSON, _ := element.ToJSON()

	paramss := []map[string]string{
		{
			"database_id": db.ID.String(),
			"stack_id":    s.ID.String(),
		},
		{
			"database_id": db.Name,
			"stack_id":    s.Name,
		},
	}

	for _, params := range paramss {
		request, err := http.NewRequest("POST",
			fmt.Sprintf("/databases/%s/stacks/%s",
				params["database_id"],
				params["stack_id"]),
			bytes.NewBuffer(expectedElementJSON))
		if err != nil {
			t.Fatal(err)
		}
		request.Header.Set("Content-Type", "application/json")

		response := httptest.NewRecorder()

		stackHandle := conn.stackHandler(&params)
		stackHandle.ServeHTTP(response, request)

		if contentType := response.Header().Get("Content-Type"); contentType != "application/json" {
			t.Errorf("Content-Type is %v, expected %v", contentType, "application/json")
		}

		if response.Code != http.StatusOK {
			t.Errorf("response code is %v, expected %v", response.Code, http.StatusOK)
		}

		elementJSON, err := ioutil.ReadAll(response.Body)
		if err != nil {
			t.Fatal(err)
		}

		if string(elementJSON) != string(expectedElementJSON) {
			t.Errorf("pushed element is %v, expected %v", string(elementJSON), string(expectedElementJSON))
		}
	}
}
开发者ID:fern4lvarez,项目名称:piladb,代码行数:61,代码来源:conn_test.go


示例8: TestStackHandler_GET

func TestStackHandler_GET(t *testing.T) {
	element := pila.Element{Value: "test-element"}
	expectedElementJSON, _ := element.ToJSON()

	createDate := time.Now().UTC()
	s := pila.NewStack("stack", createDate)
	s.Update(createDate)

	db := pila.NewDatabase("db")
	_ = db.AddStack(s)

	p := pila.NewPila()
	_ = p.AddDatabase(db)

	conn := NewConn()
	conn.Pila = p
	conn.opDate = time.Now().UTC()

	s.Push(element.Value)

	expectedStackStatusJSON, err := s.Status().ToJSON()
	if err != nil {
		t.Fatal(err)
	}

	expectedSizeJSON := s.SizeToJSON()

	inputOutput := []struct {
		input struct {
			database, stack, op string
		}
		output struct {
			response []byte
			code     int
		}
	}{
		{struct {
			database, stack, op string
		}{db.ID.String(), s.ID.String(), ""},
			struct {
				response []byte
				code     int
			}{expectedStackStatusJSON, http.StatusOK},
		},
		{struct {
			database, stack, op string
		}{db.ID.String(), s.ID.String(), "peek"},
			struct {
				response []byte
				code     int
			}{expectedElementJSON, http.StatusOK},
		},
		{struct {
			database, stack, op string
		}{db.ID.String(), s.ID.String(), "size"},
			struct {
				response []byte
				code     int
			}{expectedSizeJSON, http.StatusOK},
		},
	}

	for _, io := range inputOutput {
		request, err := http.NewRequest("GET",
			fmt.Sprintf("/databases/%s/stacks/%s?%s",
				io.input.database,
				io.input.stack,
				io.input.op),
			nil)
		if err != nil {
			t.Fatal(err)
		}

		response := httptest.NewRecorder()

		params := map[string]string{
			"database_id": io.input.database,
			"stack_id":    io.input.stack,
		}

		stackHandle := conn.stackHandler(&params)
		stackHandle.ServeHTTP(response, request)

		if peek := db.Stacks[s.ID].Peek(); peek != element.Value {
			t.Errorf("peek is %v, expected %v", peek, element.Value)
		}

		if contentType := response.Header().Get("Content-Type"); contentType != "application/json" {
			t.Errorf("Content-Type is %v, expected %v", contentType, "application/json")
		}

		if response.Code != io.output.code {
			t.Errorf("on %s response code is %v, expected %v", io.input.op, response.Code, io.output.code)
		}

		responseJSON, err := ioutil.ReadAll(response.Body)
		if err != nil {
			t.Fatal(err)
		}

//.........这里部分代码省略.........
开发者ID:fern4lvarez,项目名称:piladb,代码行数:101,代码来源:conn_test.go


示例9: TestPopStackHandler

func TestPopStackHandler(t *testing.T) {
	element := pila.Element{Value: "test-element"}
	expectedElementJSON, _ := element.ToJSON()

	s := pila.NewStack("stack", time.Now().UTC())
	s.Push(element.Value)

	db := pila.NewDatabase("db")
	_ = db.AddStack(s)

	p := pila.NewPila()
	_ = p.AddDatabase(db)

	conn := NewConn()
	conn.Pila = p

	varss := []map[string]string{
		{
			"database_id": db.ID.String(),
			"stack_id":    s.ID.String(),
		},
		{
			"database_id": db.Name,
			"stack_id":    s.Name,
		},
	}

	for _, vars := range varss {
		request, err := http.NewRequest("DELETE",
			fmt.Sprintf("/databases/%s/stacks/%s",
				vars["database_id"],
				vars["stack_id"]),
			nil)
		if err != nil {
			t.Fatal(err)
		}

		response := httptest.NewRecorder()

		conn.popStackHandler(response, request, s)

		if peek, ok := db.Stacks[s.ID].Pop(); ok {
			t.Errorf("stack contains %v, expected to be empty", peek)
		}

		if contentType := response.Header().Get("Content-Type"); contentType != "application/json" {
			t.Errorf("Content-Type is %v, expected %v", contentType, "application/json")
		}

		if response.Code != http.StatusOK {
			t.Errorf("response code is %v, expected %v", response.Code, http.StatusOK)
		}

		elementJSON, err := ioutil.ReadAll(response.Body)
		if err != nil {
			t.Fatal(err)
		}

		if string(elementJSON) != string(expectedElementJSON) {
			t.Errorf("popped element is %s, expected %s", string(elementJSON), string(expectedElementJSON))
		}

		// restore element for next table test iteration
		s.Push(element.Value)
	}
}
开发者ID:fern4lvarez,项目名称:piladb,代码行数:66,代码来源:conn_test.go


示例10: TestConfigKeyHandler

func TestConfigKeyHandler(t *testing.T) {
	conn := NewConn()
	conn.Config = config.NewConfig()
	conn.Config.Set(vars.MaxStackSize, 2)

	element := pila.Element{Value: 2}
	expectedElementJSON, _ := element.ToJSON()

	newElement := pila.Element{Value: 10}
	expectedNewElementJSON, _ := newElement.ToJSON()

	inputOutput := []struct {
		input struct {
			method, key string
			payload     io.Reader
		}
		output struct {
			value    interface{}
			response []byte
		}
	}{
		{struct {
			method, key string
			payload     io.Reader
		}{"GET", vars.MaxStackSize, nil},
			struct {
				value    interface{}
				response []byte
			}{2, expectedElementJSON},
		},
		{struct {
			method, key string
			payload     io.Reader
		}{"POST", vars.MaxStackSize, bytes.NewBuffer(expectedNewElementJSON)},
			struct {
				value    interface{}
				response []byte
			}{10, expectedNewElementJSON},
		},
	}

	for _, io := range inputOutput {
		request, err := http.NewRequest(io.input.method,
			fmt.Sprintf("/_config/%s", io.input.key),
			io.input.payload)
		if err != nil {
			t.Fatal(err)
		}

		response := httptest.NewRecorder()

		configKeyHandle := conn.configKeyHandler(io.input.key)
		configKeyHandle.ServeHTTP(response, request)

		if value := conn.Config.MaxStackSize(); value != io.output.value {
			t.Errorf("value is %d, expected %d", value, io.output.value)
		}

		if contentType := response.Header().Get("Content-Type"); contentType != "application/json" {
			t.Errorf("Content-Type is %v, expected %v", contentType, "application/json")
		}

		if response.Code != http.StatusOK {
			t.Errorf("response code is %v, expected %v", response.Code, http.StatusOK)
		}

		responseJSON, err := ioutil.ReadAll(response.Body)
		if err != nil {
			t.Fatal(err)
		}

		if string(responseJSON) != string(io.output.response) {
			t.Errorf("response is %s, expected %s", string(responseJSON), string(io.output.response))
		}
	}
}
开发者ID:fern4lvarez,项目名称:piladb,代码行数:76,代码来源:config_test.go



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang pila.Stack类代码示例发布时间:2022-05-23
下一篇:
Golang pila.NewPila函数代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap