本文整理汇总了Golang中reflect.Value函数的典型用法代码示例。如果您正苦于以下问题:Golang Value函数的具体用法?Golang Value怎么用?Golang Value使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Value函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Emit
/* Emit a signal with the given parameters */
func Emit(signal Signal, params ...Object) {
signalID := signal.id
if registeredSlot[signalID] == nil {
registeredSlot[signalID] = []SlotVal{}
}
slots := registeredSlot[signalID]
for _, slot := range slots {
slottype := reflect.Value(slot).Type()
paramlist := []reflect.Value{}
/* Try to match number of parameters */
if slottype.IsVariadic() {
for _, param := range params {
paramlist = append(paramlist, reflect.ValueOf(param))
}
} else {
for i := 0; i < slottype.NumIn(); i++ {
paramlist = append(paramlist, reflect.ValueOf(params[i]))
}
}
go func(slot SlotVal, paramlist []reflect.Value) {
defer crashHandler("Fatal: Cannot call slot/function")
reflect.Value(slot).Call(paramlist)
}(slot, paramlist)
}
}
示例2: createClientFactory
// create one client instance
func (zt *ZooThrift) createClientFactory() (interface{}, error) {
transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())
protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()
address := zt.provider.Selector()
if address != "" {
transport, err := thrift.NewTSocket(address)
if err != nil {
return nil, err
}
useTransport := transportFactory.GetTransport(transport)
client := reflect.ValueOf(zt.Service)
mutable := client.Elem()
mutable.FieldByName("Transport").Set(reflect.Value(reflect.ValueOf(useTransport)))
mutable.FieldByName("ProtocolFactory").Set(reflect.Value(reflect.ValueOf(protocolFactory)))
mutable.FieldByName("InputProtocol").Set(reflect.Value(reflect.ValueOf(protocolFactory.GetProtocol(useTransport))))
mutable.FieldByName("OutputProtocol").Set(reflect.Value(reflect.ValueOf(protocolFactory.GetProtocol(useTransport))))
mutable.FieldByName("SeqId").SetInt(0)
if err := transport.Open(); err != nil {
return nil, err
}
return zt.Service, nil
} else {
return nil, ErrSerAddress
}
}
示例3: TypeOf
func TypeOf(expr Sexp) SexpStr {
v := ""
switch e := expr.(type) {
case SexpRaw:
v = "raw"
case *SexpArray:
v = "array"
case *SexpInt:
v = "int64"
case SexpStr:
v = "string"
case SexpChar:
v = "char"
case SexpFloat:
v = "float64"
case *SexpHash:
v = e.TypeName
case SexpPair:
v = "list"
case SexpSymbol:
v = "symbol"
case *SexpFunction:
v = "func"
case SexpSentinel:
v = "nil"
case SexpTime:
v = "time.Time"
case *RegisteredType:
v = "regtype"
case *SexpPointer:
v = e.MyType.RegisteredName
case SexpReflect:
rt := expr.Type()
if rt != nil {
return SexpStr{S: rt.RegisteredName}
}
//v = reflect.Value(e).Type().Name()
//if v == "Ptr" {
// v = reflect.Value(e).Type().Elem().Kind().String()
//}
kind := reflect.Value(e).Type().Kind()
if kind == reflect.Ptr {
v = reflect.Value(e).Elem().Type().Name()
} else {
P("kind = %v", kind)
v = "reflect.Value"
}
default:
fmt.Printf("\n error: unknown type: %T in '%#v'\n", e, e)
}
return SexpStr{S: v}
}
示例4: simplifyBinaryChildExpr
// Returns a printable interface{} which replaces constant expressions with their constants
func simplifyBinaryChildExpr(parent *BinaryExpr, expr, other Expr) interface{} {
op := parent.Op()
if expr.IsConst() {
// This mess is due to automatic type conversions in binary expressions.
// It will disappear once the AST is retyped as a first step
v := expr.Const()
eT := expr.KnownType()[0]
oT := other.KnownType()
if len(oT) == 1 {
ct, eok := eT.(ConstType)
if eok {
// Separate case for shifts
if op == token.SHL || op == token.SHR {
// Don't touch the shifted operand
if parent.X == expr {
return expr
} else if isUnsignedInt(eT) {
c, _ := promoteConstToTyped(ct, constValue(expr.Const()), uintType, expr)
return reflect.Value(c).Interface()
}
} else if _, ook := oT[0].(ConstType); !ook {
if eT == ConstNil {
return "nil"
}
c, _ := promoteConstToTyped(ct, constValue(expr.Const()), oT[0], expr)
if reflect.Value(c).IsValid() {
eT = oT[0]
v = reflect.Value(c)
} else if _, ok := eT.(ConstRuneType); ok {
// For mismatched type errors
eT = RuneType
}
}
}
}
return sprintConstValue(eT, v, false)
}
expr = skipSuperfluousParens(expr)
if p, ok := expr.(*ParenExpr); ok {
// Remove parens all together from 1 + (2 * 3)
if b, ok := p.X.(*BinaryExpr); ok {
op := b.Op()
if op.Precedence() > op.Precedence() {
return p.X
}
}
}
return expr
}
示例5: checkArrayIndex
func checkArrayIndex(expr ast.Expr, env Env) (aexpr Expr, i int, ok bool, checkErrs []error) {
aexpr, checkErrs = CheckExpr(expr, env)
if !aexpr.IsConst() {
return aexpr, 0, false, checkErrs
}
t := aexpr.KnownType()[0]
var ii int64
if ct, ok := t.(ConstType); ok {
c, moreErrs := promoteConstToTyped(ct, constValue(aexpr.Const()), intType, aexpr)
if moreErrs != nil {
checkErrs = append(checkErrs, moreErrs...)
}
v := reflect.Value(c)
if v.IsValid() {
ii = v.Int()
} else {
return aexpr, 0, false, checkErrs
}
} else {
switch t.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
ii = aexpr.Const().Int()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
ii = int64(aexpr.Const().Uint())
default:
return aexpr, 0, false, checkErrs
}
}
// The limit of 2^31-1 is derived from the gc implementation,
// which seems to use this definition whilst type checking.
// The actual definition is the "largest value representable by an int"
return aexpr, int(ii), 0 <= ii && ii <= 0x7fffffff, checkErrs
}
示例6: evalInteger
// Eval a node and cast it to an int. expr must be a *ConstNumber or integral type
func evalInteger(expr Expr, env Env) (int, error) {
if expr.IsConst() {
x := expr.Const()
if ct, ok := expr.KnownType()[0].(ConstType); ok {
cx, _ := promoteConstToTyped(ct, constValue(x), intType, expr)
return int(reflect.Value(cx).Int()), nil
} else {
panic(dytc("const bool or string evaluated as int"))
}
} else {
xs, err := EvalExpr(expr, env)
if err != nil {
return 0, err
}
x := xs[0]
switch x.Type().Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return int(x.Int()), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return int(x.Uint()), nil
default:
panic(dytc("non-integral type evaluated as int"))
}
}
}
示例7: Swap
func (s intSlice) Swap(i, j int) {
v := reflect.Value(s)
ii := v.Index(i).Uint()
jj := v.Index(j).Uint()
v.Index(i).SetUint(jj)
v.Index(j).SetUint(ii)
}
示例8: parseInteger
func parseInteger(v reflect.Value, b []byte) error {
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
default:
return &TypeError{TagInteger, v.Kind()}
}
if len(b) > 8 {
return errors.New("value does not fit in a int64")
}
var n int64
for i, v := range b {
shift := uint((len(b) - i - 1) * 8)
if i == 0 {
if v&0x80 != 0 {
n -= 0x80 << shift
v &= 0x7f
}
}
n += int64(v) << shift
}
reflect.Value(v).SetInt(n)
return nil
}
示例9: checkCompositeLitMap
func checkCompositeLitMap(ctx *Ctx, lit *CompositeLit, t reflect.Type, env *Env) (*CompositeLit, []error) {
var errs, moreErrs []error
kT := t.Key()
// Don't check for duplicate interface{} keys. This is a gc bug
// http://code.google.com/p/go/issues/detail?id=7214
var seen map[interface{}]bool
if kT.Kind() != reflect.Interface {
seen = make(map[interface{}]bool, len(lit.Elts))
}
eltT := t.Elem()
for i := range lit.Elts {
if kv, ok := lit.Elts[i].(*ast.KeyValueExpr); !ok {
lit.Elts[i], moreErrs = CheckExpr(ctx, lit.Elts[i], env)
if moreErrs != nil {
errs = append(errs, moreErrs...)
}
errs = append(errs, ErrMissingMapKey{at(ctx, lit.Elts[i])})
} else {
lit.Elts[i] = &KeyValueExpr{KeyValueExpr: kv}
k, ok, moreErrs := checkExprAssignableTo(ctx, kv.Key, kT, env)
if !ok {
if len(k.KnownType()) != 0 {
kF := fakeCheckExpr(kv.Key, env)
kF.setKnownType(knownType(k.KnownType()))
errs = append(errs, ErrBadMapKey{at(ctx, kF), kT})
}
} else {
errs = append(errs, moreErrs...)
}
kv.Key = k
if seen != nil && k.IsConst() {
var constKey interface{}
if k.KnownType()[0] == ConstNil {
constKey = nil
} else if cT, ok := k.KnownType()[0].(ConstType); ok {
c, _ := promoteConstToTyped(ctx, cT, constValue(k.Const()),
cT.DefaultPromotion(), k)
constKey = reflect.Value(c).Interface()
} else {
constKey = k.Const().Interface()
}
if seen[constKey] {
errs = append(errs, ErrDuplicateMapKey{at(ctx, kv.Key)})
}
seen[constKey] = true
}
v, moreErrs := checkMapValue(ctx, kv.Value, eltT, env)
if moreErrs != nil {
errs = append(errs, moreErrs...)
}
kv.Value = v
}
}
return lit, errs
}
示例10: evalConstTypedBinaryExpr
func evalConstTypedBinaryExpr(binary *BinaryExpr, xexpr, yexpr Expr) (constValue, []error) {
// These are known not to be ConstTypes
xt := xexpr.KnownType()[0]
yt := yexpr.KnownType()[0]
op := binary.Op()
// Check that the types are compatible, handling the special alias type for runes
// For the sake of error messages, for expressions involving int32 and rune, the
// resulting type is that of the left operand
var zt reflect.Type
if xt == yt {
zt = xt
} else if xt == RuneType && yt == RuneType.Type || xt == ByteType && yt == ByteType.Type {
zt = RuneType
} else if yt == RuneType && xt == RuneType.Type || yt == ByteType && yt == ByteType.Type {
zt = xt
} else {
return constValue{}, []error{ErrInvalidBinaryOperation{binary}}
}
x, xok := convertTypedToConstNumber(xexpr.Const())
y, yok := convertTypedToConstNumber(yexpr.Const())
if xok && yok {
z, errs := evalConstBinaryNumericExpr(binary, x, y)
if isBooleanOp(op) {
return constValue(z), errs
}
if errs != nil {
if _, ok := errs[0].(ErrInvalidBinaryOperation); ok {
// This happens if the operator is not defined on x and y
return constValue(z), errs
}
}
from := reflect.Value(z).Interface().(*ConstNumber).Type
r, moreErrs := promoteConstToTyped(from, z, zt, binary)
return constValue(r), append(errs, moreErrs...)
} else if !xok && !yok {
switch zt.Kind() {
case reflect.String:
xstring := xexpr.Const().String()
ystring := yexpr.Const().String()
z, errs := evalConstBinaryStringExpr(binary, xstring, ystring)
if isBooleanOp(op) {
return constValue(z), errs
}
r, moreErrs := promoteConstToTyped(ConstString, z, zt, binary)
return constValue(r), append(errs, moreErrs...)
case reflect.Bool:
xbool := xexpr.Const().Bool()
ybool := yexpr.Const().Bool()
z, errs := evalConstBinaryBoolExpr(binary, xbool, ybool)
return constValue(z), errs
}
}
panic("go-interactive: impossible")
}
示例11: GetByteArrayLen
// TODO: document me
//
func GetByteArrayLen(v reflect.Value) (len int, ok bool) {
switch v.Kind() {
case reflect.Array, reflect.Slice:
aosv := reflect.Value(v)
return aosv.Len(), true
}
return
}
示例12: tagOf
func (a structAdaptor) tagOf(key string) reflect.StructTag {
v := reflect.Value(a)
field, ok := v.Type().FieldByName(key)
if ok {
return field.Tag
}
return ""
}
示例13: SexpString
func (r *SexpReflect) SexpString(ps *PrintState) string {
Q("in SexpReflect.SexpString(indent); top; type = %T", r)
if reflect.Value(r.Val).Type().Kind() == reflect.Ptr {
iface := reflect.Value(r.Val).Interface()
switch iface.(type) {
case *string:
return fmt.Sprintf("`%v`", reflect.Value(r.Val).Elem().Interface())
default:
return fmt.Sprintf("%v", reflect.Value(r.Val).Elem().Interface())
}
}
iface := reflect.Value(r.Val).Interface()
Q("in SexpReflect.SexpString(indent); type = %T", iface)
switch iface.(type) {
default:
return fmt.Sprintf("%v", iface)
}
}
示例14: keys
func (a structAdaptor) keys() []string {
v := reflect.Value(a)
t := v.Type()
keys := make([]string, t.NumField())
for i := range keys {
keys[i] = t.Field(i).Name
}
return keys
}
示例15: sprintUntypedConstAsTyped
// For display purposes only, display untyped const nodes as they would be
// displayed as a typed const node.
func sprintUntypedConstAsTyped(expr Expr) string {
if !expr.IsConst() {
return expr.String()
}
switch expr.KnownType()[0].(type) {
case ConstRuneType:
return sprintConstValue(RuneType, reflect.Value(expr.Const()), false)
default:
return expr.String()
}
}