Documentation ¶
Index ¶
- Constants
- Variables
- func AnOrA(e interface{}) string
- func Article(s string) string
- func ArticleUc(s string) string
- func CamelToSnakeCase(name string) string
- func FirstToLower(name string) string
- func IncludeStacktrace(flag bool)
- func JoinErrors(e interface{}) string
- func Label(e interface{}) string
- func LocationString(location Location) string
- func MapFprintf(writer io.Writer, formatString string, args H) (int, error)
- func MapSprintf(formatString string, args H) string
- func SnakeToCamelCase(name string) string
- func SnakeToCamelCaseDC(name string) string
- func UcAnOrA(e interface{}) string
- func Unindent(str string) string
- type ArgFormatter
- type Code
- type H
- type HF
- type Issue
- func ForCode(code Code) Issue
- func ForCode2(code Code) (dsc Issue, ok bool)
- func Hard(code Code, messageFormat string) Issue
- func Hard2(code Code, messageFormat string, argFormatters HF) Issue
- func Soft(code Code, messageFormat string) Issue
- func Soft2(code Code, messageFormat string, argFormats HF) Issue
- type Labeled
- type Located
- type Location
- type Named
- type Reported
- func ErrorWithStack(code Code, args H, location Location, cause error, stack string) Reported
- func ErrorWithoutStack(code Code, args H, location Location, cause error) Reported
- func NewNested(code Code, args H, locOrSkip interface{}, cause error) Reported
- func NewReported(code Code, severity Severity, args H, locOrSkip interface{}) Reported
- type Result
- type Severity
Examples ¶
Constants ¶
const ( SeverityIgnore = Severity(1) SeverityDeprecation = Severity(2) SeverityWarning = Severity(3) SeverityError = Severity(4) )
this would be an enum in most other languages
Variables ¶
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 CamelToSnakeCase ¶
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 ¶
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 MapFprintf ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 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 ¶
Returns the Issue for a Code. Will panic if the given code does not represent an existing issue
func ForCode2 ¶
Returns the Issue for a Code together with a bool indicating if the issue was found or not
func Hard2 ¶
Hard2 creates a non-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 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 ¶
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 ¶
ErrorWithoutStack is deprecated and will be removed in future versions of the code
func NewNested ¶
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 ¶
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)