id
int32 0
167k
| repo
stringlengths 5
54
| path
stringlengths 4
155
| func_name
stringlengths 1
118
| original_string
stringlengths 52
85.5k
| language
stringclasses 1
value | code
stringlengths 52
85.5k
| code_tokens
listlengths 21
1.41k
| docstring
stringlengths 6
2.61k
| docstring_tokens
listlengths 3
215
| sha
stringlengths 40
40
| url
stringlengths 85
252
|
---|---|---|---|---|---|---|---|---|---|---|---|
13,100 | emersion/go-message | entity.go | WriteTo | 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 | 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)
} | [
"func",
"(",
"e",
"*",
"Entity",
")",
"WriteTo",
"(",
"w",
"io",
".",
"Writer",
")",
"error",
"{",
"ew",
",",
"err",
":=",
"CreateWriter",
"(",
"w",
",",
"e",
".",
"Header",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"defer",
"ew",
".",
"Close",
"(",
")",
"\n\n",
"return",
"e",
".",
"writeBodyTo",
"(",
"ew",
")",
"\n",
"}"
]
| // WriteTo writes this entity's header and body to w. | [
"WriteTo",
"writes",
"this",
"entity",
"s",
"header",
"and",
"body",
"to",
"w",
"."
]
| 1e345aac1fa8858087e0443e26fd3db3dc0fc867 | https://github.com/emersion/go-message/blob/1e345aac1fa8858087e0443e26fd3db3dc0fc867/entity.go#L111-L119 |
13,101 | emersion/go-message | mail/reader.go | NewReader | 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.Entity{e})
mr = me.MultipartReader()
}
l := list.New()
l.PushBack(mr)
return &Reader{Header{e.Header}, e, l}
} | go | 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.Entity{e})
mr = me.MultipartReader()
}
l := list.New()
l.PushBack(mr)
return &Reader{Header{e.Header}, e, l}
} | [
"func",
"NewReader",
"(",
"e",
"*",
"message",
".",
"Entity",
")",
"*",
"Reader",
"{",
"mr",
":=",
"e",
".",
"MultipartReader",
"(",
")",
"\n",
"if",
"mr",
"==",
"nil",
"{",
"// Artificially create a multipart entity",
"// With this header, no error will be returned by message.NewMultipart",
"var",
"h",
"message",
".",
"Header",
"\n",
"h",
".",
"Set",
"(",
"\"",
"\"",
",",
"\"",
"\"",
")",
"\n",
"me",
",",
"_",
":=",
"message",
".",
"NewMultipart",
"(",
"h",
",",
"[",
"]",
"*",
"message",
".",
"Entity",
"{",
"e",
"}",
")",
"\n",
"mr",
"=",
"me",
".",
"MultipartReader",
"(",
")",
"\n",
"}",
"\n\n",
"l",
":=",
"list",
".",
"New",
"(",
")",
"\n",
"l",
".",
"PushBack",
"(",
"mr",
")",
"\n\n",
"return",
"&",
"Reader",
"{",
"Header",
"{",
"e",
".",
"Header",
"}",
",",
"e",
",",
"l",
"}",
"\n",
"}"
]
| // NewReader creates a new mail reader. | [
"NewReader",
"creates",
"a",
"new",
"mail",
"reader",
"."
]
| 1e345aac1fa8858087e0443e26fd3db3dc0fc867 | https://github.com/emersion/go-message/blob/1e345aac1fa8858087e0443e26fd3db3dc0fc867/mail/reader.go#L42-L57 |
13,102 | emersion/go-message | mail/reader.go | CreateReader | 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 | 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
} | [
"func",
"CreateReader",
"(",
"r",
"io",
".",
"Reader",
")",
"(",
"*",
"Reader",
",",
"error",
")",
"{",
"e",
",",
"err",
":=",
"message",
".",
"Read",
"(",
"r",
")",
"\n",
"if",
"err",
"!=",
"nil",
"&&",
"!",
"message",
".",
"IsUnknownCharset",
"(",
"err",
")",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"return",
"NewReader",
"(",
"e",
")",
",",
"err",
"\n",
"}"
]
| // CreateReader reads a mail header from r and returns a new mail reader.
//
// If the message uses an unknown transfer encoding or charset, CreateReader
// returns an error that verifies message.IsUnknownCharset, but also returns a
// Reader that can be used. | [
"CreateReader",
"reads",
"a",
"mail",
"header",
"from",
"r",
"and",
"returns",
"a",
"new",
"mail",
"reader",
".",
"If",
"the",
"message",
"uses",
"an",
"unknown",
"transfer",
"encoding",
"or",
"charset",
"CreateReader",
"returns",
"an",
"error",
"that",
"verifies",
"message",
".",
"IsUnknownCharset",
"but",
"also",
"returns",
"a",
"Reader",
"that",
"can",
"be",
"used",
"."
]
| 1e345aac1fa8858087e0443e26fd3db3dc0fc867 | https://github.com/emersion/go-message/blob/1e345aac1fa8858087e0443e26fd3db3dc0fc867/mail/reader.go#L64-L71 |
13,103 | emersion/go-message | mail/reader.go | NextPart | 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 && !message.IsUnknownCharset(err) {
return nil, err
}
if pmr := p.MultipartReader(); pmr != nil {
// This is a multipart part, read it
r.readers.PushBack(pmr)
} else {
// This is a non-multipart part, return a mail part
mp := &Part{Body: p.Body}
t, _, _ := p.Header.ContentType()
disp, _, _ := p.Header.ContentDisposition()
if disp == "inline" || (disp != "attachment" && strings.HasPrefix(t, "text/")) {
mp.Header = &InlineHeader{p.Header}
} else {
mp.Header = &AttachmentHeader{p.Header}
}
return mp, err
}
}
return nil, io.EOF
} | go | 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 && !message.IsUnknownCharset(err) {
return nil, err
}
if pmr := p.MultipartReader(); pmr != nil {
// This is a multipart part, read it
r.readers.PushBack(pmr)
} else {
// This is a non-multipart part, return a mail part
mp := &Part{Body: p.Body}
t, _, _ := p.Header.ContentType()
disp, _, _ := p.Header.ContentDisposition()
if disp == "inline" || (disp != "attachment" && strings.HasPrefix(t, "text/")) {
mp.Header = &InlineHeader{p.Header}
} else {
mp.Header = &AttachmentHeader{p.Header}
}
return mp, err
}
}
return nil, io.EOF
} | [
"func",
"(",
"r",
"*",
"Reader",
")",
"NextPart",
"(",
")",
"(",
"*",
"Part",
",",
"error",
")",
"{",
"for",
"r",
".",
"readers",
".",
"Len",
"(",
")",
">",
"0",
"{",
"e",
":=",
"r",
".",
"readers",
".",
"Back",
"(",
")",
"\n",
"mr",
":=",
"e",
".",
"Value",
".",
"(",
"message",
".",
"MultipartReader",
")",
"\n\n",
"p",
",",
"err",
":=",
"mr",
".",
"NextPart",
"(",
")",
"\n",
"if",
"err",
"==",
"io",
".",
"EOF",
"{",
"// This whole multipart entity has been read, continue with the next one",
"r",
".",
"readers",
".",
"Remove",
"(",
"e",
")",
"\n",
"continue",
"\n",
"}",
"else",
"if",
"err",
"!=",
"nil",
"&&",
"!",
"message",
".",
"IsUnknownCharset",
"(",
"err",
")",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"if",
"pmr",
":=",
"p",
".",
"MultipartReader",
"(",
")",
";",
"pmr",
"!=",
"nil",
"{",
"// This is a multipart part, read it",
"r",
".",
"readers",
".",
"PushBack",
"(",
"pmr",
")",
"\n",
"}",
"else",
"{",
"// This is a non-multipart part, return a mail part",
"mp",
":=",
"&",
"Part",
"{",
"Body",
":",
"p",
".",
"Body",
"}",
"\n",
"t",
",",
"_",
",",
"_",
":=",
"p",
".",
"Header",
".",
"ContentType",
"(",
")",
"\n",
"disp",
",",
"_",
",",
"_",
":=",
"p",
".",
"Header",
".",
"ContentDisposition",
"(",
")",
"\n",
"if",
"disp",
"==",
"\"",
"\"",
"||",
"(",
"disp",
"!=",
"\"",
"\"",
"&&",
"strings",
".",
"HasPrefix",
"(",
"t",
",",
"\"",
"\"",
")",
")",
"{",
"mp",
".",
"Header",
"=",
"&",
"InlineHeader",
"{",
"p",
".",
"Header",
"}",
"\n",
"}",
"else",
"{",
"mp",
".",
"Header",
"=",
"&",
"AttachmentHeader",
"{",
"p",
".",
"Header",
"}",
"\n",
"}",
"\n",
"return",
"mp",
",",
"err",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"nil",
",",
"io",
".",
"EOF",
"\n",
"}"
]
| // NextPart returns the next mail part. If there is no more part, io.EOF is
// returned as error.
//
// The returned Part.Body must be read completely before the next call to
// NextPart, otherwise it will be discarded.
//
// If the part uses an unknown transfer encoding or charset, NextPart returns an
// error that verifies message.IsUnknownCharset, but also returns a Part that
// can be used. | [
"NextPart",
"returns",
"the",
"next",
"mail",
"part",
".",
"If",
"there",
"is",
"no",
"more",
"part",
"io",
".",
"EOF",
"is",
"returned",
"as",
"error",
".",
"The",
"returned",
"Part",
".",
"Body",
"must",
"be",
"read",
"completely",
"before",
"the",
"next",
"call",
"to",
"NextPart",
"otherwise",
"it",
"will",
"be",
"discarded",
".",
"If",
"the",
"part",
"uses",
"an",
"unknown",
"transfer",
"encoding",
"or",
"charset",
"NextPart",
"returns",
"an",
"error",
"that",
"verifies",
"message",
".",
"IsUnknownCharset",
"but",
"also",
"returns",
"a",
"Part",
"that",
"can",
"be",
"used",
"."
]
| 1e345aac1fa8858087e0443e26fd3db3dc0fc867 | https://github.com/emersion/go-message/blob/1e345aac1fa8858087e0443e26fd3db3dc0fc867/mail/reader.go#L82-L114 |
13,104 | emersion/go-message | mail/reader.go | Close | 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 | 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
} | [
"func",
"(",
"r",
"*",
"Reader",
")",
"Close",
"(",
")",
"error",
"{",
"for",
"r",
".",
"readers",
".",
"Len",
"(",
")",
">",
"0",
"{",
"e",
":=",
"r",
".",
"readers",
".",
"Back",
"(",
")",
"\n",
"mr",
":=",
"e",
".",
"Value",
".",
"(",
"message",
".",
"MultipartReader",
")",
"\n\n",
"if",
"err",
":=",
"mr",
".",
"Close",
"(",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"r",
".",
"readers",
".",
"Remove",
"(",
"e",
")",
"\n",
"}",
"\n\n",
"return",
"nil",
"\n",
"}"
]
| // Close finishes the reader. | [
"Close",
"finishes",
"the",
"reader",
"."
]
| 1e345aac1fa8858087e0443e26fd3db3dc0fc867 | https://github.com/emersion/go-message/blob/1e345aac1fa8858087e0443e26fd3db3dc0fc867/mail/reader.go#L117-L130 |
13,105 | emersion/go-message | charset/charset.go | Reader | 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), nil
}
return nil, fmt.Errorf("unhandled charset %q", charset)
} | go | 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), nil
}
return nil, fmt.Errorf("unhandled charset %q", charset)
} | [
"func",
"Reader",
"(",
"charset",
"string",
",",
"input",
"io",
".",
"Reader",
")",
"(",
"io",
".",
"Reader",
",",
"error",
")",
"{",
"charset",
"=",
"strings",
".",
"ToLower",
"(",
"charset",
")",
"\n",
"// \"ascii\" is not in the spec but is common",
"if",
"charset",
"==",
"\"",
"\"",
"||",
"charset",
"==",
"\"",
"\"",
"||",
"charset",
"==",
"\"",
"\"",
"{",
"return",
"input",
",",
"nil",
"\n",
"}",
"\n",
"if",
"enc",
",",
"ok",
":=",
"charsets",
"[",
"charset",
"]",
";",
"ok",
"{",
"return",
"enc",
".",
"NewDecoder",
"(",
")",
".",
"Reader",
"(",
"input",
")",
",",
"nil",
"\n",
"}",
"\n",
"return",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"charset",
")",
"\n",
"}"
]
| // Reader returns an io.Reader that converts the provided charset to UTF-8. | [
"Reader",
"returns",
"an",
"io",
".",
"Reader",
"that",
"converts",
"the",
"provided",
"charset",
"to",
"UTF",
"-",
"8",
"."
]
| 1e345aac1fa8858087e0443e26fd3db3dc0fc867 | https://github.com/emersion/go-message/blob/1e345aac1fa8858087e0443e26fd3db3dc0fc867/charset/charset.go#L50-L60 |
13,106 | emersion/go-message | charset.go | charsetReader | 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)
}
return input, fmt.Errorf("message: unhandled charset %q", charset)
} | go | 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)
}
return input, fmt.Errorf("message: unhandled charset %q", charset)
} | [
"func",
"charsetReader",
"(",
"charset",
"string",
",",
"input",
"io",
".",
"Reader",
")",
"(",
"io",
".",
"Reader",
",",
"error",
")",
"{",
"charset",
"=",
"strings",
".",
"ToLower",
"(",
"charset",
")",
"\n",
"// \"ascii\" is not in the spec but is common",
"if",
"charset",
"==",
"\"",
"\"",
"||",
"charset",
"==",
"\"",
"\"",
"||",
"charset",
"==",
"\"",
"\"",
"{",
"return",
"input",
",",
"nil",
"\n",
"}",
"\n",
"if",
"CharsetReader",
"!=",
"nil",
"{",
"return",
"CharsetReader",
"(",
"charset",
",",
"input",
")",
"\n",
"}",
"\n",
"return",
"input",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"charset",
")",
"\n",
"}"
]
| // charsetReader calls CharsetReader if non-nil. | [
"charsetReader",
"calls",
"CharsetReader",
"if",
"non",
"-",
"nil",
"."
]
| 1e345aac1fa8858087e0443e26fd3db3dc0fc867 | https://github.com/emersion/go-message/blob/1e345aac1fa8858087e0443e26fd3db3dc0fc867/charset.go#L32-L42 |
13,107 | emersion/go-message | charset.go | decodeHeader | 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 | 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
} | [
"func",
"decodeHeader",
"(",
"s",
"string",
")",
"(",
"string",
",",
"error",
")",
"{",
"wordDecoder",
":=",
"mime",
".",
"WordDecoder",
"{",
"CharsetReader",
":",
"charsetReader",
"}",
"\n",
"dec",
",",
"err",
":=",
"wordDecoder",
".",
"DecodeHeader",
"(",
"s",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"s",
",",
"err",
"\n",
"}",
"\n",
"return",
"dec",
",",
"nil",
"\n",
"}"
]
| // decodeHeader decodes an internationalized header field. If it fails, it
// returns the input string and the error. | [
"decodeHeader",
"decodes",
"an",
"internationalized",
"header",
"field",
".",
"If",
"it",
"fails",
"it",
"returns",
"the",
"input",
"string",
"and",
"the",
"error",
"."
]
| 1e345aac1fa8858087e0443e26fd3db3dc0fc867 | https://github.com/emersion/go-message/blob/1e345aac1fa8858087e0443e26fd3db3dc0fc867/charset.go#L46-L53 |
13,108 | emersion/go-message | writer.go | createWriter | 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 not used (only Write is used), then the final boundary
// is expected to be written by the user too. In this case, ww.Close
// shouldn't write the final boundary.
if mediaParams["boundary"] != "" {
ww.mw.SetBoundary(mediaParams["boundary"])
} else {
mediaParams["boundary"] = ww.mw.Boundary()
header.SetContentType(mediaType, mediaParams)
}
header.Del("Content-Transfer-Encoding")
} else {
wc, err := encodingWriter(header.Get("Content-Transfer-Encoding"), ww.w)
if err != nil {
return nil, err
}
ww.w = wc
ww.c = wc
}
switch strings.ToLower(mediaParams["charset"]) {
case "", "us-ascii", "utf-8":
// This is OK
default:
// Anything else is invalid
return nil, fmt.Errorf("unhandled charset %q", mediaParams["charset"])
}
return ww, nil
} | go | 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 not used (only Write is used), then the final boundary
// is expected to be written by the user too. In this case, ww.Close
// shouldn't write the final boundary.
if mediaParams["boundary"] != "" {
ww.mw.SetBoundary(mediaParams["boundary"])
} else {
mediaParams["boundary"] = ww.mw.Boundary()
header.SetContentType(mediaType, mediaParams)
}
header.Del("Content-Transfer-Encoding")
} else {
wc, err := encodingWriter(header.Get("Content-Transfer-Encoding"), ww.w)
if err != nil {
return nil, err
}
ww.w = wc
ww.c = wc
}
switch strings.ToLower(mediaParams["charset"]) {
case "", "us-ascii", "utf-8":
// This is OK
default:
// Anything else is invalid
return nil, fmt.Errorf("unhandled charset %q", mediaParams["charset"])
}
return ww, nil
} | [
"func",
"createWriter",
"(",
"w",
"io",
".",
"Writer",
",",
"header",
"*",
"Header",
")",
"(",
"*",
"Writer",
",",
"error",
")",
"{",
"ww",
":=",
"&",
"Writer",
"{",
"w",
":",
"w",
"}",
"\n\n",
"mediaType",
",",
"mediaParams",
",",
"_",
":=",
"header",
".",
"ContentType",
"(",
")",
"\n",
"if",
"strings",
".",
"HasPrefix",
"(",
"mediaType",
",",
"\"",
"\"",
")",
"{",
"ww",
".",
"mw",
"=",
"multipart",
".",
"NewWriter",
"(",
"ww",
".",
"w",
")",
"\n\n",
"// Do not set ww's io.Closer for now: if this is a multipart entity but",
"// CreatePart is not used (only Write is used), then the final boundary",
"// is expected to be written by the user too. In this case, ww.Close",
"// shouldn't write the final boundary.",
"if",
"mediaParams",
"[",
"\"",
"\"",
"]",
"!=",
"\"",
"\"",
"{",
"ww",
".",
"mw",
".",
"SetBoundary",
"(",
"mediaParams",
"[",
"\"",
"\"",
"]",
")",
"\n",
"}",
"else",
"{",
"mediaParams",
"[",
"\"",
"\"",
"]",
"=",
"ww",
".",
"mw",
".",
"Boundary",
"(",
")",
"\n",
"header",
".",
"SetContentType",
"(",
"mediaType",
",",
"mediaParams",
")",
"\n",
"}",
"\n\n",
"header",
".",
"Del",
"(",
"\"",
"\"",
")",
"\n",
"}",
"else",
"{",
"wc",
",",
"err",
":=",
"encodingWriter",
"(",
"header",
".",
"Get",
"(",
"\"",
"\"",
")",
",",
"ww",
".",
"w",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"ww",
".",
"w",
"=",
"wc",
"\n",
"ww",
".",
"c",
"=",
"wc",
"\n",
"}",
"\n\n",
"switch",
"strings",
".",
"ToLower",
"(",
"mediaParams",
"[",
"\"",
"\"",
"]",
")",
"{",
"case",
"\"",
"\"",
",",
"\"",
"\"",
",",
"\"",
"\"",
":",
"// This is OK",
"default",
":",
"// Anything else is invalid",
"return",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"mediaParams",
"[",
"\"",
"\"",
"]",
")",
"\n",
"}",
"\n\n",
"return",
"ww",
",",
"nil",
"\n",
"}"
]
| // createWriter creates a new Writer writing to w with the provided header.
// Nothing is written to w when it is called. header is modified in-place. | [
"createWriter",
"creates",
"a",
"new",
"Writer",
"writing",
"to",
"w",
"with",
"the",
"provided",
"header",
".",
"Nothing",
"is",
"written",
"to",
"w",
"when",
"it",
"is",
"called",
".",
"header",
"is",
"modified",
"in",
"-",
"place",
"."
]
| 1e345aac1fa8858087e0443e26fd3db3dc0fc867 | https://github.com/emersion/go-message/blob/1e345aac1fa8858087e0443e26fd3db3dc0fc867/writer.go#L29-L67 |
13,109 | emersion/go-message | writer.go | CreateWriter | 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 | 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
} | [
"func",
"CreateWriter",
"(",
"w",
"io",
".",
"Writer",
",",
"header",
"Header",
")",
"(",
"*",
"Writer",
",",
"error",
")",
"{",
"ww",
",",
"err",
":=",
"createWriter",
"(",
"w",
",",
"&",
"header",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"if",
"err",
":=",
"textproto",
".",
"WriteHeader",
"(",
"w",
",",
"header",
".",
"Header",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"return",
"ww",
",",
"nil",
"\n",
"}"
]
| // CreateWriter creates a new message writer to w. If header contains an
// encoding, data written to the Writer will automatically be encoded with it.
// The charset needs to be utf-8 or us-ascii. | [
"CreateWriter",
"creates",
"a",
"new",
"message",
"writer",
"to",
"w",
".",
"If",
"header",
"contains",
"an",
"encoding",
"data",
"written",
"to",
"the",
"Writer",
"will",
"automatically",
"be",
"encoded",
"with",
"it",
".",
"The",
"charset",
"needs",
"to",
"be",
"utf",
"-",
"8",
"or",
"us",
"-",
"ascii",
"."
]
| 1e345aac1fa8858087e0443e26fd3db3dc0fc867 | https://github.com/emersion/go-message/blob/1e345aac1fa8858087e0443e26fd3db3dc0fc867/writer.go#L72-L81 |
13,110 | emersion/go-message | writer.go | CreatePart | 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
ww := &struct{ io.Writer }{nil}
cw, err := createWriter(ww, &header)
if err != nil {
return nil, err
}
pw, err := w.mw.CreatePart(headerToMap(header.Header))
if err != nil {
return nil, err
}
ww.Writer = pw
return cw, nil
} | go | 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
ww := &struct{ io.Writer }{nil}
cw, err := createWriter(ww, &header)
if err != nil {
return nil, err
}
pw, err := w.mw.CreatePart(headerToMap(header.Header))
if err != nil {
return nil, err
}
ww.Writer = pw
return cw, nil
} | [
"func",
"(",
"w",
"*",
"Writer",
")",
"CreatePart",
"(",
"header",
"Header",
")",
"(",
"*",
"Writer",
",",
"error",
")",
"{",
"if",
"w",
".",
"mw",
"==",
"nil",
"{",
"return",
"nil",
",",
"errors",
".",
"New",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"if",
"w",
".",
"c",
"==",
"nil",
"{",
"// We know that the user calls CreatePart so Close should write the final",
"// boundary",
"w",
".",
"c",
"=",
"w",
".",
"mw",
"\n",
"}",
"\n\n",
"// cw -> ww -> pw -> w.mw -> w.w",
"ww",
":=",
"&",
"struct",
"{",
"io",
".",
"Writer",
"}",
"{",
"nil",
"}",
"\n",
"cw",
",",
"err",
":=",
"createWriter",
"(",
"ww",
",",
"&",
"header",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"pw",
",",
"err",
":=",
"w",
".",
"mw",
".",
"CreatePart",
"(",
"headerToMap",
"(",
"header",
".",
"Header",
")",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"ww",
".",
"Writer",
"=",
"pw",
"\n",
"return",
"cw",
",",
"nil",
"\n",
"}"
]
| // CreatePart returns a Writer to a new part in this multipart entity. If this
// entity is not multipart, it fails. The body of the part should be written to
// the returned io.WriteCloser. | [
"CreatePart",
"returns",
"a",
"Writer",
"to",
"a",
"new",
"part",
"in",
"this",
"multipart",
"entity",
".",
"If",
"this",
"entity",
"is",
"not",
"multipart",
"it",
"fails",
".",
"The",
"body",
"of",
"the",
"part",
"should",
"be",
"written",
"to",
"the",
"returned",
"io",
".",
"WriteCloser",
"."
]
| 1e345aac1fa8858087e0443e26fd3db3dc0fc867 | https://github.com/emersion/go-message/blob/1e345aac1fa8858087e0443e26fd3db3dc0fc867/writer.go#L99-L124 |
13,111 | emersion/go-message | textproto/header.go | Add | 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 | 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)
} | [
"func",
"(",
"h",
"*",
"Header",
")",
"Add",
"(",
"k",
",",
"v",
"string",
")",
"{",
"k",
"=",
"textproto",
".",
"CanonicalMIMEHeaderKey",
"(",
"k",
")",
"\n\n",
"if",
"h",
".",
"m",
"==",
"nil",
"{",
"h",
".",
"m",
"=",
"make",
"(",
"map",
"[",
"string",
"]",
"[",
"]",
"*",
"headerField",
")",
"\n",
"}",
"\n\n",
"h",
".",
"l",
"=",
"append",
"(",
"h",
".",
"l",
",",
"newHeaderField",
"(",
"k",
",",
"v",
",",
"nil",
")",
")",
"\n",
"f",
":=",
"&",
"h",
".",
"l",
"[",
"len",
"(",
"h",
".",
"l",
")",
"-",
"1",
"]",
"\n",
"h",
".",
"m",
"[",
"k",
"]",
"=",
"append",
"(",
"h",
".",
"m",
"[",
"k",
"]",
",",
"f",
")",
"\n",
"}"
]
| // Add adds the key, value pair to the header. It prepends to any existing
// fields associated with key. | [
"Add",
"adds",
"the",
"key",
"value",
"pair",
"to",
"the",
"header",
".",
"It",
"prepends",
"to",
"any",
"existing",
"fields",
"associated",
"with",
"key",
"."
]
| 1e345aac1fa8858087e0443e26fd3db3dc0fc867 | https://github.com/emersion/go-message/blob/1e345aac1fa8858087e0443e26fd3db3dc0fc867/textproto/header.go#L60-L70 |
13,112 | emersion/go-message | textproto/header.go | Get | 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 | func (h *Header) Get(k string) string {
fields := h.m[textproto.CanonicalMIMEHeaderKey(k)]
if len(fields) == 0 {
return ""
}
return fields[len(fields)-1].v
} | [
"func",
"(",
"h",
"*",
"Header",
")",
"Get",
"(",
"k",
"string",
")",
"string",
"{",
"fields",
":=",
"h",
".",
"m",
"[",
"textproto",
".",
"CanonicalMIMEHeaderKey",
"(",
"k",
")",
"]",
"\n",
"if",
"len",
"(",
"fields",
")",
"==",
"0",
"{",
"return",
"\"",
"\"",
"\n",
"}",
"\n",
"return",
"fields",
"[",
"len",
"(",
"fields",
")",
"-",
"1",
"]",
".",
"v",
"\n",
"}"
]
| // Get gets the first value associated with the given key. If there are no
// values associated with the key, Get returns "". | [
"Get",
"gets",
"the",
"first",
"value",
"associated",
"with",
"the",
"given",
"key",
".",
"If",
"there",
"are",
"no",
"values",
"associated",
"with",
"the",
"key",
"Get",
"returns",
"."
]
| 1e345aac1fa8858087e0443e26fd3db3dc0fc867 | https://github.com/emersion/go-message/blob/1e345aac1fa8858087e0443e26fd3db3dc0fc867/textproto/header.go#L74-L80 |
13,113 | emersion/go-message | textproto/header.go | Set | func (h *Header) Set(k, v string) {
h.Del(k)
h.Add(k, v)
} | go | func (h *Header) Set(k, v string) {
h.Del(k)
h.Add(k, v)
} | [
"func",
"(",
"h",
"*",
"Header",
")",
"Set",
"(",
"k",
",",
"v",
"string",
")",
"{",
"h",
".",
"Del",
"(",
"k",
")",
"\n",
"h",
".",
"Add",
"(",
"k",
",",
"v",
")",
"\n",
"}"
]
| // Set sets the header fields associated with key to the single field value.
// It replaces any existing values associated with key. | [
"Set",
"sets",
"the",
"header",
"fields",
"associated",
"with",
"key",
"to",
"the",
"single",
"field",
"value",
".",
"It",
"replaces",
"any",
"existing",
"values",
"associated",
"with",
"key",
"."
]
| 1e345aac1fa8858087e0443e26fd3db3dc0fc867 | https://github.com/emersion/go-message/blob/1e345aac1fa8858087e0443e26fd3db3dc0fc867/textproto/header.go#L84-L87 |
13,114 | emersion/go-message | textproto/header.go | Has | func (h *Header) Has(k string) bool {
_, ok := h.m[textproto.CanonicalMIMEHeaderKey(k)]
return ok
} | go | func (h *Header) Has(k string) bool {
_, ok := h.m[textproto.CanonicalMIMEHeaderKey(k)]
return ok
} | [
"func",
"(",
"h",
"*",
"Header",
")",
"Has",
"(",
"k",
"string",
")",
"bool",
"{",
"_",
",",
"ok",
":=",
"h",
".",
"m",
"[",
"textproto",
".",
"CanonicalMIMEHeaderKey",
"(",
"k",
")",
"]",
"\n",
"return",
"ok",
"\n",
"}"
]
| // Has checks whether the header has a field with the specified key. | [
"Has",
"checks",
"whether",
"the",
"header",
"has",
"a",
"field",
"with",
"the",
"specified",
"key",
"."
]
| 1e345aac1fa8858087e0443e26fd3db3dc0fc867 | https://github.com/emersion/go-message/blob/1e345aac1fa8858087e0443e26fd3db3dc0fc867/textproto/header.go#L104-L107 |
13,115 | emersion/go-message | textproto/header.go | FieldsByKey | func (h *Header) FieldsByKey(k string) HeaderFields {
return &headerFieldsByKey{h, textproto.CanonicalMIMEHeaderKey(k), -1}
} | go | func (h *Header) FieldsByKey(k string) HeaderFields {
return &headerFieldsByKey{h, textproto.CanonicalMIMEHeaderKey(k), -1}
} | [
"func",
"(",
"h",
"*",
"Header",
")",
"FieldsByKey",
"(",
"k",
"string",
")",
"HeaderFields",
"{",
"return",
"&",
"headerFieldsByKey",
"{",
"h",
",",
"textproto",
".",
"CanonicalMIMEHeaderKey",
"(",
"k",
")",
",",
"-",
"1",
"}",
"\n",
"}"
]
| // FieldsByKey iterates over all fields having the specified key.
//
// The header may not be mutated while iterating, except using HeaderFields.Del. | [
"FieldsByKey",
"iterates",
"over",
"all",
"fields",
"having",
"the",
"specified",
"key",
".",
"The",
"header",
"may",
"not",
"be",
"mutated",
"while",
"iterating",
"except",
"using",
"HeaderFields",
".",
"Del",
"."
]
| 1e345aac1fa8858087e0443e26fd3db3dc0fc867 | https://github.com/emersion/go-message/blob/1e345aac1fa8858087e0443e26fd3db3dc0fc867/textproto/header.go#L234-L236 |
13,116 | emersion/go-message | textproto/header.go | trim | 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 | 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]
} | [
"func",
"trim",
"(",
"s",
"[",
"]",
"byte",
")",
"[",
"]",
"byte",
"{",
"i",
":=",
"0",
"\n",
"for",
"i",
"<",
"len",
"(",
"s",
")",
"&&",
"isSpace",
"(",
"s",
"[",
"i",
"]",
")",
"{",
"i",
"++",
"\n",
"}",
"\n",
"n",
":=",
"len",
"(",
"s",
")",
"\n",
"for",
"n",
">",
"i",
"&&",
"isSpace",
"(",
"s",
"[",
"n",
"-",
"1",
"]",
")",
"{",
"n",
"--",
"\n",
"}",
"\n",
"return",
"s",
"[",
"i",
":",
"n",
"]",
"\n",
"}"
]
| // trim returns s with leading and trailing spaces and tabs removed.
// It does not assume Unicode or UTF-8. | [
"trim",
"returns",
"s",
"with",
"leading",
"and",
"trailing",
"spaces",
"and",
"tabs",
"removed",
".",
"It",
"does",
"not",
"assume",
"Unicode",
"or",
"UTF",
"-",
"8",
"."
]
| 1e345aac1fa8858087e0443e26fd3db3dc0fc867 | https://github.com/emersion/go-message/blob/1e345aac1fa8858087e0443e26fd3db3dc0fc867/textproto/header.go#L265-L275 |
13,117 | emersion/go-message | textproto/header.go | skipSpace | 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 | 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
} | [
"func",
"skipSpace",
"(",
"r",
"*",
"bufio",
".",
"Reader",
")",
"int",
"{",
"n",
":=",
"0",
"\n",
"for",
"{",
"c",
",",
"err",
":=",
"r",
".",
"ReadByte",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"// bufio will keep err until next read.",
"break",
"\n",
"}",
"\n",
"if",
"!",
"isSpace",
"(",
"c",
")",
"{",
"r",
".",
"UnreadByte",
"(",
")",
"\n",
"break",
"\n",
"}",
"\n",
"n",
"++",
"\n",
"}",
"\n",
"return",
"n",
"\n",
"}"
]
| // skipSpace skips R over all spaces and returns the number of bytes skipped. | [
"skipSpace",
"skips",
"R",
"over",
"all",
"spaces",
"and",
"returns",
"the",
"number",
"of",
"bytes",
"skipped",
"."
]
| 1e345aac1fa8858087e0443e26fd3db3dc0fc867 | https://github.com/emersion/go-message/blob/1e345aac1fa8858087e0443e26fd3db3dc0fc867/textproto/header.go#L278-L293 |
13,118 | emersion/go-message | textproto/header.go | trimAroundNewlines | 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 | 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()
} | [
"func",
"trimAroundNewlines",
"(",
"v",
"[",
"]",
"byte",
")",
"string",
"{",
"var",
"b",
"strings",
".",
"Builder",
"\n",
"for",
"{",
"i",
":=",
"bytes",
".",
"IndexByte",
"(",
"v",
",",
"'\\n'",
")",
"\n",
"if",
"i",
"<",
"0",
"{",
"writeContinued",
"(",
"&",
"b",
",",
"v",
")",
"\n",
"break",
"\n",
"}",
"\n",
"writeContinued",
"(",
"&",
"b",
",",
"v",
"[",
":",
"i",
"]",
")",
"\n",
"v",
"=",
"v",
"[",
"i",
"+",
"1",
":",
"]",
"\n",
"}",
"\n\n",
"return",
"b",
".",
"String",
"(",
")",
"\n",
"}"
]
| // Strip newlines and spaces around newlines. | [
"Strip",
"newlines",
"and",
"spaces",
"around",
"newlines",
"."
]
| 1e345aac1fa8858087e0443e26fd3db3dc0fc867 | https://github.com/emersion/go-message/blob/1e345aac1fa8858087e0443e26fd3db3dc0fc867/textproto/header.go#L346-L359 |
13,119 | emersion/go-message | textproto/header.go | formatHeaderField | 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) {
// We reached the end of the string
if v[len(v)-1] != '\n' {
// If there isn't already a trailing CRLF, insert one
folding = "\r\n"
}
} else {
// Find the last QP character before limit
foldAtQP := qpReg.FindAllStringIndex(v[:foldBefore], -1)
// Find the closest whitespace before i
foldAtEOL := strings.LastIndexAny(v[:foldBefore], " \t\n")
// Fold at the latest whitespace by default
foldAt = foldAtEOL
// if there are QP characters in the string
if len(foldAtQP) > 0 {
// Get the start index of the last QP character
foldAtQPLastIndex := foldAtQP[len(foldAtQP)-1][0]
if foldAtQPLastIndex > foldAt {
// Fold at the latest QP character if there are no whitespaces after it and before line hard limit
foldAt = foldAtQPLastIndex
}
}
if foldAt == 0 {
// The whitespace we found was the previous folding WSP
foldAt = foldBefore - 1
} else if foldAt < 0 {
// We didn't find any whitespace, we have to insert one
foldAt = foldBefore - 2
}
switch v[foldAt] {
case ' ', '\t':
if v[foldAt-1] != '\n' {
folding = "\r\n" // The next char will be a WSP, don't need to insert one
}
case '\n':
folding = "" // There is already a CRLF, nothing to do
default:
folding = "\r\n " // Another char, we need to insert CRLF + WSP
}
}
s += v[:foldAt] + folding
v = v[foldAt:]
first = false
}
return s
} | go | 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) {
// We reached the end of the string
if v[len(v)-1] != '\n' {
// If there isn't already a trailing CRLF, insert one
folding = "\r\n"
}
} else {
// Find the last QP character before limit
foldAtQP := qpReg.FindAllStringIndex(v[:foldBefore], -1)
// Find the closest whitespace before i
foldAtEOL := strings.LastIndexAny(v[:foldBefore], " \t\n")
// Fold at the latest whitespace by default
foldAt = foldAtEOL
// if there are QP characters in the string
if len(foldAtQP) > 0 {
// Get the start index of the last QP character
foldAtQPLastIndex := foldAtQP[len(foldAtQP)-1][0]
if foldAtQPLastIndex > foldAt {
// Fold at the latest QP character if there are no whitespaces after it and before line hard limit
foldAt = foldAtQPLastIndex
}
}
if foldAt == 0 {
// The whitespace we found was the previous folding WSP
foldAt = foldBefore - 1
} else if foldAt < 0 {
// We didn't find any whitespace, we have to insert one
foldAt = foldBefore - 2
}
switch v[foldAt] {
case ' ', '\t':
if v[foldAt-1] != '\n' {
folding = "\r\n" // The next char will be a WSP, don't need to insert one
}
case '\n':
folding = "" // There is already a CRLF, nothing to do
default:
folding = "\r\n " // Another char, we need to insert CRLF + WSP
}
}
s += v[:foldAt] + folding
v = v[foldAt:]
first = false
}
return s
} | [
"func",
"formatHeaderField",
"(",
"k",
",",
"v",
"string",
")",
"string",
"{",
"s",
":=",
"k",
"+",
"\"",
"\"",
"\n\n",
"if",
"v",
"==",
"\"",
"\"",
"{",
"return",
"s",
"+",
"\"",
"\\r",
"\\n",
"\"",
"\n",
"}",
"\n\n",
"first",
":=",
"true",
"\n",
"for",
"len",
"(",
"v",
")",
">",
"0",
"{",
"maxlen",
":=",
"maxHeaderLen",
"\n",
"if",
"first",
"{",
"maxlen",
"-=",
"len",
"(",
"s",
")",
"\n",
"}",
"\n\n",
"// We'll need to fold before i",
"foldBefore",
":=",
"maxlen",
"+",
"1",
"\n",
"foldAt",
":=",
"len",
"(",
"v",
")",
"\n\n",
"var",
"folding",
"string",
"\n",
"if",
"foldBefore",
">",
"len",
"(",
"v",
")",
"{",
"// We reached the end of the string",
"if",
"v",
"[",
"len",
"(",
"v",
")",
"-",
"1",
"]",
"!=",
"'\\n'",
"{",
"// If there isn't already a trailing CRLF, insert one",
"folding",
"=",
"\"",
"\\r",
"\\n",
"\"",
"\n",
"}",
"\n",
"}",
"else",
"{",
"// Find the last QP character before limit",
"foldAtQP",
":=",
"qpReg",
".",
"FindAllStringIndex",
"(",
"v",
"[",
":",
"foldBefore",
"]",
",",
"-",
"1",
")",
"\n",
"// Find the closest whitespace before i",
"foldAtEOL",
":=",
"strings",
".",
"LastIndexAny",
"(",
"v",
"[",
":",
"foldBefore",
"]",
",",
"\"",
"\\t",
"\\n",
"\"",
")",
"\n\n",
"// Fold at the latest whitespace by default",
"foldAt",
"=",
"foldAtEOL",
"\n\n",
"// if there are QP characters in the string",
"if",
"len",
"(",
"foldAtQP",
")",
">",
"0",
"{",
"// Get the start index of the last QP character",
"foldAtQPLastIndex",
":=",
"foldAtQP",
"[",
"len",
"(",
"foldAtQP",
")",
"-",
"1",
"]",
"[",
"0",
"]",
"\n",
"if",
"foldAtQPLastIndex",
">",
"foldAt",
"{",
"// Fold at the latest QP character if there are no whitespaces after it and before line hard limit",
"foldAt",
"=",
"foldAtQPLastIndex",
"\n",
"}",
"\n",
"}",
"\n\n",
"if",
"foldAt",
"==",
"0",
"{",
"// The whitespace we found was the previous folding WSP",
"foldAt",
"=",
"foldBefore",
"-",
"1",
"\n",
"}",
"else",
"if",
"foldAt",
"<",
"0",
"{",
"// We didn't find any whitespace, we have to insert one",
"foldAt",
"=",
"foldBefore",
"-",
"2",
"\n",
"}",
"\n\n",
"switch",
"v",
"[",
"foldAt",
"]",
"{",
"case",
"' '",
",",
"'\\t'",
":",
"if",
"v",
"[",
"foldAt",
"-",
"1",
"]",
"!=",
"'\\n'",
"{",
"folding",
"=",
"\"",
"\\r",
"\\n",
"\"",
"// The next char will be a WSP, don't need to insert one",
"\n",
"}",
"\n",
"case",
"'\\n'",
":",
"folding",
"=",
"\"",
"\"",
"// There is already a CRLF, nothing to do",
"\n",
"default",
":",
"folding",
"=",
"\"",
"\\r",
"\\n",
"\"",
"// Another char, we need to insert CRLF + WSP",
"\n",
"}",
"\n",
"}",
"\n\n",
"s",
"+=",
"v",
"[",
":",
"foldAt",
"]",
"+",
"folding",
"\n",
"v",
"=",
"v",
"[",
"foldAt",
":",
"]",
"\n",
"first",
"=",
"false",
"\n",
"}",
"\n\n",
"return",
"s",
"\n",
"}"
]
| // formatHeaderField formats a header field, ensuring each line is no longer
// than 76 characters. It tries to fold lines at whitespace characters if
// possible. If the header contains a word longer than this limit, it will be
// split. | [
"formatHeaderField",
"formats",
"a",
"header",
"field",
"ensuring",
"each",
"line",
"is",
"no",
"longer",
"than",
"76",
"characters",
".",
"It",
"tries",
"to",
"fold",
"lines",
"at",
"whitespace",
"characters",
"if",
"possible",
".",
"If",
"the",
"header",
"contains",
"a",
"word",
"longer",
"than",
"this",
"limit",
"it",
"will",
"be",
"split",
"."
]
| 1e345aac1fa8858087e0443e26fd3db3dc0fc867 | https://github.com/emersion/go-message/blob/1e345aac1fa8858087e0443e26fd3db3dc0fc867/textproto/header.go#L419-L489 |
13,120 | emersion/go-message | textproto/header.go | WriteHeader | 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 | 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
} | [
"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",
"]",
"\n\n",
"if",
"f",
".",
"b",
"==",
"nil",
"{",
"f",
".",
"b",
"=",
"[",
"]",
"byte",
"(",
"formatHeaderField",
"(",
"f",
".",
"k",
",",
"f",
".",
"v",
")",
")",
"\n",
"}",
"\n\n",
"if",
"_",
",",
"err",
":=",
"w",
".",
"Write",
"(",
"f",
".",
"b",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"}",
"\n\n",
"_",
",",
"err",
":=",
"w",
".",
"Write",
"(",
"[",
"]",
"byte",
"{",
"'\\r'",
",",
"'\\n'",
"}",
")",
"\n",
"return",
"err",
"\n",
"}"
]
| // WriteHeader writes a MIME header to w. | [
"WriteHeader",
"writes",
"a",
"MIME",
"header",
"to",
"w",
"."
]
| 1e345aac1fa8858087e0443e26fd3db3dc0fc867 | https://github.com/emersion/go-message/blob/1e345aac1fa8858087e0443e26fd3db3dc0fc867/textproto/header.go#L492-L509 |
13,121 | emersion/go-message | mail/writer.go | CreateWriter | 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 | 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
} | [
"func",
"CreateWriter",
"(",
"w",
"io",
".",
"Writer",
",",
"header",
"Header",
")",
"(",
"*",
"Writer",
",",
"error",
")",
"{",
"header",
".",
"Set",
"(",
"\"",
"\"",
",",
"\"",
"\"",
")",
"\n\n",
"mw",
",",
"err",
":=",
"message",
".",
"CreateWriter",
"(",
"w",
",",
"header",
".",
"Header",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"return",
"&",
"Writer",
"{",
"mw",
"}",
",",
"nil",
"\n",
"}"
]
| // CreateWriter writes a mail header to w and creates a new Writer. | [
"CreateWriter",
"writes",
"a",
"mail",
"header",
"to",
"w",
"and",
"creates",
"a",
"new",
"Writer",
"."
]
| 1e345aac1fa8858087e0443e26fd3db3dc0fc867 | https://github.com/emersion/go-message/blob/1e345aac1fa8858087e0443e26fd3db3dc0fc867/mail/writer.go#L39-L48 |
13,122 | emersion/go-message | mail/writer.go | CreateInline | 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 | 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
} | [
"func",
"(",
"w",
"*",
"Writer",
")",
"CreateInline",
"(",
")",
"(",
"*",
"InlineWriter",
",",
"error",
")",
"{",
"var",
"h",
"message",
".",
"Header",
"\n",
"h",
".",
"Set",
"(",
"\"",
"\"",
",",
"\"",
"\"",
")",
"\n\n",
"mw",
",",
"err",
":=",
"w",
".",
"mw",
".",
"CreatePart",
"(",
"h",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"return",
"&",
"InlineWriter",
"{",
"mw",
"}",
",",
"nil",
"\n",
"}"
]
| // CreateInline creates a InlineWriter. One or more parts representing the same
// text in different formats can be written to a InlineWriter. | [
"CreateInline",
"creates",
"a",
"InlineWriter",
".",
"One",
"or",
"more",
"parts",
"representing",
"the",
"same",
"text",
"in",
"different",
"formats",
"can",
"be",
"written",
"to",
"a",
"InlineWriter",
"."
]
| 1e345aac1fa8858087e0443e26fd3db3dc0fc867 | https://github.com/emersion/go-message/blob/1e345aac1fa8858087e0443e26fd3db3dc0fc867/mail/writer.go#L52-L61 |
13,123 | emersion/go-message | mail/writer.go | CreateSingleInline | func (w *Writer) CreateSingleInline(h InlineHeader) (io.WriteCloser, error) {
initInlineHeader(&h)
return w.mw.CreatePart(h.Header)
} | go | func (w *Writer) CreateSingleInline(h InlineHeader) (io.WriteCloser, error) {
initInlineHeader(&h)
return w.mw.CreatePart(h.Header)
} | [
"func",
"(",
"w",
"*",
"Writer",
")",
"CreateSingleInline",
"(",
"h",
"InlineHeader",
")",
"(",
"io",
".",
"WriteCloser",
",",
"error",
")",
"{",
"initInlineHeader",
"(",
"&",
"h",
")",
"\n",
"return",
"w",
".",
"mw",
".",
"CreatePart",
"(",
"h",
".",
"Header",
")",
"\n",
"}"
]
| // CreateSingleInline creates a new single text part with the provided header.
// The body of the part should be written to the returned io.WriteCloser. Only
// one single text part should be written, use CreateInline if you want multiple
// text parts. | [
"CreateSingleInline",
"creates",
"a",
"new",
"single",
"text",
"part",
"with",
"the",
"provided",
"header",
".",
"The",
"body",
"of",
"the",
"part",
"should",
"be",
"written",
"to",
"the",
"returned",
"io",
".",
"WriteCloser",
".",
"Only",
"one",
"single",
"text",
"part",
"should",
"be",
"written",
"use",
"CreateInline",
"if",
"you",
"want",
"multiple",
"text",
"parts",
"."
]
| 1e345aac1fa8858087e0443e26fd3db3dc0fc867 | https://github.com/emersion/go-message/blob/1e345aac1fa8858087e0443e26fd3db3dc0fc867/mail/writer.go#L67-L70 |
13,124 | emersion/go-message | mail/writer.go | CreateAttachment | func (w *Writer) CreateAttachment(h AttachmentHeader) (io.WriteCloser, error) {
initAttachmentHeader(&h)
return w.mw.CreatePart(h.Header)
} | go | func (w *Writer) CreateAttachment(h AttachmentHeader) (io.WriteCloser, error) {
initAttachmentHeader(&h)
return w.mw.CreatePart(h.Header)
} | [
"func",
"(",
"w",
"*",
"Writer",
")",
"CreateAttachment",
"(",
"h",
"AttachmentHeader",
")",
"(",
"io",
".",
"WriteCloser",
",",
"error",
")",
"{",
"initAttachmentHeader",
"(",
"&",
"h",
")",
"\n",
"return",
"w",
".",
"mw",
".",
"CreatePart",
"(",
"h",
".",
"Header",
")",
"\n",
"}"
]
| // CreateAttachment creates a new attachment with the provided header. The body
// of the part should be written to the returned io.WriteCloser. | [
"CreateAttachment",
"creates",
"a",
"new",
"attachment",
"with",
"the",
"provided",
"header",
".",
"The",
"body",
"of",
"the",
"part",
"should",
"be",
"written",
"to",
"the",
"returned",
"io",
".",
"WriteCloser",
"."
]
| 1e345aac1fa8858087e0443e26fd3db3dc0fc867 | https://github.com/emersion/go-message/blob/1e345aac1fa8858087e0443e26fd3db3dc0fc867/mail/writer.go#L74-L77 |
13,125 | emersion/go-message | mail/writer.go | CreatePart | func (w *InlineWriter) CreatePart(h InlineHeader) (io.WriteCloser, error) {
initInlineHeader(&h)
return w.mw.CreatePart(h.Header)
} | go | func (w *InlineWriter) CreatePart(h InlineHeader) (io.WriteCloser, error) {
initInlineHeader(&h)
return w.mw.CreatePart(h.Header)
} | [
"func",
"(",
"w",
"*",
"InlineWriter",
")",
"CreatePart",
"(",
"h",
"InlineHeader",
")",
"(",
"io",
".",
"WriteCloser",
",",
"error",
")",
"{",
"initInlineHeader",
"(",
"&",
"h",
")",
"\n",
"return",
"w",
".",
"mw",
".",
"CreatePart",
"(",
"h",
".",
"Header",
")",
"\n",
"}"
]
| // CreatePart creates a new text part with the provided header. The body of the
// part should be written to the returned io.WriteCloser. | [
"CreatePart",
"creates",
"a",
"new",
"text",
"part",
"with",
"the",
"provided",
"header",
".",
"The",
"body",
"of",
"the",
"part",
"should",
"be",
"written",
"to",
"the",
"returned",
"io",
".",
"WriteCloser",
"."
]
| 1e345aac1fa8858087e0443e26fd3db3dc0fc867 | https://github.com/emersion/go-message/blob/1e345aac1fa8858087e0443e26fd3db3dc0fc867/mail/writer.go#L91-L94 |
13,126 | kataras/go-sessions | memstore.go | GetEntry | 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 | 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
} | [
"func",
"(",
"r",
"*",
"Store",
")",
"GetEntry",
"(",
"key",
"string",
")",
"*",
"Entry",
"{",
"args",
":=",
"*",
"r",
"\n",
"n",
":=",
"len",
"(",
"args",
")",
"\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"n",
";",
"i",
"++",
"{",
"kv",
":=",
"&",
"args",
"[",
"i",
"]",
"\n",
"if",
"kv",
".",
"Key",
"==",
"key",
"{",
"return",
"kv",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"nil",
"\n",
"}"
]
| // GetEntry returns a pointer to the "Entry" found with the given "key"
// if nothing found then it returns nil, so be careful with that,
// it's not supposed to be used by end-developers. | [
"GetEntry",
"returns",
"a",
"pointer",
"to",
"the",
"Entry",
"found",
"with",
"the",
"given",
"key",
"if",
"nothing",
"found",
"then",
"it",
"returns",
"nil",
"so",
"be",
"careful",
"with",
"that",
"it",
"s",
"not",
"supposed",
"to",
"be",
"used",
"by",
"end",
"-",
"developers",
"."
]
| 174e8dc7ddaa920bc139b467f9af29ff8663745a | https://github.com/kataras/go-sessions/blob/174e8dc7ddaa920bc139b467f9af29ff8663745a/memstore.go#L348-L359 |
13,127 | kataras/go-sessions | memstore.go | GetStringDefault | func (r *Store) GetStringDefault(key string, def string) string {
v := r.GetEntry(key)
if v == nil {
return def
}
return v.StringDefault(def)
} | go | func (r *Store) GetStringDefault(key string, def string) string {
v := r.GetEntry(key)
if v == nil {
return def
}
return v.StringDefault(def)
} | [
"func",
"(",
"r",
"*",
"Store",
")",
"GetStringDefault",
"(",
"key",
"string",
",",
"def",
"string",
")",
"string",
"{",
"v",
":=",
"r",
".",
"GetEntry",
"(",
"key",
")",
"\n",
"if",
"v",
"==",
"nil",
"{",
"return",
"def",
"\n",
"}",
"\n\n",
"return",
"v",
".",
"StringDefault",
"(",
"def",
")",
"\n",
"}"
]
| // GetStringDefault returns the entry's value as string, based on its key.
// If not found returns "def". | [
"GetStringDefault",
"returns",
"the",
"entry",
"s",
"value",
"as",
"string",
"based",
"on",
"its",
"key",
".",
"If",
"not",
"found",
"returns",
"def",
"."
]
| 174e8dc7ddaa920bc139b467f9af29ff8663745a | https://github.com/kataras/go-sessions/blob/174e8dc7ddaa920bc139b467f9af29ff8663745a/memstore.go#L394-L401 |
13,128 | kataras/go-sessions | memstore.go | Serialize | 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 | 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()
} | [
"func",
"(",
"r",
"Store",
")",
"Serialize",
"(",
")",
"[",
"]",
"byte",
"{",
"w",
":=",
"new",
"(",
"bytes",
".",
"Buffer",
")",
"\n",
"enc",
":=",
"gob",
".",
"NewEncoder",
"(",
"w",
")",
"\n",
"err",
":=",
"enc",
".",
"Encode",
"(",
"r",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
"\n",
"}",
"\n\n",
"return",
"w",
".",
"Bytes",
"(",
")",
"\n",
"}"
]
| // Serialize returns the byte representation of the current Store. | [
"Serialize",
"returns",
"the",
"byte",
"representation",
"of",
"the",
"current",
"Store",
"."
]
| 174e8dc7ddaa920bc139b467f9af29ff8663745a | https://github.com/kataras/go-sessions/blob/174e8dc7ddaa920bc139b467f9af29ff8663745a/memstore.go#L532-L541 |
13,129 | kataras/go-sessions | sessions.go | StartFasthttp | 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.config.Expires)
sess.isNew = s.provider.db.Len(sid) == 0
s.updateCookieFasthttp(ctx, sid, s.config.Expires)
return sess
}
sess := s.provider.Read(cookieValue, s.config.Expires)
return sess
} | go | 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.config.Expires)
sess.isNew = s.provider.db.Len(sid) == 0
s.updateCookieFasthttp(ctx, sid, s.config.Expires)
return sess
}
sess := s.provider.Read(cookieValue, s.config.Expires)
return sess
} | [
"func",
"(",
"s",
"*",
"Sessions",
")",
"StartFasthttp",
"(",
"ctx",
"*",
"fasthttp",
".",
"RequestCtx",
")",
"*",
"Session",
"{",
"cookieValue",
":=",
"s",
".",
"decodeCookieValue",
"(",
"GetCookieFasthttp",
"(",
"ctx",
",",
"s",
".",
"config",
".",
"Cookie",
")",
")",
"\n\n",
"if",
"cookieValue",
"==",
"\"",
"\"",
"{",
"// cookie doesn't exists, let's generate a session and add set a cookie",
"sid",
":=",
"s",
".",
"config",
".",
"SessionIDGenerator",
"(",
")",
"\n\n",
"sess",
":=",
"s",
".",
"provider",
".",
"Init",
"(",
"sid",
",",
"s",
".",
"config",
".",
"Expires",
")",
"\n",
"sess",
".",
"isNew",
"=",
"s",
".",
"provider",
".",
"db",
".",
"Len",
"(",
"sid",
")",
"==",
"0",
"\n\n",
"s",
".",
"updateCookieFasthttp",
"(",
"ctx",
",",
"sid",
",",
"s",
".",
"config",
".",
"Expires",
")",
"\n\n",
"return",
"sess",
"\n",
"}",
"\n\n",
"sess",
":=",
"s",
".",
"provider",
".",
"Read",
"(",
"cookieValue",
",",
"s",
".",
"config",
".",
"Expires",
")",
"\n\n",
"return",
"sess",
"\n",
"}"
]
| // StartFasthttp starts the session for the particular request. | [
"StartFasthttp",
"starts",
"the",
"session",
"for",
"the",
"particular",
"request",
"."
]
| 174e8dc7ddaa920bc139b467f9af29ff8663745a | https://github.com/kataras/go-sessions/blob/174e8dc7ddaa920bc139b467f9af29ff8663745a/sessions.go#L205-L222 |
13,130 | kataras/go-sessions | sessions.go | ShiftExpirationFasthttp | func (s *Sessions) ShiftExpirationFasthttp(ctx *fasthttp.RequestCtx) {
s.UpdateExpirationFasthttp(ctx, s.config.Expires)
} | go | func (s *Sessions) ShiftExpirationFasthttp(ctx *fasthttp.RequestCtx) {
s.UpdateExpirationFasthttp(ctx, s.config.Expires)
} | [
"func",
"(",
"s",
"*",
"Sessions",
")",
"ShiftExpirationFasthttp",
"(",
"ctx",
"*",
"fasthttp",
".",
"RequestCtx",
")",
"{",
"s",
".",
"UpdateExpirationFasthttp",
"(",
"ctx",
",",
"s",
".",
"config",
".",
"Expires",
")",
"\n",
"}"
]
| // ShiftExpirationFasthttp move the expire date of a session to a new date
// by using session default timeout configuration. | [
"ShiftExpirationFasthttp",
"move",
"the",
"expire",
"date",
"of",
"a",
"session",
"to",
"a",
"new",
"date",
"by",
"using",
"session",
"default",
"timeout",
"configuration",
"."
]
| 174e8dc7ddaa920bc139b467f9af29ff8663745a | https://github.com/kataras/go-sessions/blob/174e8dc7ddaa920bc139b467f9af29ff8663745a/sessions.go#L244-L246 |
13,131 | kataras/go-sessions | sessions.go | UpdateExpiration | func UpdateExpiration(w http.ResponseWriter, r *http.Request, expires time.Duration) {
Default.UpdateExpiration(w, r, expires)
} | go | func UpdateExpiration(w http.ResponseWriter, r *http.Request, expires time.Duration) {
Default.UpdateExpiration(w, r, expires)
} | [
"func",
"UpdateExpiration",
"(",
"w",
"http",
".",
"ResponseWriter",
",",
"r",
"*",
"http",
".",
"Request",
",",
"expires",
"time",
".",
"Duration",
")",
"{",
"Default",
".",
"UpdateExpiration",
"(",
"w",
",",
"r",
",",
"expires",
")",
"\n",
"}"
]
| // UpdateExpiration change expire date of a session to a new date
// by using timeout value passed by `expires` receiver. | [
"UpdateExpiration",
"change",
"expire",
"date",
"of",
"a",
"session",
"to",
"a",
"new",
"date",
"by",
"using",
"timeout",
"value",
"passed",
"by",
"expires",
"receiver",
"."
]
| 174e8dc7ddaa920bc139b467f9af29ff8663745a | https://github.com/kataras/go-sessions/blob/174e8dc7ddaa920bc139b467f9af29ff8663745a/sessions.go#L250-L252 |
13,132 | kataras/go-sessions | sessions.go | DestroyFasthttp | func (s *Sessions) DestroyFasthttp(ctx *fasthttp.RequestCtx) {
cookieValue := GetCookieFasthttp(ctx, s.config.Cookie)
s.destroy(cookieValue)
RemoveCookieFasthttp(ctx, s.config)
} | go | func (s *Sessions) DestroyFasthttp(ctx *fasthttp.RequestCtx) {
cookieValue := GetCookieFasthttp(ctx, s.config.Cookie)
s.destroy(cookieValue)
RemoveCookieFasthttp(ctx, s.config)
} | [
"func",
"(",
"s",
"*",
"Sessions",
")",
"DestroyFasthttp",
"(",
"ctx",
"*",
"fasthttp",
".",
"RequestCtx",
")",
"{",
"cookieValue",
":=",
"GetCookieFasthttp",
"(",
"ctx",
",",
"s",
".",
"config",
".",
"Cookie",
")",
"\n",
"s",
".",
"destroy",
"(",
"cookieValue",
")",
"\n",
"RemoveCookieFasthttp",
"(",
"ctx",
",",
"s",
".",
"config",
")",
"\n",
"}"
]
| // DestroyFasthttp remove the session data and remove the associated cookie. | [
"DestroyFasthttp",
"remove",
"the",
"session",
"data",
"and",
"remove",
"the",
"associated",
"cookie",
"."
]
| 174e8dc7ddaa920bc139b467f9af29ff8663745a | https://github.com/kataras/go-sessions/blob/174e8dc7ddaa920bc139b467f9af29ff8663745a/sessions.go#L347-L351 |
13,133 | kataras/go-sessions | session.go | GetInt | 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 | 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)
} | [
"func",
"(",
"s",
"*",
"Session",
")",
"GetInt",
"(",
"key",
"string",
")",
"(",
"int",
",",
"error",
")",
"{",
"v",
":=",
"s",
".",
"Get",
"(",
"key",
")",
"\n\n",
"if",
"vint",
",",
"ok",
":=",
"v",
".",
"(",
"int",
")",
";",
"ok",
"{",
"return",
"vint",
",",
"nil",
"\n",
"}",
"\n\n",
"if",
"vstring",
",",
"sok",
":=",
"v",
".",
"(",
"string",
")",
";",
"sok",
"{",
"return",
"strconv",
".",
"Atoi",
"(",
"vstring",
")",
"\n",
"}",
"\n\n",
"return",
"-",
"1",
",",
"fmt",
".",
"Errorf",
"(",
"errFindParse",
",",
"\"",
"\"",
",",
"key",
")",
"\n",
"}"
]
| // GetInt same as `Get` but returns its int representation,
// if key doesn't exist then it returns -1 and a non-nil error. | [
"GetInt",
"same",
"as",
"Get",
"but",
"returns",
"its",
"int",
"representation",
"if",
"key",
"doesn",
"t",
"exist",
"then",
"it",
"returns",
"-",
"1",
"and",
"a",
"non",
"-",
"nil",
"error",
"."
]
| 174e8dc7ddaa920bc139b467f9af29ff8663745a | https://github.com/kataras/go-sessions/blob/174e8dc7ddaa920bc139b467f9af29ff8663745a/session.go#L169-L181 |
13,134 | kataras/go-sessions | session.go | GetFloat32 | 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 {
vfloat64, err := strconv.ParseFloat(vstring, 32)
if err != nil {
return -1, err
}
return float32(vfloat64), nil
}
return -1, fmt.Errorf(errFindParse, "float32", key)
} | go | 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 {
vfloat64, err := strconv.ParseFloat(vstring, 32)
if err != nil {
return -1, err
}
return float32(vfloat64), nil
}
return -1, fmt.Errorf(errFindParse, "float32", key)
} | [
"func",
"(",
"s",
"*",
"Session",
")",
"GetFloat32",
"(",
"key",
"string",
")",
"(",
"float32",
",",
"error",
")",
"{",
"v",
":=",
"s",
".",
"Get",
"(",
"key",
")",
"\n\n",
"if",
"vfloat32",
",",
"ok",
":=",
"v",
".",
"(",
"float32",
")",
";",
"ok",
"{",
"return",
"vfloat32",
",",
"nil",
"\n",
"}",
"\n\n",
"if",
"vfloat64",
",",
"ok",
":=",
"v",
".",
"(",
"float64",
")",
";",
"ok",
"{",
"return",
"float32",
"(",
"vfloat64",
")",
",",
"nil",
"\n",
"}",
"\n\n",
"if",
"vint",
",",
"ok",
":=",
"v",
".",
"(",
"int",
")",
";",
"ok",
"{",
"return",
"float32",
"(",
"vint",
")",
",",
"nil",
"\n",
"}",
"\n\n",
"if",
"vstring",
",",
"sok",
":=",
"v",
".",
"(",
"string",
")",
";",
"sok",
"{",
"vfloat64",
",",
"err",
":=",
"strconv",
".",
"ParseFloat",
"(",
"vstring",
",",
"32",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"-",
"1",
",",
"err",
"\n",
"}",
"\n",
"return",
"float32",
"(",
"vfloat64",
")",
",",
"nil",
"\n",
"}",
"\n\n",
"return",
"-",
"1",
",",
"fmt",
".",
"Errorf",
"(",
"errFindParse",
",",
"\"",
"\"",
",",
"key",
")",
"\n",
"}"
]
| // GetFloat32 same as `Get` but returns its float32 representation,
// if key doesn't exist then it returns -1 and a non-nil error. | [
"GetFloat32",
"same",
"as",
"Get",
"but",
"returns",
"its",
"float32",
"representation",
"if",
"key",
"doesn",
"t",
"exist",
"then",
"it",
"returns",
"-",
"1",
"and",
"a",
"non",
"-",
"nil",
"error",
"."
]
| 174e8dc7ddaa920bc139b467f9af29ff8663745a | https://github.com/kataras/go-sessions/blob/174e8dc7ddaa920bc139b467f9af29ff8663745a/session.go#L244-L268 |
13,135 | kataras/go-sessions | cookie.go | GetCookieFasthttp | func GetCookieFasthttp(ctx *fasthttp.RequestCtx, name string) (value string) {
bcookie := ctx.Request.Header.Cookie(name)
if bcookie != nil {
value = string(bcookie)
}
return
} | go | func GetCookieFasthttp(ctx *fasthttp.RequestCtx, name string) (value string) {
bcookie := ctx.Request.Header.Cookie(name)
if bcookie != nil {
value = string(bcookie)
}
return
} | [
"func",
"GetCookieFasthttp",
"(",
"ctx",
"*",
"fasthttp",
".",
"RequestCtx",
",",
"name",
"string",
")",
"(",
"value",
"string",
")",
"{",
"bcookie",
":=",
"ctx",
".",
"Request",
".",
"Header",
".",
"Cookie",
"(",
"name",
")",
"\n",
"if",
"bcookie",
"!=",
"nil",
"{",
"value",
"=",
"string",
"(",
"bcookie",
")",
"\n",
"}",
"\n",
"return",
"\n",
"}"
]
| // GetCookieFasthttp returns cookie's value by it's name
// returns empty string if nothing was found. | [
"GetCookieFasthttp",
"returns",
"cookie",
"s",
"value",
"by",
"it",
"s",
"name",
"returns",
"empty",
"string",
"if",
"nothing",
"was",
"found",
"."
]
| 174e8dc7ddaa920bc139b467f9af29ff8663745a | https://github.com/kataras/go-sessions/blob/174e8dc7ddaa920bc139b467f9af29ff8663745a/cookie.go#L33-L39 |
13,136 | kataras/go-sessions | cookie.go | AddCookieFasthttp | func AddCookieFasthttp(ctx *fasthttp.RequestCtx, cookie *fasthttp.Cookie) {
ctx.Response.Header.SetCookie(cookie)
} | go | func AddCookieFasthttp(ctx *fasthttp.RequestCtx, cookie *fasthttp.Cookie) {
ctx.Response.Header.SetCookie(cookie)
} | [
"func",
"AddCookieFasthttp",
"(",
"ctx",
"*",
"fasthttp",
".",
"RequestCtx",
",",
"cookie",
"*",
"fasthttp",
".",
"Cookie",
")",
"{",
"ctx",
".",
"Response",
".",
"Header",
".",
"SetCookie",
"(",
"cookie",
")",
"\n",
"}"
]
| // AddCookieFasthttp adds a cookie. | [
"AddCookieFasthttp",
"adds",
"a",
"cookie",
"."
]
| 174e8dc7ddaa920bc139b467f9af29ff8663745a | https://github.com/kataras/go-sessions/blob/174e8dc7ddaa920bc139b467f9af29ff8663745a/cookie.go#L50-L52 |
13,137 | kataras/go-sessions | sessiondb/boltdb/database.go | OnUpdateExpiration | 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 := getExpirationBucketName([]byte(sid))
root := db.getBucket(tx)
b := root.Bucket(expirationName)
if b == nil {
// golog.Debugf("tried to reset the expiration value for '%s' while its configured lifetime is unlimited or the session is already expired and not found now", sid)
return sessions.ErrNotFound
}
return b.Put(expirationKey, timeBytes)
})
} | go | 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 := getExpirationBucketName([]byte(sid))
root := db.getBucket(tx)
b := root.Bucket(expirationName)
if b == nil {
// golog.Debugf("tried to reset the expiration value for '%s' while its configured lifetime is unlimited or the session is already expired and not found now", sid)
return sessions.ErrNotFound
}
return b.Put(expirationKey, timeBytes)
})
} | [
"func",
"(",
"db",
"*",
"Database",
")",
"OnUpdateExpiration",
"(",
"sid",
"string",
",",
"newExpires",
"time",
".",
"Duration",
")",
"error",
"{",
"expirationTime",
":=",
"time",
".",
"Now",
"(",
")",
".",
"Add",
"(",
"newExpires",
")",
"\n",
"timeBytes",
",",
"err",
":=",
"sessions",
".",
"DefaultTranscoder",
".",
"Marshal",
"(",
"expirationTime",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"return",
"db",
".",
"Service",
".",
"Update",
"(",
"func",
"(",
"tx",
"*",
"bolt",
".",
"Tx",
")",
"error",
"{",
"expirationName",
":=",
"getExpirationBucketName",
"(",
"[",
"]",
"byte",
"(",
"sid",
")",
")",
"\n",
"root",
":=",
"db",
".",
"getBucket",
"(",
"tx",
")",
"\n",
"b",
":=",
"root",
".",
"Bucket",
"(",
"expirationName",
")",
"\n",
"if",
"b",
"==",
"nil",
"{",
"// golog.Debugf(\"tried to reset the expiration value for '%s' while its configured lifetime is unlimited or the session is already expired and not found now\", sid)",
"return",
"sessions",
".",
"ErrNotFound",
"\n",
"}",
"\n\n",
"return",
"b",
".",
"Put",
"(",
"expirationKey",
",",
"timeBytes",
")",
"\n",
"}",
")",
"\n",
"}"
]
| // OnUpdateExpiration will re-set the database's session's entry ttl. | [
"OnUpdateExpiration",
"will",
"re",
"-",
"set",
"the",
"database",
"s",
"session",
"s",
"entry",
"ttl",
"."
]
| 174e8dc7ddaa920bc139b467f9af29ff8663745a | https://github.com/kataras/go-sessions/blob/174e8dc7ddaa920bc139b467f9af29ff8663745a/sessiondb/boltdb/database.go#L208-L226 |
13,138 | apcera/termtables | table.go | CreateTable | 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 | 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
} | [
"func",
"CreateTable",
"(",
")",
"*",
"Table",
"{",
"t",
":=",
"&",
"Table",
"{",
"elements",
":",
"[",
"]",
"Element",
"{",
"}",
",",
"Style",
":",
"DefaultStyle",
"}",
"\n",
"if",
"outputsEnabled",
".",
"UTF8",
"{",
"t",
".",
"Style",
".",
"setUtfBoxStyle",
"(",
")",
"\n",
"}",
"\n",
"if",
"outputsEnabled",
".",
"titleStyle",
"!=",
"titleStyle",
"(",
"0",
")",
"{",
"t",
".",
"Style",
".",
"htmlRules",
".",
"title",
"=",
"outputsEnabled",
".",
"titleStyle",
"\n",
"}",
"\n",
"t",
".",
"outputMode",
"=",
"defaultOutputMode",
"\n",
"return",
"t",
"\n",
"}"
]
| // CreateTable creates an empty Table using defaults for style. | [
"CreateTable",
"creates",
"an",
"empty",
"Table",
"using",
"defaults",
"for",
"style",
"."
]
| bcbc5dc54055d14996b256d348fb75cc6debe282 | https://github.com/apcera/termtables/blob/bcbc5dc54055d14996b256d348fb75cc6debe282/table.go#L139-L149 |
13,139 | apcera/termtables | table.go | AddRow | func (t *Table) AddRow(items ...interface{}) *Row {
row := CreateRow(items)
t.elements = append(t.elements, row)
return row
} | go | func (t *Table) AddRow(items ...interface{}) *Row {
row := CreateRow(items)
t.elements = append(t.elements, row)
return row
} | [
"func",
"(",
"t",
"*",
"Table",
")",
"AddRow",
"(",
"items",
"...",
"interface",
"{",
"}",
")",
"*",
"Row",
"{",
"row",
":=",
"CreateRow",
"(",
"items",
")",
"\n",
"t",
".",
"elements",
"=",
"append",
"(",
"t",
".",
"elements",
",",
"row",
")",
"\n",
"return",
"row",
"\n",
"}"
]
| // AddRow adds the supplied items as cells in one row of the table. | [
"AddRow",
"adds",
"the",
"supplied",
"items",
"as",
"cells",
"in",
"one",
"row",
"of",
"the",
"table",
"."
]
| bcbc5dc54055d14996b256d348fb75cc6debe282 | https://github.com/apcera/termtables/blob/bcbc5dc54055d14996b256d348fb75cc6debe282/table.go#L158-L162 |
13,140 | apcera/termtables | table.go | AddHeaders | func (t *Table) AddHeaders(headers ...interface{}) {
t.headers = append(t.headers, headers...)
} | go | func (t *Table) AddHeaders(headers ...interface{}) {
t.headers = append(t.headers, headers...)
} | [
"func",
"(",
"t",
"*",
"Table",
")",
"AddHeaders",
"(",
"headers",
"...",
"interface",
"{",
"}",
")",
"{",
"t",
".",
"headers",
"=",
"append",
"(",
"t",
".",
"headers",
",",
"headers",
"...",
")",
"\n",
"}"
]
| // AddHeaders supplies column headers for the table. | [
"AddHeaders",
"supplies",
"column",
"headers",
"for",
"the",
"table",
"."
]
| bcbc5dc54055d14996b256d348fb75cc6debe282 | https://github.com/apcera/termtables/blob/bcbc5dc54055d14996b256d348fb75cc6debe282/table.go#L171-L173 |
13,141 | apcera/termtables | table.go | SetAlign | 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 | 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
}
} | [
"func",
"(",
"t",
"*",
"Table",
")",
"SetAlign",
"(",
"align",
"tableAlignment",
",",
"column",
"int",
")",
"{",
"if",
"column",
"<",
"0",
"{",
"return",
"\n",
"}",
"\n",
"for",
"i",
":=",
"range",
"t",
".",
"elements",
"{",
"row",
",",
"ok",
":=",
"t",
".",
"elements",
"[",
"i",
"]",
".",
"(",
"*",
"Row",
")",
"\n",
"if",
"!",
"ok",
"{",
"continue",
"\n",
"}",
"\n",
"if",
"column",
">=",
"len",
"(",
"row",
".",
"cells",
")",
"{",
"continue",
"\n",
"}",
"\n",
"row",
".",
"cells",
"[",
"column",
"-",
"1",
"]",
".",
"alignment",
"=",
"&",
"align",
"\n",
"}",
"\n",
"}"
]
| // SetAlign changes the alignment for elements in a column of the table;
// alignments are stored with each cell, so cells added after a call to
// SetAlign will not pick up the change. Columns are numbered from 1. | [
"SetAlign",
"changes",
"the",
"alignment",
"for",
"elements",
"in",
"a",
"column",
"of",
"the",
"table",
";",
"alignments",
"are",
"stored",
"with",
"each",
"cell",
"so",
"cells",
"added",
"after",
"a",
"call",
"to",
"SetAlign",
"will",
"not",
"pick",
"up",
"the",
"change",
".",
"Columns",
"are",
"numbered",
"from",
"1",
"."
]
| bcbc5dc54055d14996b256d348fb75cc6debe282 | https://github.com/apcera/termtables/blob/bcbc5dc54055d14996b256d348fb75cc6debe282/table.go#L178-L192 |
13,142 | apcera/termtables | table.go | SetHTMLStyleTitle | func (t *Table) SetHTMLStyleTitle(want titleStyle) {
t.Style.htmlRules.title = want
} | go | func (t *Table) SetHTMLStyleTitle(want titleStyle) {
t.Style.htmlRules.title = want
} | [
"func",
"(",
"t",
"*",
"Table",
")",
"SetHTMLStyleTitle",
"(",
"want",
"titleStyle",
")",
"{",
"t",
".",
"Style",
".",
"htmlRules",
".",
"title",
"=",
"want",
"\n",
"}"
]
| // SetHTMLStyleTitle lets an HTML output mode be chosen; we should rework this
// into a more generic and extensible API as we clean up termtables. | [
"SetHTMLStyleTitle",
"lets",
"an",
"HTML",
"output",
"mode",
"be",
"chosen",
";",
"we",
"should",
"rework",
"this",
"into",
"a",
"more",
"generic",
"and",
"extensible",
"API",
"as",
"we",
"clean",
"up",
"termtables",
"."
]
| bcbc5dc54055d14996b256d348fb75cc6debe282 | https://github.com/apcera/termtables/blob/bcbc5dc54055d14996b256d348fb75cc6debe282/table.go#L220-L222 |
13,143 | apcera/termtables | table.go | renderTerminal | 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{&Separator{where: LINE_SUBTOP}}, tt.elements...)
} else if tt.title == nil && tt.headers == nil {
tt.elements = append([]Element{&Separator{where: LINE_TOP}}, tt.elements...)
} else {
tt.elements = append([]Element{&Separator{where: LINE_INNER}}, tt.elements...)
}
}
// If we have headers, include them.
if tt.headers != nil {
ne := make([]Element, 2)
ne[1] = CreateRow(tt.headers)
if tt.title != nil {
ne[0] = &Separator{where: LINE_SUBTOP}
} else {
ne[0] = &Separator{where: LINE_TOP}
}
tt.elements = append(ne, tt.elements...)
}
// If we have a title, write it.
if tt.title != nil {
// Match changes to this into renderMarkdown too.
tt.titleCell = CreateCell(tt.title, &CellStyle{Alignment: AlignCenter, ColSpan: 999})
ne := []Element{
&StraightSeparator{where: LINE_TOP},
CreateRow([]interface{}{tt.titleCell}),
}
tt.elements = append(ne, tt.elements...)
}
// Create a new table from the
// generate the runtime style. Must include all cells being printed.
style := createRenderStyle(tt)
// Loop over the elements and render them.
b := bytes.NewBuffer(nil)
for _, e := range tt.elements {
b.WriteString(e.Render(style))
b.WriteString("\n")
}
// Add bottom line.
if !style.SkipBorder {
b.WriteString((&Separator{where: LINE_BOTTOM}).Render(style) + "\n")
}
return b.String()
} | go | 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{&Separator{where: LINE_SUBTOP}}, tt.elements...)
} else if tt.title == nil && tt.headers == nil {
tt.elements = append([]Element{&Separator{where: LINE_TOP}}, tt.elements...)
} else {
tt.elements = append([]Element{&Separator{where: LINE_INNER}}, tt.elements...)
}
}
// If we have headers, include them.
if tt.headers != nil {
ne := make([]Element, 2)
ne[1] = CreateRow(tt.headers)
if tt.title != nil {
ne[0] = &Separator{where: LINE_SUBTOP}
} else {
ne[0] = &Separator{where: LINE_TOP}
}
tt.elements = append(ne, tt.elements...)
}
// If we have a title, write it.
if tt.title != nil {
// Match changes to this into renderMarkdown too.
tt.titleCell = CreateCell(tt.title, &CellStyle{Alignment: AlignCenter, ColSpan: 999})
ne := []Element{
&StraightSeparator{where: LINE_TOP},
CreateRow([]interface{}{tt.titleCell}),
}
tt.elements = append(ne, tt.elements...)
}
// Create a new table from the
// generate the runtime style. Must include all cells being printed.
style := createRenderStyle(tt)
// Loop over the elements and render them.
b := bytes.NewBuffer(nil)
for _, e := range tt.elements {
b.WriteString(e.Render(style))
b.WriteString("\n")
}
// Add bottom line.
if !style.SkipBorder {
b.WriteString((&Separator{where: LINE_BOTTOM}).Render(style) + "\n")
}
return b.String()
} | [
"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",
"(",
")",
"\n\n",
"// Initial top line.",
"if",
"!",
"tt",
".",
"Style",
".",
"SkipBorder",
"{",
"if",
"tt",
".",
"title",
"!=",
"nil",
"&&",
"tt",
".",
"headers",
"==",
"nil",
"{",
"tt",
".",
"elements",
"=",
"append",
"(",
"[",
"]",
"Element",
"{",
"&",
"Separator",
"{",
"where",
":",
"LINE_SUBTOP",
"}",
"}",
",",
"tt",
".",
"elements",
"...",
")",
"\n",
"}",
"else",
"if",
"tt",
".",
"title",
"==",
"nil",
"&&",
"tt",
".",
"headers",
"==",
"nil",
"{",
"tt",
".",
"elements",
"=",
"append",
"(",
"[",
"]",
"Element",
"{",
"&",
"Separator",
"{",
"where",
":",
"LINE_TOP",
"}",
"}",
",",
"tt",
".",
"elements",
"...",
")",
"\n",
"}",
"else",
"{",
"tt",
".",
"elements",
"=",
"append",
"(",
"[",
"]",
"Element",
"{",
"&",
"Separator",
"{",
"where",
":",
"LINE_INNER",
"}",
"}",
",",
"tt",
".",
"elements",
"...",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"// If we have headers, include them.",
"if",
"tt",
".",
"headers",
"!=",
"nil",
"{",
"ne",
":=",
"make",
"(",
"[",
"]",
"Element",
",",
"2",
")",
"\n",
"ne",
"[",
"1",
"]",
"=",
"CreateRow",
"(",
"tt",
".",
"headers",
")",
"\n",
"if",
"tt",
".",
"title",
"!=",
"nil",
"{",
"ne",
"[",
"0",
"]",
"=",
"&",
"Separator",
"{",
"where",
":",
"LINE_SUBTOP",
"}",
"\n",
"}",
"else",
"{",
"ne",
"[",
"0",
"]",
"=",
"&",
"Separator",
"{",
"where",
":",
"LINE_TOP",
"}",
"\n",
"}",
"\n",
"tt",
".",
"elements",
"=",
"append",
"(",
"ne",
",",
"tt",
".",
"elements",
"...",
")",
"\n",
"}",
"\n\n",
"// If we have a title, write it.",
"if",
"tt",
".",
"title",
"!=",
"nil",
"{",
"// Match changes to this into renderMarkdown too.",
"tt",
".",
"titleCell",
"=",
"CreateCell",
"(",
"tt",
".",
"title",
",",
"&",
"CellStyle",
"{",
"Alignment",
":",
"AlignCenter",
",",
"ColSpan",
":",
"999",
"}",
")",
"\n",
"ne",
":=",
"[",
"]",
"Element",
"{",
"&",
"StraightSeparator",
"{",
"where",
":",
"LINE_TOP",
"}",
",",
"CreateRow",
"(",
"[",
"]",
"interface",
"{",
"}",
"{",
"tt",
".",
"titleCell",
"}",
")",
",",
"}",
"\n",
"tt",
".",
"elements",
"=",
"append",
"(",
"ne",
",",
"tt",
".",
"elements",
"...",
")",
"\n",
"}",
"\n\n",
"// Create a new table from the",
"// generate the runtime style. Must include all cells being printed.",
"style",
":=",
"createRenderStyle",
"(",
"tt",
")",
"\n\n",
"// Loop over the elements and render them.",
"b",
":=",
"bytes",
".",
"NewBuffer",
"(",
"nil",
")",
"\n",
"for",
"_",
",",
"e",
":=",
"range",
"tt",
".",
"elements",
"{",
"b",
".",
"WriteString",
"(",
"e",
".",
"Render",
"(",
"style",
")",
")",
"\n",
"b",
".",
"WriteString",
"(",
"\"",
"\\n",
"\"",
")",
"\n",
"}",
"\n\n",
"// Add bottom line.",
"if",
"!",
"style",
".",
"SkipBorder",
"{",
"b",
".",
"WriteString",
"(",
"(",
"&",
"Separator",
"{",
"where",
":",
"LINE_BOTTOM",
"}",
")",
".",
"Render",
"(",
"style",
")",
"+",
"\"",
"\\n",
"\"",
")",
"\n",
"}",
"\n\n",
"return",
"b",
".",
"String",
"(",
")",
"\n",
"}"
]
| // renderTerminal returns a string representation of a fully rendered table,
// drawn out for display, with embedded newlines. | [
"renderTerminal",
"returns",
"a",
"string",
"representation",
"of",
"a",
"fully",
"rendered",
"table",
"drawn",
"out",
"for",
"display",
"with",
"embedded",
"newlines",
"."
]
| bcbc5dc54055d14996b256d348fb75cc6debe282 | https://github.com/apcera/termtables/blob/bcbc5dc54055d14996b256d348fb75cc6debe282/table.go#L243-L299 |
13,144 | apcera/termtables | straight_separator.go | Render | 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 + strings.Repeat(style.BorderX, width-1) + style.BorderTopRight
case LINE_INNER, LINE_SUBTOP:
return style.BorderLeft + strings.Repeat(style.BorderX, width-1) + style.BorderRight
case LINE_BOTTOM:
return style.BorderBottomLeft + strings.Repeat(style.BorderX, width-1) + style.BorderBottomRight
}
panic("not reached")
} | go | 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 + strings.Repeat(style.BorderX, width-1) + style.BorderTopRight
case LINE_INNER, LINE_SUBTOP:
return style.BorderLeft + strings.Repeat(style.BorderX, width-1) + style.BorderRight
case LINE_BOTTOM:
return style.BorderBottomLeft + strings.Repeat(style.BorderX, width-1) + style.BorderBottomRight
}
panic("not reached")
} | [
"func",
"(",
"s",
"*",
"StraightSeparator",
")",
"Render",
"(",
"style",
"*",
"renderStyle",
")",
"string",
"{",
"// loop over getting dashes",
"width",
":=",
"0",
"\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"style",
".",
"columns",
";",
"i",
"++",
"{",
"width",
"+=",
"style",
".",
"PaddingLeft",
"+",
"style",
".",
"CellWidth",
"(",
"i",
")",
"+",
"style",
".",
"PaddingRight",
"+",
"utf8",
".",
"RuneCountInString",
"(",
"style",
".",
"BorderI",
")",
"\n",
"}",
"\n\n",
"switch",
"s",
".",
"where",
"{",
"case",
"LINE_TOP",
":",
"return",
"style",
".",
"BorderTopLeft",
"+",
"strings",
".",
"Repeat",
"(",
"style",
".",
"BorderX",
",",
"width",
"-",
"1",
")",
"+",
"style",
".",
"BorderTopRight",
"\n",
"case",
"LINE_INNER",
",",
"LINE_SUBTOP",
":",
"return",
"style",
".",
"BorderLeft",
"+",
"strings",
".",
"Repeat",
"(",
"style",
".",
"BorderX",
",",
"width",
"-",
"1",
")",
"+",
"style",
".",
"BorderRight",
"\n",
"case",
"LINE_BOTTOM",
":",
"return",
"style",
".",
"BorderBottomLeft",
"+",
"strings",
".",
"Repeat",
"(",
"style",
".",
"BorderX",
",",
"width",
"-",
"1",
")",
"+",
"style",
".",
"BorderBottomRight",
"\n",
"}",
"\n",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}"
]
| // Render returns a string representing this separator, with all border
// crossings appropriately chosen. | [
"Render",
"returns",
"a",
"string",
"representing",
"this",
"separator",
"with",
"all",
"border",
"crossings",
"appropriately",
"chosen",
"."
]
| bcbc5dc54055d14996b256d348fb75cc6debe282 | https://github.com/apcera/termtables/blob/bcbc5dc54055d14996b256d348fb75cc6debe282/straight_separator.go#L20-L36 |
13,145 | apcera/termtables | term/env.go | GetEnvWindowSize | 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 != "" {
nColumns, err = strconv.Atoi(columns)
if err != nil || nColumns < 0 {
return nil
}
}
return &Size{
Lines: nLines,
Columns: nColumns,
}
} | go | 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 != "" {
nColumns, err = strconv.Atoi(columns)
if err != nil || nColumns < 0 {
return nil
}
}
return &Size{
Lines: nLines,
Columns: nColumns,
}
} | [
"func",
"GetEnvWindowSize",
"(",
")",
"*",
"Size",
"{",
"lines",
":=",
"os",
".",
"Getenv",
"(",
"\"",
"\"",
")",
"\n",
"columns",
":=",
"os",
".",
"Getenv",
"(",
"\"",
"\"",
")",
"\n",
"if",
"lines",
"==",
"\"",
"\"",
"&&",
"columns",
"==",
"\"",
"\"",
"{",
"return",
"nil",
"\n",
"}",
"\n\n",
"nLines",
":=",
"0",
"\n",
"nColumns",
":=",
"0",
"\n",
"var",
"err",
"error",
"\n",
"if",
"lines",
"!=",
"\"",
"\"",
"{",
"nLines",
",",
"err",
"=",
"strconv",
".",
"Atoi",
"(",
"lines",
")",
"\n",
"if",
"err",
"!=",
"nil",
"||",
"nLines",
"<",
"0",
"{",
"return",
"nil",
"\n",
"}",
"\n",
"}",
"\n",
"if",
"columns",
"!=",
"\"",
"\"",
"{",
"nColumns",
",",
"err",
"=",
"strconv",
".",
"Atoi",
"(",
"columns",
")",
"\n",
"if",
"err",
"!=",
"nil",
"||",
"nColumns",
"<",
"0",
"{",
"return",
"nil",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"&",
"Size",
"{",
"Lines",
":",
"nLines",
",",
"Columns",
":",
"nColumns",
",",
"}",
"\n",
"}"
]
| // GetEnvWindowSize returns the window Size, as determined by process
// environment; if either LINES or COLUMNS is present, and whichever is
// present is also numeric, the Size will be non-nil. If Size is nil,
// there's insufficient data in environ. If one entry is 0, that means
// that the environment does not include that data. If a value is
// negative, we treat that as an error. | [
"GetEnvWindowSize",
"returns",
"the",
"window",
"Size",
"as",
"determined",
"by",
"process",
"environment",
";",
"if",
"either",
"LINES",
"or",
"COLUMNS",
"is",
"present",
"and",
"whichever",
"is",
"present",
"is",
"also",
"numeric",
"the",
"Size",
"will",
"be",
"non",
"-",
"nil",
".",
"If",
"Size",
"is",
"nil",
"there",
"s",
"insufficient",
"data",
"in",
"environ",
".",
"If",
"one",
"entry",
"is",
"0",
"that",
"means",
"that",
"the",
"environment",
"does",
"not",
"include",
"that",
"data",
".",
"If",
"a",
"value",
"is",
"negative",
"we",
"treat",
"that",
"as",
"an",
"error",
"."
]
| bcbc5dc54055d14996b256d348fb75cc6debe282 | https://github.com/apcera/termtables/blob/bcbc5dc54055d14996b256d348fb75cc6debe282/term/env.go#L16-L43 |
13,146 | apcera/termtables | separator.go | Render | 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 style.BorderTopLeft + strings.Join(parts, style.BorderTop) + style.BorderTopRight
case LINE_SUBTOP:
return style.BorderLeft + strings.Join(parts, style.BorderTop) + style.BorderRight
case LINE_BOTTOM:
return style.BorderBottomLeft + strings.Join(parts, style.BorderBottom) + style.BorderBottomRight
case LINE_INNER:
return style.BorderLeft + strings.Join(parts, style.BorderI) + style.BorderRight
}
panic("not reached")
} | go | 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 style.BorderTopLeft + strings.Join(parts, style.BorderTop) + style.BorderTopRight
case LINE_SUBTOP:
return style.BorderLeft + strings.Join(parts, style.BorderTop) + style.BorderRight
case LINE_BOTTOM:
return style.BorderBottomLeft + strings.Join(parts, style.BorderBottom) + style.BorderBottomRight
case LINE_INNER:
return style.BorderLeft + strings.Join(parts, style.BorderI) + style.BorderRight
}
panic("not reached")
} | [
"func",
"(",
"s",
"*",
"Separator",
")",
"Render",
"(",
"style",
"*",
"renderStyle",
")",
"string",
"{",
"// loop over getting dashes",
"parts",
":=",
"[",
"]",
"string",
"{",
"}",
"\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"style",
".",
"columns",
";",
"i",
"++",
"{",
"w",
":=",
"style",
".",
"PaddingLeft",
"+",
"style",
".",
"CellWidth",
"(",
"i",
")",
"+",
"style",
".",
"PaddingRight",
"\n",
"parts",
"=",
"append",
"(",
"parts",
",",
"strings",
".",
"Repeat",
"(",
"style",
".",
"BorderX",
",",
"w",
")",
")",
"\n",
"}",
"\n\n",
"switch",
"s",
".",
"where",
"{",
"case",
"LINE_TOP",
":",
"return",
"style",
".",
"BorderTopLeft",
"+",
"strings",
".",
"Join",
"(",
"parts",
",",
"style",
".",
"BorderTop",
")",
"+",
"style",
".",
"BorderTopRight",
"\n",
"case",
"LINE_SUBTOP",
":",
"return",
"style",
".",
"BorderLeft",
"+",
"strings",
".",
"Join",
"(",
"parts",
",",
"style",
".",
"BorderTop",
")",
"+",
"style",
".",
"BorderRight",
"\n",
"case",
"LINE_BOTTOM",
":",
"return",
"style",
".",
"BorderBottomLeft",
"+",
"strings",
".",
"Join",
"(",
"parts",
",",
"style",
".",
"BorderBottom",
")",
"+",
"style",
".",
"BorderBottomRight",
"\n",
"case",
"LINE_INNER",
":",
"return",
"style",
".",
"BorderLeft",
"+",
"strings",
".",
"Join",
"(",
"parts",
",",
"style",
".",
"BorderI",
")",
"+",
"style",
".",
"BorderRight",
"\n",
"}",
"\n",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}"
]
| // Render returns the string representation of a horizontal rule line in the
// table. | [
"Render",
"returns",
"the",
"string",
"representation",
"of",
"a",
"horizontal",
"rule",
"line",
"in",
"the",
"table",
"."
]
| bcbc5dc54055d14996b256d348fb75cc6debe282 | https://github.com/apcera/termtables/blob/bcbc5dc54055d14996b256d348fb75cc6debe282/separator.go#L41-L60 |
13,147 | apcera/termtables | row.go | CreateRow | func CreateRow(items []interface{}) *Row {
row := &Row{cells: []*Cell{}}
for _, item := range items {
row.AddCell(item)
}
return row
} | go | func CreateRow(items []interface{}) *Row {
row := &Row{cells: []*Cell{}}
for _, item := range items {
row.AddCell(item)
}
return row
} | [
"func",
"CreateRow",
"(",
"items",
"[",
"]",
"interface",
"{",
"}",
")",
"*",
"Row",
"{",
"row",
":=",
"&",
"Row",
"{",
"cells",
":",
"[",
"]",
"*",
"Cell",
"{",
"}",
"}",
"\n",
"for",
"_",
",",
"item",
":=",
"range",
"items",
"{",
"row",
".",
"AddCell",
"(",
"item",
")",
"\n",
"}",
"\n",
"return",
"row",
"\n",
"}"
]
| // CreateRow returns a Row where the cells are created as needed to hold each
// item given; each item can be a Cell or content to go into a Cell created
// to hold it. | [
"CreateRow",
"returns",
"a",
"Row",
"where",
"the",
"cells",
"are",
"created",
"as",
"needed",
"to",
"hold",
"each",
"item",
"given",
";",
"each",
"item",
"can",
"be",
"a",
"Cell",
"or",
"content",
"to",
"go",
"into",
"a",
"Cell",
"created",
"to",
"hold",
"it",
"."
]
| bcbc5dc54055d14996b256d348fb75cc6debe282 | https://github.com/apcera/termtables/blob/bcbc5dc54055d14996b256d348fb75cc6debe282/row.go#L16-L22 |
13,148 | apcera/termtables | row.go | AddCell | 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 | 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))
}
} | [
"func",
"(",
"r",
"*",
"Row",
")",
"AddCell",
"(",
"item",
"interface",
"{",
"}",
")",
"{",
"if",
"c",
",",
"ok",
":=",
"item",
".",
"(",
"*",
"Cell",
")",
";",
"ok",
"{",
"c",
".",
"column",
"=",
"len",
"(",
"r",
".",
"cells",
")",
"\n",
"r",
".",
"cells",
"=",
"append",
"(",
"r",
".",
"cells",
",",
"c",
")",
"\n",
"}",
"else",
"{",
"r",
".",
"cells",
"=",
"append",
"(",
"r",
".",
"cells",
",",
"createCell",
"(",
"len",
"(",
"r",
".",
"cells",
")",
",",
"item",
",",
"nil",
")",
")",
"\n",
"}",
"\n",
"}"
]
| // AddCell adds one item to a row as a new cell, where the item is either a
// Cell or content to be put into a cell. | [
"AddCell",
"adds",
"one",
"item",
"to",
"a",
"row",
"as",
"a",
"new",
"cell",
"where",
"the",
"item",
"is",
"either",
"a",
"Cell",
"or",
"content",
"to",
"be",
"put",
"into",
"a",
"cell",
"."
]
| bcbc5dc54055d14996b256d348fb75cc6debe282 | https://github.com/apcera/termtables/blob/bcbc5dc54055d14996b256d348fb75cc6debe282/row.go#L26-L33 |
13,149 | apcera/termtables | html.go | HTML | 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] = " align='center'"
case AlignRight:
attrs[i] = " align='right'"
}
}
elems[i] = html.EscapeString(strings.TrimSpace(r.cells[i].Render(style)))
}
// WAG as to max capacity, plus a bit
buf := bytes.NewBuffer(make([]byte, 0, 8192))
buf.WriteString("<tr>")
for i := range elems {
fmt.Fprintf(buf, "<%s%s>%s</%s>", tag, attrs[i], elems[i], tag)
}
buf.WriteString("</tr>\n")
return buf.String()
} | go | 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] = " align='center'"
case AlignRight:
attrs[i] = " align='right'"
}
}
elems[i] = html.EscapeString(strings.TrimSpace(r.cells[i].Render(style)))
}
// WAG as to max capacity, plus a bit
buf := bytes.NewBuffer(make([]byte, 0, 8192))
buf.WriteString("<tr>")
for i := range elems {
fmt.Fprintf(buf, "<%s%s>%s</%s>", tag, attrs[i], elems[i], tag)
}
buf.WriteString("</tr>\n")
return buf.String()
} | [
"func",
"(",
"r",
"*",
"Row",
")",
"HTML",
"(",
"tag",
"string",
",",
"style",
"*",
"renderStyle",
")",
"string",
"{",
"attrs",
":=",
"make",
"(",
"[",
"]",
"string",
",",
"len",
"(",
"r",
".",
"cells",
")",
")",
"\n",
"elems",
":=",
"make",
"(",
"[",
"]",
"string",
",",
"len",
"(",
"r",
".",
"cells",
")",
")",
"\n",
"for",
"i",
":=",
"range",
"r",
".",
"cells",
"{",
"if",
"r",
".",
"cells",
"[",
"i",
"]",
".",
"alignment",
"!=",
"nil",
"{",
"switch",
"*",
"r",
".",
"cells",
"[",
"i",
"]",
".",
"alignment",
"{",
"case",
"AlignLeft",
":",
"attrs",
"[",
"i",
"]",
"=",
"\"",
"\"",
"\n",
"case",
"AlignCenter",
":",
"attrs",
"[",
"i",
"]",
"=",
"\"",
"\"",
"\n",
"case",
"AlignRight",
":",
"attrs",
"[",
"i",
"]",
"=",
"\"",
"\"",
"\n",
"}",
"\n",
"}",
"\n",
"elems",
"[",
"i",
"]",
"=",
"html",
".",
"EscapeString",
"(",
"strings",
".",
"TrimSpace",
"(",
"r",
".",
"cells",
"[",
"i",
"]",
".",
"Render",
"(",
"style",
")",
")",
")",
"\n",
"}",
"\n",
"// WAG as to max capacity, plus a bit",
"buf",
":=",
"bytes",
".",
"NewBuffer",
"(",
"make",
"(",
"[",
"]",
"byte",
",",
"0",
",",
"8192",
")",
")",
"\n",
"buf",
".",
"WriteString",
"(",
"\"",
"\"",
")",
"\n",
"for",
"i",
":=",
"range",
"elems",
"{",
"fmt",
".",
"Fprintf",
"(",
"buf",
",",
"\"",
"\"",
",",
"tag",
",",
"attrs",
"[",
"i",
"]",
",",
"elems",
"[",
"i",
"]",
",",
"tag",
")",
"\n",
"}",
"\n",
"buf",
".",
"WriteString",
"(",
"\"",
"\\n",
"\"",
")",
"\n",
"return",
"buf",
".",
"String",
"(",
")",
"\n",
"}"
]
| // HTML returns an HTML representations of the contents of one row of a table. | [
"HTML",
"returns",
"an",
"HTML",
"representations",
"of",
"the",
"contents",
"of",
"one",
"row",
"of",
"a",
"table",
"."
]
| bcbc5dc54055d14996b256d348fb75cc6debe282 | https://github.com/apcera/termtables/blob/bcbc5dc54055d14996b256d348fb75cc6debe282/html.go#L26-L50 |
13,150 | apcera/termtables | html.go | RenderHTML | 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.elements)+6)
if t.title != nil || t.headers != nil {
rowsText = append(rowsText, "<thead>\n")
if t.title != nil {
rowsText = append(rowsText, generateHtmlTitleRow(t.title, t, style))
}
if t.headers != nil {
rowsText = append(rowsText, CreateRow(t.headers).HTML("th", style))
}
rowsText = append(rowsText, "</thead>\n")
}
rowsText = append(rowsText, "<tbody>\n")
// loop over the elements and render them
for i := range t.elements {
if row, ok := t.elements[i].(*Row); ok {
rowsText = append(rowsText, row.HTML("td", style))
} else {
rowsText = append(rowsText, fmt.Sprintf("<!-- unable to render line %d, unhandled type -->\n", i))
}
}
rowsText = append(rowsText, "</tbody>\n")
return "<table class=\"termtable\">\n" + strings.Join(rowsText, "") + "</table>\n"
} | go | 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.elements)+6)
if t.title != nil || t.headers != nil {
rowsText = append(rowsText, "<thead>\n")
if t.title != nil {
rowsText = append(rowsText, generateHtmlTitleRow(t.title, t, style))
}
if t.headers != nil {
rowsText = append(rowsText, CreateRow(t.headers).HTML("th", style))
}
rowsText = append(rowsText, "</thead>\n")
}
rowsText = append(rowsText, "<tbody>\n")
// loop over the elements and render them
for i := range t.elements {
if row, ok := t.elements[i].(*Row); ok {
rowsText = append(rowsText, row.HTML("td", style))
} else {
rowsText = append(rowsText, fmt.Sprintf("<!-- unable to render line %d, unhandled type -->\n", i))
}
}
rowsText = append(rowsText, "</tbody>\n")
return "<table class=\"termtable\">\n" + strings.Join(rowsText, "") + "</table>\n"
} | [
"func",
"(",
"t",
"*",
"Table",
")",
"RenderHTML",
"(",
")",
"(",
"buffer",
"string",
")",
"{",
"// elements is already populated with row data",
"// generate the runtime style",
"style",
":=",
"createRenderStyle",
"(",
"t",
")",
"\n",
"style",
".",
"PaddingLeft",
"=",
"0",
"\n",
"style",
".",
"PaddingRight",
"=",
"0",
"\n\n",
"// TODO: control CSS styles to suppress border based upon t.Style.SkipBorder",
"rowsText",
":=",
"make",
"(",
"[",
"]",
"string",
",",
"0",
",",
"len",
"(",
"t",
".",
"elements",
")",
"+",
"6",
")",
"\n\n",
"if",
"t",
".",
"title",
"!=",
"nil",
"||",
"t",
".",
"headers",
"!=",
"nil",
"{",
"rowsText",
"=",
"append",
"(",
"rowsText",
",",
"\"",
"\\n",
"\"",
")",
"\n",
"if",
"t",
".",
"title",
"!=",
"nil",
"{",
"rowsText",
"=",
"append",
"(",
"rowsText",
",",
"generateHtmlTitleRow",
"(",
"t",
".",
"title",
",",
"t",
",",
"style",
")",
")",
"\n",
"}",
"\n",
"if",
"t",
".",
"headers",
"!=",
"nil",
"{",
"rowsText",
"=",
"append",
"(",
"rowsText",
",",
"CreateRow",
"(",
"t",
".",
"headers",
")",
".",
"HTML",
"(",
"\"",
"\"",
",",
"style",
")",
")",
"\n",
"}",
"\n",
"rowsText",
"=",
"append",
"(",
"rowsText",
",",
"\"",
"\\n",
"\"",
")",
"\n",
"}",
"\n\n",
"rowsText",
"=",
"append",
"(",
"rowsText",
",",
"\"",
"\\n",
"\"",
")",
"\n",
"// loop over the elements and render them",
"for",
"i",
":=",
"range",
"t",
".",
"elements",
"{",
"if",
"row",
",",
"ok",
":=",
"t",
".",
"elements",
"[",
"i",
"]",
".",
"(",
"*",
"Row",
")",
";",
"ok",
"{",
"rowsText",
"=",
"append",
"(",
"rowsText",
",",
"row",
".",
"HTML",
"(",
"\"",
"\"",
",",
"style",
")",
")",
"\n",
"}",
"else",
"{",
"rowsText",
"=",
"append",
"(",
"rowsText",
",",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\\n",
"\"",
",",
"i",
")",
")",
"\n",
"}",
"\n",
"}",
"\n",
"rowsText",
"=",
"append",
"(",
"rowsText",
",",
"\"",
"\\n",
"\"",
")",
"\n\n",
"return",
"\"",
"\\\"",
"\\\"",
"\\n",
"\"",
"+",
"strings",
".",
"Join",
"(",
"rowsText",
",",
"\"",
"\"",
")",
"+",
"\"",
"\\n",
"\"",
"\n",
"}"
]
| // RenderHTML returns a string representation of a the table, suitable for
// inclusion as HTML elsewhere. Primary use-case controlling layout style
// is for inclusion into Markdown documents, documenting normal table use.
// Thus we leave the padding in place to have columns align when viewed as
// plain text and rely upon HTML ignoring extra whitespace. | [
"RenderHTML",
"returns",
"a",
"string",
"representation",
"of",
"a",
"the",
"table",
"suitable",
"for",
"inclusion",
"as",
"HTML",
"elsewhere",
".",
"Primary",
"use",
"-",
"case",
"controlling",
"layout",
"style",
"is",
"for",
"inclusion",
"into",
"Markdown",
"documents",
"documenting",
"normal",
"table",
"use",
".",
"Thus",
"we",
"leave",
"the",
"padding",
"in",
"place",
"to",
"have",
"columns",
"align",
"when",
"viewed",
"as",
"plain",
"text",
"and",
"rely",
"upon",
"HTML",
"ignoring",
"extra",
"whitespace",
"."
]
| bcbc5dc54055d14996b256d348fb75cc6debe282 | https://github.com/apcera/termtables/blob/bcbc5dc54055d14996b256d348fb75cc6debe282/html.go#L73-L107 |
13,151 | apcera/termtables | cell.go | renderValue | 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, 'f', 2, 64)
case fmt.Stringer:
return vv.String()
}
return fmt.Sprintf("%v", v)
} | go | 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, 'f', 2, 64)
case fmt.Stringer:
return vv.String()
}
return fmt.Sprintf("%v", v)
} | [
"func",
"renderValue",
"(",
"v",
"interface",
"{",
"}",
")",
"string",
"{",
"switch",
"vv",
":=",
"v",
".",
"(",
"type",
")",
"{",
"case",
"string",
":",
"return",
"vv",
"\n",
"case",
"bool",
":",
"return",
"strconv",
".",
"FormatBool",
"(",
"vv",
")",
"\n",
"case",
"int",
":",
"return",
"strconv",
".",
"Itoa",
"(",
"vv",
")",
"\n",
"case",
"int64",
":",
"return",
"strconv",
".",
"FormatInt",
"(",
"vv",
",",
"10",
")",
"\n",
"case",
"uint64",
":",
"return",
"strconv",
".",
"FormatUint",
"(",
"vv",
",",
"10",
")",
"\n",
"case",
"float64",
":",
"return",
"strconv",
".",
"FormatFloat",
"(",
"vv",
",",
"'f'",
",",
"2",
",",
"64",
")",
"\n",
"case",
"fmt",
".",
"Stringer",
":",
"return",
"vv",
".",
"String",
"(",
")",
"\n",
"}",
"\n",
"return",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"v",
")",
"\n",
"}"
]
| // Format the raw value as a string depending on the type | [
"Format",
"the",
"raw",
"value",
"as",
"a",
"string",
"depending",
"on",
"the",
"type"
]
| bcbc5dc54055d14996b256d348fb75cc6debe282 | https://github.com/apcera/termtables/blob/bcbc5dc54055d14996b256d348fb75cc6debe282/cell.go#L150-L168 |
13,152 | apcera/termtables | style.go | setUtfBoxStyle | 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 | 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 = "╯"
} | [
"func",
"(",
"s",
"*",
"TableStyle",
")",
"setUtfBoxStyle",
"(",
")",
"{",
"s",
".",
"BorderX",
"=",
"\"",
"",
"\n",
"s",
".",
"BorderY",
"=",
"\"",
"",
"\n",
"s",
".",
"BorderI",
"=",
"\"",
"",
"\n",
"s",
".",
"BorderTop",
"=",
"\"",
"",
"\n",
"s",
".",
"BorderBottom",
"=",
"\"",
"",
"\n",
"s",
".",
"BorderLeft",
"=",
"\"",
"",
"\n",
"s",
".",
"BorderRight",
"=",
"\"",
"",
"\n",
"s",
".",
"BorderTopLeft",
"=",
"\"",
"",
"\n",
"s",
".",
"BorderTopRight",
"=",
"\"",
"",
"\n",
"s",
".",
"BorderBottomLeft",
"=",
"\"",
"",
"\n",
"s",
".",
"BorderBottomRight",
"=",
"\"",
"",
"\n",
"}"
]
| // setUtfBoxStyle changes the border characters to be suitable for use when
// the output stream can render UTF-8 characters. | [
"setUtfBoxStyle",
"changes",
"the",
"border",
"characters",
"to",
"be",
"suitable",
"for",
"use",
"when",
"the",
"output",
"stream",
"can",
"render",
"UTF",
"-",
"8",
"characters",
"."
]
| bcbc5dc54055d14996b256d348fb75cc6debe282 | https://github.com/apcera/termtables/blob/bcbc5dc54055d14996b256d348fb75cc6debe282/style.go#L80-L92 |
13,153 | apcera/termtables | style.go | fillStyleRules | 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.BorderI
}
if s.BorderTopRight == "" {
s.BorderTopRight = s.BorderI
}
if s.BorderBottomLeft == "" {
s.BorderBottomLeft = s.BorderI
}
if s.BorderBottomRight == "" {
s.BorderBottomRight = s.BorderI
}
} | go | 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.BorderI
}
if s.BorderTopRight == "" {
s.BorderTopRight = s.BorderI
}
if s.BorderBottomLeft == "" {
s.BorderBottomLeft = s.BorderI
}
if s.BorderBottomRight == "" {
s.BorderBottomRight = s.BorderI
}
} | [
"func",
"(",
"s",
"*",
"TableStyle",
")",
"fillStyleRules",
"(",
")",
"{",
"if",
"s",
".",
"BorderTop",
"==",
"\"",
"\"",
"{",
"s",
".",
"BorderTop",
"=",
"s",
".",
"BorderI",
"\n",
"}",
"\n",
"if",
"s",
".",
"BorderBottom",
"==",
"\"",
"\"",
"{",
"s",
".",
"BorderBottom",
"=",
"s",
".",
"BorderI",
"\n",
"}",
"\n",
"if",
"s",
".",
"BorderLeft",
"==",
"\"",
"\"",
"{",
"s",
".",
"BorderLeft",
"=",
"s",
".",
"BorderI",
"\n",
"}",
"\n",
"if",
"s",
".",
"BorderRight",
"==",
"\"",
"\"",
"{",
"s",
".",
"BorderRight",
"=",
"s",
".",
"BorderI",
"\n",
"}",
"\n",
"if",
"s",
".",
"BorderTopLeft",
"==",
"\"",
"\"",
"{",
"s",
".",
"BorderTopLeft",
"=",
"s",
".",
"BorderI",
"\n",
"}",
"\n",
"if",
"s",
".",
"BorderTopRight",
"==",
"\"",
"\"",
"{",
"s",
".",
"BorderTopRight",
"=",
"s",
".",
"BorderI",
"\n",
"}",
"\n",
"if",
"s",
".",
"BorderBottomLeft",
"==",
"\"",
"\"",
"{",
"s",
".",
"BorderBottomLeft",
"=",
"s",
".",
"BorderI",
"\n",
"}",
"\n",
"if",
"s",
".",
"BorderBottomRight",
"==",
"\"",
"\"",
"{",
"s",
".",
"BorderBottomRight",
"=",
"s",
".",
"BorderI",
"\n",
"}",
"\n",
"}"
]
| // fillStyleRules populates members of the TableStyle box-drawing specification
// with BorderI as the default. | [
"fillStyleRules",
"populates",
"members",
"of",
"the",
"TableStyle",
"box",
"-",
"drawing",
"specification",
"with",
"BorderI",
"as",
"the",
"default",
"."
]
| bcbc5dc54055d14996b256d348fb75cc6debe282 | https://github.com/apcera/termtables/blob/bcbc5dc54055d14996b256d348fb75cc6debe282/style.go#L106-L131 |
13,154 | apcera/termtables | style.go | buildReplaceContent | 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 | 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)
}
} | [
"func",
"(",
"s",
"*",
"renderStyle",
")",
"buildReplaceContent",
"(",
"bad",
"string",
")",
"{",
"replacement",
":=",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"bad",
")",
"\n",
"s",
".",
"replaceContent",
"=",
"func",
"(",
"old",
"string",
")",
"string",
"{",
"return",
"strings",
".",
"Replace",
"(",
"old",
",",
"bad",
",",
"replacement",
",",
"-",
"1",
")",
"\n",
"}",
"\n",
"}"
]
| // buildReplaceContent creates a function closure, with minimal bound lexical
// state, which replaces content | [
"buildReplaceContent",
"creates",
"a",
"function",
"closure",
"with",
"minimal",
"bound",
"lexical",
"state",
"which",
"replaces",
"content"
]
| bcbc5dc54055d14996b256d348fb75cc6debe282 | https://github.com/apcera/termtables/blob/bcbc5dc54055d14996b256d348fb75cc6debe282/style.go#L209-L214 |
13,155 | apcera/termtables | term/sizes_windows.go | GetTerminalWindowSize | 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),
}, nil
} | go | 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),
}, nil
} | [
"func",
"GetTerminalWindowSize",
"(",
"file",
"*",
"os",
".",
"File",
")",
"(",
"*",
"Size",
",",
"error",
")",
"{",
"var",
"info",
"consoleScreenBufferInfo",
"\n",
"_",
",",
"_",
",",
"e",
":=",
"syscall",
".",
"Syscall",
"(",
"procGetConsoleScreenBufferInfo",
".",
"Addr",
"(",
")",
",",
"2",
",",
"file",
".",
"Fd",
"(",
")",
",",
"uintptr",
"(",
"unsafe",
".",
"Pointer",
"(",
"&",
"info",
")",
")",
",",
"0",
")",
"\n",
"if",
"e",
"!=",
"0",
"{",
"return",
"nil",
",",
"error",
"(",
"e",
")",
"\n",
"}",
"\n",
"return",
"&",
"Size",
"{",
"Lines",
":",
"int",
"(",
"info",
".",
"size",
".",
"y",
")",
",",
"Columns",
":",
"int",
"(",
"info",
".",
"size",
".",
"x",
")",
",",
"}",
",",
"nil",
"\n",
"}"
]
| // GetTerminalWindowSize returns the width and height of a terminal in Windows. | [
"GetTerminalWindowSize",
"returns",
"the",
"width",
"and",
"height",
"of",
"a",
"terminal",
"in",
"Windows",
"."
]
| bcbc5dc54055d14996b256d348fb75cc6debe282 | https://github.com/apcera/termtables/blob/bcbc5dc54055d14996b256d348fb75cc6debe282/term/sizes_windows.go#L47-L57 |
13,156 | unrolled/render | buffer.go | Get | 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 | func (bp *BufferPool) Get() (b *bytes.Buffer) {
select {
case b = <-bp.c:
// reuse existing buffer
default:
// create new buffer
b = bytes.NewBuffer([]byte{})
}
return
} | [
"func",
"(",
"bp",
"*",
"BufferPool",
")",
"Get",
"(",
")",
"(",
"b",
"*",
"bytes",
".",
"Buffer",
")",
"{",
"select",
"{",
"case",
"b",
"=",
"<-",
"bp",
".",
"c",
":",
"// reuse existing buffer",
"default",
":",
"// create new buffer",
"b",
"=",
"bytes",
".",
"NewBuffer",
"(",
"[",
"]",
"byte",
"{",
"}",
")",
"\n",
"}",
"\n",
"return",
"\n",
"}"
]
| // Get gets a Buffer from the BufferPool, or creates a new one if none are
// available in the pool. | [
"Get",
"gets",
"a",
"Buffer",
"from",
"the",
"BufferPool",
"or",
"creates",
"a",
"new",
"one",
"if",
"none",
"are",
"available",
"in",
"the",
"pool",
"."
]
| 1ac792296fd4f4de9559a5f9a85c277c54ffb1cf | https://github.com/unrolled/render/blob/1ac792296fd4f4de9559a5f9a85c277c54ffb1cf/buffer.go#L23-L32 |
13,157 | unrolled/render | render.go | New | 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 | 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
} | [
"func",
"New",
"(",
"options",
"...",
"Options",
")",
"*",
"Render",
"{",
"var",
"o",
"Options",
"\n",
"if",
"len",
"(",
"options",
")",
">",
"0",
"{",
"o",
"=",
"options",
"[",
"0",
"]",
"\n",
"}",
"\n\n",
"r",
":=",
"Render",
"{",
"opt",
":",
"o",
",",
"}",
"\n\n",
"r",
".",
"prepareOptions",
"(",
")",
"\n",
"r",
".",
"compileTemplates",
"(",
")",
"\n\n",
"return",
"&",
"r",
"\n",
"}"
]
| // New constructs a new Render instance with the supplied options. | [
"New",
"constructs",
"a",
"new",
"Render",
"instance",
"with",
"the",
"supplied",
"options",
"."
]
| 1ac792296fd4f4de9559a5f9a85c277c54ffb1cf | https://github.com/unrolled/render/blob/1ac792296fd4f4de9559a5f9a85c277c54ffb1cf/render.go#L124-L138 |
13,158 | unrolled/render | render.go | TemplateLookup | func (r *Render) TemplateLookup(t string) *template.Template {
return r.templates.Lookup(t)
} | go | func (r *Render) TemplateLookup(t string) *template.Template {
return r.templates.Lookup(t)
} | [
"func",
"(",
"r",
"*",
"Render",
")",
"TemplateLookup",
"(",
"t",
"string",
")",
"*",
"template",
".",
"Template",
"{",
"return",
"r",
".",
"templates",
".",
"Lookup",
"(",
"t",
")",
"\n",
"}"
]
| // TemplateLookup is a wrapper around template.Lookup and returns
// the template with the given name that is associated with t, or nil
// if there is no such template. | [
"TemplateLookup",
"is",
"a",
"wrapper",
"around",
"template",
".",
"Lookup",
"and",
"returns",
"the",
"template",
"with",
"the",
"given",
"name",
"that",
"is",
"associated",
"with",
"t",
"or",
"nil",
"if",
"there",
"is",
"no",
"such",
"template",
"."
]
| 1ac792296fd4f4de9559a5f9a85c277c54ffb1cf | https://github.com/unrolled/render/blob/1ac792296fd4f4de9559a5f9a85c277c54ffb1cf/render.go#L279-L281 |
13,159 | unrolled/render | render.go | Render | 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 | 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
} | [
"func",
"(",
"r",
"*",
"Render",
")",
"Render",
"(",
"w",
"io",
".",
"Writer",
",",
"e",
"Engine",
",",
"data",
"interface",
"{",
"}",
")",
"error",
"{",
"err",
":=",
"e",
".",
"Render",
"(",
"w",
",",
"data",
")",
"\n",
"if",
"hw",
",",
"ok",
":=",
"w",
".",
"(",
"http",
".",
"ResponseWriter",
")",
";",
"err",
"!=",
"nil",
"&&",
"!",
"r",
".",
"opt",
".",
"DisableHTTPErrorRendering",
"&&",
"ok",
"{",
"http",
".",
"Error",
"(",
"hw",
",",
"err",
".",
"Error",
"(",
")",
",",
"http",
".",
"StatusInternalServerError",
")",
"\n",
"}",
"\n",
"return",
"err",
"\n",
"}"
]
| // Render is the generic function called by XML, JSON, Data, HTML, and can be called by custom implementations. | [
"Render",
"is",
"the",
"generic",
"function",
"called",
"by",
"XML",
"JSON",
"Data",
"HTML",
"and",
"can",
"be",
"called",
"by",
"custom",
"implementations",
"."
]
| 1ac792296fd4f4de9559a5f9a85c277c54ffb1cf | https://github.com/unrolled/render/blob/1ac792296fd4f4de9559a5f9a85c277c54ffb1cf/render.go#L340-L346 |
13,160 | unrolled/render | render.go | Data | 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 | 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)
} | [
"func",
"(",
"r",
"*",
"Render",
")",
"Data",
"(",
"w",
"io",
".",
"Writer",
",",
"status",
"int",
",",
"v",
"[",
"]",
"byte",
")",
"error",
"{",
"head",
":=",
"Head",
"{",
"ContentType",
":",
"r",
".",
"opt",
".",
"BinaryContentType",
",",
"Status",
":",
"status",
",",
"}",
"\n\n",
"d",
":=",
"Data",
"{",
"Head",
":",
"head",
",",
"}",
"\n\n",
"return",
"r",
".",
"Render",
"(",
"w",
",",
"d",
",",
"v",
")",
"\n",
"}"
]
| // Data writes out the raw bytes as binary data. | [
"Data",
"writes",
"out",
"the",
"raw",
"bytes",
"as",
"binary",
"data",
"."
]
| 1ac792296fd4f4de9559a5f9a85c277c54ffb1cf | https://github.com/unrolled/render/blob/1ac792296fd4f4de9559a5f9a85c277c54ffb1cf/render.go#L349-L360 |
13,161 | unrolled/render | render.go | HTML | 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.prepareHTMLOptions(htmlOpt)
// Assign a layout if there is one.
if len(opt.Layout) > 0 {
r.addLayoutFuncs(name, binding)
name = opt.Layout
}
head := Head{
ContentType: r.opt.HTMLContentType + r.compiledCharset,
Status: status,
}
h := HTML{
Head: head,
Name: name,
Templates: r.templates,
}
return r.Render(w, h, binding)
} | go | 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.prepareHTMLOptions(htmlOpt)
// Assign a layout if there is one.
if len(opt.Layout) > 0 {
r.addLayoutFuncs(name, binding)
name = opt.Layout
}
head := Head{
ContentType: r.opt.HTMLContentType + r.compiledCharset,
Status: status,
}
h := HTML{
Head: head,
Name: name,
Templates: r.templates,
}
return r.Render(w, h, binding)
} | [
"func",
"(",
"r",
"*",
"Render",
")",
"HTML",
"(",
"w",
"io",
".",
"Writer",
",",
"status",
"int",
",",
"name",
"string",
",",
"binding",
"interface",
"{",
"}",
",",
"htmlOpt",
"...",
"HTMLOptions",
")",
"error",
"{",
"r",
".",
"templatesLk",
".",
"Lock",
"(",
")",
"\n",
"defer",
"r",
".",
"templatesLk",
".",
"Unlock",
"(",
")",
"\n\n",
"// If we are in development mode, recompile the templates on every HTML request.",
"if",
"r",
".",
"opt",
".",
"IsDevelopment",
"{",
"r",
".",
"compileTemplates",
"(",
")",
"\n",
"}",
"\n\n",
"opt",
":=",
"r",
".",
"prepareHTMLOptions",
"(",
"htmlOpt",
")",
"\n",
"// Assign a layout if there is one.",
"if",
"len",
"(",
"opt",
".",
"Layout",
")",
">",
"0",
"{",
"r",
".",
"addLayoutFuncs",
"(",
"name",
",",
"binding",
")",
"\n",
"name",
"=",
"opt",
".",
"Layout",
"\n",
"}",
"\n\n",
"head",
":=",
"Head",
"{",
"ContentType",
":",
"r",
".",
"opt",
".",
"HTMLContentType",
"+",
"r",
".",
"compiledCharset",
",",
"Status",
":",
"status",
",",
"}",
"\n\n",
"h",
":=",
"HTML",
"{",
"Head",
":",
"head",
",",
"Name",
":",
"name",
",",
"Templates",
":",
"r",
".",
"templates",
",",
"}",
"\n\n",
"return",
"r",
".",
"Render",
"(",
"w",
",",
"h",
",",
"binding",
")",
"\n",
"}"
]
| // HTML builds up the response from the specified template and bindings. | [
"HTML",
"builds",
"up",
"the",
"response",
"from",
"the",
"specified",
"template",
"and",
"bindings",
"."
]
| 1ac792296fd4f4de9559a5f9a85c277c54ffb1cf | https://github.com/unrolled/render/blob/1ac792296fd4f4de9559a5f9a85c277c54ffb1cf/render.go#L363-L391 |
13,162 | unrolled/render | render.go | JSON | 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,
StreamingJSON: r.opt.StreamingJSON,
}
return r.Render(w, j, v)
} | go | 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,
StreamingJSON: r.opt.StreamingJSON,
}
return r.Render(w, j, v)
} | [
"func",
"(",
"r",
"*",
"Render",
")",
"JSON",
"(",
"w",
"io",
".",
"Writer",
",",
"status",
"int",
",",
"v",
"interface",
"{",
"}",
")",
"error",
"{",
"head",
":=",
"Head",
"{",
"ContentType",
":",
"r",
".",
"opt",
".",
"JSONContentType",
"+",
"r",
".",
"compiledCharset",
",",
"Status",
":",
"status",
",",
"}",
"\n\n",
"j",
":=",
"JSON",
"{",
"Head",
":",
"head",
",",
"Indent",
":",
"r",
".",
"opt",
".",
"IndentJSON",
",",
"Prefix",
":",
"r",
".",
"opt",
".",
"PrefixJSON",
",",
"UnEscapeHTML",
":",
"r",
".",
"opt",
".",
"UnEscapeHTML",
",",
"StreamingJSON",
":",
"r",
".",
"opt",
".",
"StreamingJSON",
",",
"}",
"\n\n",
"return",
"r",
".",
"Render",
"(",
"w",
",",
"j",
",",
"v",
")",
"\n",
"}"
]
| // JSON marshals the given interface object and writes the JSON response. | [
"JSON",
"marshals",
"the",
"given",
"interface",
"object",
"and",
"writes",
"the",
"JSON",
"response",
"."
]
| 1ac792296fd4f4de9559a5f9a85c277c54ffb1cf | https://github.com/unrolled/render/blob/1ac792296fd4f4de9559a5f9a85c277c54ffb1cf/render.go#L394-L409 |
13,163 | unrolled/render | render.go | JSONP | 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 | 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)
} | [
"func",
"(",
"r",
"*",
"Render",
")",
"JSONP",
"(",
"w",
"io",
".",
"Writer",
",",
"status",
"int",
",",
"callback",
"string",
",",
"v",
"interface",
"{",
"}",
")",
"error",
"{",
"head",
":=",
"Head",
"{",
"ContentType",
":",
"r",
".",
"opt",
".",
"JSONPContentType",
"+",
"r",
".",
"compiledCharset",
",",
"Status",
":",
"status",
",",
"}",
"\n\n",
"j",
":=",
"JSONP",
"{",
"Head",
":",
"head",
",",
"Indent",
":",
"r",
".",
"opt",
".",
"IndentJSON",
",",
"Callback",
":",
"callback",
",",
"}",
"\n\n",
"return",
"r",
".",
"Render",
"(",
"w",
",",
"j",
",",
"v",
")",
"\n",
"}"
]
| // JSONP marshals the given interface object and writes the JSON response. | [
"JSONP",
"marshals",
"the",
"given",
"interface",
"object",
"and",
"writes",
"the",
"JSON",
"response",
"."
]
| 1ac792296fd4f4de9559a5f9a85c277c54ffb1cf | https://github.com/unrolled/render/blob/1ac792296fd4f4de9559a5f9a85c277c54ffb1cf/render.go#L412-L425 |
13,164 | unrolled/render | render.go | XML | 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 | 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)
} | [
"func",
"(",
"r",
"*",
"Render",
")",
"XML",
"(",
"w",
"io",
".",
"Writer",
",",
"status",
"int",
",",
"v",
"interface",
"{",
"}",
")",
"error",
"{",
"head",
":=",
"Head",
"{",
"ContentType",
":",
"r",
".",
"opt",
".",
"XMLContentType",
"+",
"r",
".",
"compiledCharset",
",",
"Status",
":",
"status",
",",
"}",
"\n\n",
"x",
":=",
"XML",
"{",
"Head",
":",
"head",
",",
"Indent",
":",
"r",
".",
"opt",
".",
"IndentXML",
",",
"Prefix",
":",
"r",
".",
"opt",
".",
"PrefixXML",
",",
"}",
"\n\n",
"return",
"r",
".",
"Render",
"(",
"w",
",",
"x",
",",
"v",
")",
"\n",
"}"
]
| // XML marshals the given interface object and writes the XML response. | [
"XML",
"marshals",
"the",
"given",
"interface",
"object",
"and",
"writes",
"the",
"XML",
"response",
"."
]
| 1ac792296fd4f4de9559a5f9a85c277c54ffb1cf | https://github.com/unrolled/render/blob/1ac792296fd4f4de9559a5f9a85c277c54ffb1cf/render.go#L442-L455 |
13,165 | unrolled/render | engine.go | Write | func (h Head) Write(w http.ResponseWriter) {
w.Header().Set(ContentType, h.ContentType)
w.WriteHeader(h.Status)
} | go | func (h Head) Write(w http.ResponseWriter) {
w.Header().Set(ContentType, h.ContentType)
w.WriteHeader(h.Status)
} | [
"func",
"(",
"h",
"Head",
")",
"Write",
"(",
"w",
"http",
".",
"ResponseWriter",
")",
"{",
"w",
".",
"Header",
"(",
")",
".",
"Set",
"(",
"ContentType",
",",
"h",
".",
"ContentType",
")",
"\n",
"w",
".",
"WriteHeader",
"(",
"h",
".",
"Status",
")",
"\n",
"}"
]
| // Write outputs the header content. | [
"Write",
"outputs",
"the",
"header",
"content",
"."
]
| 1ac792296fd4f4de9559a5f9a85c277c54ffb1cf | https://github.com/unrolled/render/blob/1ac792296fd4f4de9559a5f9a85c277c54ffb1cf/engine.go#L64-L67 |
13,166 | unrolled/render | engine.go | Render | 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 | 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
} | [
"func",
"(",
"d",
"Data",
")",
"Render",
"(",
"w",
"io",
".",
"Writer",
",",
"v",
"interface",
"{",
"}",
")",
"error",
"{",
"if",
"hw",
",",
"ok",
":=",
"w",
".",
"(",
"http",
".",
"ResponseWriter",
")",
";",
"ok",
"{",
"c",
":=",
"hw",
".",
"Header",
"(",
")",
".",
"Get",
"(",
"ContentType",
")",
"\n",
"if",
"c",
"!=",
"\"",
"\"",
"{",
"d",
".",
"Head",
".",
"ContentType",
"=",
"c",
"\n",
"}",
"\n",
"d",
".",
"Head",
".",
"Write",
"(",
"hw",
")",
"\n",
"}",
"\n\n",
"w",
".",
"Write",
"(",
"v",
".",
"(",
"[",
"]",
"byte",
")",
")",
"\n",
"return",
"nil",
"\n",
"}"
]
| // Render a data response. | [
"Render",
"a",
"data",
"response",
"."
]
| 1ac792296fd4f4de9559a5f9a85c277c54ffb1cf | https://github.com/unrolled/render/blob/1ac792296fd4f4de9559a5f9a85c277c54ffb1cf/engine.go#L70-L81 |
13,167 | unrolled/render | engine.go | Render | 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 the buffer to the pool.
bufPool.Put(out)
return nil
} | go | 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 the buffer to the pool.
bufPool.Put(out)
return nil
} | [
"func",
"(",
"h",
"HTML",
")",
"Render",
"(",
"w",
"io",
".",
"Writer",
",",
"binding",
"interface",
"{",
"}",
")",
"error",
"{",
"// Retrieve a buffer from the pool to write to.",
"out",
":=",
"bufPool",
".",
"Get",
"(",
")",
"\n",
"err",
":=",
"h",
".",
"Templates",
".",
"ExecuteTemplate",
"(",
"out",
",",
"h",
".",
"Name",
",",
"binding",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"if",
"hw",
",",
"ok",
":=",
"w",
".",
"(",
"http",
".",
"ResponseWriter",
")",
";",
"ok",
"{",
"h",
".",
"Head",
".",
"Write",
"(",
"hw",
")",
"\n",
"}",
"\n",
"out",
".",
"WriteTo",
"(",
"w",
")",
"\n\n",
"// Return the buffer to the pool.",
"bufPool",
".",
"Put",
"(",
"out",
")",
"\n",
"return",
"nil",
"\n",
"}"
]
| // Render a HTML response. | [
"Render",
"a",
"HTML",
"response",
"."
]
| 1ac792296fd4f4de9559a5f9a85c277c54ffb1cf | https://github.com/unrolled/render/blob/1ac792296fd4f4de9559a5f9a85c277c54ffb1cf/engine.go#L84-L100 |
13,168 | unrolled/render | engine.go | Render | 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 err
}
// Unescape HTML if needed.
if j.UnEscapeHTML {
result = bytes.Replace(result, []byte("\\u003c"), []byte("<"), -1)
result = bytes.Replace(result, []byte("\\u003e"), []byte(">"), -1)
result = bytes.Replace(result, []byte("\\u0026"), []byte("&"), -1)
}
// JSON marshaled fine, write out the result.
if hw, ok := w.(http.ResponseWriter); ok {
j.Head.Write(hw)
}
if len(j.Prefix) > 0 {
w.Write(j.Prefix)
}
w.Write(result)
return nil
} | go | 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 err
}
// Unescape HTML if needed.
if j.UnEscapeHTML {
result = bytes.Replace(result, []byte("\\u003c"), []byte("<"), -1)
result = bytes.Replace(result, []byte("\\u003e"), []byte(">"), -1)
result = bytes.Replace(result, []byte("\\u0026"), []byte("&"), -1)
}
// JSON marshaled fine, write out the result.
if hw, ok := w.(http.ResponseWriter); ok {
j.Head.Write(hw)
}
if len(j.Prefix) > 0 {
w.Write(j.Prefix)
}
w.Write(result)
return nil
} | [
"func",
"(",
"j",
"JSON",
")",
"Render",
"(",
"w",
"io",
".",
"Writer",
",",
"v",
"interface",
"{",
"}",
")",
"error",
"{",
"if",
"j",
".",
"StreamingJSON",
"{",
"return",
"j",
".",
"renderStreamingJSON",
"(",
"w",
",",
"v",
")",
"\n",
"}",
"\n\n",
"var",
"result",
"[",
"]",
"byte",
"\n",
"var",
"err",
"error",
"\n\n",
"if",
"j",
".",
"Indent",
"{",
"result",
",",
"err",
"=",
"json",
".",
"MarshalIndent",
"(",
"v",
",",
"\"",
"\"",
",",
"\"",
"\"",
")",
"\n",
"result",
"=",
"append",
"(",
"result",
",",
"'\\n'",
")",
"\n",
"}",
"else",
"{",
"result",
",",
"err",
"=",
"json",
".",
"Marshal",
"(",
"v",
")",
"\n",
"}",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"// Unescape HTML if needed.",
"if",
"j",
".",
"UnEscapeHTML",
"{",
"result",
"=",
"bytes",
".",
"Replace",
"(",
"result",
",",
"[",
"]",
"byte",
"(",
"\"",
"\\\\",
"\"",
")",
",",
"[",
"]",
"byte",
"(",
"\"",
"\"",
")",
",",
"-",
"1",
")",
"\n",
"result",
"=",
"bytes",
".",
"Replace",
"(",
"result",
",",
"[",
"]",
"byte",
"(",
"\"",
"\\\\",
"\"",
")",
",",
"[",
"]",
"byte",
"(",
"\"",
"\"",
")",
",",
"-",
"1",
")",
"\n",
"result",
"=",
"bytes",
".",
"Replace",
"(",
"result",
",",
"[",
"]",
"byte",
"(",
"\"",
"\\\\",
"\"",
")",
",",
"[",
"]",
"byte",
"(",
"\"",
"\"",
")",
",",
"-",
"1",
")",
"\n",
"}",
"\n\n",
"// JSON marshaled fine, write out the result.",
"if",
"hw",
",",
"ok",
":=",
"w",
".",
"(",
"http",
".",
"ResponseWriter",
")",
";",
"ok",
"{",
"j",
".",
"Head",
".",
"Write",
"(",
"hw",
")",
"\n",
"}",
"\n",
"if",
"len",
"(",
"j",
".",
"Prefix",
")",
">",
"0",
"{",
"w",
".",
"Write",
"(",
"j",
".",
"Prefix",
")",
"\n",
"}",
"\n",
"w",
".",
"Write",
"(",
"result",
")",
"\n",
"return",
"nil",
"\n",
"}"
]
| // Render a JSON response. | [
"Render",
"a",
"JSON",
"response",
"."
]
| 1ac792296fd4f4de9559a5f9a85c277c54ffb1cf | https://github.com/unrolled/render/blob/1ac792296fd4f4de9559a5f9a85c277c54ffb1cf/engine.go#L103-L137 |
13,169 | unrolled/render | engine.go | Render | 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); ok {
j.Head.Write(hw)
}
w.Write([]byte(j.Callback + "("))
w.Write(result)
w.Write([]byte(");"))
// If indenting, append a new line.
if j.Indent {
w.Write([]byte("\n"))
}
return nil
} | go | 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); ok {
j.Head.Write(hw)
}
w.Write([]byte(j.Callback + "("))
w.Write(result)
w.Write([]byte(");"))
// If indenting, append a new line.
if j.Indent {
w.Write([]byte("\n"))
}
return nil
} | [
"func",
"(",
"j",
"JSONP",
")",
"Render",
"(",
"w",
"io",
".",
"Writer",
",",
"v",
"interface",
"{",
"}",
")",
"error",
"{",
"var",
"result",
"[",
"]",
"byte",
"\n",
"var",
"err",
"error",
"\n\n",
"if",
"j",
".",
"Indent",
"{",
"result",
",",
"err",
"=",
"json",
".",
"MarshalIndent",
"(",
"v",
",",
"\"",
"\"",
",",
"\"",
"\"",
")",
"\n",
"}",
"else",
"{",
"result",
",",
"err",
"=",
"json",
".",
"Marshal",
"(",
"v",
")",
"\n",
"}",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"// JSON marshaled fine, write out the result.",
"if",
"hw",
",",
"ok",
":=",
"w",
".",
"(",
"http",
".",
"ResponseWriter",
")",
";",
"ok",
"{",
"j",
".",
"Head",
".",
"Write",
"(",
"hw",
")",
"\n",
"}",
"\n",
"w",
".",
"Write",
"(",
"[",
"]",
"byte",
"(",
"j",
".",
"Callback",
"+",
"\"",
"\"",
")",
")",
"\n",
"w",
".",
"Write",
"(",
"result",
")",
"\n",
"w",
".",
"Write",
"(",
"[",
"]",
"byte",
"(",
"\"",
"\"",
")",
")",
"\n\n",
"// If indenting, append a new line.",
"if",
"j",
".",
"Indent",
"{",
"w",
".",
"Write",
"(",
"[",
"]",
"byte",
"(",
"\"",
"\\n",
"\"",
")",
")",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
]
| // Render a JSONP response. | [
"Render",
"a",
"JSONP",
"response",
"."
]
| 1ac792296fd4f4de9559a5f9a85c277c54ffb1cf | https://github.com/unrolled/render/blob/1ac792296fd4f4de9559a5f9a85c277c54ffb1cf/engine.go#L151-L177 |
13,170 | unrolled/render | engine.go | Render | 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 | 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
} | [
"func",
"(",
"t",
"Text",
")",
"Render",
"(",
"w",
"io",
".",
"Writer",
",",
"v",
"interface",
"{",
"}",
")",
"error",
"{",
"if",
"hw",
",",
"ok",
":=",
"w",
".",
"(",
"http",
".",
"ResponseWriter",
")",
";",
"ok",
"{",
"c",
":=",
"hw",
".",
"Header",
"(",
")",
".",
"Get",
"(",
"ContentType",
")",
"\n",
"if",
"c",
"!=",
"\"",
"\"",
"{",
"t",
".",
"Head",
".",
"ContentType",
"=",
"c",
"\n",
"}",
"\n",
"t",
".",
"Head",
".",
"Write",
"(",
"hw",
")",
"\n",
"}",
"\n\n",
"w",
".",
"Write",
"(",
"[",
"]",
"byte",
"(",
"v",
".",
"(",
"string",
")",
")",
")",
"\n",
"return",
"nil",
"\n",
"}"
]
| // Render a text response. | [
"Render",
"a",
"text",
"response",
"."
]
| 1ac792296fd4f4de9559a5f9a85c277c54ffb1cf | https://github.com/unrolled/render/blob/1ac792296fd4f4de9559a5f9a85c277c54ffb1cf/engine.go#L180-L191 |
13,171 | unrolled/render | engine.go | Render | 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 := w.(http.ResponseWriter); ok {
x.Head.Write(hw)
}
if len(x.Prefix) > 0 {
w.Write(x.Prefix)
}
w.Write(result)
return nil
} | go | 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 := w.(http.ResponseWriter); ok {
x.Head.Write(hw)
}
if len(x.Prefix) > 0 {
w.Write(x.Prefix)
}
w.Write(result)
return nil
} | [
"func",
"(",
"x",
"XML",
")",
"Render",
"(",
"w",
"io",
".",
"Writer",
",",
"v",
"interface",
"{",
"}",
")",
"error",
"{",
"var",
"result",
"[",
"]",
"byte",
"\n",
"var",
"err",
"error",
"\n\n",
"if",
"x",
".",
"Indent",
"{",
"result",
",",
"err",
"=",
"xml",
".",
"MarshalIndent",
"(",
"v",
",",
"\"",
"\"",
",",
"\"",
"\"",
")",
"\n",
"result",
"=",
"append",
"(",
"result",
",",
"'\\n'",
")",
"\n",
"}",
"else",
"{",
"result",
",",
"err",
"=",
"xml",
".",
"Marshal",
"(",
"v",
")",
"\n",
"}",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"// XML marshaled fine, write out the result.",
"if",
"hw",
",",
"ok",
":=",
"w",
".",
"(",
"http",
".",
"ResponseWriter",
")",
";",
"ok",
"{",
"x",
".",
"Head",
".",
"Write",
"(",
"hw",
")",
"\n",
"}",
"\n",
"if",
"len",
"(",
"x",
".",
"Prefix",
")",
">",
"0",
"{",
"w",
".",
"Write",
"(",
"x",
".",
"Prefix",
")",
"\n",
"}",
"\n",
"w",
".",
"Write",
"(",
"result",
")",
"\n",
"return",
"nil",
"\n",
"}"
]
| // Render an XML response. | [
"Render",
"an",
"XML",
"response",
"."
]
| 1ac792296fd4f4de9559a5f9a85c277c54ffb1cf | https://github.com/unrolled/render/blob/1ac792296fd4f4de9559a5f9a85c277c54ffb1cf/engine.go#L194-L217 |
13,172 | h2non/gentleman | dispatcher.go | Dispatch | 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.Context, bool) {
return d.doDial(ctx)
},
func(ctx *c.Context) (*c.Context, bool) {
return d.runAfter("after dial", ctx)
},
func(ctx *c.Context) (*c.Context, bool) {
return d.runAfter("response", ctx)
},
}
// Reference to initial context
ctx := d.req.Context
// Execute tasks in order, stopping in case of error or explicit stop.
for _, task := range pipeline {
var stop bool
if ctx, stop = task(ctx); stop {
break
}
}
return ctx
} | go | 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.Context, bool) {
return d.doDial(ctx)
},
func(ctx *c.Context) (*c.Context, bool) {
return d.runAfter("after dial", ctx)
},
func(ctx *c.Context) (*c.Context, bool) {
return d.runAfter("response", ctx)
},
}
// Reference to initial context
ctx := d.req.Context
// Execute tasks in order, stopping in case of error or explicit stop.
for _, task := range pipeline {
var stop bool
if ctx, stop = task(ctx); stop {
break
}
}
return ctx
} | [
"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",
"(",
"\"",
"\"",
",",
"ctx",
")",
"\n",
"}",
",",
"func",
"(",
"ctx",
"*",
"c",
".",
"Context",
")",
"(",
"*",
"c",
".",
"Context",
",",
"bool",
")",
"{",
"return",
"d",
".",
"runBefore",
"(",
"\"",
"\"",
",",
"ctx",
")",
"\n",
"}",
",",
"func",
"(",
"ctx",
"*",
"c",
".",
"Context",
")",
"(",
"*",
"c",
".",
"Context",
",",
"bool",
")",
"{",
"return",
"d",
".",
"doDial",
"(",
"ctx",
")",
"\n",
"}",
",",
"func",
"(",
"ctx",
"*",
"c",
".",
"Context",
")",
"(",
"*",
"c",
".",
"Context",
",",
"bool",
")",
"{",
"return",
"d",
".",
"runAfter",
"(",
"\"",
"\"",
",",
"ctx",
")",
"\n",
"}",
",",
"func",
"(",
"ctx",
"*",
"c",
".",
"Context",
")",
"(",
"*",
"c",
".",
"Context",
",",
"bool",
")",
"{",
"return",
"d",
".",
"runAfter",
"(",
"\"",
"\"",
",",
"ctx",
")",
"\n",
"}",
",",
"}",
"\n\n",
"// Reference to initial context",
"ctx",
":=",
"d",
".",
"req",
".",
"Context",
"\n\n",
"// Execute tasks in order, stopping in case of error or explicit stop.",
"for",
"_",
",",
"task",
":=",
"range",
"pipeline",
"{",
"var",
"stop",
"bool",
"\n",
"if",
"ctx",
",",
"stop",
"=",
"task",
"(",
"ctx",
")",
";",
"stop",
"{",
"break",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"ctx",
"\n",
"}"
]
| // Dispatch triggers the middleware chains and performs the HTTP request. | [
"Dispatch",
"triggers",
"the",
"middleware",
"chains",
"and",
"performs",
"the",
"HTTP",
"request",
"."
]
| c4d45b509aa1d29d813abbb19203887071efe506 | https://github.com/h2non/gentleman/blob/c4d45b509aa1d29d813abbb19203887071efe506/dispatcher.go#L23-L55 |
13,173 | h2non/gentleman | middleware/middleware.go | Use | func (s *Layer) Use(plugin plugin.Plugin) Middleware {
s.mtx.Lock()
s.stack = append(s.stack, plugin)
s.mtx.Unlock()
return s
} | go | func (s *Layer) Use(plugin plugin.Plugin) Middleware {
s.mtx.Lock()
s.stack = append(s.stack, plugin)
s.mtx.Unlock()
return s
} | [
"func",
"(",
"s",
"*",
"Layer",
")",
"Use",
"(",
"plugin",
"plugin",
".",
"Plugin",
")",
"Middleware",
"{",
"s",
".",
"mtx",
".",
"Lock",
"(",
")",
"\n",
"s",
".",
"stack",
"=",
"append",
"(",
"s",
".",
"stack",
",",
"plugin",
")",
"\n",
"s",
".",
"mtx",
".",
"Unlock",
"(",
")",
"\n",
"return",
"s",
"\n",
"}"
]
| // Use registers a new plugin to the middleware stack. | [
"Use",
"registers",
"a",
"new",
"plugin",
"to",
"the",
"middleware",
"stack",
"."
]
| c4d45b509aa1d29d813abbb19203887071efe506 | https://github.com/h2non/gentleman/blob/c4d45b509aa1d29d813abbb19203887071efe506/middleware/middleware.go#L68-L73 |
13,174 | h2non/gentleman | middleware/middleware.go | UseHandler | 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 | 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
} | [
"func",
"(",
"s",
"*",
"Layer",
")",
"UseHandler",
"(",
"phase",
"string",
",",
"fn",
"c",
".",
"HandlerFunc",
")",
"Middleware",
"{",
"s",
".",
"mtx",
".",
"Lock",
"(",
")",
"\n",
"s",
".",
"stack",
"=",
"append",
"(",
"s",
".",
"stack",
",",
"plugin",
".",
"NewPhasePlugin",
"(",
"phase",
",",
"fn",
")",
")",
"\n",
"s",
".",
"mtx",
".",
"Unlock",
"(",
")",
"\n",
"return",
"s",
"\n",
"}"
]
| // UseHandler registers a phase specific plugin handler in the middleware stack. | [
"UseHandler",
"registers",
"a",
"phase",
"specific",
"plugin",
"handler",
"in",
"the",
"middleware",
"stack",
"."
]
| c4d45b509aa1d29d813abbb19203887071efe506 | https://github.com/h2non/gentleman/blob/c4d45b509aa1d29d813abbb19203887071efe506/middleware/middleware.go#L76-L81 |
13,175 | h2non/gentleman | middleware/middleware.go | Flush | func (s *Layer) Flush() {
s.mtx.Lock()
s.stack = s.stack[:0]
s.mtx.Unlock()
} | go | func (s *Layer) Flush() {
s.mtx.Lock()
s.stack = s.stack[:0]
s.mtx.Unlock()
} | [
"func",
"(",
"s",
"*",
"Layer",
")",
"Flush",
"(",
")",
"{",
"s",
".",
"mtx",
".",
"Lock",
"(",
")",
"\n",
"s",
".",
"stack",
"=",
"s",
".",
"stack",
"[",
":",
"0",
"]",
"\n",
"s",
".",
"mtx",
".",
"Unlock",
"(",
")",
"\n",
"}"
]
| // Flush flushes the plugins stack. | [
"Flush",
"flushes",
"the",
"plugins",
"stack",
"."
]
| c4d45b509aa1d29d813abbb19203887071efe506 | https://github.com/h2non/gentleman/blob/c4d45b509aa1d29d813abbb19203887071efe506/middleware/middleware.go#L116-L120 |
13,176 | h2non/gentleman | middleware/middleware.go | GetStack | func (s *Layer) GetStack() []plugin.Plugin {
s.mtx.RLock()
defer s.mtx.RUnlock()
return s.stack
} | go | func (s *Layer) GetStack() []plugin.Plugin {
s.mtx.RLock()
defer s.mtx.RUnlock()
return s.stack
} | [
"func",
"(",
"s",
"*",
"Layer",
")",
"GetStack",
"(",
")",
"[",
"]",
"plugin",
".",
"Plugin",
"{",
"s",
".",
"mtx",
".",
"RLock",
"(",
")",
"\n",
"defer",
"s",
".",
"mtx",
".",
"RUnlock",
"(",
")",
"\n",
"return",
"s",
".",
"stack",
"\n",
"}"
]
| // GetStack gets the current middleware plugins stack. | [
"GetStack",
"gets",
"the",
"current",
"middleware",
"plugins",
"stack",
"."
]
| c4d45b509aa1d29d813abbb19203887071efe506 | https://github.com/h2non/gentleman/blob/c4d45b509aa1d29d813abbb19203887071efe506/middleware/middleware.go#L130-L134 |
13,177 | h2non/gentleman | middleware/middleware.go | Clone | 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 | 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
} | [
"func",
"(",
"s",
"*",
"Layer",
")",
"Clone",
"(",
")",
"Middleware",
"{",
"mw",
":=",
"New",
"(",
")",
"\n",
"mw",
".",
"parent",
"=",
"s",
".",
"parent",
"\n",
"s",
".",
"mtx",
".",
"Lock",
"(",
")",
"\n",
"mw",
".",
"stack",
"=",
"append",
"(",
"[",
"]",
"plugin",
".",
"Plugin",
"(",
"nil",
")",
",",
"s",
".",
"stack",
"...",
")",
"\n",
"s",
".",
"mtx",
".",
"Unlock",
"(",
")",
"\n",
"return",
"mw",
"\n",
"}"
]
| // Clone creates a new Middleware instance based on the current one. | [
"Clone",
"creates",
"a",
"new",
"Middleware",
"instance",
"based",
"on",
"the",
"current",
"one",
"."
]
| c4d45b509aa1d29d813abbb19203887071efe506 | https://github.com/h2non/gentleman/blob/c4d45b509aa1d29d813abbb19203887071efe506/middleware/middleware.go#L137-L144 |
13,178 | h2non/gentleman | plugins/url/url.go | URL | 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 | 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)
})
} | [
"func",
"URL",
"(",
"uri",
"string",
")",
"p",
".",
"Plugin",
"{",
"return",
"p",
".",
"NewRequestPlugin",
"(",
"func",
"(",
"ctx",
"*",
"c",
".",
"Context",
",",
"h",
"c",
".",
"Handler",
")",
"{",
"u",
",",
"err",
":=",
"url",
".",
"Parse",
"(",
"normalize",
"(",
"uri",
")",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"h",
".",
"Error",
"(",
"ctx",
",",
"err",
")",
"\n",
"return",
"\n",
"}",
"\n\n",
"ctx",
".",
"Request",
".",
"URL",
"=",
"u",
"\n",
"h",
".",
"Next",
"(",
"ctx",
")",
"\n",
"}",
")",
"\n",
"}"
]
| // URL parses and defines a new URL in the outgoing request | [
"URL",
"parses",
"and",
"defines",
"a",
"new",
"URL",
"in",
"the",
"outgoing",
"request"
]
| c4d45b509aa1d29d813abbb19203887071efe506 | https://github.com/h2non/gentleman/blob/c4d45b509aa1d29d813abbb19203887071efe506/plugins/url/url.go#L12-L23 |
13,179 | h2non/gentleman | plugins/url/url.go | Path | 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 | 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)
})
} | [
"func",
"Path",
"(",
"path",
"string",
")",
"p",
".",
"Plugin",
"{",
"return",
"p",
".",
"NewRequestPlugin",
"(",
"func",
"(",
"ctx",
"*",
"c",
".",
"Context",
",",
"h",
"c",
".",
"Handler",
")",
"{",
"ctx",
".",
"Request",
".",
"URL",
".",
"Path",
"=",
"normalizePath",
"(",
"path",
")",
"\n",
"h",
".",
"Next",
"(",
"ctx",
")",
"\n",
"}",
")",
"\n",
"}"
]
| // Path defines a new URL path in the outgoing request | [
"Path",
"defines",
"a",
"new",
"URL",
"path",
"in",
"the",
"outgoing",
"request"
]
| c4d45b509aa1d29d813abbb19203887071efe506 | https://github.com/h2non/gentleman/blob/c4d45b509aa1d29d813abbb19203887071efe506/plugins/url/url.go#L41-L46 |
13,180 | h2non/gentleman | plugins/url/url.go | Param | 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 | 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)
})
} | [
"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",
")",
"\n",
"h",
".",
"Next",
"(",
"ctx",
")",
"\n",
"}",
")",
"\n",
"}"
]
| // Param replaces one or multiple path param expressions by the given value | [
"Param",
"replaces",
"one",
"or",
"multiple",
"path",
"param",
"expressions",
"by",
"the",
"given",
"value"
]
| c4d45b509aa1d29d813abbb19203887071efe506 | https://github.com/h2non/gentleman/blob/c4d45b509aa1d29d813abbb19203887071efe506/plugins/url/url.go#L65-L70 |
13,181 | h2non/gentleman | plugins/body/body.go | String | 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 | 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)
})
} | [
"func",
"String",
"(",
"data",
"string",
")",
"p",
".",
"Plugin",
"{",
"return",
"p",
".",
"NewRequestPlugin",
"(",
"func",
"(",
"ctx",
"*",
"c",
".",
"Context",
",",
"h",
"c",
".",
"Handler",
")",
"{",
"ctx",
".",
"Request",
".",
"Method",
"=",
"getMethod",
"(",
"ctx",
")",
"\n",
"ctx",
".",
"Request",
".",
"Body",
"=",
"utils",
".",
"StringReader",
"(",
"data",
")",
"\n",
"ctx",
".",
"Request",
".",
"ContentLength",
"=",
"int64",
"(",
"bytes",
".",
"NewBufferString",
"(",
"data",
")",
".",
"Len",
"(",
")",
")",
"\n",
"h",
".",
"Next",
"(",
"ctx",
")",
"\n",
"}",
")",
"\n",
"}"
]
| // String defines the HTTP request body based on the given string. | [
"String",
"defines",
"the",
"HTTP",
"request",
"body",
"based",
"on",
"the",
"given",
"string",
"."
]
| c4d45b509aa1d29d813abbb19203887071efe506 | https://github.com/h2non/gentleman/blob/c4d45b509aa1d29d813abbb19203887071efe506/plugins/body/body.go#L17-L24 |
13,182 | h2non/gentleman | plugins/body/body.go | JSON | 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.Error(ctx, err)
return
}
}
ctx.Request.Method = getMethod(ctx)
ctx.Request.Body = ioutil.NopCloser(buf)
ctx.Request.ContentLength = int64(buf.Len())
ctx.Request.Header.Set("Content-Type", "application/json")
h.Next(ctx)
})
} | go | 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.Error(ctx, err)
return
}
}
ctx.Request.Method = getMethod(ctx)
ctx.Request.Body = ioutil.NopCloser(buf)
ctx.Request.ContentLength = int64(buf.Len())
ctx.Request.Header.Set("Content-Type", "application/json")
h.Next(ctx)
})
} | [
"func",
"JSON",
"(",
"data",
"interface",
"{",
"}",
")",
"p",
".",
"Plugin",
"{",
"return",
"p",
".",
"NewRequestPlugin",
"(",
"func",
"(",
"ctx",
"*",
"c",
".",
"Context",
",",
"h",
"c",
".",
"Handler",
")",
"{",
"buf",
":=",
"&",
"bytes",
".",
"Buffer",
"{",
"}",
"\n\n",
"switch",
"data",
".",
"(",
"type",
")",
"{",
"case",
"string",
":",
"buf",
".",
"WriteString",
"(",
"data",
".",
"(",
"string",
")",
")",
"\n",
"case",
"[",
"]",
"byte",
":",
"buf",
".",
"Write",
"(",
"data",
".",
"(",
"[",
"]",
"byte",
")",
")",
"\n",
"default",
":",
"if",
"err",
":=",
"json",
".",
"NewEncoder",
"(",
"buf",
")",
".",
"Encode",
"(",
"data",
")",
";",
"err",
"!=",
"nil",
"{",
"h",
".",
"Error",
"(",
"ctx",
",",
"err",
")",
"\n",
"return",
"\n",
"}",
"\n",
"}",
"\n\n",
"ctx",
".",
"Request",
".",
"Method",
"=",
"getMethod",
"(",
"ctx",
")",
"\n",
"ctx",
".",
"Request",
".",
"Body",
"=",
"ioutil",
".",
"NopCloser",
"(",
"buf",
")",
"\n",
"ctx",
".",
"Request",
".",
"ContentLength",
"=",
"int64",
"(",
"buf",
".",
"Len",
"(",
")",
")",
"\n",
"ctx",
".",
"Request",
".",
"Header",
".",
"Set",
"(",
"\"",
"\"",
",",
"\"",
"\"",
")",
"\n\n",
"h",
".",
"Next",
"(",
"ctx",
")",
"\n",
"}",
")",
"\n",
"}"
]
| // JSON defines a JSON body in the outgoing request.
// Supports strings, array of bytes or buffer. | [
"JSON",
"defines",
"a",
"JSON",
"body",
"in",
"the",
"outgoing",
"request",
".",
"Supports",
"strings",
"array",
"of",
"bytes",
"or",
"buffer",
"."
]
| c4d45b509aa1d29d813abbb19203887071efe506 | https://github.com/h2non/gentleman/blob/c4d45b509aa1d29d813abbb19203887071efe506/plugins/body/body.go#L28-L51 |
13,183 | h2non/gentleman | plugins/body/body.go | Reader | 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.Len())
case *bytes.Reader:
req.ContentLength = int64(v.Len())
case *strings.Reader:
req.ContentLength = int64(v.Len())
}
}
req.Body = rc
ctx.Request.Method = getMethod(ctx)
h.Next(ctx)
})
} | go | 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.Len())
case *bytes.Reader:
req.ContentLength = int64(v.Len())
case *strings.Reader:
req.ContentLength = int64(v.Len())
}
}
req.Body = rc
ctx.Request.Method = getMethod(ctx)
h.Next(ctx)
})
} | [
"func",
"Reader",
"(",
"body",
"io",
".",
"Reader",
")",
"p",
".",
"Plugin",
"{",
"return",
"p",
".",
"NewRequestPlugin",
"(",
"func",
"(",
"ctx",
"*",
"c",
".",
"Context",
",",
"h",
"c",
".",
"Handler",
")",
"{",
"rc",
",",
"ok",
":=",
"body",
".",
"(",
"io",
".",
"ReadCloser",
")",
"\n",
"if",
"!",
"ok",
"&&",
"body",
"!=",
"nil",
"{",
"rc",
"=",
"ioutil",
".",
"NopCloser",
"(",
"body",
")",
"\n",
"}",
"\n\n",
"req",
":=",
"ctx",
".",
"Request",
"\n",
"if",
"body",
"!=",
"nil",
"{",
"switch",
"v",
":=",
"body",
".",
"(",
"type",
")",
"{",
"case",
"*",
"bytes",
".",
"Buffer",
":",
"req",
".",
"ContentLength",
"=",
"int64",
"(",
"v",
".",
"Len",
"(",
")",
")",
"\n",
"case",
"*",
"bytes",
".",
"Reader",
":",
"req",
".",
"ContentLength",
"=",
"int64",
"(",
"v",
".",
"Len",
"(",
")",
")",
"\n",
"case",
"*",
"strings",
".",
"Reader",
":",
"req",
".",
"ContentLength",
"=",
"int64",
"(",
"v",
".",
"Len",
"(",
")",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"req",
".",
"Body",
"=",
"rc",
"\n",
"ctx",
".",
"Request",
".",
"Method",
"=",
"getMethod",
"(",
"ctx",
")",
"\n\n",
"h",
".",
"Next",
"(",
"ctx",
")",
"\n",
"}",
")",
"\n",
"}"
]
| // Reader defines a io.Reader stream as request body.
// Content-Type header won't be defined automatically, you have to declare it manually. | [
"Reader",
"defines",
"a",
"io",
".",
"Reader",
"stream",
"as",
"request",
"body",
".",
"Content",
"-",
"Type",
"header",
"won",
"t",
"be",
"defined",
"automatically",
"you",
"have",
"to",
"declare",
"it",
"manually",
"."
]
| c4d45b509aa1d29d813abbb19203887071efe506 | https://github.com/h2non/gentleman/blob/c4d45b509aa1d29d813abbb19203887071efe506/plugins/body/body.go#L82-L106 |
13,184 | h2non/gentleman | mux/compose.go | If | func If(muxes ...*Mux) *Mux {
mx := New()
for _, mm := range muxes {
mx.AddMatcher(mm.Matchers...)
}
return mx
} | go | func If(muxes ...*Mux) *Mux {
mx := New()
for _, mm := range muxes {
mx.AddMatcher(mm.Matchers...)
}
return mx
} | [
"func",
"If",
"(",
"muxes",
"...",
"*",
"Mux",
")",
"*",
"Mux",
"{",
"mx",
":=",
"New",
"(",
")",
"\n",
"for",
"_",
",",
"mm",
":=",
"range",
"muxes",
"{",
"mx",
".",
"AddMatcher",
"(",
"mm",
".",
"Matchers",
"...",
")",
"\n",
"}",
"\n",
"return",
"mx",
"\n",
"}"
]
| // If creates a new multiplexer that will be executed if all the mux matchers passes. | [
"If",
"creates",
"a",
"new",
"multiplexer",
"that",
"will",
"be",
"executed",
"if",
"all",
"the",
"mux",
"matchers",
"passes",
"."
]
| c4d45b509aa1d29d813abbb19203887071efe506 | https://github.com/h2non/gentleman/blob/c4d45b509aa1d29d813abbb19203887071efe506/mux/compose.go#L8-L14 |
13,185 | h2non/gentleman | mux/compose.go | Or | 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 | func Or(muxes ...*Mux) *Mux {
return Match(func(ctx *c.Context) bool {
for _, mm := range muxes {
if mm.Match(ctx) {
return true
}
}
return false
})
} | [
"func",
"Or",
"(",
"muxes",
"...",
"*",
"Mux",
")",
"*",
"Mux",
"{",
"return",
"Match",
"(",
"func",
"(",
"ctx",
"*",
"c",
".",
"Context",
")",
"bool",
"{",
"for",
"_",
",",
"mm",
":=",
"range",
"muxes",
"{",
"if",
"mm",
".",
"Match",
"(",
"ctx",
")",
"{",
"return",
"true",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"false",
"\n",
"}",
")",
"\n",
"}"
]
| // Or creates a new multiplexer that will be executed if at least one mux matcher passes. | [
"Or",
"creates",
"a",
"new",
"multiplexer",
"that",
"will",
"be",
"executed",
"if",
"at",
"least",
"one",
"mux",
"matcher",
"passes",
"."
]
| c4d45b509aa1d29d813abbb19203887071efe506 | https://github.com/h2non/gentleman/blob/c4d45b509aa1d29d813abbb19203887071efe506/mux/compose.go#L17-L26 |
13,186 | h2non/gentleman | mux/matchers.go | Match | func Match(matchers ...Matcher) *Mux {
mx := New()
mx.AddMatcher(matchers...)
return mx
} | go | func Match(matchers ...Matcher) *Mux {
mx := New()
mx.AddMatcher(matchers...)
return mx
} | [
"func",
"Match",
"(",
"matchers",
"...",
"Matcher",
")",
"*",
"Mux",
"{",
"mx",
":=",
"New",
"(",
")",
"\n",
"mx",
".",
"AddMatcher",
"(",
"matchers",
"...",
")",
"\n",
"return",
"mx",
"\n",
"}"
]
| // Match creates a new multiplexer based on a given matcher function. | [
"Match",
"creates",
"a",
"new",
"multiplexer",
"based",
"on",
"a",
"given",
"matcher",
"function",
"."
]
| c4d45b509aa1d29d813abbb19203887071efe506 | https://github.com/h2non/gentleman/blob/c4d45b509aa1d29d813abbb19203887071efe506/mux/matchers.go#L14-L18 |
13,187 | h2non/gentleman | mux/matchers.go | Path | 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 | 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
})
} | [
"func",
"Path",
"(",
"pattern",
"string",
")",
"*",
"Mux",
"{",
"return",
"Match",
"(",
"func",
"(",
"ctx",
"*",
"c",
".",
"Context",
")",
"bool",
"{",
"if",
"ctx",
".",
"GetString",
"(",
"\"",
"\"",
")",
"!=",
"\"",
"\"",
"{",
"return",
"false",
"\n",
"}",
"\n",
"matched",
",",
"_",
":=",
"regexp",
".",
"MatchString",
"(",
"pattern",
",",
"ctx",
".",
"Request",
".",
"URL",
".",
"Path",
")",
"\n",
"return",
"matched",
"\n",
"}",
")",
"\n",
"}"
]
| // Path returns a new multiplexer who matches an HTTP request
// path based on the given regexp pattern. | [
"Path",
"returns",
"a",
"new",
"multiplexer",
"who",
"matches",
"an",
"HTTP",
"request",
"path",
"based",
"on",
"the",
"given",
"regexp",
"pattern",
"."
]
| c4d45b509aa1d29d813abbb19203887071efe506 | https://github.com/h2non/gentleman/blob/c4d45b509aa1d29d813abbb19203887071efe506/mux/matchers.go#L37-L45 |
13,188 | h2non/gentleman | mux/matchers.go | Type | 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 | 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)
})
} | [
"func",
"Type",
"(",
"kind",
"string",
")",
"*",
"Mux",
"{",
"return",
"Match",
"(",
"func",
"(",
"ctx",
"*",
"c",
".",
"Context",
")",
"bool",
"{",
"if",
"ctx",
".",
"GetString",
"(",
"\"",
"\"",
")",
"!=",
"\"",
"\"",
"{",
"return",
"false",
"\n",
"}",
"\n",
"if",
"value",
",",
"ok",
":=",
"types",
".",
"Types",
"[",
"kind",
"]",
";",
"ok",
"{",
"kind",
"=",
"value",
"\n",
"}",
"\n",
"return",
"strings",
".",
"Contains",
"(",
"ctx",
".",
"Response",
".",
"Header",
".",
"Get",
"(",
"\"",
"\"",
")",
",",
"kind",
")",
"\n",
"}",
")",
"\n",
"}"
]
| // Type returns a new multiplexer who matches an HTTP response
// Content-Type header field based on the given type string. | [
"Type",
"returns",
"a",
"new",
"multiplexer",
"who",
"matches",
"an",
"HTTP",
"response",
"Content",
"-",
"Type",
"header",
"field",
"based",
"on",
"the",
"given",
"type",
"string",
"."
]
| c4d45b509aa1d29d813abbb19203887071efe506 | https://github.com/h2non/gentleman/blob/c4d45b509aa1d29d813abbb19203887071efe506/mux/matchers.go#L109-L119 |
13,189 | h2non/gentleman | mux/matchers.go | Status | 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 | 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
})
} | [
"func",
"Status",
"(",
"codes",
"...",
"int",
")",
"*",
"Mux",
"{",
"return",
"Match",
"(",
"func",
"(",
"ctx",
"*",
"c",
".",
"Context",
")",
"bool",
"{",
"if",
"ctx",
".",
"GetString",
"(",
"\"",
"\"",
")",
"!=",
"\"",
"\"",
"{",
"return",
"false",
"\n",
"}",
"\n",
"for",
"_",
",",
"code",
":=",
"range",
"codes",
"{",
"if",
"ctx",
".",
"Response",
".",
"StatusCode",
"==",
"code",
"{",
"return",
"true",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"false",
"\n",
"}",
")",
"\n",
"}"
]
| // Status returns a new multiplexer who matches an HTTP response
// status code based on the given status codes. | [
"Status",
"returns",
"a",
"new",
"multiplexer",
"who",
"matches",
"an",
"HTTP",
"response",
"status",
"code",
"based",
"on",
"the",
"given",
"status",
"codes",
"."
]
| c4d45b509aa1d29d813abbb19203887071efe506 | https://github.com/h2non/gentleman/blob/c4d45b509aa1d29d813abbb19203887071efe506/mux/matchers.go#L123-L135 |
13,190 | h2non/gentleman | mux/matchers.go | StatusRange | 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 | 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
})
} | [
"func",
"StatusRange",
"(",
"start",
",",
"end",
"int",
")",
"*",
"Mux",
"{",
"return",
"Match",
"(",
"func",
"(",
"ctx",
"*",
"c",
".",
"Context",
")",
"bool",
"{",
"return",
"ctx",
".",
"GetString",
"(",
"\"",
"\"",
")",
"==",
"\"",
"\"",
"&&",
"ctx",
".",
"Response",
".",
"StatusCode",
">=",
"start",
"&&",
"ctx",
".",
"Response",
".",
"StatusCode",
"<=",
"end",
"\n",
"}",
")",
"\n",
"}"
]
| // StatusRange returns a new multiplexer who matches an HTTP response
// status code based on the given status range, including both numbers. | [
"StatusRange",
"returns",
"a",
"new",
"multiplexer",
"who",
"matches",
"an",
"HTTP",
"response",
"status",
"code",
"based",
"on",
"the",
"given",
"status",
"range",
"including",
"both",
"numbers",
"."
]
| c4d45b509aa1d29d813abbb19203887071efe506 | https://github.com/h2non/gentleman/blob/c4d45b509aa1d29d813abbb19203887071efe506/mux/matchers.go#L139-L143 |
13,191 | h2non/gentleman | mux/matchers.go | Error | 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 | 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)
})
} | [
"func",
"Error",
"(",
")",
"*",
"Mux",
"{",
"return",
"Match",
"(",
"func",
"(",
"ctx",
"*",
"c",
".",
"Context",
")",
"bool",
"{",
"return",
"(",
"ctx",
".",
"GetString",
"(",
"\"",
"\"",
")",
"==",
"\"",
"\"",
"&&",
"ctx",
".",
"Error",
"!=",
"nil",
")",
"||",
"(",
"ctx",
".",
"GetString",
"(",
"\"",
"\"",
")",
"==",
"\"",
"\"",
"&&",
"ctx",
".",
"Response",
".",
"StatusCode",
">=",
"500",
")",
"\n",
"}",
")",
"\n",
"}"
]
| // Error returns a new multiplexer who matches errors originated
// in the client or in the server. | [
"Error",
"returns",
"a",
"new",
"multiplexer",
"who",
"matches",
"errors",
"originated",
"in",
"the",
"client",
"or",
"in",
"the",
"server",
"."
]
| c4d45b509aa1d29d813abbb19203887071efe506 | https://github.com/h2non/gentleman/blob/c4d45b509aa1d29d813abbb19203887071efe506/mux/matchers.go#L147-L152 |
13,192 | h2non/gentleman | mux/matchers.go | ServerError | func ServerError() *Mux {
return Match(func(ctx *c.Context) bool {
return ctx.GetString("$phase") == "response" && ctx.Response.StatusCode >= 500
})
} | go | func ServerError() *Mux {
return Match(func(ctx *c.Context) bool {
return ctx.GetString("$phase") == "response" && ctx.Response.StatusCode >= 500
})
} | [
"func",
"ServerError",
"(",
")",
"*",
"Mux",
"{",
"return",
"Match",
"(",
"func",
"(",
"ctx",
"*",
"c",
".",
"Context",
")",
"bool",
"{",
"return",
"ctx",
".",
"GetString",
"(",
"\"",
"\"",
")",
"==",
"\"",
"\"",
"&&",
"ctx",
".",
"Response",
".",
"StatusCode",
">=",
"500",
"\n",
"}",
")",
"\n",
"}"
]
| // ServerError returns a new multiplexer who matches response errors by the server. | [
"ServerError",
"returns",
"a",
"new",
"multiplexer",
"who",
"matches",
"response",
"errors",
"by",
"the",
"server",
"."
]
| c4d45b509aa1d29d813abbb19203887071efe506 | https://github.com/h2non/gentleman/blob/c4d45b509aa1d29d813abbb19203887071efe506/mux/matchers.go#L155-L159 |
13,193 | h2non/gentleman | mux/mux.go | New | func New() *Mux {
m := &Mux{Layer: plugin.New()}
m.Middleware = middleware.New()
handler := m.Handler()
m.DefaultHandler = handler
return m
} | go | func New() *Mux {
m := &Mux{Layer: plugin.New()}
m.Middleware = middleware.New()
handler := m.Handler()
m.DefaultHandler = handler
return m
} | [
"func",
"New",
"(",
")",
"*",
"Mux",
"{",
"m",
":=",
"&",
"Mux",
"{",
"Layer",
":",
"plugin",
".",
"New",
"(",
")",
"}",
"\n",
"m",
".",
"Middleware",
"=",
"middleware",
".",
"New",
"(",
")",
"\n",
"handler",
":=",
"m",
".",
"Handler",
"(",
")",
"\n",
"m",
".",
"DefaultHandler",
"=",
"handler",
"\n",
"return",
"m",
"\n",
"}"
]
| // New creates a new multiplexer with default settings. | [
"New",
"creates",
"a",
"new",
"multiplexer",
"with",
"default",
"settings",
"."
]
| c4d45b509aa1d29d813abbb19203887071efe506 | https://github.com/h2non/gentleman/blob/c4d45b509aa1d29d813abbb19203887071efe506/mux/mux.go#L26-L32 |
13,194 | h2non/gentleman | mux/mux.go | Match | func (m *Mux) Match(ctx *c.Context) bool {
for _, matcher := range m.Matchers {
if !matcher(ctx) {
return false
}
}
return true
} | go | func (m *Mux) Match(ctx *c.Context) bool {
for _, matcher := range m.Matchers {
if !matcher(ctx) {
return false
}
}
return true
} | [
"func",
"(",
"m",
"*",
"Mux",
")",
"Match",
"(",
"ctx",
"*",
"c",
".",
"Context",
")",
"bool",
"{",
"for",
"_",
",",
"matcher",
":=",
"range",
"m",
".",
"Matchers",
"{",
"if",
"!",
"matcher",
"(",
"ctx",
")",
"{",
"return",
"false",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"true",
"\n",
"}"
]
| // Match matches the give Context againts a list of matchers and
// returns `true` if all the matchers passed. | [
"Match",
"matches",
"the",
"give",
"Context",
"againts",
"a",
"list",
"of",
"matchers",
"and",
"returns",
"true",
"if",
"all",
"the",
"matchers",
"passed",
"."
]
| c4d45b509aa1d29d813abbb19203887071efe506 | https://github.com/h2non/gentleman/blob/c4d45b509aa1d29d813abbb19203887071efe506/mux/mux.go#L36-L43 |
13,195 | h2non/gentleman | mux/mux.go | AddMatcher | func (m *Mux) AddMatcher(matchers ...Matcher) *Mux {
m.Matchers = append(m.Matchers, matchers...)
return m
} | go | func (m *Mux) AddMatcher(matchers ...Matcher) *Mux {
m.Matchers = append(m.Matchers, matchers...)
return m
} | [
"func",
"(",
"m",
"*",
"Mux",
")",
"AddMatcher",
"(",
"matchers",
"...",
"Matcher",
")",
"*",
"Mux",
"{",
"m",
".",
"Matchers",
"=",
"append",
"(",
"m",
".",
"Matchers",
",",
"matchers",
"...",
")",
"\n",
"return",
"m",
"\n",
"}"
]
| // AddMatcher adds a new matcher function in the current mumultiplexer matchers stack. | [
"AddMatcher",
"adds",
"a",
"new",
"matcher",
"function",
"in",
"the",
"current",
"mumultiplexer",
"matchers",
"stack",
"."
]
| c4d45b509aa1d29d813abbb19203887071efe506 | https://github.com/h2non/gentleman/blob/c4d45b509aa1d29d813abbb19203887071efe506/mux/mux.go#L46-L49 |
13,196 | h2non/gentleman | mux/mux.go | Handler | 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 | 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)
}
} | [
"func",
"(",
"m",
"*",
"Mux",
")",
"Handler",
"(",
")",
"c",
".",
"HandlerFunc",
"{",
"return",
"func",
"(",
"ctx",
"*",
"c",
".",
"Context",
",",
"h",
"c",
".",
"Handler",
")",
"{",
"if",
"!",
"m",
".",
"Match",
"(",
"ctx",
")",
"{",
"h",
".",
"Next",
"(",
"ctx",
")",
"\n",
"return",
"\n",
"}",
"\n\n",
"ctx",
"=",
"m",
".",
"Middleware",
".",
"Run",
"(",
"ctx",
".",
"GetString",
"(",
"\"",
"\"",
")",
",",
"ctx",
")",
"\n",
"if",
"ctx",
".",
"Error",
"!=",
"nil",
"{",
"h",
".",
"Error",
"(",
"ctx",
",",
"ctx",
".",
"Error",
")",
"\n",
"return",
"\n",
"}",
"\n",
"if",
"ctx",
".",
"Stopped",
"{",
"h",
".",
"Stop",
"(",
"ctx",
")",
"\n",
"return",
"\n",
"}",
"\n\n",
"h",
".",
"Next",
"(",
"ctx",
")",
"\n",
"}",
"\n",
"}"
]
| // Handler returns the function handler to match an incoming HTTP transacion
// and trigger the equivalent middleware phase. | [
"Handler",
"returns",
"the",
"function",
"handler",
"to",
"match",
"an",
"incoming",
"HTTP",
"transacion",
"and",
"trigger",
"the",
"equivalent",
"middleware",
"phase",
"."
]
| c4d45b509aa1d29d813abbb19203887071efe506 | https://github.com/h2non/gentleman/blob/c4d45b509aa1d29d813abbb19203887071efe506/mux/mux.go#L53-L72 |
13,197 | h2non/gentleman | mux/mux.go | Use | func (m *Mux) Use(p plugin.Plugin) *Mux {
m.Middleware.Use(p)
return m
} | go | func (m *Mux) Use(p plugin.Plugin) *Mux {
m.Middleware.Use(p)
return m
} | [
"func",
"(",
"m",
"*",
"Mux",
")",
"Use",
"(",
"p",
"plugin",
".",
"Plugin",
")",
"*",
"Mux",
"{",
"m",
".",
"Middleware",
".",
"Use",
"(",
"p",
")",
"\n",
"return",
"m",
"\n",
"}"
]
| // Use registers a new plugin in the middleware stack. | [
"Use",
"registers",
"a",
"new",
"plugin",
"in",
"the",
"middleware",
"stack",
"."
]
| c4d45b509aa1d29d813abbb19203887071efe506 | https://github.com/h2non/gentleman/blob/c4d45b509aa1d29d813abbb19203887071efe506/mux/mux.go#L75-L78 |
13,198 | h2non/gentleman | mux/mux.go | UseHandler | func (m *Mux) UseHandler(phase string, fn c.HandlerFunc) *Mux {
m.Middleware.UseHandler(phase, fn)
return m
} | go | func (m *Mux) UseHandler(phase string, fn c.HandlerFunc) *Mux {
m.Middleware.UseHandler(phase, fn)
return m
} | [
"func",
"(",
"m",
"*",
"Mux",
")",
"UseHandler",
"(",
"phase",
"string",
",",
"fn",
"c",
".",
"HandlerFunc",
")",
"*",
"Mux",
"{",
"m",
".",
"Middleware",
".",
"UseHandler",
"(",
"phase",
",",
"fn",
")",
"\n",
"return",
"m",
"\n",
"}"
]
| // UseHandler registers a new error phase middleware handler. | [
"UseHandler",
"registers",
"a",
"new",
"error",
"phase",
"middleware",
"handler",
"."
]
| c4d45b509aa1d29d813abbb19203887071efe506 | https://github.com/h2non/gentleman/blob/c4d45b509aa1d29d813abbb19203887071efe506/mux/mux.go#L99-L102 |
13,199 | h2non/gentleman | plugins/timeout/timeout.go | TLS | func TLS(timeout time.Duration) p.Plugin {
return All(Timeouts{TLS: timeout})
} | go | func TLS(timeout time.Duration) p.Plugin {
return All(Timeouts{TLS: timeout})
} | [
"func",
"TLS",
"(",
"timeout",
"time",
".",
"Duration",
")",
"p",
".",
"Plugin",
"{",
"return",
"All",
"(",
"Timeouts",
"{",
"TLS",
":",
"timeout",
"}",
")",
"\n",
"}"
]
| // TLS defines the maximum amount of time waiting for a TLS handshake | [
"TLS",
"defines",
"the",
"maximum",
"amount",
"of",
"time",
"waiting",
"for",
"a",
"TLS",
"handshake"
]
| c4d45b509aa1d29d813abbb19203887071efe506 | https://github.com/h2non/gentleman/blob/c4d45b509aa1d29d813abbb19203887071efe506/plugins/timeout/timeout.go#L37-L39 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.