-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
- Loading branch information
1 parent
2cbdde9
commit 21b0251
Showing
3 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package reflect | ||
|
||
import ( | ||
"reflect" | ||
) | ||
|
||
func GetKind(value interface{}) reflect.Kind { | ||
if value == nil { | ||
return reflect.Invalid | ||
} | ||
return reflect.TypeOf(value).Kind() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package reflect | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
) | ||
|
||
func MatchScalar(expected, actual interface{}) (bool, error) { | ||
if actual == nil && expected == nil { | ||
return true, nil | ||
} | ||
if actual == expected { | ||
return true, nil | ||
} | ||
// if they are the same type we can use reflect.DeepEqual | ||
if reflect.TypeOf(expected) == reflect.TypeOf(actual) { | ||
return reflect.DeepEqual(expected, actual), nil | ||
} | ||
e := reflect.ValueOf(expected) | ||
a := reflect.ValueOf(actual) | ||
if !a.IsValid() && !e.IsValid() { | ||
return true, nil | ||
} | ||
if a.CanComplex() && e.CanComplex() { | ||
return a.Complex() == e.Complex(), nil | ||
} | ||
if a.CanFloat() && e.CanFloat() { | ||
return a.Float() == e.Float(), nil | ||
} | ||
if a.CanInt() && e.CanInt() { | ||
return a.Int() == e.Int(), nil | ||
} | ||
if a.CanUint() && e.CanUint() { | ||
return a.Uint() == e.Uint(), nil | ||
} | ||
if a, ok := ToNumber(a); ok { | ||
if e, ok := ToNumber(e); ok { | ||
return a == e, nil | ||
} | ||
} | ||
return false, fmt.Errorf("types are not comparable, %s - %s", GetKind(expected), GetKind(actual)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package reflect | ||
|
||
import ( | ||
"reflect" | ||
) | ||
|
||
func ToNumber(value reflect.Value) (float64, bool) { | ||
if value.CanFloat() { | ||
return value.Float(), true | ||
} | ||
if value.CanInt() { | ||
return float64(value.Int()), true | ||
} | ||
if value.CanUint() { | ||
return float64(value.Uint()), true | ||
} | ||
return 0, false | ||
} |