parser

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2021 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FieldNameData  = FieldName("data")
	FieldNameEvent = FieldName("event")
	FieldNameRetry = FieldName("retry")
	FieldNameID    = FieldName("id")
)

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 []byte) (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.

func RemovePrefix added in v0.2.0

func RemovePrefix(r io.Reader, prefix string) io.Reader

RemovePrefix returns a reader that removes the given prefix from the input. If you want to remove a long prefix make sure you read using a buffer sized equally to or greater than the prefix, otherwise an allocation will be made. The function returns the given reader if the prefix is an empty string.

Types

type ByteParser

type ByteParser struct {
	// contains filtered or unexported fields
}

ByteParser extracts fields from a byte slice.

func NewByteParser

func NewByteParser(b []byte) *ByteParser

NewByteParser creates a parser that extracts fields from the given byte slice.

func (*ByteParser) Err

func (b *ByteParser) Err() error

Err returns the last error encountered by the parser. It is either nil or ErrUnexpectedEOF.

func (*ByteParser) Field

func (b *ByteParser) Field() Field

Field returns the last parsed field.

func (*ByteParser) Reset

func (b *ByteParser) Reset(p []byte)

Reset changes the buffer ByteParser parses fields from.

func (*ByteParser) Scan

func (b *ByteParser) Scan() bool

Scan parses the next available filed in the remaining buffer. It returns false if there are no fields to parse.

type ChunkScanner

type ChunkScanner struct {
	// contains filtered or unexported fields
}

ChunkScanner reads through the lines of a byte slice. The returned chunks include the line endings. A line's ending is specified in the Event Stream standard's documentation: https://html.spec.whatwg.org/multipage/server-sent-events.html#server-sent-events

func NewChunkScanner

func NewChunkScanner(buffer []byte) ChunkScanner

NewChunkScanner creates a ChunkScanner that will start scanning from the given buffer.

func (*ChunkScanner) Chunk

func (s *ChunkScanner) Chunk() (chunk []byte, endsInNewline bool)

Chunk returns the last scanned chunk of data. It also returns a flag that indicates whether the data ends in a newline sequence.

The returned chunk is nil only if no scan was run or if there was nothing to read from the provided source, otherwise its length is always greater than 0.

func (*ChunkScanner) Reset

func (s *ChunkScanner) Reset(buffer []byte)

Reset changes the scanner's buffer. Calling Scan after reset will return the first chunk from this buffer.

func (*ChunkScanner) Scan

func (s *ChunkScanner) Scan() bool

Scan retrieves the next chunk from the buffer. It returns true if there is data left in the buffer. Calling Scan multiple times after there is no data will change the buffer!

type Field

type Field struct {
	Name  FieldName
	Value []byte
}

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. The Value is not owned by the Field struct.

As a special case, if a parser (ByteParser or ReaderParser) 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 ReaderParser

type ReaderParser struct {
	// contains filtered or unexported fields
}

ReaderParser extracts fields from a reader. Reading is buffered using a bufio.Scanner. The ReaderParser also removes the UTF-8 BOM if it exists.

func NewReaderParser

func NewReaderParser(r io.Reader) *ReaderParser

NewReaderParser returns a parser that extracts fields from a reader.

func (*ReaderParser) Err

func (r *ReaderParser) Err() error

Err returns the last read error.

func (*ReaderParser) Field

func (r *ReaderParser) Field() Field

Field returns the last parsed field. The Field's Value byte slice isn't owned by the field, the underlying buffer is owned by the bufio.Scanner that is used by the ReaderParser.

func (*ReaderParser) Scan

func (r *ReaderParser) Scan() bool

Scan 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