Documentation
¶
Index ¶
Constants ¶
const ( FieldNameData = FieldName("data") FieldNameEvent = FieldName("event") FieldNameRetry = FieldName("retry") FieldNameID = FieldName("id") // FieldNameComment is a sentinel value that indicates // comment fields. It is not a valid field name that should // be written to an SSE stream. FieldNameComment = FieldName(":") )
Valid field names.
Variables ¶
var ErrUnexpectedEOF = errors.New("go-sse: unexpected end of input")
ErrUnexpectedEOF is returned when the input is completely parsed but no complete field was found at the end.
Functions ¶
func NewlineIndex ¶
NewlineIndex returns the index of the first occurrence of a newline sequence (\n, \r, or \r\n). It also returns the sequence's length. If no sequence is found, index is equal to len(s) and length is 0.
The newline is defined in the Event Stream standard's documentation: https://html.spec.whatwg.org/multipage/server-sent-events.html#server-sent-events
func NextChunk ¶ added in v0.5.0
NextChunk retrieves the next chunk of data from the given string along with the data remaining after the returned chunk. A chunk is a string of data delimited by a newline. If the returned chunk is the last one, len(remaining) will be 0.
The newline is defined in the Event Stream standard's documentation: https://html.spec.whatwg.org/multipage/server-sent-events.html#server-sent-events
Types ¶
type Field ¶
A Field represents an unprocessed field of a single event. The Name is the field's identifier, which is used to process the fields afterwards.
As a special case, if a parser (FieldParser or Parser) returns a field without a name, it means that a whole event was parsed. In other words, all the fields before the one without a name and after another such field are part of the same event.
type FieldParser ¶ added in v0.4.3
type FieldParser struct {
// contains filtered or unexported fields
}
FieldParser extracts fields from a byte slice.
func NewFieldParser ¶ added in v0.4.3
func NewFieldParser(data string) *FieldParser
NewFieldParser creates a parser that extracts fields from the given string.
func (*FieldParser) Err ¶ added in v0.4.3
func (f *FieldParser) Err() error
Err returns the last error encountered by the parser. It is either nil or ErrUnexpectedEOF.
func (*FieldParser) KeepComments ¶ added in v0.5.0
func (f *FieldParser) KeepComments(shouldKeep bool)
KeepComments configures the FieldParser to parse/ignore comment fields. By default comment fields are ignored.
func (*FieldParser) Next ¶ added in v0.5.0
func (f *FieldParser) Next(r *Field) bool
Next parses the next available field in the remaining buffer. It returns false if there are no more fields to parse.
func (*FieldParser) RemoveBOM ¶ added in v0.5.2
func (f *FieldParser) RemoveBOM(shouldRemove bool)
RemoveBOM configures the FieldParser to try and remove the Unicode BOM when parsing the first field, if it exists. If, at the time this option is set, the input is untouched (no fields were parsed), it will also be attempted to remove the BOM.
func (*FieldParser) Reset ¶ added in v0.4.3
func (f *FieldParser) Reset(data string)
Reset changes the buffer from which fields are parsed.
func (*FieldParser) Started ¶ added in v0.5.2
func (f *FieldParser) Started() bool
Started tells whether parsing has started (a call to Next which consumed input was made or the BOM was removed, if it existed). Started will be true if the FieldParser has advanced through the data.
type Parser ¶ added in v0.4.3
type Parser struct {
// contains filtered or unexported fields
}
Parser extracts fields from a reader. Reading is buffered using a bufio.Scanner. The Parser also removes the UTF-8 BOM if it exists.
func (*Parser) Buffer ¶ added in v0.5.2
Buffer sets the buffer used to scan the input. For more information, see the documentation on bufio.Scanner.Buffer. Do not call this after parsing has started – the method will panic!