location

package
v0.60.0 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const BugLine = `Cannot render line properly, please fill a bug report`

BugLine is a fallback line when the line is not available.

Variables

This section is empty.

Functions

func PrintPrettyError

func PrintPrettyError(w io.Writer, color bool, err error) bool

PrintPrettyError prints the error in a pretty way and returns true if it was printed successfully.

Types

type Error

type Error struct {
	File File
	Pos  Position
	Err  error
}

Error is a wrapper for an error that has a location.

func (*Error) Error

func (e *Error) Error() string

Error implements error.

func (*Error) Format

func (e *Error) Format(s fmt.State, verb rune)

Format implements fmt.Formatter.

func (*Error) FormatError

func (e *Error) FormatError(p errors.Printer) error

FormatError implements errors.Formatter.

func (*Error) PrettyPrint added in v0.53.0

func (e *Error) PrettyPrint(w io.Writer, color bool) bool

PrettyPrint prints the error in a pretty way and returns true if it was printed successfully.

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap implements errors.Wrapper.

type File added in v0.53.0

type File struct {
	// Name is the file name.
	Name string
	// Source is the path or URL of the file.
	Source string
	// Lines stores newline offsets.
	Lines Lines
}

File is a source file.

func NewFile added in v0.53.0

func NewFile(name, source string, data []byte) File

NewFile creates a new File.

Do not modify the data after calling this function, Lines will point to it.

func (File) IsZero added in v0.53.0

func (f File) IsZero() bool

IsZero returns true if file has zero value.

type Lines

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

Lines is a sorted slice of newline offsets.

func (*Lines) Collect

func (l *Lines) Collect(data []byte)

Collect fills the given slice with the offset of newlines.

func (Lines) IsZero added in v0.53.0

func (l Lines) IsZero() bool

IsZero returns true if lines has zero value.

func (Lines) Line

func (l Lines) Line(n int) (start, end int)

Line returns offset range of the line.

NOTE: the line number is 1-based. Returns (-1, -1) if the line is invalid.

func (Lines) PrintListing

func (l Lines) PrintListing(w io.Writer, msg string, loc Position, opts PrintListingOptions) error

PrintListing prints given message with line number and file listing to the writer.

The context parameter defines the number of lines to print before and after.

type Locatable

type Locatable interface {
	// SetPosition sets the position of the value.
	SetPosition(Position)

	// Position returns the position of the value if it is set.
	Position() (Position, bool)
}

Locatable is an interface for JSON value position store.

type Locator

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

Locator stores the Position of a JSON value.

func (Locator) Field

func (l Locator) Field(key string) (loc Locator)

Field tries to find the child node using given key and returns its position.

See Field method of Position.

func (Locator) Index

func (l Locator) Index(idx int) (loc Locator)

Index tries to find the child node using given index and returns its position.

See Index method of Position.

func (Locator) Key

func (l Locator) Key(key string) (loc Locator)

Key tries to find the child node using given key and returns its position.

See Key method of Position.

func (Locator) Pointer added in v0.54.0

func (l Locator) Pointer(file File) Pointer

Pointer makes a Pointer from the Locator and given File.

func (Locator) Position added in v0.53.0

func (l Locator) Position() (Position, bool)

Position returns the position of the value if it is set.

func (*Locator) SetPosition added in v0.53.0

func (l *Locator) SetPosition(loc Position)

SetPosition sets the position of the value.

func (*Locator) UnmarshalYAML

func (l *Locator) UnmarshalYAML(n *yaml.Node) error

UnmarshalYAML implements yaml.Unmarshaler.

type Pointer added in v0.54.0

type Pointer struct {
	// Source is the File where the value is located.
	Source File
	// Locator stores the Position of a value.
	Locator Locator
}

Pointer is a location of a value.

func (Pointer) File added in v0.54.0

func (p Pointer) File() File

File returns the File where the value is located.

func (Pointer) Position added in v0.54.0

func (p Pointer) Position() (Position, bool)

Position returns the position of the value if it is set.

type Position added in v0.53.0

type Position struct {
	Line, Column int
	Node         *yaml.Node
}

Position is a JSON value position.

func (Position) Field added in v0.53.0

func (p Position) Field(key string) (loc Position)

Field tries to find the child node using given key and returns its position. If such node is not found or parent node is not a mapping, Field returns position of the parent node.

NOTE: child position will point to the value node, not to the key node. Use Key if you want position of the key.

func (*Position) FromNode added in v0.53.0

func (p *Position) FromNode(node *yaml.Node)

FromNode sets the position of the value from the given node.

func (Position) Index added in v0.53.0

func (p Position) Index(idx int) (loc Position)

Index tries to find the child node using given index and returns its position. If such node is not found or parent node is not a sequence, Field returns position of the parent node.

func (Position) Key added in v0.53.0

func (p Position) Key(key string) (loc Position)

Key tries to find the child node using given key and returns its position. If such node is not found or parent node is not a mapping, Key returns position of the parent node.

NOTE: child position will point to the key node, not to the value node. Use Field if you want position of the value.

func (Position) String added in v0.53.0

func (p Position) String() string

String implements fmt.Stringer.

func (Position) WithFilename added in v0.53.0

func (p Position) WithFilename(filename string) string

WithFilename prints the position with the given filename.

If filename is empty, the position is printed as is.

type PrintListingOptions

type PrintListingOptions struct {
	// Filename is a name of the file to print with location.
	Filename string
	// Context is the number of lines to print before and after the error line.
	//
	// If is zero, the default value 5 is used.
	Context int
	// If is nil, the default value color.New(color.FgRed) is used.
	ErrColor *color.Color
	// If is nil, the default value color.New(color.Reset) is used.
	PlainColor *color.Color
}

PrintListingOptions is a set of options for PrintListing.

func (PrintListingOptions) WithoutColor added in v0.52.0

func (o PrintListingOptions) WithoutColor() PrintListingOptions

WithoutColor creates a copy of the options with disabled color.

Jump to

Keyboard shortcuts

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