This repository has been archived by the owner on Feb 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
3 changed files
with
37 additions
and
35 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,32 @@ | ||
package unmarshal | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// ErrUnexpectedType is returned when a field contains an unexpected type. | ||
type ErrUnexpectedType struct { | ||
t interface{} | ||
} | ||
|
||
// Error implements the error interface. | ||
func (e *ErrUnexpectedType) Error() string { | ||
return fmt.Sprintf("Unexpected field type: %T", e.t) | ||
} | ||
|
||
// GetIntStringField uses type asserstion to attempt to determine if the | ||
// field argument is a string or a int. Returns a string or an int or an error | ||
// if neither. | ||
func GetIntStringField(field interface{}) (string, int, error) { | ||
stringField, ok := field.(string) | ||
if ok { | ||
return stringField, -1, nil | ||
} | ||
|
||
floatField, ok := field.(float64) | ||
if !ok { | ||
return "", -1, &ErrUnexpectedType{t: field} | ||
} | ||
|
||
return "", int(floatField), nil | ||
} |
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