location

package
v1.8.1 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package location provides utilities to track values over the spec.

Index

Constants

View Source
const (
	// BugLine is a fallback line when the line is not available.
	BugLine = `Cannot render line properly, please fill a bug report`
)

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 ColorFunc

type ColorFunc func(w io.Writer, s string, args ...any) (int, error)

ColorFunc defines a simple printer callback.

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) Unwrap

func (e *Error) Unwrap() error

Unwrap implements errors.Wrapper.

type File

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

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) HumanName added in v0.83.0

func (f File) HumanName() string

HumanName returns human-friendly name for this File.

func (File) IsZero

func (f File) IsZero() bool

IsZero returns true if file has zero value.

func (File) PrintHighlights

func (f File) PrintHighlights(w io.Writer, msg string, highlights []Highlight, opts PrintListingOptions) error

PrintHighlights prints all given highlights.

func (File) PrintListing

func (f File) PrintListing(w io.Writer, msg string, pos 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 Highlight

type Highlight struct {
	Pos   Position
	Color ColorFunc
}

Highlight is a highlighted position.

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

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.

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 is a Position holder.

Basically, it is a simple wrapper around Position to embed it to spec types.

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

func (l Locator) Pointer(file File) Pointer

Pointer makes a Pointer from the Locator and given File.

func (Locator) Position

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

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

func (*Locator) SetPosition

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 MultiError

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

MultiError contains multiple Reports.

func (*MultiError) Error

func (e *MultiError) Error() string

Error implements error.

func (*MultiError) Format

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

Format implements fmt.Formatter.

func (*MultiError) FormatError

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

FormatError implements errors.Formatter.

func (*MultiError) Report

func (e *MultiError) Report(file File, l Locator, msg string)

Report adds report to the list.

func (*MultiError) ReportPtr

func (e *MultiError) ReportPtr(ptr Pointer, msg string)

ReportPtr adds report to the list at given pointer.

type Pointer

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) Field

func (p Pointer) Field(key string) (ptr Pointer)

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

See Field method of Locator.

func (Pointer) File

func (p Pointer) File() File

File returns the File where the value is located.

func (Pointer) Index

func (p Pointer) Index(idx int) (ptr Pointer)

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

See Index method of Locator.

func (Pointer) Key

func (p Pointer) Key(key string) (ptr Pointer)

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

See Key method of Locator.

func (Pointer) Position

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

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

type Position

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

Position is a value position.

func (Position) Field

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

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

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

func (Position) Index

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

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

func (p Position) String() string

String implements fmt.Stringer.

func (Position) WithFilename

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 {
	// 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
	// MsgColor sets message color.
	MsgColor ColorFunc
	// TextColor sets text color.
	PlainColor ColorFunc
}

PrintListingOptions is a set of options for PrintListing.

func (PrintListingOptions) WithoutColor

func (o PrintListingOptions) WithoutColor() PrintListingOptions

WithoutColor creates a copy of the options with disabled color.

type Report

type Report struct {
	File File
	Pos  Position
	Msg  string
}

Report is element of MultiError container.

func (Report) String

func (r Report) String() string

String returns textual represntation of Report.

Jump to

Keyboard shortcuts

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