本文整理汇总了Golang中reflect.Value.Interface方法的典型用法代码示例。如果您正苦于以下问题:Golang Value.Interface方法的具体用法?Golang Value.Interface怎么用?Golang Value.Interface使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类reflect.Value
的用法示例。
在下文中一共展示了Value.Interface方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: rawScanStruct
// scan the value by fields, and set to v
func rawScanStruct(v reflect.Value, fields []string, scanner rowScaner) (err error) {
if v.IsNil() {
e := fmt.Sprintf("struct can not be nil, but got %#v", v.Interface())
return errors.New(e)
}
dest := make([]interface{}, len(fields))
for v.Kind() == reflect.Ptr {
v = v.Elem()
}
// Loop over column names and find field in s to bind to
// based on column name. all returned columns must match
// a field in the s struct
for x, fieldName := range fields {
f := v.FieldByName(fieldName)
if f == zeroVal {
e := fmt.Sprintf("Scanner: No field %s in type %s",
fieldName, v.Type())
return errors.New(e)
} else {
dest[x] = f.Addr().Interface()
}
}
err = scanner.Scan(dest...)
return
}
示例2: ConfigItemWhenValidation
// ConfigItemWhenValidation will validate that the when element of a config item is in a valid format and references other valid, created objects.
func ConfigItemWhenValidation(v *validator.Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
root, ok := topStruct.Interface().(*RootConfig)
if !ok {
// this is an issue with the code and really should be a panic
return true
}
if fieldKind != reflect.String {
// this is an issue with the code and really should be a panic
return true
}
var whenValue string
whenValue = field.String()
if whenValue == "" {
return true
}
splitString := "="
if strings.Contains(whenValue, "!=") {
splitString = "!="
}
parts := strings.SplitN(whenValue, splitString, 2)
if len(parts) >= 2 {
whenValue = parts[0]
}
return configItemExists(whenValue, root)
}
示例3: ContainerNameUnique
func ContainerNameUnique(v *validator.Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
if fieldKind != reflect.String {
return true
}
containerName := field.String()
if containerName == "" {
return true
}
if hasReplTemplate(field) {
// all bets are off
return true
}
root, ok := topStruct.Interface().(*RootConfig)
if !ok {
// this is an issue with the code and really should be a panic
return true
}
currentContainer := getCurrentContainer(currentStructOrField)
if currentContainer == nil {
// this is an issue with the code and really should be a panic
return true
}
container := getContainerFromName(containerName, currentContainer, root)
if container != nil {
return false
}
return true
}
示例4: SqlTag
func (d *commonDialect) SqlTag(value reflect.Value, size int) string {
switch value.Kind() {
case reflect.Bool:
return "BOOLEAN"
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr:
return "INTEGER"
case reflect.Int64, reflect.Uint64:
return "BIGINT"
case reflect.Float32, reflect.Float64:
return "FLOAT"
case reflect.String:
if size > 0 && size < 65532 {
return fmt.Sprintf("VARCHAR(%d)", size)
} else {
return "VARCHAR(65532)"
}
case reflect.Struct:
if value.Type() == timeType {
return "TIMESTAMP"
}
default:
if _, ok := value.Interface().([]byte); ok {
if size > 0 && size < 65532 {
return fmt.Sprintf("BINARY(%d)", size)
} else {
return "BINARY(65532)"
}
}
}
panic(fmt.Sprintf("invalid sql type %s (%s) for commonDialect", value.Type().Name(), value.Kind().String()))
}
示例5: 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
}
示例6: SqlTag
func (mssql) SqlTag(value reflect.Value, size int, autoIncrease bool) string {
switch value.Kind() {
case reflect.Bool:
return "bit"
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr:
if autoIncrease {
return "int IDENTITY(1,1)"
}
return "int"
case reflect.Int64, reflect.Uint64:
if autoIncrease {
return "bigint IDENTITY(1,1)"
}
return "bigint"
case reflect.Float32, reflect.Float64:
return "float"
case reflect.String:
if size > 0 && size < 65532 {
return fmt.Sprintf("nvarchar(%d)", size)
}
return "text"
case reflect.Struct:
if _, ok := value.Interface().(time.Time); ok {
return "datetime2"
}
default:
if _, ok := value.Interface().([]byte); ok {
if size > 0 && size < 65532 {
return fmt.Sprintf("varchar(%d)", size)
}
return "text"
}
}
panic(fmt.Sprintf("invalid sql type %s (%s) for mssql", value.Type().Name(), value.Kind().String()))
}
示例7: isZero
// IsZero returns true when the value is a zero for the type
func isZero(data reflect.Value) bool {
if !data.CanInterface() {
return true
}
tpe := data.Type()
return reflect.DeepEqual(data.Interface(), reflect.Zero(tpe).Interface())
}
示例8: 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
}
示例9: haveError
func (t *SpecTest) haveError(fn interface{}) (bool, error) {
t.doDebug(func() { t.Logf("Function %#v has an error", fn) })
var errval reflect.Value
switch fn.(type) {
case FnCall:
fncall := fn.(FnCall)
fntyp := fncall.fn.Type()
if !reflect.TypeOf(errors.New("error")).AssignableTo(fntyp.Out(fntyp.NumOut() - 1)) {
return false, errors.New("HaveError function call's last Value must be os.Error")
}
errval = fncall.out[len(fncall.out)-1]
default:
return false, errors.New("HaveError needs a function call Value")
}
var fnerr error
switch v := errval.Interface(); v.(type) {
case nil:
fnerr = nil
case error:
fnerr = v.(error)
default:
return false, errors.New("Function call can not have error")
}
return fnerr != nil, nil
}
示例10: parse_unmarshaler
func (d *decoder) parse_unmarshaler(v reflect.Value) bool {
m, ok := v.Interface().(Unmarshaler)
if !ok {
// T doesn't work, try *T
if v.Kind() != reflect.Ptr && v.CanAddr() {
m, ok = v.Addr().Interface().(Unmarshaler)
if ok {
v = v.Addr()
}
}
}
if ok && (v.Kind() != reflect.Ptr || !v.IsNil()) {
if d.read_one_value() {
err := m.UnmarshalBencode(d.buf.Bytes())
d.buf.Reset()
if err != nil {
panic(&UnmarshalerError{v.Type(), err})
}
return true
}
d.buf.Reset()
}
return false
}
示例11: callSliceRequired
// callSliceRequired returns true if CallSlice is required instead of Call.
func callSliceRequired(param reflect.Type, val reflect.Value) bool {
vt := val.Type()
for param.Kind() == reflect.Slice {
if val.Kind() == reflect.Interface {
val = reflect.ValueOf(val.Interface())
vt = val.Type()
}
if vt.Kind() != reflect.Slice {
return false
}
vt = vt.Elem()
if val.Kind() != reflect.Invalid {
if val.Len() > 0 {
val = val.Index(0)
} else {
val = reflect.Value{}
}
}
param = param.Elem()
}
return true
}
示例12: getColumnsAsScope
func (scope *Scope) getColumnsAsScope(column string) *Scope {
values := scope.IndirectValue()
switch values.Kind() {
case reflect.Slice:
modelType := values.Type().Elem()
if modelType.Kind() == reflect.Ptr {
modelType = modelType.Elem()
}
fieldStruct, _ := modelType.FieldByName(column)
var columns reflect.Value
if fieldStruct.Type.Kind() == reflect.Slice || fieldStruct.Type.Kind() == reflect.Ptr {
columns = reflect.New(reflect.SliceOf(reflect.PtrTo(fieldStruct.Type.Elem()))).Elem()
} else {
columns = reflect.New(reflect.SliceOf(reflect.PtrTo(fieldStruct.Type))).Elem()
}
for i := 0; i < values.Len(); i++ {
column := reflect.Indirect(values.Index(i)).FieldByName(column)
if column.Kind() == reflect.Ptr {
column = column.Elem()
}
if column.Kind() == reflect.Slice {
for i := 0; i < column.Len(); i++ {
columns = reflect.Append(columns, column.Index(i).Addr())
}
} else {
columns = reflect.Append(columns, column.Addr())
}
}
return scope.New(columns.Interface())
case reflect.Struct:
return scope.New(values.FieldByName(column).Addr().Interface())
}
return nil
}
示例13: 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
}
示例14: ext
func (f encFnInfo) ext(rv reflect.Value) {
// if this is a struct|array and it was addressable, then pass the address directly (not the value)
if k := rv.Kind(); (k == reflect.Struct || k == reflect.Array) && rv.CanAddr() {
rv = rv.Addr()
}
f.ee.EncodeExt(rv.Interface(), f.xfTag, f.xfFn, f.e)
}
示例15: isZero
func isZero(v reflect.Value) bool {
switch v.Kind() {
case reflect.Func, reflect.Map, reflect.Slice:
return v.IsNil()
case reflect.Array:
z := true
for i := 0; i < v.Len(); i++ {
z = z && isZero(v.Index(i))
}
return z
case reflect.Struct:
if v.Type() == reflect.TypeOf(t) {
if v.Interface().(time.Time).IsZero() {
return true
}
return false
}
z := true
for i := 0; i < v.NumField(); i++ {
z = z && isZero(v.Field(i))
}
return z
}
// Compare other types directly:
z := reflect.Zero(v.Type())
return v.Interface() == z.Interface()
}