本文整理汇总了Golang中reflect.Value.NumMethod方法的典型用法代码示例。如果您正苦于以下问题:Golang Value.NumMethod方法的具体用法?Golang Value.NumMethod怎么用?Golang Value.NumMethod使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类reflect.Value
的用法示例。
在下文中一共展示了Value.NumMethod方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: overwriteFields
// overwriteFields replaces configuration values with alternate values specified
// through the environment. Precondition: an empty path slice must never be
// passed in.
func (p *Parser) overwriteFields(v reflect.Value, fullpath string, path []string, payload string) error {
for v.Kind() == reflect.Ptr {
if v.IsNil() {
panic("encountered nil pointer while handling environment variable " + fullpath)
}
v = reflect.Indirect(v)
}
switch v.Kind() {
case reflect.Struct:
return p.overwriteStruct(v, fullpath, path, payload)
case reflect.Map:
return p.overwriteMap(v, fullpath, path, payload)
case reflect.Interface:
if v.NumMethod() == 0 {
if !v.IsNil() {
return p.overwriteFields(v.Elem(), fullpath, path, payload)
}
// Interface was empty; create an implicit map
var template map[string]interface{}
wrappedV := reflect.MakeMap(reflect.TypeOf(template))
v.Set(wrappedV)
return p.overwriteMap(wrappedV, fullpath, path, payload)
}
}
return nil
}
示例2: unmarshalUint
func (d *decoder) unmarshalUint(size uint, offset uint, result reflect.Value, uintType uint) (uint, error) {
if size > uintType/8 {
return 0, newInvalidDatabaseError("the MaxMind DB file's data section contains bad data (uint%v size of %v)", uintType, size)
}
value, newOffset, err := d.decodeUint(size, offset)
if err != nil {
return 0, err
}
switch result.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
n := int64(value)
if !result.OverflowInt(n) {
result.SetInt(n)
return newOffset, nil
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
if !result.OverflowUint(value) {
result.SetUint(value)
return newOffset, nil
}
case reflect.Interface:
if result.NumMethod() == 0 {
result.Set(reflect.ValueOf(value))
return newOffset, nil
}
}
return newOffset, newUnmarshalTypeError(value, result.Type())
}
示例3: configureInterface
func (c *Config) configureInterface(v, config reflect.Value, path string) error {
// nil interface
if v.NumMethod() == 0 {
v.Set(config)
return nil
}
tk := mapIndex(config, typeKey)
if !tk.IsValid() {
return &ConfigValueError{path, "missing the type element"}
}
if tk.Kind() != reflect.String {
return &ConfigValueError{path, "type must be a string"}
}
builder, ok := c.types[tk.String()]
if !ok {
return &ConfigValueError{path, fmt.Sprintf("type %q is unknown", tk.String())}
}
object := builder.Call([]reflect.Value{})[0]
s := indirect(object)
if !s.Addr().Type().Implements(v.Type()) {
return &ConfigValueError{path, fmt.Sprintf("%v does not implement %v", s.Type(), v.Type())}
}
v.Set(object)
return c.configureStruct(s, config, path)
}
示例4: Indirect
func Indirect(rv reflect.Value) (encoding.TextUnmarshaler, reflect.Value) {
if rv.Kind() != reflect.Ptr && rv.Type().Name() != "" && rv.CanAddr() {
rv = rv.Addr()
}
for {
// rv is an non-nil interface
if rv.Kind() == reflect.Interface && !rv.IsNil() {
if e := rv.Elem(); e.Kind() == reflect.Ptr && !e.IsNil() {
rv = e
}
}
// rv is not a pointer
if rv.Kind() != reflect.Ptr {
break
}
// rv is a pointer
if rv.IsNil() {
rv.Set(reflect.New(rv.Type()))
}
if rv.NumMethod() > 0 {
if um, ok := rv.Interface().(encoding.TextUnmarshaler); ok {
return um, reflect.Value{}
}
}
rv = rv.Elem()
}
return nil, reflect.Value{}
}
示例5: dumpStringer
// dumpStringer checks if rv implements the Stringer interface.
// If so, String() is called and its output logged.
// Returns false if this fails.
func dumpStringer(rv reflect.Value, prefix, indent string) bool {
if rv.NumMethod() == 0 {
return false
}
method, ok := rv.Type().MethodByName("String")
if !ok {
return false
}
mt := method.Type
if mt.NumOut() != 1 {
return false
}
if mt.Out(0).Kind() != reflect.String {
return false
}
ret := method.Func.Call([]reflect.Value{rv})
if len(ret) == 0 {
return false
}
fmt.Printf("%s%s%v\n", indent, prefix, ret[0])
return true
}
示例6: unmarshalInteger
func unmarshalInteger(i int64, v reflect.Value) {
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if v.OverflowInt(i) {
panic(&UnmarshalOverflowError{"integer " + strconv.FormatInt(i, 10), v.Type()})
}
v.SetInt(i)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if i < 0 {
panic(&UnmarshalOverflowError{"integer " + strconv.FormatInt(i, 10), v.Type()})
}
u := uint64(i)
if v.OverflowUint(u) {
panic(&UnmarshalOverflowError{"integer " + strconv.FormatUint(u, 10), v.Type()})
}
v.SetUint(u)
case reflect.Interface:
if v.NumMethod() == 0 {
v.Set(reflect.ValueOf(i))
return
}
fallthrough
default:
panic(&UnmarshalTypeError{"integer " + strconv.FormatInt(i, 10), v.Type()})
}
}
示例7: indirectValue
func indirectValue(v reflect.Value) (encoding.TextUnmarshaler, reflect.Value) {
if v.Kind() != reflect.Ptr && v.CanAddr() {
v = v.Addr()
}
var u encoding.TextUnmarshaler
for {
if v.Kind() == reflect.Interface && !v.IsNil() {
e := v.Elem()
if e.Kind() == reflect.Ptr && !e.IsNil() {
v = e
continue
}
}
if v.Kind() != reflect.Ptr {
break
}
if v.IsNil() {
v.Set(reflect.New(v.Type().Elem()))
}
if v.NumMethod() > 0 {
// TOML has native Datetime support, while time.Time implements
// encoding.TextUnmarshaler. For native Datetime, we need settable
// time.Time struct, so continue here.
if i, ok := v.Interface().(encoding.TextUnmarshaler); ok {
u = i
}
}
v = v.Elem()
}
return u, v
}
示例8: mapping
func (d *Decoder) mapping(v reflect.Value) {
u, pv := d.indirect(v)
if u != nil {
defer func() {
if err := u.UnmarshalYAML("!!map", pv.Interface()); err != nil {
d.error(err)
}
}()
_, pv = d.indirect(pv)
}
v = pv
// Decoding into nil interface? Switch to non-reflect code.
if v.Kind() == reflect.Interface && v.NumMethod() == 0 {
v.Set(reflect.ValueOf(d.mappingInterface()))
return
}
// Check type of target: struct or map[X]Y
switch v.Kind() {
case reflect.Struct:
d.mappingStruct(v)
return
case reflect.Map:
default:
d.error(errors.New("mapping: invalid type: " + v.Type().String()))
}
mapt := v.Type()
if v.IsNil() {
v.Set(reflect.MakeMap(mapt))
}
d.nextEvent()
keyt := mapt.Key()
mapElemt := mapt.Elem()
var mapElem reflect.Value
for {
if d.event.event_type == yaml_MAPPING_END_EVENT {
break
}
key := reflect.New(keyt)
d.parse(key.Elem())
if !mapElem.IsValid() {
mapElem = reflect.New(mapElemt).Elem()
} else {
mapElem.Set(reflect.Zero(mapElemt))
}
d.parse(mapElem)
v.SetMapIndex(key.Elem(), mapElem)
}
d.nextEvent()
}
示例9: unify
// unify performs a sort of type unification based on the structure of `rv`,
// which is the client representation.
//
// Any type mismatch produces an error. Finding a type that we don't know
// how to handle produces an unsupported type error.
func unify(data interface{}, rv reflect.Value) error {
// Special case. Look for a `Primitive` value.
if rv.Type() == reflect.TypeOf((*Primitive)(nil)).Elem() {
return unifyAnything(data, rv)
}
// Special case. Look for a value satisfying the TextUnmarshaler interface.
if v, ok := rv.Interface().(TextUnmarshaler); ok {
return unifyText(data, v)
}
// BUG(burntsushi)
// The behavior here is incorrect whenever a Go type satisfies the
// encoding.TextUnmarshaler interface but also corresponds to a TOML
// hash or array. In particular, the unmarshaler should only be applied
// to primitive TOML values. But at this point, it will be applied to
// all kinds of values and produce an incorrect error whenever those values
// are hashes or arrays (including arrays of tables).
k := rv.Kind()
// laziness
if k >= reflect.Int && k <= reflect.Uint64 {
return unifyInt(data, rv)
}
switch k {
case reflect.Ptr:
elem := reflect.New(rv.Type().Elem())
err := unify(data, reflect.Indirect(elem))
if err != nil {
return err
}
rv.Set(elem)
return nil
case reflect.Struct:
return unifyStruct(data, rv)
case reflect.Map:
return unifyMap(data, rv)
case reflect.Array:
return unifyArray(data, rv)
case reflect.Slice:
return unifySlice(data, rv)
case reflect.String:
return unifyString(data, rv)
case reflect.Bool:
return unifyBool(data, rv)
case reflect.Interface:
// we only support empty interfaces.
if rv.NumMethod() > 0 {
return e("Unsupported type '%s'.", rv.Kind())
}
return unifyAnything(data, rv)
case reflect.Float32:
fallthrough
case reflect.Float64:
return unifyFloat64(data, rv)
}
return e("Unsupported type '%s'.", rv.Kind())
}
示例10: collectMethods
func (s *Scrubber) collectMethods(v reflect.Value, path Path, callbackMap map[string]Path) {
for i := 0; i < v.NumMethod(); i++ {
if v.Type().Method(i).PkgPath == "" { // exported
name := v.Type().Method(i).Name
name = strings.ToLower(name[0:1]) + name[1:]
s.registerCallback(v.Method(i), append(path, name), callbackMap)
}
}
}
示例11: unmarshalString
func unmarshalString(s string, v reflect.Value, options tagOptions) {
u, v := indirectValue(v)
if u != nil {
err := u.UnmarshalText([]byte(s))
if err != nil {
panic(err)
}
if v.Type().ConvertibleTo(datetimeType) {
t := v.Addr().Convert(reflect.PtrTo(datetimeType)).Interface().(*time.Time)
if t.IsZero() {
*t = time.Time{}
}
}
return
}
switch v.Kind() {
case reflect.String:
if options.Has("string") {
t, err := strconv.Unquote(s)
if err != nil {
panic(err)
}
v.SetString(t)
break
}
v.SetString(s)
case reflect.Slice:
if v.Type().Elem().Kind() != reflect.Uint8 {
goto typeError
}
b := make([]byte, base64.StdEncoding.DecodedLen(len(s)))
n, err := base64.StdEncoding.Decode(b, []byte(s))
if err != nil {
panic(err)
}
v.SetBytes(b[:n])
case reflect.Bool,
reflect.Float32, reflect.Float64,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if !options.Has("string") {
goto typeError
}
unmarshalQuoted(s, v)
case reflect.Interface:
if v.NumMethod() != 0 {
goto typeError
}
v.Set(reflect.ValueOf(s))
default:
goto typeError
}
return
typeError:
panic(&UnmarshalTypeError{fmt.Sprintf("string: %q", s), v.Type()})
}
示例12: parse_value
// returns true if there was a value and it's now stored in 'v', otherwise there
// was an end symbol ("e") and no value was stored
func (d *decoder) parse_value(v reflect.Value) bool {
// we support one level of indirection at the moment
if v.Kind() == reflect.Ptr {
// if the pointer is nil, allocate a new element of the type it
// points to
if v.IsNil() {
v.Set(reflect.New(v.Type().Elem()))
}
v = v.Elem()
}
if d.parse_unmarshaler(v) {
return true
}
// common case: interface{}
if v.Kind() == reflect.Interface && v.NumMethod() == 0 {
iface, _ := d.parse_value_interface()
v.Set(reflect.ValueOf(iface))
return true
}
b, err := d.ReadByte()
if err != nil {
panic(err)
}
d.offset++
switch b {
case 'e':
return false
case 'd':
d.parse_dict(v)
case 'l':
d.parse_list(v)
case 'i':
d.parse_int(v)
default:
if b >= '0' && b <= '9' {
// string
// append first digit of the length to the buffer
d.buf.WriteByte(b)
d.parse_string(v)
break
}
// unknown value
panic(&SyntaxError{
Offset: d.offset - 1,
what: "unknown value type (invalid bencode?)",
})
}
return true
}
示例13: indirect
// indirect is taken from 'text/template/exec.go'
func indirect(v reflect.Value) (rv reflect.Value, isNil bool) {
for ; v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface; v = v.Elem() {
if v.IsNil() {
return v, true
}
if v.Kind() == reflect.Interface && v.NumMethod() > 0 {
break
}
}
return v, false
}
示例14: unify
// unify performs a sort of type unification based on the structure of `rv`,
// which is the client representation.
//
// Any type mismatch produces an error. Finding a type that we don't know
// how to handle produces an unsupported type error.
func unify(data interface{}, rv reflect.Value) error {
// Special case. Look for a `Primitive` value.
if rv.Type() == reflect.TypeOf((*Primitive)(nil)).Elem() {
return unifyAnything(data, rv)
}
// Special case. Go's `time.Time` is a struct, which we don't want
// to confuse with a user struct.
if rv.Type().AssignableTo(rvalue(time.Time{}).Type()) {
return unifyDatetime(data, rv)
}
k := rv.Kind()
// laziness
if k >= reflect.Int && k <= reflect.Uint64 {
return unifyInt(data, rv)
}
switch k {
case reflect.Ptr:
elem := reflect.New(rv.Type().Elem())
err := unify(data, reflect.Indirect(elem))
if err != nil {
return err
}
rv.Set(elem)
return nil
case reflect.Struct:
return unifyStruct(data, rv)
case reflect.Map:
return unifyMap(data, rv)
case reflect.Slice:
return unifySlice(data, rv)
case reflect.String:
return unifyString(data, rv)
case reflect.Bool:
return unifyBool(data, rv)
case reflect.Interface:
// we only support empty interfaces.
if rv.NumMethod() > 0 {
return e("Unsupported type '%s'.", rv.Kind())
}
return unifyAnything(data, rv)
case reflect.Float32:
fallthrough
case reflect.Float64:
return unifyFloat64(data, rv)
}
return e("Unsupported type '%s'.", rv.Kind())
}
示例15: methods
// methods walks over a structure and scrubs its exported methods.
func (s *Scrubber) methods(rv reflect.Value, path Path, callbacks map[string]Path) {
for i := 0; i < rv.NumMethod(); i++ {
if rv.Type().Method(i).PkgPath == "" { // exported
cb, ok := rv.Method(i).Interface().(func(*Partial))
if !ok {
continue
}
name := rv.Type().Method(i).Name
name = strings.ToLower(name[0:1]) + name[1:]
s.register(cb, append(path, name), callbacks)
}
}
}