本文整理汇总了Golang中reflect.Value.Index方法的典型用法代码示例。如果您正苦于以下问题:Golang Value.Index方法的具体用法?Golang Value.Index怎么用?Golang Value.Index使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类reflect.Value
的用法示例。
在下文中一共展示了Value.Index方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: encodeValue
func encodeValue(buf []byte, prefix, name string, fv reflect.Value, inArray, arrayTable bool) ([]byte, error) {
switch t := fv.Interface().(type) {
case Marshaler:
b, err := t.MarshalTOML()
if err != nil {
return nil, err
}
return appendNewline(append(appendKey(buf, name, inArray, arrayTable), b...), inArray, arrayTable), nil
case time.Time:
return appendNewline(encodeTime(appendKey(buf, name, inArray, arrayTable), t), inArray, arrayTable), nil
}
switch fv.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return appendNewline(encodeInt(appendKey(buf, name, inArray, arrayTable), fv.Int()), inArray, arrayTable), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return appendNewline(encodeUint(appendKey(buf, name, inArray, arrayTable), fv.Uint()), inArray, arrayTable), nil
case reflect.Float32, reflect.Float64:
return appendNewline(encodeFloat(appendKey(buf, name, inArray, arrayTable), fv.Float()), inArray, arrayTable), nil
case reflect.Bool:
return appendNewline(encodeBool(appendKey(buf, name, inArray, arrayTable), fv.Bool()), inArray, arrayTable), nil
case reflect.String:
return appendNewline(encodeString(appendKey(buf, name, inArray, arrayTable), fv.String()), inArray, arrayTable), nil
case reflect.Slice, reflect.Array:
ft := fv.Type().Elem()
for ft.Kind() == reflect.Ptr {
ft = ft.Elem()
}
if ft.Kind() == reflect.Struct {
name := tableName(prefix, name)
var err error
for i := 0; i < fv.Len(); i++ {
if buf, err = marshal(append(append(append(buf, '[', '['), name...), ']', ']', '\n'), name, fv.Index(i), false, true); err != nil {
return nil, err
}
}
return buf, nil
}
buf = append(appendKey(buf, name, inArray, arrayTable), '[')
var err error
for i := 0; i < fv.Len(); i++ {
if i != 0 {
buf = append(buf, ',')
}
if buf, err = encodeValue(buf, prefix, name, fv.Index(i), true, false); err != nil {
return nil, err
}
}
return appendNewline(append(buf, ']'), inArray, arrayTable), nil
case reflect.Struct:
name := tableName(prefix, name)
return marshal(append(append(append(buf, '['), name...), ']', '\n'), name, fv, inArray, arrayTable)
case reflect.Interface:
var err error
if buf, err = encodeInterface(appendKey(buf, name, inArray, arrayTable), fv.Interface()); err != nil {
return nil, err
}
return appendNewline(buf, inArray, arrayTable), nil
}
return nil, fmt.Errorf("toml: marshal: unsupported type %v", fv.Kind())
}
示例2: readArrayDocTo
func (d *decoder) readArrayDocTo(out reflect.Value) {
end := int(d.readInt32())
end += d.i - 4
if end <= d.i || end > len(d.in) || d.in[end-1] != '\x00' {
corrupted()
}
i := 0
l := out.Len()
for d.in[d.i] != '\x00' {
if i >= l {
panic("Length mismatch on array field")
}
kind := d.readByte()
for d.i < end && d.in[d.i] != '\x00' {
d.i++
}
if d.i >= end {
corrupted()
}
d.i++
d.readElemTo(out.Index(i), kind)
if d.i >= end {
corrupted()
}
i++
}
if i != l {
panic("Length mismatch on array field")
}
d.i++ // '\x00'
if d.i != end {
corrupted()
}
}
示例3: 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()
}
示例4: 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
}
示例5: convertSliceToCFArrayHelper
func convertSliceToCFArrayHelper(slice reflect.Value, helper func(reflect.Value) (cfTypeRef, error)) (C.CFArrayRef, error) {
if slice.Len() == 0 {
// short-circuit 0, so we can assume plists[0] is valid later
return C.CFArrayCreate(nil, nil, 0, nil), nil
}
// assume slice is a slice/array, because our caller already checked
plists := make([]cfTypeRef, slice.Len())
// defer the release
defer func() {
for _, cfObj := range plists {
cfRelease(cfObj)
}
}()
// convert the slice
for i := 0; i < slice.Len(); i++ {
cfType, err := helper(slice.Index(i))
if err != nil {
return nil, err
}
plists[i] = cfType
}
// create the array
callbacks := (*C.CFArrayCallBacks)(&C.kCFTypeArrayCallBacks)
return C.CFArrayCreate(nil, (*unsafe.Pointer)(&plists[0]), C.CFIndex(len(plists)), callbacks), nil
}
示例6: normalizeArray
func normalizeArray(
opts *options,
tagOpts tagOptions,
ctx context,
v reflect.Value,
) (value, Error) {
l := v.Len()
out := make([]value, 0, l)
cfg := New()
cfg.metadata = opts.meta
cfg.ctx = ctx
val := cfgSub{cfg}
for i := 0; i < l; i++ {
idx := fmt.Sprintf("%v", i)
ctx := context{
parent: val,
field: idx,
}
tmp, err := normalizeValue(opts, tagOpts, ctx, v.Index(i))
if err != nil {
return nil, err
}
out = append(out, tmp)
}
cfg.fields.a = out
return val, nil
}
示例7: readSlice
func readSlice(in io.Reader, obj reflect.Value) {
len := int(readUint(in))
obj.Set(reflect.MakeSlice(obj.Type(), len, len))
for i := 0; i < len; i++ {
readAny(in, obj.Index(i))
}
}
示例8: checkWhereArray
// checkWhereArray handles the where-matching logic when the seqv value is an
// Array or Slice.
func checkWhereArray(
seqv, kv, mv reflect.Value,
path []string, op string) (interface{}, error) {
rv := reflect.MakeSlice(seqv.Type(), 0, 0)
for i := 0; i < seqv.Len(); i++ {
var vvv reflect.Value
rvv := seqv.Index(i)
if kv.Kind() == reflect.String {
vvv = rvv
for _, elemName := range path {
var err error
vvv, err = evaluateSubElem(vvv, elemName)
if err != nil {
return nil, err
}
}
} else {
vv, _ := indirect(rvv)
if vv.Kind() == reflect.Map &&
kv.Type().AssignableTo(vv.Type().Key()) {
vvv = vv.MapIndex(kv)
}
}
if ok, err := checkCondition(vvv, mv, op); ok {
rv = reflect.Append(rv, rvv)
} else if err != nil {
return nil, err
}
}
return rv.Interface(), nil
}
示例9: isZero
// isZero reports whether the value is the zero of its type.
func isZero(val reflect.Value) bool {
switch val.Kind() {
case reflect.Array:
for i := 0; i < val.Len(); i++ {
if !isZero(val.Index(i)) {
return false
}
}
return true
case reflect.Map, reflect.Slice, reflect.String:
return val.Len() == 0
case reflect.Bool:
return !val.Bool()
case reflect.Complex64, reflect.Complex128:
return val.Complex() == 0
case reflect.Chan, reflect.Func, reflect.Ptr:
return val.IsNil()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return val.Int() == 0
case reflect.Float32, reflect.Float64:
return val.Float() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return val.Uint() == 0
case reflect.Struct:
for i := 0; i < val.NumField(); i++ {
if !isZero(val.Field(i)) {
return false
}
}
return true
}
panic("unknown type in isZero " + val.Type().String())
}
示例10: visit
// visit calls the visitor function for each string it finds, and will descend
// recursively into structures and slices. If any visitor returns an error then
// the search will stop and that error will be returned.
func visit(path string, v reflect.Value, t reflect.Type, fn visitor) error {
switch v.Kind() {
case reflect.String:
return fn(path, v)
case reflect.Struct:
for i := 0; i < v.NumField(); i++ {
vf := v.Field(i)
tf := t.Field(i)
newPath := fmt.Sprintf("%s.%s", path, tf.Name)
if err := visit(newPath, vf, tf.Type, fn); err != nil {
return err
}
}
case reflect.Slice:
for i := 0; i < v.Len(); i++ {
vi := v.Index(i)
ti := vi.Type()
newPath := fmt.Sprintf("%s[%d]", path, i)
if err := visit(newPath, vi, ti, fn); err != nil {
return err
}
}
}
return nil
}
示例11: slicev
func (e *encoder) slicev(tag string, in reflect.Value) {
var ctag *C.yaml_char_t
var free func()
var cimplicit C.int
if tag != "" {
ctag, free = ystr(tag)
defer free()
cimplicit = 0
} else {
cimplicit = 1
}
cstyle := C.yaml_sequence_style_t(C.YAML_BLOCK_SEQUENCE_STYLE)
if e.flow {
e.flow = false
cstyle = C.YAML_FLOW_SEQUENCE_STYLE
}
C.yaml_sequence_start_event_initialize(&e.event, nil, ctag, cimplicit,
cstyle)
e.emit()
n := in.Len()
for i := 0; i < n; i++ {
e.marshal("", in.Index(i))
}
C.yaml_sequence_end_event_initialize(&e.event)
e.emit()
}
示例12: encodeArray
func encodeArray(v reflect.Value) interface{} {
n := node{}
for i := 0; i < v.Len(); i++ {
n[strconv.Itoa(i)] = encodeValue(v.Index(i))
}
return n
}
示例13: Size
func (f *Field) Size(val reflect.Value, options *Options) int {
typ := f.Type.Resolve(options)
size := 0
if typ == Struct {
vals := []reflect.Value{val}
if f.Slice {
vals = make([]reflect.Value, val.Len())
for i := 0; i < val.Len(); i++ {
vals[i] = val.Index(i)
}
}
for _, val := range vals {
size += f.Fields.Sizeof(val, options)
}
} else if typ == Pad {
size = f.Len
} else if f.Slice || f.kind == reflect.String {
length := val.Len()
if f.Len > 1 {
length = f.Len
}
size = length * typ.Size()
} else if typ == CustomType {
return val.Addr().Interface().(Custom).Size(options)
} else {
size = typ.Size()
}
align := options.ByteAlign
if align > 0 && size < align {
size = align
}
return size
}
示例14: Unpack
func (f *Field) Unpack(buf []byte, val reflect.Value, length int, options *Options) error {
typ := f.Type.Resolve(options)
if typ == Pad || f.kind == reflect.String {
if typ == Pad {
return nil
} else {
val.SetString(string(buf))
return nil
}
} else if f.Slice {
if val.Cap() < length {
val.Set(reflect.MakeSlice(val.Type(), length, length))
} else if val.Len() < length {
val.Set(val.Slice(0, length))
}
// special case byte slices for performance
if !f.Array && typ == Uint8 && f.defType == Uint8 {
copy(val.Bytes(), buf[:length])
return nil
}
pos := 0
size := typ.Size()
for i := 0; i < length; i++ {
if err := f.unpackVal(buf[pos:pos+size], val.Index(i), 1, options); err != nil {
return err
}
pos += size
}
return nil
} else {
return f.unpackVal(buf, val, length, options)
}
}
示例15: convertToFloat64
func convertToFloat64(depth int, v reflect.Value) (float64, bool) {
if v.Kind() == reflect.Interface {
v = v.Elem()
}
switch v.Kind() {
case reflect.Float32, reflect.Float64:
return v.Float(), true
case reflect.Int16, reflect.Int8, reflect.Int, reflect.Int32, reflect.Int64:
return float64(v.Int()), true
case reflect.String:
s := v.String()
var f float64
var err error
if strings.HasPrefix(s, "0x") {
f, err = strconv.ParseFloat(s, 64)
} else {
f, err = strconv.ParseFloat(s, 64)
}
if err == nil {
return float64(f), true
}
if depth == 0 {
s = intStrReplacer.Replace(s)
rv := reflect.ValueOf(s)
return convertToFloat64(1, rv)
}
case reflect.Slice:
// Should we grab first one? Or Error?
//u.Warnf("ToFloat() but is slice?: %T first=%v", v, v.Index(0))
return convertToFloat64(0, v.Index(0))
default:
//u.Warnf("Cannot convert type? %v", v.Kind())
}
return math.NaN(), false
}