_id stringlengths 2 7 | title stringlengths 1 118 | partition stringclasses 3
values | text stringlengths 52 85.5k | language stringclasses 1
value | meta_information dict |
|---|---|---|---|---|---|
q13100 | WriteTo | train | func (e *Entity) WriteTo(w io.Writer) error {
ew, err := CreateWriter(w, e.Header)
if err != nil {
return err
}
defer ew.Close()
return e.writeBodyTo(ew)
} | go | {
"resource": ""
} |
q13101 | NewReader | train | func NewReader(e *message.Entity) *Reader {
mr := e.MultipartReader()
if mr == nil {
// Artificially create a multipart entity
// With this header, no error will be returned by message.NewMultipart
var h message.Header
h.Set("Content-Type", "multipart/mixed")
me, _ := message.NewMultipart(h, []*message.Enti... | go | {
"resource": ""
} |
q13102 | CreateReader | train | func CreateReader(r io.Reader) (*Reader, error) {
e, err := message.Read(r)
if err != nil && !message.IsUnknownCharset(err) {
return nil, err
}
return NewReader(e), err
} | go | {
"resource": ""
} |
q13103 | NextPart | train | func (r *Reader) NextPart() (*Part, error) {
for r.readers.Len() > 0 {
e := r.readers.Back()
mr := e.Value.(message.MultipartReader)
p, err := mr.NextPart()
if err == io.EOF {
// This whole multipart entity has been read, continue with the next one
r.readers.Remove(e)
continue
} else if err != nil ... | go | {
"resource": ""
} |
q13104 | Close | train | func (r *Reader) Close() error {
for r.readers.Len() > 0 {
e := r.readers.Back()
mr := e.Value.(message.MultipartReader)
if err := mr.Close(); err != nil {
return err
}
r.readers.Remove(e)
}
return nil
} | go | {
"resource": ""
} |
q13105 | Reader | train | func Reader(charset string, input io.Reader) (io.Reader, error) {
charset = strings.ToLower(charset)
// "ascii" is not in the spec but is common
if charset == "utf-8" || charset == "us-ascii" || charset == "ascii" {
return input, nil
}
if enc, ok := charsets[charset]; ok {
return enc.NewDecoder().Reader(input)... | go | {
"resource": ""
} |
q13106 | charsetReader | train | func charsetReader(charset string, input io.Reader) (io.Reader, error) {
charset = strings.ToLower(charset)
// "ascii" is not in the spec but is common
if charset == "utf-8" || charset == "us-ascii" || charset == "ascii" {
return input, nil
}
if CharsetReader != nil {
return CharsetReader(charset, input)
}
r... | go | {
"resource": ""
} |
q13107 | decodeHeader | train | func decodeHeader(s string) (string, error) {
wordDecoder := mime.WordDecoder{CharsetReader: charsetReader}
dec, err := wordDecoder.DecodeHeader(s)
if err != nil {
return s, err
}
return dec, nil
} | go | {
"resource": ""
} |
q13108 | createWriter | train | func createWriter(w io.Writer, header *Header) (*Writer, error) {
ww := &Writer{w: w}
mediaType, mediaParams, _ := header.ContentType()
if strings.HasPrefix(mediaType, "multipart/") {
ww.mw = multipart.NewWriter(ww.w)
// Do not set ww's io.Closer for now: if this is a multipart entity but
// CreatePart is no... | go | {
"resource": ""
} |
q13109 | CreateWriter | train | func CreateWriter(w io.Writer, header Header) (*Writer, error) {
ww, err := createWriter(w, &header)
if err != nil {
return nil, err
}
if err := textproto.WriteHeader(w, header.Header); err != nil {
return nil, err
}
return ww, nil
} | go | {
"resource": ""
} |
q13110 | CreatePart | train | func (w *Writer) CreatePart(header Header) (*Writer, error) {
if w.mw == nil {
return nil, errors.New("cannot create a part in a non-multipart message")
}
if w.c == nil {
// We know that the user calls CreatePart so Close should write the final
// boundary
w.c = w.mw
}
// cw -> ww -> pw -> w.mw -> w.w
... | go | {
"resource": ""
} |
q13111 | Add | train | func (h *Header) Add(k, v string) {
k = textproto.CanonicalMIMEHeaderKey(k)
if h.m == nil {
h.m = make(map[string][]*headerField)
}
h.l = append(h.l, newHeaderField(k, v, nil))
f := &h.l[len(h.l)-1]
h.m[k] = append(h.m[k], f)
} | go | {
"resource": ""
} |
q13112 | Get | train | func (h *Header) Get(k string) string {
fields := h.m[textproto.CanonicalMIMEHeaderKey(k)]
if len(fields) == 0 {
return ""
}
return fields[len(fields)-1].v
} | go | {
"resource": ""
} |
q13113 | Set | train | func (h *Header) Set(k, v string) {
h.Del(k)
h.Add(k, v)
} | go | {
"resource": ""
} |
q13114 | Has | train | func (h *Header) Has(k string) bool {
_, ok := h.m[textproto.CanonicalMIMEHeaderKey(k)]
return ok
} | go | {
"resource": ""
} |
q13115 | FieldsByKey | train | func (h *Header) FieldsByKey(k string) HeaderFields {
return &headerFieldsByKey{h, textproto.CanonicalMIMEHeaderKey(k), -1}
} | go | {
"resource": ""
} |
q13116 | trim | train | func trim(s []byte) []byte {
i := 0
for i < len(s) && isSpace(s[i]) {
i++
}
n := len(s)
for n > i && isSpace(s[n-1]) {
n--
}
return s[i:n]
} | go | {
"resource": ""
} |
q13117 | skipSpace | train | func skipSpace(r *bufio.Reader) int {
n := 0
for {
c, err := r.ReadByte()
if err != nil {
// bufio will keep err until next read.
break
}
if !isSpace(c) {
r.UnreadByte()
break
}
n++
}
return n
} | go | {
"resource": ""
} |
q13118 | trimAroundNewlines | train | func trimAroundNewlines(v []byte) string {
var b strings.Builder
for {
i := bytes.IndexByte(v, '\n')
if i < 0 {
writeContinued(&b, v)
break
}
writeContinued(&b, v[:i])
v = v[i+1:]
}
return b.String()
} | go | {
"resource": ""
} |
q13119 | formatHeaderField | train | func formatHeaderField(k, v string) string {
s := k + ": "
if v == "" {
return s + "\r\n"
}
first := true
for len(v) > 0 {
maxlen := maxHeaderLen
if first {
maxlen -= len(s)
}
// We'll need to fold before i
foldBefore := maxlen + 1
foldAt := len(v)
var folding string
if foldBefore > len(v)... | go | {
"resource": ""
} |
q13120 | WriteHeader | train | func WriteHeader(w io.Writer, h Header) error {
// TODO: wrap lines when necessary
for i := len(h.l) - 1; i >= 0; i-- {
f := h.l[i]
if f.b == nil {
f.b = []byte(formatHeaderField(f.k, f.v))
}
if _, err := w.Write(f.b); err != nil {
return err
}
}
_, err := w.Write([]byte{'\r', '\n'})
return err... | go | {
"resource": ""
} |
q13121 | CreateWriter | train | func CreateWriter(w io.Writer, header Header) (*Writer, error) {
header.Set("Content-Type", "multipart/mixed")
mw, err := message.CreateWriter(w, header.Header)
if err != nil {
return nil, err
}
return &Writer{mw}, nil
} | go | {
"resource": ""
} |
q13122 | CreateInline | train | func (w *Writer) CreateInline() (*InlineWriter, error) {
var h message.Header
h.Set("Content-Type", "multipart/alternative")
mw, err := w.mw.CreatePart(h)
if err != nil {
return nil, err
}
return &InlineWriter{mw}, nil
} | go | {
"resource": ""
} |
q13123 | CreateSingleInline | train | func (w *Writer) CreateSingleInline(h InlineHeader) (io.WriteCloser, error) {
initInlineHeader(&h)
return w.mw.CreatePart(h.Header)
} | go | {
"resource": ""
} |
q13124 | CreateAttachment | train | func (w *Writer) CreateAttachment(h AttachmentHeader) (io.WriteCloser, error) {
initAttachmentHeader(&h)
return w.mw.CreatePart(h.Header)
} | go | {
"resource": ""
} |
q13125 | CreatePart | train | func (w *InlineWriter) CreatePart(h InlineHeader) (io.WriteCloser, error) {
initInlineHeader(&h)
return w.mw.CreatePart(h.Header)
} | go | {
"resource": ""
} |
q13126 | GetEntry | train | func (r *Store) GetEntry(key string) *Entry {
args := *r
n := len(args)
for i := 0; i < n; i++ {
kv := &args[i]
if kv.Key == key {
return kv
}
}
return nil
} | go | {
"resource": ""
} |
q13127 | GetStringDefault | train | func (r *Store) GetStringDefault(key string, def string) string {
v := r.GetEntry(key)
if v == nil {
return def
}
return v.StringDefault(def)
} | go | {
"resource": ""
} |
q13128 | Serialize | train | func (r Store) Serialize() []byte {
w := new(bytes.Buffer)
enc := gob.NewEncoder(w)
err := enc.Encode(r)
if err != nil {
return nil
}
return w.Bytes()
} | go | {
"resource": ""
} |
q13129 | StartFasthttp | train | func (s *Sessions) StartFasthttp(ctx *fasthttp.RequestCtx) *Session {
cookieValue := s.decodeCookieValue(GetCookieFasthttp(ctx, s.config.Cookie))
if cookieValue == "" { // cookie doesn't exists, let's generate a session and add set a cookie
sid := s.config.SessionIDGenerator()
sess := s.provider.Init(sid, s.con... | go | {
"resource": ""
} |
q13130 | ShiftExpirationFasthttp | train | func (s *Sessions) ShiftExpirationFasthttp(ctx *fasthttp.RequestCtx) {
s.UpdateExpirationFasthttp(ctx, s.config.Expires)
} | go | {
"resource": ""
} |
q13131 | UpdateExpiration | train | func UpdateExpiration(w http.ResponseWriter, r *http.Request, expires time.Duration) {
Default.UpdateExpiration(w, r, expires)
} | go | {
"resource": ""
} |
q13132 | DestroyFasthttp | train | func (s *Sessions) DestroyFasthttp(ctx *fasthttp.RequestCtx) {
cookieValue := GetCookieFasthttp(ctx, s.config.Cookie)
s.destroy(cookieValue)
RemoveCookieFasthttp(ctx, s.config)
} | go | {
"resource": ""
} |
q13133 | GetInt | train | func (s *Session) GetInt(key string) (int, error) {
v := s.Get(key)
if vint, ok := v.(int); ok {
return vint, nil
}
if vstring, sok := v.(string); sok {
return strconv.Atoi(vstring)
}
return -1, fmt.Errorf(errFindParse, "int", key)
} | go | {
"resource": ""
} |
q13134 | GetFloat32 | train | func (s *Session) GetFloat32(key string) (float32, error) {
v := s.Get(key)
if vfloat32, ok := v.(float32); ok {
return vfloat32, nil
}
if vfloat64, ok := v.(float64); ok {
return float32(vfloat64), nil
}
if vint, ok := v.(int); ok {
return float32(vint), nil
}
if vstring, sok := v.(string); sok {
v... | go | {
"resource": ""
} |
q13135 | GetCookieFasthttp | train | func GetCookieFasthttp(ctx *fasthttp.RequestCtx, name string) (value string) {
bcookie := ctx.Request.Header.Cookie(name)
if bcookie != nil {
value = string(bcookie)
}
return
} | go | {
"resource": ""
} |
q13136 | AddCookieFasthttp | train | func AddCookieFasthttp(ctx *fasthttp.RequestCtx, cookie *fasthttp.Cookie) {
ctx.Response.Header.SetCookie(cookie)
} | go | {
"resource": ""
} |
q13137 | OnUpdateExpiration | train | func (db *Database) OnUpdateExpiration(sid string, newExpires time.Duration) error {
expirationTime := time.Now().Add(newExpires)
timeBytes, err := sessions.DefaultTranscoder.Marshal(expirationTime)
if err != nil {
return err
}
return db.Service.Update(func(tx *bolt.Tx) error {
expirationName := getExpiration... | go | {
"resource": ""
} |
q13138 | CreateTable | train | func CreateTable() *Table {
t := &Table{elements: []Element{}, Style: DefaultStyle}
if outputsEnabled.UTF8 {
t.Style.setUtfBoxStyle()
}
if outputsEnabled.titleStyle != titleStyle(0) {
t.Style.htmlRules.title = outputsEnabled.titleStyle
}
t.outputMode = defaultOutputMode
return t
} | go | {
"resource": ""
} |
q13139 | AddRow | train | func (t *Table) AddRow(items ...interface{}) *Row {
row := CreateRow(items)
t.elements = append(t.elements, row)
return row
} | go | {
"resource": ""
} |
q13140 | AddHeaders | train | func (t *Table) AddHeaders(headers ...interface{}) {
t.headers = append(t.headers, headers...)
} | go | {
"resource": ""
} |
q13141 | SetAlign | train | func (t *Table) SetAlign(align tableAlignment, column int) {
if column < 0 {
return
}
for i := range t.elements {
row, ok := t.elements[i].(*Row)
if !ok {
continue
}
if column >= len(row.cells) {
continue
}
row.cells[column-1].alignment = &align
}
} | go | {
"resource": ""
} |
q13142 | SetHTMLStyleTitle | train | func (t *Table) SetHTMLStyleTitle(want titleStyle) {
t.Style.htmlRules.title = want
} | go | {
"resource": ""
} |
q13143 | renderTerminal | train | func (t *Table) renderTerminal() string {
// Use a placeholder rather than adding titles/headers to the tables
// elements or else successive calls will compound them.
tt := t.clone()
// Initial top line.
if !tt.Style.SkipBorder {
if tt.title != nil && tt.headers == nil {
tt.elements = append([]Element{&Sepa... | go | {
"resource": ""
} |
q13144 | Render | train | func (s *StraightSeparator) Render(style *renderStyle) string {
// loop over getting dashes
width := 0
for i := 0; i < style.columns; i++ {
width += style.PaddingLeft + style.CellWidth(i) + style.PaddingRight + utf8.RuneCountInString(style.BorderI)
}
switch s.where {
case LINE_TOP:
return style.BorderTopLeft... | go | {
"resource": ""
} |
q13145 | GetEnvWindowSize | train | func GetEnvWindowSize() *Size {
lines := os.Getenv("LINES")
columns := os.Getenv("COLUMNS")
if lines == "" && columns == "" {
return nil
}
nLines := 0
nColumns := 0
var err error
if lines != "" {
nLines, err = strconv.Atoi(lines)
if err != nil || nLines < 0 {
return nil
}
}
if columns != "" {
nC... | go | {
"resource": ""
} |
q13146 | Render | train | func (s *Separator) Render(style *renderStyle) string {
// loop over getting dashes
parts := []string{}
for i := 0; i < style.columns; i++ {
w := style.PaddingLeft + style.CellWidth(i) + style.PaddingRight
parts = append(parts, strings.Repeat(style.BorderX, w))
}
switch s.where {
case LINE_TOP:
return styl... | go | {
"resource": ""
} |
q13147 | CreateRow | train | func CreateRow(items []interface{}) *Row {
row := &Row{cells: []*Cell{}}
for _, item := range items {
row.AddCell(item)
}
return row
} | go | {
"resource": ""
} |
q13148 | AddCell | train | func (r *Row) AddCell(item interface{}) {
if c, ok := item.(*Cell); ok {
c.column = len(r.cells)
r.cells = append(r.cells, c)
} else {
r.cells = append(r.cells, createCell(len(r.cells), item, nil))
}
} | go | {
"resource": ""
} |
q13149 | HTML | train | func (r *Row) HTML(tag string, style *renderStyle) string {
attrs := make([]string, len(r.cells))
elems := make([]string, len(r.cells))
for i := range r.cells {
if r.cells[i].alignment != nil {
switch *r.cells[i].alignment {
case AlignLeft:
attrs[i] = " align='left'"
case AlignCenter:
attrs[i] = "... | go | {
"resource": ""
} |
q13150 | RenderHTML | train | func (t *Table) RenderHTML() (buffer string) {
// elements is already populated with row data
// generate the runtime style
style := createRenderStyle(t)
style.PaddingLeft = 0
style.PaddingRight = 0
// TODO: control CSS styles to suppress border based upon t.Style.SkipBorder
rowsText := make([]string, 0, len(t... | go | {
"resource": ""
} |
q13151 | renderValue | train | func renderValue(v interface{}) string {
switch vv := v.(type) {
case string:
return vv
case bool:
return strconv.FormatBool(vv)
case int:
return strconv.Itoa(vv)
case int64:
return strconv.FormatInt(vv, 10)
case uint64:
return strconv.FormatUint(vv, 10)
case float64:
return strconv.FormatFloat(vv, '... | go | {
"resource": ""
} |
q13152 | setUtfBoxStyle | train | func (s *TableStyle) setUtfBoxStyle() {
s.BorderX = "─"
s.BorderY = "│"
s.BorderI = "┼"
s.BorderTop = "┬"
s.BorderBottom = "┴"
s.BorderLeft = "├"
s.BorderRight = "┤"
s.BorderTopLeft = "╭"
s.BorderTopRight = "╮"
s.BorderBottomLeft = "╰"
s.BorderBottomRight = "╯"
} | go | {
"resource": ""
} |
q13153 | fillStyleRules | train | func (s *TableStyle) fillStyleRules() {
if s.BorderTop == "" {
s.BorderTop = s.BorderI
}
if s.BorderBottom == "" {
s.BorderBottom = s.BorderI
}
if s.BorderLeft == "" {
s.BorderLeft = s.BorderI
}
if s.BorderRight == "" {
s.BorderRight = s.BorderI
}
if s.BorderTopLeft == "" {
s.BorderTopLeft = s.Border... | go | {
"resource": ""
} |
q13154 | buildReplaceContent | train | func (s *renderStyle) buildReplaceContent(bad string) {
replacement := fmt.Sprintf("&#x%02x;", bad)
s.replaceContent = func(old string) string {
return strings.Replace(old, bad, replacement, -1)
}
} | go | {
"resource": ""
} |
q13155 | GetTerminalWindowSize | train | func GetTerminalWindowSize(file *os.File) (*Size, error) {
var info consoleScreenBufferInfo
_, _, e := syscall.Syscall(procGetConsoleScreenBufferInfo.Addr(), 2, file.Fd(), uintptr(unsafe.Pointer(&info)), 0)
if e != 0 {
return nil, error(e)
}
return &Size{
Lines: int(info.size.y),
Columns: int(info.size.x),... | go | {
"resource": ""
} |
q13156 | Get | train | func (bp *BufferPool) Get() (b *bytes.Buffer) {
select {
case b = <-bp.c:
// reuse existing buffer
default:
// create new buffer
b = bytes.NewBuffer([]byte{})
}
return
} | go | {
"resource": ""
} |
q13157 | New | train | func New(options ...Options) *Render {
var o Options
if len(options) > 0 {
o = options[0]
}
r := Render{
opt: o,
}
r.prepareOptions()
r.compileTemplates()
return &r
} | go | {
"resource": ""
} |
q13158 | TemplateLookup | train | func (r *Render) TemplateLookup(t string) *template.Template {
return r.templates.Lookup(t)
} | go | {
"resource": ""
} |
q13159 | Render | train | func (r *Render) Render(w io.Writer, e Engine, data interface{}) error {
err := e.Render(w, data)
if hw, ok := w.(http.ResponseWriter); err != nil && !r.opt.DisableHTTPErrorRendering && ok {
http.Error(hw, err.Error(), http.StatusInternalServerError)
}
return err
} | go | {
"resource": ""
} |
q13160 | Data | train | func (r *Render) Data(w io.Writer, status int, v []byte) error {
head := Head{
ContentType: r.opt.BinaryContentType,
Status: status,
}
d := Data{
Head: head,
}
return r.Render(w, d, v)
} | go | {
"resource": ""
} |
q13161 | HTML | train | func (r *Render) HTML(w io.Writer, status int, name string, binding interface{}, htmlOpt ...HTMLOptions) error {
r.templatesLk.Lock()
defer r.templatesLk.Unlock()
// If we are in development mode, recompile the templates on every HTML request.
if r.opt.IsDevelopment {
r.compileTemplates()
}
opt := r.prepareHT... | go | {
"resource": ""
} |
q13162 | JSON | train | func (r *Render) JSON(w io.Writer, status int, v interface{}) error {
head := Head{
ContentType: r.opt.JSONContentType + r.compiledCharset,
Status: status,
}
j := JSON{
Head: head,
Indent: r.opt.IndentJSON,
Prefix: r.opt.PrefixJSON,
UnEscapeHTML: r.opt.UnEscapeHTML,
Stream... | go | {
"resource": ""
} |
q13163 | JSONP | train | func (r *Render) JSONP(w io.Writer, status int, callback string, v interface{}) error {
head := Head{
ContentType: r.opt.JSONPContentType + r.compiledCharset,
Status: status,
}
j := JSONP{
Head: head,
Indent: r.opt.IndentJSON,
Callback: callback,
}
return r.Render(w, j, v)
} | go | {
"resource": ""
} |
q13164 | XML | train | func (r *Render) XML(w io.Writer, status int, v interface{}) error {
head := Head{
ContentType: r.opt.XMLContentType + r.compiledCharset,
Status: status,
}
x := XML{
Head: head,
Indent: r.opt.IndentXML,
Prefix: r.opt.PrefixXML,
}
return r.Render(w, x, v)
} | go | {
"resource": ""
} |
q13165 | Write | train | func (h Head) Write(w http.ResponseWriter) {
w.Header().Set(ContentType, h.ContentType)
w.WriteHeader(h.Status)
} | go | {
"resource": ""
} |
q13166 | Render | train | func (d Data) Render(w io.Writer, v interface{}) error {
if hw, ok := w.(http.ResponseWriter); ok {
c := hw.Header().Get(ContentType)
if c != "" {
d.Head.ContentType = c
}
d.Head.Write(hw)
}
w.Write(v.([]byte))
return nil
} | go | {
"resource": ""
} |
q13167 | Render | train | func (h HTML) Render(w io.Writer, binding interface{}) error {
// Retrieve a buffer from the pool to write to.
out := bufPool.Get()
err := h.Templates.ExecuteTemplate(out, h.Name, binding)
if err != nil {
return err
}
if hw, ok := w.(http.ResponseWriter); ok {
h.Head.Write(hw)
}
out.WriteTo(w)
// Return ... | go | {
"resource": ""
} |
q13168 | Render | train | func (j JSON) Render(w io.Writer, v interface{}) error {
if j.StreamingJSON {
return j.renderStreamingJSON(w, v)
}
var result []byte
var err error
if j.Indent {
result, err = json.MarshalIndent(v, "", " ")
result = append(result, '\n')
} else {
result, err = json.Marshal(v)
}
if err != nil {
return... | go | {
"resource": ""
} |
q13169 | Render | train | func (j JSONP) Render(w io.Writer, v interface{}) error {
var result []byte
var err error
if j.Indent {
result, err = json.MarshalIndent(v, "", " ")
} else {
result, err = json.Marshal(v)
}
if err != nil {
return err
}
// JSON marshaled fine, write out the result.
if hw, ok := w.(http.ResponseWriter);... | go | {
"resource": ""
} |
q13170 | Render | train | func (t Text) Render(w io.Writer, v interface{}) error {
if hw, ok := w.(http.ResponseWriter); ok {
c := hw.Header().Get(ContentType)
if c != "" {
t.Head.ContentType = c
}
t.Head.Write(hw)
}
w.Write([]byte(v.(string)))
return nil
} | go | {
"resource": ""
} |
q13171 | Render | train | func (x XML) Render(w io.Writer, v interface{}) error {
var result []byte
var err error
if x.Indent {
result, err = xml.MarshalIndent(v, "", " ")
result = append(result, '\n')
} else {
result, err = xml.Marshal(v)
}
if err != nil {
return err
}
// XML marshaled fine, write out the result.
if hw, ok ... | go | {
"resource": ""
} |
q13172 | Dispatch | train | func (d *Dispatcher) Dispatch() *c.Context {
// Pipeline of tasks to execute in FIFO order
pipeline := []task{
func(ctx *c.Context) (*c.Context, bool) {
return d.runBefore("request", ctx)
},
func(ctx *c.Context) (*c.Context, bool) {
return d.runBefore("before dial", ctx)
},
func(ctx *c.Context) (*c.Co... | go | {
"resource": ""
} |
q13173 | Use | train | func (s *Layer) Use(plugin plugin.Plugin) Middleware {
s.mtx.Lock()
s.stack = append(s.stack, plugin)
s.mtx.Unlock()
return s
} | go | {
"resource": ""
} |
q13174 | UseHandler | train | func (s *Layer) UseHandler(phase string, fn c.HandlerFunc) Middleware {
s.mtx.Lock()
s.stack = append(s.stack, plugin.NewPhasePlugin(phase, fn))
s.mtx.Unlock()
return s
} | go | {
"resource": ""
} |
q13175 | Flush | train | func (s *Layer) Flush() {
s.mtx.Lock()
s.stack = s.stack[:0]
s.mtx.Unlock()
} | go | {
"resource": ""
} |
q13176 | GetStack | train | func (s *Layer) GetStack() []plugin.Plugin {
s.mtx.RLock()
defer s.mtx.RUnlock()
return s.stack
} | go | {
"resource": ""
} |
q13177 | Clone | train | func (s *Layer) Clone() Middleware {
mw := New()
mw.parent = s.parent
s.mtx.Lock()
mw.stack = append([]plugin.Plugin(nil), s.stack...)
s.mtx.Unlock()
return mw
} | go | {
"resource": ""
} |
q13178 | URL | train | func URL(uri string) p.Plugin {
return p.NewRequestPlugin(func(ctx *c.Context, h c.Handler) {
u, err := url.Parse(normalize(uri))
if err != nil {
h.Error(ctx, err)
return
}
ctx.Request.URL = u
h.Next(ctx)
})
} | go | {
"resource": ""
} |
q13179 | Path | train | func Path(path string) p.Plugin {
return p.NewRequestPlugin(func(ctx *c.Context, h c.Handler) {
ctx.Request.URL.Path = normalizePath(path)
h.Next(ctx)
})
} | go | {
"resource": ""
} |
q13180 | Param | train | func Param(key, value string) p.Plugin {
return p.NewRequestPlugin(func(ctx *c.Context, h c.Handler) {
ctx.Request.URL.Path = replace(ctx.Request.URL.Path, key, value)
h.Next(ctx)
})
} | go | {
"resource": ""
} |
q13181 | String | train | func String(data string) p.Plugin {
return p.NewRequestPlugin(func(ctx *c.Context, h c.Handler) {
ctx.Request.Method = getMethod(ctx)
ctx.Request.Body = utils.StringReader(data)
ctx.Request.ContentLength = int64(bytes.NewBufferString(data).Len())
h.Next(ctx)
})
} | go | {
"resource": ""
} |
q13182 | JSON | train | func JSON(data interface{}) p.Plugin {
return p.NewRequestPlugin(func(ctx *c.Context, h c.Handler) {
buf := &bytes.Buffer{}
switch data.(type) {
case string:
buf.WriteString(data.(string))
case []byte:
buf.Write(data.([]byte))
default:
if err := json.NewEncoder(buf).Encode(data); err != nil {
h... | go | {
"resource": ""
} |
q13183 | Reader | train | func Reader(body io.Reader) p.Plugin {
return p.NewRequestPlugin(func(ctx *c.Context, h c.Handler) {
rc, ok := body.(io.ReadCloser)
if !ok && body != nil {
rc = ioutil.NopCloser(body)
}
req := ctx.Request
if body != nil {
switch v := body.(type) {
case *bytes.Buffer:
req.ContentLength = int64(v... | go | {
"resource": ""
} |
q13184 | If | train | func If(muxes ...*Mux) *Mux {
mx := New()
for _, mm := range muxes {
mx.AddMatcher(mm.Matchers...)
}
return mx
} | go | {
"resource": ""
} |
q13185 | Or | train | func Or(muxes ...*Mux) *Mux {
return Match(func(ctx *c.Context) bool {
for _, mm := range muxes {
if mm.Match(ctx) {
return true
}
}
return false
})
} | go | {
"resource": ""
} |
q13186 | Match | train | func Match(matchers ...Matcher) *Mux {
mx := New()
mx.AddMatcher(matchers...)
return mx
} | go | {
"resource": ""
} |
q13187 | Path | train | func Path(pattern string) *Mux {
return Match(func(ctx *c.Context) bool {
if ctx.GetString("$phase") != "request" {
return false
}
matched, _ := regexp.MatchString(pattern, ctx.Request.URL.Path)
return matched
})
} | go | {
"resource": ""
} |
q13188 | Type | train | func Type(kind string) *Mux {
return Match(func(ctx *c.Context) bool {
if ctx.GetString("$phase") != "response" {
return false
}
if value, ok := types.Types[kind]; ok {
kind = value
}
return strings.Contains(ctx.Response.Header.Get("Content-Type"), kind)
})
} | go | {
"resource": ""
} |
q13189 | Status | train | func Status(codes ...int) *Mux {
return Match(func(ctx *c.Context) bool {
if ctx.GetString("$phase") != "response" {
return false
}
for _, code := range codes {
if ctx.Response.StatusCode == code {
return true
}
}
return false
})
} | go | {
"resource": ""
} |
q13190 | StatusRange | train | func StatusRange(start, end int) *Mux {
return Match(func(ctx *c.Context) bool {
return ctx.GetString("$phase") == "response" && ctx.Response.StatusCode >= start && ctx.Response.StatusCode <= end
})
} | go | {
"resource": ""
} |
q13191 | Error | train | func Error() *Mux {
return Match(func(ctx *c.Context) bool {
return (ctx.GetString("$phase") == "error" && ctx.Error != nil) ||
(ctx.GetString("$phase") == "response" && ctx.Response.StatusCode >= 500)
})
} | go | {
"resource": ""
} |
q13192 | ServerError | train | func ServerError() *Mux {
return Match(func(ctx *c.Context) bool {
return ctx.GetString("$phase") == "response" && ctx.Response.StatusCode >= 500
})
} | go | {
"resource": ""
} |
q13193 | New | train | func New() *Mux {
m := &Mux{Layer: plugin.New()}
m.Middleware = middleware.New()
handler := m.Handler()
m.DefaultHandler = handler
return m
} | go | {
"resource": ""
} |
q13194 | Match | train | func (m *Mux) Match(ctx *c.Context) bool {
for _, matcher := range m.Matchers {
if !matcher(ctx) {
return false
}
}
return true
} | go | {
"resource": ""
} |
q13195 | AddMatcher | train | func (m *Mux) AddMatcher(matchers ...Matcher) *Mux {
m.Matchers = append(m.Matchers, matchers...)
return m
} | go | {
"resource": ""
} |
q13196 | Handler | train | func (m *Mux) Handler() c.HandlerFunc {
return func(ctx *c.Context, h c.Handler) {
if !m.Match(ctx) {
h.Next(ctx)
return
}
ctx = m.Middleware.Run(ctx.GetString("$phase"), ctx)
if ctx.Error != nil {
h.Error(ctx, ctx.Error)
return
}
if ctx.Stopped {
h.Stop(ctx)
return
}
h.Next(ctx)
}... | go | {
"resource": ""
} |
q13197 | Use | train | func (m *Mux) Use(p plugin.Plugin) *Mux {
m.Middleware.Use(p)
return m
} | go | {
"resource": ""
} |
q13198 | UseHandler | train | func (m *Mux) UseHandler(phase string, fn c.HandlerFunc) *Mux {
m.Middleware.UseHandler(phase, fn)
return m
} | go | {
"resource": ""
} |
q13199 | TLS | train | func TLS(timeout time.Duration) p.Plugin {
return All(Timeouts{TLS: timeout})
} | go | {
"resource": ""
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.