location

package
v0.49.0-alpha.2 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2022 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package location is a helper package for working with locations.

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, filename string, data []byte, 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 string
	Loc  Location
	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) (next error)

FormatError implements errors.Formatter.

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap implements errors.Wrapper.

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) 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 Location, 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.

func (Lines) Search

func (l Lines) Search(offset int) int

Search returns the nearest line number to the given offset.

NOTE: may return index bigger than lines length.

type Locatable

type Locatable interface {
	// SetLocation sets the location of the value.
	SetLocation(Location)

	// Location returns the location of the value if it is set.
	Location() (Location, bool)
}

Locatable is an interface for JSON value location store.

type Location

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

Location is a JSON value location.

func (Location) Field

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

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

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

func (*Location) FromNode

func (l *Location) FromNode(node *yaml.Node)

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

func (Location) Index

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

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

func (Location) Key

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

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

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

func (Location) String

func (l Location) String() string

String implements fmt.Stringer.

func (Location) WithFilename

func (l Location) WithFilename(filename string) string

WithFilename prints the location with the given filename.

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

type Locator

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

Locator stores the Location 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 location.

See Field method of Location.

func (Locator) Index

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

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

See Index method of Location.

func (Locator) Key

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

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

See Key method of Location.

func (Locator) Location

func (l Locator) Location() (Location, bool)

Location returns the location of the value if it is set.

func (*Locator) MarshalYAML

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

MarshalYAML implements yaml.Marshaler.

func (*Locator) SetLocation

func (l *Locator) SetLocation(loc Location)

SetLocation sets the location of the value.

func (*Locator) UnmarshalYAML

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

UnmarshalYAML implements yaml.Unmarshaler.

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.

Jump to

Keyboard shortcuts

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