Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from muir/duration
time.Duration & flag.Value
- Loading branch information
Showing
3 changed files
with
186 additions
and
81 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,46 @@ | ||
package reflectutils | ||
|
||
import ( | ||
"reflect" | ||
"time" | ||
) | ||
|
||
func init() { | ||
RegisterStringSetter(time.ParseDuration) | ||
} | ||
|
||
var settersByType = make(map[reflect.Type]reflect.Value) | ||
|
||
// RegisterStringSetter registers functions that can be used to transform | ||
// strings into specific types. The fn argument must be a function that | ||
// takes a string and returns an arbitrary type and an error. An example | ||
// of such a function is time.ParseDuration. Any call to RegisterStringSetter | ||
// with a value that is not a function of that sort will panic. | ||
// | ||
// RegisterStringSetter is not thread safe and should probably only be | ||
// used during init(). | ||
// | ||
// These functions are used by MakeStringSetter() when there is an opportunity | ||
// to do so. | ||
func RegisterStringSetter(fn interface{}) { | ||
v := reflect.ValueOf(fn) | ||
if !v.IsValid() { | ||
panic("call to RegisterStringSetter with an invalid value") | ||
} | ||
if v.Type().Kind() != reflect.Func { | ||
panic("call to RegisterStringSetter with something other than a function") | ||
} | ||
if v.Type().NumIn() != 1 { | ||
panic("call to RegisterStringSetter with something other than a function that takes one arg") | ||
} | ||
if v.Type().NumOut() != 2 { | ||
panic("call to RegisterStringSetter with something other than a function that takes returns two values") | ||
} | ||
if v.Type().In(0) != reflect.TypeOf((*string)(nil)).Elem() { | ||
panic("call to RegisterStringSetter with something other than a function that takes something other than string") | ||
} | ||
if v.Type().Out(1) != reflect.TypeOf((*error)(nil)).Elem() { | ||
panic("call to RegisterStringSetter with something other than a function that returns something other than error") | ||
} | ||
settersByType[v.Type().Out(0)] = v | ||
} |
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