issue

package
v0.0.0-...-e082d68 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2019 License: Apache-2.0 Imports: 10 Imported by: 36

Documentation

Index

Examples

Constants

View Source
const (
	SeverityIgnore      = Severity(1)
	SeverityDeprecation = Severity(2)
	SeverityWarning     = Severity(3)
	SeverityError       = Severity(4)
)

this would be an enum in most other languages

Variables

View Source
var NoArgs = H{}

Functions

func AnOrA

func AnOrA(e interface{}) string

AnOrA returns the non capitalized article for the label of the given argument

Example
fmt.Println(AnOrA(`string`))
fmt.Println(AnOrA(`integer`))
Output:

a string
an integer

func Article

func Article(s string) string

Article returns the non capitalized article for the given string

func ArticleUc

func ArticleUc(s string) string

ArticleUc returns the capitalized article for the given string

func CamelToSnakeCase

func CamelToSnakeCase(name string) string

CamelToSnakeCase converts a camel cased name like "NameIsBob" to its corresponding snake cased "name_is_bob"

Example
fmt.Println(CamelToSnakeCase(`MyNameIsBob`))
fmt.Println(CamelToSnakeCase(`_MyNameIsBob`))
fmt.Println(CamelToSnakeCase(`__MyNameIsBob`))
fmt.Println(CamelToSnakeCase(`SomeID`))
fmt.Println(CamelToSnakeCase(`SomeIDrum`))
fmt.Println(CamelToSnakeCase(`SomeID_OfSomething`))
Output:

my_name_is_bob
_my_name_is_bob
__my_name_is_bob
some_id
some_id_rum
some_id_of_something

func FirstToLower

func FirstToLower(name string) string

FirstToLower ensures that the first character in a name like "NameIsBob" is lowercase. Leading // underscore characters are left as is.

Example
fmt.Println(FirstToLower(`MyNameIsBob`))
fmt.Println(FirstToLower(`MyNameIs_Bob`))
fmt.Println(FirstToLower(`_MyNameIsBob`))
fmt.Println(FirstToLower(`__MyNameIsBob`))
Output:

myNameIsBob
myNameIs_Bob
_myNameIsBob
__myNameIsBob

func IncludeStacktrace

func IncludeStacktrace(flag bool)

IncludeStacktrace can be set to true to get all Reported to include a stacktrace.

func JoinErrors

func JoinErrors(e interface{}) string

JoinErrors joins a set of errors into a string using newline separators. The argument can be a Result, a Reported, an error, a string, or a slice of Reported, error, or string,

Example
fmt.Println(JoinErrors([]string{`first`, `second`, `third`}))
Output:

first
second
third

func Label

func Label(e interface{}) string

Label returns the Label for a Labeled argument, the Name for a Named argument, a string verbatim, or the resulting text from doing a Sprintf with "value of type %T" for other types of arguments.

func LocationString

func LocationString(location Location) string

func MapFprintf

func MapFprintf(writer io.Writer, formatString string, args H) (int, error)

MapFprintf is like fmt.Fprintf but it allows named arguments and it assumes a map as the arguments that follow the format string.

The notation %{name} maps to the 'name' key of the map and uses the default format (%v) The notation %<name>2.2s maps to the 'name' key of the map and uses the %2.2s format.

Example
_, _ = MapFprintf(os.Stdout, "%{foo} %{fee} %{fum}", H{"foo": "hello", "fee": "great", "fum": "world"})
Output:

hello great world
Example (DuplicateArguments)
_, _ = MapFprintf(os.Stdout, "%{foo} %{fee} %{foo}", H{"foo": "boys", "fee": "will be"})
Output:

boys will be boys
Example (Flags)
_, _ = MapFprintf(os.Stdout, "%<foo>4d, %<foo>o, %<foo>X", H{"foo": 23})
Output:

23, 27, 17
Example (IgnoredFlags)
_, _ = MapFprintf(os.Stdout, "%{foo}4d, %{foo}o, %{foo}X", H{"foo": 23})
Output:

234d, 23o, 23X
Example (MissingKey)
defer func() {
	if err := recover(); err != nil {
		fmt.Println(err)
	}
}()
_, _ = MapFprintf(os.Stdout, "%{foo} %{fee} %{fum}", H{"foo": "hello", "fum": "world"})
Output:

hello %!{fee}(MISSING) world

func MapSprintf

func MapSprintf(formatString string, args H) string

MapSprintf calls MapFprintf with a string Buffer and returns string that is output to that buffer

Example
fmt.Print(MapSprintf("%{foo} %{fee} %{fum}\n", H{"foo": "hello", "fee": "great", "fum": "world"}))
Output:

hello great world

func SnakeToCamelCase

func SnakeToCamelCase(name string) string

SnakeToCamelCase converts a snake cased name like "name_is_bob" to its corresponding camel cased "NameIsBob"

Example
fmt.Println(SnakeToCamelCase(`my_name_is_bob`))
fmt.Println(SnakeToCamelCase(`_my_name_is_bob`))
fmt.Println(SnakeToCamelCase(`__my_name_is_bob`))
Output:

MyNameIsBob
_MyNameIsBob
__MyNameIsBob

func SnakeToCamelCaseDC

func SnakeToCamelCaseDC(name string) string

SnakeToCamelCaseDC converts a snake cased name like "name_is_bob" to its corresponding camel cased de-capitalized name "nameIsBob". Leading underscore characters are left as is.

Example
fmt.Println(SnakeToCamelCaseDC(`my_name_is_bob`))
fmt.Println(SnakeToCamelCaseDC(`_my_name_is_bob`))
fmt.Println(SnakeToCamelCaseDC(`__my_name_is_bob`))
Output:

myNameIsBob
_myNameIsBob
__myNameIsBob

func UcAnOrA

func UcAnOrA(e interface{}) string

UcAnOrA returns the capitalized article for the label of the given argument

func Unindent

func Unindent(str string) string

Unindent determines the maximum indent that can be stripped by looking at leading whitespace on all lines. Lines that consists entirely of whitespace are not included in the computation. Strips first line if it's empty, then strips the computed indent from all lines and returns the result.

Example
fmt.Println(Unindent(`
     No whitespace in front of this line.
       Two whitespaces in front of this one
     `))
Output:

No whitespace in front of this line.
  Two whitespaces in front of this one

Types

type ArgFormatter

type ArgFormatter func(value interface{}) string

An ArgFormatter function, provided to the Issue constructors via the HF map, is responsible for formatting a named argument in the format string before the final formatting takes place.

Typical formatters are AnOrA or UcAnOrA. Both will prefix the named argument with an article. The difference between the two is that UcAnOrA uses a capitalized article.

type Code

type Code string

A Code is a unique string representation of an issue. It should be all uppercase and words must be separated by underscores, not spaces. Since all issues live in the same global namespace, it's recommended that the code is prefixed with a package name.

Example:

const EVAL_UNKNOWN_FUNCTION = `EVAL_UNKNOWN_FUNCTION`

type H

type H map[string]interface{}

type HF

type HF map[string]ArgFormatter

A HF is used for passing ArgFormatters to an Issue constructor

type Issue

type Issue interface {
	// ArgFormatters returns the argument formatters or nil if no such
	// formatters exists
	ArgFormatters() HF

	// Code returns the issue code
	Code() Code

	// Format uses the receivers format string and the given arguments to
	// write a string onto the given buffer
	Format(b *bytes.Buffer, arguments H)

	// IsDemotable returns false for soft issues and true for hard issues
	IsDemotable() bool

	// MessageFormat returns the format used when formatting the receiver
	MessageFormat() string
}

An Issue is a formal description of a warning or an error.

func ForCode

func ForCode(code Code) Issue

Returns the Issue for a Code. Will panic if the given code does not represent an existing issue

func ForCode2

func ForCode2(code Code) (dsc Issue, ok bool)

Returns the Issue for a Code together with a bool indicating if the issue was found or not

func Hard

func Hard(code Code, messageFormat string) Issue

Hard creates a non-demotable Issue with the given code and messageFormat

func Hard2

func Hard2(code Code, messageFormat string, argFormatters HF) Issue

Hard2 creates a non-demotable Issue with the given code, messageFormat, and argFormatters map.

func Soft

func Soft(code Code, messageFormat string) Issue

Hard creates a demotable Issue with the given code and messageFormat

func Soft2

func Soft2(code Code, messageFormat string, argFormats HF) Issue

Soft2 creates a demotable Issue with the given code, messageFormat, and argFormatters map.

type Labeled

type Labeled interface {
	// Returns a very brief description of this expression suitable to use in error messages
	Label() string
}

A Labeled is an object that has a label in the form of a string

type Located

type Located interface {
	Location() Location
}

type Location

type Location interface {
	File() string

	Line() int

	// Position on line
	Pos() int
}

func NewLocation

func NewLocation(file string, line, pos int) Location

func ParseLocation

func ParseLocation(loc string) Location

type Named

type Named interface {
	Name() string
}

A Named is an object that has a name in the form of a string

type Reported

type Reported interface {
	// Argument returns the argument for the given key or nil if no
	// such argument exists
	Argument(key string) interface{}

	// Keys returns the list of argument keys
	Keys() []string

	// Code returns the issue code
	Code() Code

	// Cause returns the cause of this Reported issue, if any.
	Cause() error

	// Error produces a string from the receives issue and arguments
	Error() string

	// Error produces a string from the receives issue and arguments
	// and writes it to the given buffer
	ErrorTo(*bytes.Buffer)

	// Location returns the location where the issue was reported
	Location() Location

	// OffsetByLocation returns a copy of the receiver where the location
	// is offset by the given location. This is useful when the original
	// source is embedded in a another file.
	OffsetByLocation(location Location) Reported

	// WithLocation returns a copy of the receiver where the location has
	// been overwritten by the argument
	WithLocation(location Location) Reported

	// Severity returns the severity
	Severity() Severity

	// Stack returns the formatted stack trace of the error or an empty string if
	// stack traces where not enabled using IncludeStackTrace()
	Stack() string

	// String is an alias for Error
	String() string
}

A Reported instance contains information of an issue such as an error or a warning. It contains an Issue and arguments needed to format that issue. It also contains the location where the issue was reported.

func ErrorWithStack

func ErrorWithStack(code Code, args H, location Location, cause error, stack string) Reported

ErrorWithoutStack creates a new instance of the Reported based on code, arguments, and location. It does not add a stack trace.

This constructor is mainly intended for errors that are propagated from an external executable and a local stack trace would not make sense.

func ErrorWithoutStack

func ErrorWithoutStack(code Code, args H, location Location, cause error) Reported

ErrorWithoutStack is deprecated and will be removed in future versions of the code

func NewNested

func NewNested(code Code, args H, locOrSkip interface{}, cause error) Reported

NewNested creates a new instance of the Reported error with a given Code, argument hash, and nested error. The locOrSkip must either be nil, a Location, or an int denoting the number of frames to skip in a stacktrace, counting from the caller of NewNested.

func NewReported

func NewReported(code Code, severity Severity, args H, locOrSkip interface{}) Reported

NewReported creates a new instance of the Reported with a given Code, Severity, and argument hash. The locOrSkip must either be nil, a Location, or an int denoting the number of frames to skip in a stacktrace, counting from the caller of NewReported.

Example
// Issues are normally declared in an init() function and they end up in a global
// variable. The withIssues() function used here is only for test purposes
withIssues(func() {
	const (
		FirstIssue  = `FIRST_ISSUE`
		SecondIssue = `SECOND_ISSUE`
	)

	// Issue using %{name} notation to represent a value with default format (%v) and
	// %<> notation to use a specific format (here %T)
	Hard(FirstIssue, "The %{item} is of incorrect type. Expected int32, got %<value>T")

	// Issue using an ArgumentsFormatter to prefix the item with a capitalized article
	Hard2(SecondIssue, "%{item} cannot be used here", HF{`item`: UcAnOrA})

	err1 := NewReported(FirstIssue, SeverityError, H{`item`: `width`, `value`: int16(12)}, NewLocation(`/tmp/test`, 32, 14))

	err2 := NewReported(SecondIssue, SeverityError, H{`item`: `integer`}, NewLocation(`/tmp/test`, 42, 8))

	fmt.Println(err1)
	fmt.Println(err2)

})
Output:

The width is of incorrect type. Expected int32, got int16 (file: /tmp/test, line: 32, column: 14)
An integer cannot be used here (file: /tmp/test, line: 42, column: 8)

type Result

type Result interface {
	// Error returns true if errors where found or false if this result
	// contains only warnings.
	Error() bool

	// Issues returns all errors and warnings. An empty slice is returned
	// when no errors or warnings were generated
	Issues() []Reported
}

func NewResult

func NewResult(issues []Reported) Result

NewResult creates a Result from a slice of Reported

type Severity

type Severity int

Severity used in reported issues

func (Severity) AssertValid

func (severity Severity) AssertValid()

AssertValid checks that the given severity is one of the recognized severities

func (Severity) String

func (severity Severity) String() string

String returns the severity in lowercase string form

Jump to

Keyboard shortcuts

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