Remove package variables

This commit is contained in:
Jordan Liggitt 2022-04-21 21:24:49 -04:00
parent 7a261d9fa0
commit 87494389ac
2 changed files with 22 additions and 32 deletions

View File

@ -8,18 +8,15 @@ import (
"reflect" "reflect"
) )
var Indirect = indirect
var PrintableValue = printableValue
var ( var (
errorType = reflect.TypeOf((*error)(nil)).Elem() errorType = reflect.TypeOf((*error)(nil)).Elem()
fmtStringerType = reflect.TypeOf((*fmt.Stringer)(nil)).Elem() fmtStringerType = reflect.TypeOf((*fmt.Stringer)(nil)).Elem()
) )
// indirect returns the item at the end of indirection, and a bool to indicate if it's nil. // Indirect returns the item at the end of indirection, and a bool to indicate if it's nil.
// We indirect through pointers and empty interfaces (only) because // We indirect through pointers and empty interfaces (only) because
// non-empty interfaces have methods we might need. // non-empty interfaces have methods we might need.
func indirect(v reflect.Value) (rv reflect.Value, isNil bool) { func Indirect(v reflect.Value) (rv reflect.Value, isNil bool) {
for ; v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface; v = v.Elem() { for ; v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface; v = v.Elem() {
if v.IsNil() { if v.IsNil() {
return v, true return v, true
@ -31,11 +28,11 @@ func indirect(v reflect.Value) (rv reflect.Value, isNil bool) {
return v, false return v, false
} }
// printableValue returns the, possibly indirected, interface value inside v that // PrintableValue returns the, possibly indirected, interface value inside v that
// is best for a call to formatted printer. // is best for a call to formatted printer.
func printableValue(v reflect.Value) (interface{}, bool) { func PrintableValue(v reflect.Value) (interface{}, bool) {
if v.Kind() == reflect.Ptr { if v.Kind() == reflect.Ptr {
v, _ = indirect(v) // fmt.Fprint handles nil. v, _ = Indirect(v) // fmt.Fprint handles nil.
} }
if !v.IsValid() { if !v.IsValid() {
return "<no value>", true return "<no value>", true

View File

@ -8,13 +8,6 @@ import (
"reflect" "reflect"
) )
var Equal = eq
var GreaterEqual = ge
var Greater = gt
var LessEqual = le
var Less = lt
var NotEqual = ne
var ( var (
errBadComparisonType = errors.New("invalid type for comparison") errBadComparisonType = errors.New("invalid type for comparison")
errBadComparison = errors.New("incompatible types for comparison") errBadComparison = errors.New("incompatible types for comparison")
@ -52,8 +45,8 @@ func basicKind(v reflect.Value) (kind, error) {
return invalidKind, errBadComparisonType return invalidKind, errBadComparisonType
} }
// eq evaluates the comparison a == b || a == c || ... // Equal evaluates the comparison a == b || a == c || ...
func eq(arg1 interface{}, arg2 ...interface{}) (bool, error) { func Equal(arg1 interface{}, arg2 ...interface{}) (bool, error) {
v1 := reflect.ValueOf(arg1) v1 := reflect.ValueOf(arg1)
k1, err := basicKind(v1) k1, err := basicKind(v1)
if err != nil { if err != nil {
@ -104,15 +97,15 @@ func eq(arg1 interface{}, arg2 ...interface{}) (bool, error) {
return false, nil return false, nil
} }
// ne evaluates the comparison a != b. // NotEqual evaluates the comparison a != b.
func ne(arg1, arg2 interface{}) (bool, error) { func NotEqual(arg1, arg2 interface{}) (bool, error) {
// != is the inverse of ==. // != is the inverse of ==.
equal, err := eq(arg1, arg2) equal, err := Equal(arg1, arg2)
return !equal, err return !equal, err
} }
// lt evaluates the comparison a < b. // Less evaluates the comparison a < b.
func lt(arg1, arg2 interface{}) (bool, error) { func Less(arg1, arg2 interface{}) (bool, error) {
v1 := reflect.ValueOf(arg1) v1 := reflect.ValueOf(arg1)
k1, err := basicKind(v1) k1, err := basicKind(v1)
if err != nil { if err != nil {
@ -153,30 +146,30 @@ func lt(arg1, arg2 interface{}) (bool, error) {
return truth, nil return truth, nil
} }
// le evaluates the comparison <= b. // LessEqual evaluates the comparison <= b.
func le(arg1, arg2 interface{}) (bool, error) { func LessEqual(arg1, arg2 interface{}) (bool, error) {
// <= is < or ==. // <= is < or ==.
lessThan, err := lt(arg1, arg2) lessThan, err := Less(arg1, arg2)
if lessThan || err != nil { if lessThan || err != nil {
return lessThan, err return lessThan, err
} }
return eq(arg1, arg2) return Equal(arg1, arg2)
} }
// gt evaluates the comparison a > b. // Greater evaluates the comparison a > b.
func gt(arg1, arg2 interface{}) (bool, error) { func Greater(arg1, arg2 interface{}) (bool, error) {
// > is the inverse of <=. // > is the inverse of <=.
lessOrEqual, err := le(arg1, arg2) lessOrEqual, err := LessEqual(arg1, arg2)
if err != nil { if err != nil {
return false, err return false, err
} }
return !lessOrEqual, nil return !lessOrEqual, nil
} }
// ge evaluates the comparison a >= b. // GreaterEqual evaluates the comparison a >= b.
func ge(arg1, arg2 interface{}) (bool, error) { func GreaterEqual(arg1, arg2 interface{}) (bool, error) {
// >= is the inverse of <. // >= is the inverse of <.
lessThan, err := lt(arg1, arg2) lessThan, err := Less(arg1, arg2)
if err != nil { if err != nil {
return false, err return false, err
} }