本文整理汇总了Golang中github.com/goadesign/goa.Context.Get方法的典型用法代码示例。如果您正苦于以下问题:Golang Context.Get方法的具体用法?Golang Context.Get怎么用?Golang Context.Get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/goadesign/goa.Context
的用法示例。
在下文中一共展示了Context.Get方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: NewListBottleContext
// NewListBottleContext parses the incoming request URL and body, performs validations and creates the
// context used by the bottle controller list action.
func NewListBottleContext(c *goa.Context) (*ListBottleContext, error) {
var err error
ctx := ListBottleContext{Context: c}
rawAccountID := c.Get("accountID")
if rawAccountID != "" {
if accountID, err2 := strconv.Atoi(rawAccountID); err2 == nil {
ctx.AccountID = int(accountID)
} else {
err = goa.InvalidParamTypeError("accountID", rawAccountID, "integer", err)
}
}
rawYears := c.Get("years")
if rawYears != "" {
elemsYears := strings.Split(rawYears, ",")
elemsYears2 := make([]int, len(elemsYears))
for i, rawElem := range elemsYears {
if elem, err2 := strconv.Atoi(rawElem); err2 == nil {
elemsYears2[i] = int(elem)
} else {
err = goa.InvalidParamTypeError("elem", rawElem, "integer", err)
}
}
ctx.Years = elemsYears2
}
return &ctx, err
}
示例2: NewShowAccountContext
// NewShowAccountContext parses the incoming request URL and body, performs validations and creates the
// context used by the account controller show action.
func NewShowAccountContext(c *goa.Context) (*ShowAccountContext, error) {
var err error
ctx := ShowAccountContext{Context: c}
rawAccountID := c.Get("accountID")
if rawAccountID != "" {
if accountID, err2 := strconv.Atoi(rawAccountID); err2 == nil {
ctx.AccountID = int(accountID)
} else {
err = goa.InvalidParamTypeError("accountID", rawAccountID, "integer", err)
}
}
return &ctx, err
}
示例3:
Describe("ResponseStatus", func() {
It("returns 0 if not initialized", func() {
Ω(ctx.ResponseStatus()).Should(Equal(0))
})
})
Describe("ResponseLength", func() {
It("returns 0 if not initialized", func() {
Ω(ctx.ResponseLength()).Should(Equal(0))
})
})
Describe("Get", func() {
It(`returns "", false if not initialized`, func() {
p := ctx.Get("foo")
Ω(p).Should(Equal(""))
})
})
Describe("GetMany", func() {
It("returns nil if not initialized", func() {
Ω(ctx.GetMany("foo")).Should(BeNil())
})
})
Describe("RawPayload", func() {
It("returns nil if not initialized", func() {
Ω(ctx.RawPayload()).Should(BeNil())
})
})
示例4:
var p url.Values
BeforeEach(func() {
var err error
r, err = http.NewRequest("GET", "/foo", nil)
Ω(err).ShouldNot(HaveOccurred())
rw = new(TestResponseWriter)
p = url.Values{"id": []string{"42"}, "sort": []string{"asc"}}
})
JustBeforeEach(func() {
handleFunc(rw, r, p)
})
It("creates a handle that handles the request", func() {
i := ctx.Get("id")
Ω(i).Should(Equal("42"))
s := ctx.Get("sort")
Ω(s).Should(Equal("asc"))
tw := rw.(*TestResponseWriter)
Ω(tw.Status).Should(Equal(respStatus))
Ω(tw.Body).Should(Equal(respContent))
})
Context("and middleware", func() {
middlewareCalled := false
BeforeEach(func() {
s.Use(TMiddleware(&middlewareCalled))
})