parser

package
v0.11.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 13, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
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

View Source
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

func NewlineIndex(s string) (index, length int)

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

func NextChunk(s string) (chunk, remaining string, hasNewline bool)

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

type Field struct {
	Name  FieldName
	Value string
}

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 FieldName

type FieldName string

FieldName is the name of the field.

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 New added in v0.4.3

func New(r io.Reader) *Parser

New returns a Parser that extracts fields from a reader.

func (*Parser) Buffer added in v0.5.2

func (r *Parser) Buffer(buf []byte, maxSize int)

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!

func (*Parser) Err added in v0.4.3

func (r *Parser) Err() error

Err returns the last read error. At the end of input it will always be equal to io.EOF.

func (*Parser) Next added in v0.5.0

func (r *Parser) Next(f *Field) bool

Next parses a single field from the reader. It returns false when there are no more fields to parse.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL