Skip to content

Commit

Permalink
asserter flag added
Browse files Browse the repository at this point in the history
  • Loading branch information
lainio committed Sep 24, 2023
1 parent 7676ae2 commit 15849e9
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
22 changes: 22 additions & 0 deletions assert/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions err2_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 15849e9

Please sign in to comment.