Documentation ¶
Overview ¶
Package spur the structured processing uniform results package. Implements a way to return uniform processing results across packages
Package spur the structured processing uniform results package. Implements a way to return uniform processing results across packages
Example ¶
package main import ( "fmt" "github.com/nehemming/spur" ) var ( WorkerDone = spur.Make(1, 34) ) func worker(input int) *spur.Result { // worker // basic example with fixed error message return spur.NewInfo(WorkerDone, spur.NMeta("worker", 1)). WithMessage("Done ${worker} ${1:%03d}", input).Result() } func main() { res := worker(10) fmt.Println(res.Message()) // run a few more results := spur.New(worker(11), worker(12)) for _, v := range results.Results() { fmt.Println(v.Level(), v.Message()) } } func main() { main() }
Output: Done 1 010 Info Done 1 011 Info Done 1 012
Example (Two) ¶
package main import ( "context" "fmt" "github.com/nehemming/pulp" "github.com/nehemming/spur" "golang.org/x/text/language" ) // myPackID is the unique type to identify my language packs in pulp type myPackID int const ( // Create a langPack language pack id // See pulp for more info on language pack ids LangPack = myPackID(iota) ) // MyResultCode is the unique code type that we will use for this packages results // The result code implements the ResultCoder interface so it can be used with spur and // also Stringer so that the result codes output the same string as the ResultCoder interface // will generate. type MyResultCode int // Create unique error code ids const ( // Good practice to create a Zero value messages for all types NoProblem = MyResultCode(iota) // MyWorkIsDone is the messagee id that work is complete MyWorkIsDone ) func (c MyResultCode) ResultCode() spur.ResultCode { // The MyWorkIsDone has a underlying value of int 1 // This demonstrates how the id can be used to create a // different result code value (and show its not the result code thats used to locate the message) return spur.Make(int(c), 19) } func (c MyResultCode) String() string { // Having gon to the effort to make result codes different // we can now use the result code string function to create a consistent // output string. // (ResultCode().String() actually uses pulp to find the standard output format) return c.ResultCode().String() } // myWorker does some work func myWorker(ctx context.Context, input int) *spur.Result { // worker // basic example with looked up message // Meta "worker" is passed in as is the input as pos arg $1 // WithLookupMessage will use the MyWorkIsDone id to locate the result // message. return spur.NewInfo(MyWorkIsDone, spur.NMeta("worker", 1)). WithLookupMessage(ctx, input).Result() } func init() { // Register formats and english language pack pulp.Register(LangPack, func(packID pulp.LangPackID, langTag language.Tag) (pulp.TextMap, []pulp.NamedArg) { return pulp.TextMap{ // creating an in line text map for simplicity. // the ${@id} format is a special pulp parameter that returns the string representation of the // the code used to look up this error message (essentally MyWorkIsDone.String() here) MyWorkIsDone: "Done ${worker} ${1:%03d} today ${@id}", }, nil }, pulp.Package, language.English) } func main2() { res := myWorker(context.TODO(), 10) if res.ResultCode() != NoProblem.ResultCode() { fmt.Println(res.Message()) } } func main() { main2() }
Output: Done 1 010 today 013-00001
Index ¶
- Constants
- type Builder
- func NewCritical(code ResultCoder, meta ...MetaPair) Builder
- func NewError(code ResultCoder, meta ...MetaPair) Builder
- func NewFailure(code ResultCoder, meta ...MetaPair) Builder
- func NewInfo(code ResultCoder, meta ...MetaPair) Builder
- func NewResult(level Level, code ResultCoder, meta ...MetaPair) Builder
- func NewSuccess(meta ...MetaPair) Builder
- func NewWarn(code ResultCoder, meta ...MetaPair) Builder
- type Level
- type MetaKey
- type MetaMap
- type MetaPair
- type MetaValue
- type Result
- func (r *Result) Error() string
- func (r *Result) Level() Level
- func (r *Result) MarshalJSON() ([]byte, error)
- func (r *Result) MarshalYAML() (interface{}, error)
- func (r *Result) Message() string
- func (r *Result) Meta(key MetaKey) MetaValue
- func (r *Result) MetaKeys() []MetaKey
- func (r *Result) ResultCode() ResultCode
- func (r *Result) String() string
- func (r *Result) Timestamp() Timestamp
- func (r *Result) TryMeta(key MetaKey) (val MetaValue, found bool)
- func (r *Result) UnmarshalJSON(data []byte) error
- func (r *Result) UnmarshalYAML(value *yaml.Node) error
- type ResultCode
- type ResultCoder
- type Results
- type Timestamp
Examples ¶
Constants ¶
const ( // Success success Success = Level(iota) // Info info message, non error Info // Warn non erroneous warning Warn // Error processing an item, not necessarily fatal Error // Failure in processing, possibly recoverable with a retry Failure // Critical failure, stop all further processing Critical )
const GenericError = ResultCode(1)
GenericError code
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Builder ¶
type Builder interface { // WithMeta attaches meta data to a result WithMeta(key MetaKey, value MetaValue) Builder // WithMetaPairs attaches meta data to a result WithMetaPairs(meta ...MetaPair) Builder // WithMessage sets the message text WithMessage(message string, args ...interface{}) Builder // WithLookupMessage looks up the message using the pulp provider found from the context WithLookupMessage(ctx context.Context, args ...interface{}) Builder //Result the result Result() *Result //Build a results set from the single result Results() *Results }
Builder creates one or more immutable results Call Result() to create a result and Results() to create a result set with one result member
func NewCritical ¶
func NewCritical(code ResultCoder, meta ...MetaPair) Builder
NewCritical new Critical result
func NewFailure ¶
func NewFailure(code ResultCoder, meta ...MetaPair) Builder
NewFailure new Failure result
type Level ¶
type Level int
Level result level
func (Level) CanContinue ¶
CanContinue is true if level is less than or equal to Error
func (Level) IsSuccessful ¶
IsSuccessful is true if the result level is Success or Info
type Result ¶
type Result struct {
// contains filtered or unexported fields
}
Result irepresents a single result Result is best instanciated using the New functions
func (*Result) MarshalJSON ¶
MarshalJSON outputs the result in the JSON format
func (*Result) MarshalYAML ¶
MarshalYAML outputs the result in the YAML format
func (*Result) ResultCode ¶
func (r *Result) ResultCode() ResultCode
ResultCode gets the result code
func (*Result) UnmarshalJSON ¶
UnmarshalJSON unmarshal the result from JSON
func (*Result) UnmarshalYAML ¶
UnmarshalYAML converts YAML back to a result
type ResultCode ¶
type ResultCode int
ResultCode is the numeric code fr the error
func (ResultCode) ResultCode ¶
func (r ResultCode) ResultCode() ResultCode
ResultCode converts a result code into its self, so it can be used as a Code
func (ResultCode) String ¶
func (r ResultCode) String() string
Convert the standard result code into XXX-ddddd format
type ResultCoder ¶
type ResultCoder interface {
ResultCode() ResultCode
}
ResultCoder represents any type that can be converted to a result code If pulp is used the code is used to resolve the error message string
type Results ¶
type Results struct {
// contains filtered or unexported fields
}
Results is a collection of results and outputs
func (*Results) AppendError ¶
AppendError appends a result to the exiting results set
func (*Results) AppendResult ¶
AppendResult appends a result to the exiting results set
func (*Results) AppendResults ¶
AppendResults appends one or more results sets to the receiver
type Timestamp ¶
Timestamp is used to record the time for logging
func (Timestamp) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface. Format is provided by the language pack
func (*Timestamp) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface. The time is expected to match the format provided by the language pack