-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d612da4
commit 883b421
Showing
6 changed files
with
198 additions
and
23 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,52 @@ | ||
package reflectcommon | ||
|
||
import "reflect" | ||
|
||
func FitsWithinSignedIntegerRange(value reflect.Value, targetType reflect.Type) bool { | ||
min, err := getMinInt(targetType) | ||
if err != nil { | ||
return false | ||
} | ||
max, err := getMaxInt(targetType) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
switch value.Kind() { | ||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: | ||
return value.Int() >= min && value.Int() <= max | ||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: | ||
return value.Uint() <= uint64(max) | ||
} | ||
|
||
return false | ||
} | ||
|
||
func FitsWithinUnsignedIntegerRange(value reflect.Value, targetType reflect.Type) bool { | ||
max, err := getMaxUint(targetType) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
switch value.Kind() { | ||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: | ||
return value.Int() >= 0 && uint64(value.Int()) <= max | ||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: | ||
return value.Uint() <= max | ||
} | ||
|
||
return false | ||
} | ||
|
||
func FitsWithinFloatRange(value reflect.Value, targetType reflect.Type) bool { | ||
min, err := getMinFloat(targetType) | ||
if err != nil { | ||
return false | ||
} | ||
max, err := getMaxFloat(targetType) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
return value.Float() >= min && value.Float() <= max | ||
} |
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
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
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
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
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