diff --git a/assert/assert.go b/assert/assert.go index 58772d8..c0279d0 100644 --- a/assert/assert.go +++ b/assert/assert.go @@ -97,6 +97,11 @@ var ( // These two are our indexing system for default asserter. Note also the // mutex blew. All of this is done to keep client package race detector // cool. + // Production + // Development + // Test + // TestFull + // Debug defAsserter = []Asserter{P, B, T, TF, D} def defInd @@ -536,6 +541,23 @@ func SetDefault(i defInd) Asserter { return defAsserter[i] } +// mapDefInd runtime asserters, that's why test asserts are removed for now. +var mapDefInd = map[string]defInd{ + "Production": Production, + "Development": Development, + //"Test": Test, + //"TestFull": TestFull, + "Debug": Debug, +} + +func NewDefInd(v string) defInd { + ind, found := mapDefInd[v] + if !found { + return Production + } + return ind +} + func combineArgs(format string, a []any) []any { args := make([]any, 1, len(a)+1) args[0] = format diff --git a/err2_init.go b/err2_init.go index fa93c34..78438f5 100644 --- a/err2_init.go +++ b/err2_init.go @@ -3,11 +3,42 @@ package err2 import ( "flag" + "github.com/lainio/err2/assert" "github.com/lainio/err2/internal/tracer" ) +var ( + // asserter def index flag has following values: + // + // Production + // Development + // Test + // TestFull + // Debug + asserterFlag flagAsserter +) + func init() { flag.Var(&tracer.Log, "err2-log", "stream for logging") flag.Var(&tracer.Error, "err2-trace", "stream for error tracing") flag.Var(&tracer.Panic, "err2-panic-trace", "stream for panic tracing") + flag.Var(&asserterFlag, "err2-asserter", "asserter: Production, Development, Debug") +} + +type flagAsserter struct { + v string +} + +// String is part of the flag interfaces +func (f *flagAsserter) String() string { + return f.v +} + +// Get is part of the flag interfaces, getter. +func (*flagAsserter) Get() any { return nil } + +// Set is part of the flag.Value interface. +func (f *flagAsserter) Set(flagAsserter string) error { + assert.SetDefault(assert.NewDefInd(flagAsserter)) + return nil }