本文整理汇总了Golang中reflect.Value.Addr方法的典型用法代码示例。如果您正苦于以下问题:Golang Value.Addr方法的具体用法?Golang Value.Addr怎么用?Golang Value.Addr使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类reflect.Value
的用法示例。
在下文中一共展示了Value.Addr方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: emitAddrMarshaler
func (e *Encoder) emitAddrMarshaler(tag string, v reflect.Value) {
if !v.CanAddr() {
e.marshal(tag, v, false)
return
}
va := v.Addr()
if va.IsNil() {
e.emitNil()
return
}
m := v.Interface().(Marshaler)
t, val, err := m.MarshalYAML()
if err != nil {
panic(err)
}
if val == nil {
e.emitNil()
return
}
e.marshal(t, reflect.ValueOf(val), false)
}
示例2: decodeInt
func decodeInt(val *uint16, data []byte, sf *SprotoField, v reflect.Value) error {
var n uint64
if val != nil {
n = uint64(*val)
} else {
switch len(data) {
case 0:
n = 0
case 4:
n = uint64(readUint32(data))
case 8:
n = readUint64(data)
default:
return fmt.Errorf("sproto: malformed integer data for field %s", sf.Name)
}
}
e := v.Type().Elem()
v.Addr().Elem().Set(reflect.New(e))
switch e.Kind() {
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
v.Elem().SetInt(int64(n))
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint:
v.Elem().SetUint(n)
}
return nil
}
示例3: ParseValue
func (par *parserParser) ParseValue(ctx *parseContext, valueOf reflect.Value, location int, err *Error) int {
var v Parser
if par.ptr {
v = valueOf.Addr().Interface().(Parser)
} else {
if valueOf.Kind() == reflect.Ptr {
valueOf = reflect.New(valueOf.Type().Elem())
}
v = valueOf.Interface().(Parser)
}
l, e := v.ParseValue(ctx.str, location)
if e != nil {
switch ev := e.(type) {
case Error:
err.Location = ev.Location
err.Message = ev.Message
err.Str = ev.Str
return -1
}
err.Location = location
err.Message = e.Error()
return -1
}
location = l
if location > len(ctx.str) {
panic("Invalid parser")
}
return location
}
示例4: getValueForMarshalInterface
func (f encFnInfo) getValueForMarshalInterface(rv reflect.Value, indir int8) (v interface{}, proceed bool) {
if indir == 0 {
v = rv.Interface()
} else if indir == -1 {
// If a non-pointer was passed to Encode(), then that value is not addressable.
// Take addr if addresable, else copy value to an addressable value.
if rv.CanAddr() {
v = rv.Addr().Interface()
} else {
rv2 := reflect.New(rv.Type())
rv2.Elem().Set(rv)
v = rv2.Interface()
// fmt.Printf("rv.Type: %v, rv2.Type: %v, v: %v\n", rv.Type(), rv2.Type(), v)
}
} else {
for j := int8(0); j < indir; j++ {
if rv.IsNil() {
f.ee.EncodeNil()
return
}
rv = rv.Elem()
}
v = rv.Interface()
}
return v, true
}
示例5: setModelValue
func (d base) setModelValue(driverValue, fieldValue reflect.Value) error {
switch fieldValue.Type().Kind() {
case reflect.Bool:
fieldValue.SetBool(d.dialect.parseBool(driverValue.Elem()))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
fieldValue.SetInt(driverValue.Elem().Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
// reading uint from int value causes panic
switch driverValue.Elem().Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
fieldValue.SetUint(uint64(driverValue.Elem().Int()))
default:
fieldValue.SetUint(driverValue.Elem().Uint())
}
case reflect.Float32, reflect.Float64:
fieldValue.SetFloat(driverValue.Elem().Float())
case reflect.String:
fieldValue.SetString(string(driverValue.Elem().Bytes()))
case reflect.Slice:
if reflect.TypeOf(driverValue.Interface()).Elem().Kind() == reflect.Uint8 {
fieldValue.SetBytes(driverValue.Elem().Bytes())
}
case reflect.Struct:
switch fieldValue.Interface().(type) {
case time.Time:
fieldValue.Set(driverValue.Elem())
default:
if scanner, ok := fieldValue.Addr().Interface().(sql.Scanner); ok {
return scanner.Scan(driverValue.Interface())
}
}
}
return nil
}
示例6: getSetter
func getSetter(outt reflect.Type, out reflect.Value) Setter {
setterMutex.RLock()
style := setterStyle[outt]
setterMutex.RUnlock()
if style == setterNone {
return nil
}
if style == setterUnknown {
setterMutex.Lock()
defer setterMutex.Unlock()
if outt.Implements(setterIface) {
setterStyle[outt] = setterType
} else if reflect.PtrTo(outt).Implements(setterIface) {
setterStyle[outt] = setterAddr
} else {
setterStyle[outt] = setterNone
return nil
}
style = setterStyle[outt]
}
if style == setterAddr {
if !out.CanAddr() {
return nil
}
out = out.Addr()
} else if outt.Kind() == reflect.Ptr && out.IsNil() {
out.Set(reflect.New(outt.Elem()))
}
return out.Interface().(Setter)
}
示例7: ScanToStruct
func (scan *Scan) ScanToStruct(rows *sql.Rows, record reflect.Value) error {
columns, err := rows.Columns()
if err != nil {
return err
}
values := make([]interface{}, len(columns))
for i, column := range columns {
var field reflect.Value
fieldName := scan.SQLColumnDict[column]
if scan.ToPointers {
field = record.Elem().FieldByName(fieldName)
} else {
field = record.FieldByName(fieldName)
}
if field.IsValid() {
values[i] = field.Addr().Interface()
} else {
values[i] = &values[i]
}
}
return rows.Scan(values...)
}
示例8: addCmd
func (o *Opts) addCmd(sf reflect.StructField, val reflect.Value) {
if o.arglist != nil {
o.errorf("argslists and commands cannot be used together")
return
}
//requires address
switch sf.Type.Kind() {
case reflect.Ptr:
//if nil ptr, auto-create new struct
if val.IsNil() {
ptr := reflect.New(val.Type().Elem())
val.Set(ptr)
}
case reflect.Struct:
val = val.Addr()
}
name := sf.Tag.Get("name")
if name == "" || name == "!" {
name = camel2dash(sf.Name) //default to struct field name
}
// log.Printf("define cmd: %s =====", subname)
sub := fork(o, val)
sub.name = name
sub.help = sf.Tag.Get("help")
o.cmds[name] = sub
}
示例9: validateType
// validateType guarantees that the value is valid and assignable to the type.
func (s *state) validateType(value reflect.Value, typ reflect.Type) reflect.Value {
if !value.IsValid() {
switch typ.Kind() {
case reflect.Interface, reflect.Ptr, reflect.Chan, reflect.Map, reflect.Slice, reflect.Func:
// An untyped nil interface{}. Accept as a proper nil value.
// TODO: Can we delete the other types in this list? Should we?
value = reflect.Zero(typ)
default:
s.errorf("invalid value; expected %s", typ)
}
}
if !value.Type().AssignableTo(typ) {
// Does one dereference or indirection work? We could do more, as we
// do with method receivers, but that gets messy and method receivers
// are much more constrained, so it makes more sense there than here.
// Besides, one is almost always all you need.
switch {
case value.Kind() == reflect.Ptr && value.Type().Elem().AssignableTo(typ):
value = value.Elem()
case reflect.PtrTo(value.Type()).AssignableTo(typ) && value.CanAddr():
value = value.Addr()
default:
s.errorf("wrong type for value; expected %s; got %s", typ, value.Type())
}
}
return value
}
示例10: callCustom
// callCustom calls 'custom' with sv & dv. custom must be a conversion function.
func (c *Converter) callCustom(sv, dv, custom reflect.Value, scope *scope) error {
if !sv.CanAddr() {
sv2 := reflect.New(sv.Type())
sv2.Elem().Set(sv)
sv = sv2
} else {
sv = sv.Addr()
}
if !dv.CanAddr() {
if !dv.CanSet() {
return scope.errorf("can't addr or set dest.")
}
dvOrig := dv
dv := reflect.New(dvOrig.Type())
defer func() { dvOrig.Set(dv) }()
} else {
dv = dv.Addr()
}
args := []reflect.Value{sv, dv, reflect.ValueOf(scope)}
ret := custom.Call(args)[0].Interface()
// This convolution is necessary because nil interfaces won't convert
// to errors.
if ret == nil {
return nil
}
return ret.(error)
}
示例11: convert
// convert recursively copies sv into dv, calling an appropriate conversion function if
// one is registered.
func (c *Converter) convert(sv, dv reflect.Value, scope *scope) error {
dt, st := dv.Type(), sv.Type()
// Apply default values.
if fv, ok := c.defaultingFuncs[st]; ok {
if c.Debug != nil {
c.Debug.Logf("Applying defaults for '%v'", st)
}
args := []reflect.Value{sv.Addr()}
fv.Call(args)
}
// Convert sv to dv.
if fv, ok := c.conversionFuncs[typePair{st, dt}]; ok {
if c.Debug != nil {
c.Debug.Logf("Calling custom conversion of '%v' to '%v'", st, dt)
}
return c.callCustom(sv, dv, fv, scope)
}
if fv, ok := c.generatedConversionFuncs[typePair{st, dt}]; ok {
if c.Debug != nil {
c.Debug.Logf("Calling custom conversion of '%v' to '%v'", st, dt)
}
return c.callCustom(sv, dv, fv, scope)
}
return c.defaultConvert(sv, dv, scope)
}
示例12: GetValue
func (me Record) GetValue(key string, value reflect.Value) (err error) {
/*stringer, ok := value.Interface().(fmt.Stringer)
if ok {
me.Gut(key, stringer.String())
return
}*/
monolog.Debug("GetValue: %s %v", key, value)
switch value.Kind() {
case reflect.Int, reflect.Int32, reflect.Int64:
value.SetInt(int64(me.GetIntDefault(key, 0)))
case reflect.Uint, reflect.Uint32, reflect.Uint64:
value.SetUint(uint64(me.GetIntDefault(key, 0)))
case reflect.Float32, reflect.Float64:
f, err := me.GetFloat(key)
if err != nil {
return err
}
value.SetFloat(f)
case reflect.String:
s, ok := me.MayGet(key)
if !ok {
return fmt.Errorf("Could not get string for key %s", key)
}
value.SetString(s)
case reflect.Struct:
me.GetStruct(key+".", value.Addr().Interface())
default:
monolog.Warning("Don't know what to do with %v", value)
}
return nil
}
示例13: isAny
// isAny reports whether sv is a google.protobuf.Any message
func isAny(sv reflect.Value) bool {
type wkt interface {
XXX_WellKnownType() string
}
t, ok := sv.Addr().Interface().(wkt)
return ok && t.XXX_WellKnownType() == "Any"
}
示例14: reifyInto
func reifyInto(opts *options, to reflect.Value, from *Config) Error {
to = chaseValuePointers(to)
if to, ok := tryTConfig(to); ok {
return mergeConfig(opts, to.Addr().Interface().(*Config), from)
}
tTo := chaseTypePointers(to.Type())
k := tTo.Kind()
switch k {
case reflect.Map:
return reifyMap(opts, to, from)
case reflect.Struct:
return reifyStruct(opts, to, from)
case reflect.Slice, reflect.Array:
fopts := fieldOptions{opts: opts, tag: tagOptions{}, validators: nil}
v, err := reifyMergeValue(fopts, to, cfgSub{from})
if err != nil {
return err
}
to.Set(v)
return nil
}
return raiseInvalidTopLevelType(to.Interface())
}
示例15: autoMapType
func (this *databaseImplement) autoMapType(v reflect.Value) *core.Table {
t := v.Type()
table := core.NewEmptyTable()
if tb, ok := v.Interface().(tableName); ok {
table.Name = tb.TableName()
} else {
if v.CanAddr() {
if tb, ok = v.Addr().Interface().(tableName); ok {
table.Name = tb.TableName()
}
}
if table.Name == "" {
table.Name = this.TableMapper.Obj2Table(t.Name())
}
}
table.Type = t
for i := 0; i < t.NumField(); i++ {
tag := t.Field(i).Tag
ormTagStr := tag.Get("xorm")
if ormTagStr == "-" || ormTagStr == "<-" {
continue
}
col := &core.Column{FieldName: t.Field(i).Name, Nullable: true, IsPrimaryKey: false,
IsAutoIncrement: false, MapType: core.TWOSIDES, Indexes: make(map[string]bool)}
col.Name = this.ColumnMapper.Obj2Table(t.Field(i).Name)
table.AddColumn(col)
}
return table
}