本文整理汇总了Golang中reflect.Value.SetBytes方法的典型用法代码示例。如果您正苦于以下问题:Golang Value.SetBytes方法的具体用法?Golang Value.SetBytes怎么用?Golang Value.SetBytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类reflect.Value
的用法示例。
在下文中一共展示了Value.SetBytes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: decodeSliceValue
func decodeSliceValue(dst reflect.Value, f []byte) error {
elemType := dst.Type().Elem()
switch elemType.Kind() {
case reflect.Uint8:
b, err := decodeBytes(f)
if err != nil {
return err
}
dst.SetBytes(b)
return nil
case reflect.String:
s, err := decodeStringSlice(f)
if err != nil {
return err
}
dst.Set(reflect.ValueOf(s))
return nil
case reflect.Int:
s, err := decodeIntSlice(f)
if err != nil {
return err
}
dst.Set(reflect.ValueOf(s))
return nil
case reflect.Int64:
s, err := decodeInt64Slice(f)
if err != nil {
return err
}
dst.Set(reflect.ValueOf(s))
return nil
}
return errorf("pg: unsupported dst: %s", dst)
}
示例2: kSlice
func (f *decFnInfo) kSlice(rv reflect.Value) {
// A slice can be set from a map or array in stream.
currEncodedType := f.dd.currentEncodedType()
switch currEncodedType {
case valueTypeBytes, valueTypeString:
if f.ti.rtid == uint8SliceTypId || f.ti.rt.Elem().Kind() == reflect.Uint8 {
if bs2, changed2 := f.dd.decodeBytes(rv.Bytes()); changed2 {
rv.SetBytes(bs2)
}
return
}
}
if shortCircuitReflectToFastPath && rv.CanAddr() {
switch f.ti.rtid {
case intfSliceTypId:
f.d.decSliceIntf(rv.Addr().Interface().(*[]interface{}), currEncodedType, f.array)
return
case uint64SliceTypId:
f.d.decSliceUint64(rv.Addr().Interface().(*[]uint64), currEncodedType, f.array)
return
case int64SliceTypId:
f.d.decSliceInt64(rv.Addr().Interface().(*[]int64), currEncodedType, f.array)
return
case strSliceTypId:
f.d.decSliceStr(rv.Addr().Interface().(*[]string), currEncodedType, f.array)
return
}
}
containerLen, containerLenS := decContLens(f.dd, currEncodedType)
// an array can never return a nil slice. so no need to check f.array here.
if rv.IsNil() {
rv.Set(reflect.MakeSlice(f.ti.rt, containerLenS, containerLenS))
}
if containerLen == 0 {
return
}
if rvcap, rvlen := rv.Len(), rv.Cap(); containerLenS > rvcap {
if f.array { // !rv.CanSet()
decErr(msgDecCannotExpandArr, rvcap, containerLenS)
}
rvn := reflect.MakeSlice(f.ti.rt, containerLenS, containerLenS)
if rvlen > 0 {
reflect.Copy(rvn, rv)
}
rv.Set(rvn)
} else if containerLenS > rvlen {
rv.SetLen(containerLenS)
}
for j := 0; j < containerLenS; j++ {
f.d.decodeValue(rv.Index(j))
}
}
示例3: sliceValue
func (d *Decoder) sliceValue(v reflect.Value) error {
elemType := v.Type().Elem()
switch elemType.Kind() {
case reflect.Uint8:
b, err := d.DecodeBytes()
if err != nil {
return err
}
v.SetBytes(b)
return nil
}
n, err := d.DecodeSliceLen()
if err != nil {
return err
}
if v.Len() < n || (v.Kind() == reflect.Slice && v.IsNil()) {
v.Set(reflect.MakeSlice(v.Type(), n, n))
}
for i := 0; i < n; i++ {
sv := v.Index(i)
if err := d.DecodeValue(sv); err != nil {
return err
}
}
return nil
}
示例4: ValueToField
func (d *MySQL) ValueToField(value reflect.Value, field reflect.Value) error {
switch field.Type().Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
field.SetInt(value.Elem().Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
// reading uint from int value causes panic
switch value.Elem().Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
field.SetUint(uint64(value.Elem().Int()))
default:
field.SetUint(value.Elem().Uint())
}
case reflect.Float32, reflect.Float64:
field.SetFloat(value.Elem().Float())
case reflect.String:
if !value.Elem().IsValid() {
field.SetString("")
return nil
}
field.SetString(string(value.Elem().Bytes()))
case reflect.Slice:
if reflect.TypeOf(value.Interface()).Elem().Kind() == reflect.Uint8 {
field.SetBytes(value.Elem().Bytes())
}
case reflect.Struct:
if field.Type() == reflect.TypeOf(time.Time{}) {
field.Set(value.Elem())
}
}
return nil
}
示例5: SetModelValue
func (d *base) SetModelValue(driverValue, fieldValue reflect.Value) error {
// ignore zero types
if !driverValue.Elem().IsValid() {
return nil
}
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:
if _, ok := fieldValue.Interface().(time.Time); ok {
fieldValue.Set(driverValue.Elem())
}
}
return nil
}
示例6: decodeArrayish
func (t *Transport) decodeArrayish(r reader, val reflect.Value) error {
usize, err := binary.ReadUvarint(r)
if err != nil {
return err
}
size := int(usize)
// Special cases: []byte, string
isByteArr := val.Kind() == reflect.Array && val.Type().Elem().Kind() == reflect.Uint8
isString := val.Kind() == reflect.String
if isByteArr || isString {
raw := make([]byte, size)
if _, err := io.ReadFull(r, raw); err != nil {
return err
}
switch {
case isString:
val.SetString(string(raw))
case isByteArr:
val.SetBytes(raw)
}
return nil
}
slice := reflect.MakeSlice(val.Type(), size, size)
for i := 0; i < size; i++ {
if err := t.decodeValue(r, slice.Index(i)); err != nil {
return err
}
}
val.Set(slice)
return nil
}
示例7: rewrite
func rewrite(nodeVal reflect.Value, rewriter Rewriter) {
if !nodeVal.IsValid() {
return
}
nodeTyp := nodeVal.Type()
switch nodeTyp.Kind() {
case reflect.Slice:
if nodeTyp == typeOfBytes && !nodeVal.IsNil() {
val := rewriter(nodeVal.Bytes()) //use rewriter to rewrite the bytes
nodeVal.SetBytes(val)
} else if nodeTyp.Implements(typeOfSQLNode) {
for i := 0; i < nodeVal.Len(); i++ {
m := nodeVal.Index(i)
rewrite(m, rewriter)
}
}
case reflect.Struct:
for i := 0; i < nodeVal.NumField(); i++ {
f := nodeVal.Field(i)
rewrite(f, rewriter)
}
case reflect.Ptr, reflect.Interface:
rewrite(nodeVal.Elem(), rewriter)
}
}
示例8: shortBinary
// type(1) | name length(1) | content length(1) | raw name bytes |
// 0x00 | content bytes
func (d *decodeState) shortBinary(v reflect.Value) {
d.off += 1 //type
klen := int(Int8(d.data[d.off:]))
d.off += 1 // name length
vlen := int(Int8(d.data[d.off:]))
d.off += 1 // content length
if klen > 0 {
key := d.data[d.off : d.off+klen-1]
d.off += klen // name and 0x00
v = fieldByTag(v, key)
u, pv := d.indirect(v, false)
if u != nil {
d.off -= 1 + 1 + 1 + klen
if err := u.UnmarshalMCPACK(d.next()); err != nil {
d.error(err)
}
return
}
v = pv
}
val := d.data[d.off : d.off+vlen]
d.off += vlen // value
if !v.IsValid() {
return
}
v.SetBytes(val)
}
示例9: decodeSlice
func decodeSlice(v reflect.Value, x interface{}) {
t := v.Type()
if t.Elem().Kind() == reflect.Uint8 {
// Allow, but don't require, byte slices to be encoded as a single string.
if s, ok := x.(string); ok {
v.SetBytes([]byte(s))
return
}
}
// NOTE: Implicit indexing is currently done at the parseValues level,
// so if if an implicitKey reaches here it will always replace the last.
implicit := 0
for k, c := range getNode(x) {
var i int
if k == implicitKey {
i = implicit
implicit++
} else {
explicit, err := strconv.Atoi(k)
if err != nil {
panic(k + " is not a valid index for type " + t.String())
}
i = explicit
implicit = explicit + 1
}
// "Extend" the slice if it's too short.
if l := v.Len(); i >= l {
delta := i - l + 1
v.Set(reflect.AppendSlice(v, reflect.MakeSlice(t, delta, delta)))
}
decodeValue(v.Index(i), c)
}
}
示例10: 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
}
示例11: convertAssignBulkString
func convertAssignBulkString(d reflect.Value, s []byte) (err error) {
switch d.Type().Kind() {
case reflect.Float32, reflect.Float64:
var x float64
x, err = strconv.ParseFloat(string(s), d.Type().Bits())
d.SetFloat(x)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
var x int64
x, err = strconv.ParseInt(string(s), 10, d.Type().Bits())
d.SetInt(x)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
var x uint64
x, err = strconv.ParseUint(string(s), 10, d.Type().Bits())
d.SetUint(x)
case reflect.Bool:
var x bool
x, err = strconv.ParseBool(string(s))
d.SetBool(x)
case reflect.String:
d.SetString(string(s))
case reflect.Slice:
if d.Type().Elem().Kind() != reflect.Uint8 {
err = cannotConvert(d, s)
} else {
d.SetBytes(s)
}
default:
err = cannotConvert(d, s)
}
return
}
示例12: inflateScalar
// inflate the results of a match into a string
func inflateScalar(dest reflect.Value, match *match, captureIndex int) error {
if captureIndex == -1 {
// This means the field generated a regex but we did not want the results
return nil
}
region := match.captures[captureIndex]
if !region.wasMatched() {
// This means the region was optional and was not matched
return nil
}
buf := match.input[region.begin:region.end]
dest = ensureAlloc(dest)
switch dest.Type() {
case stringType:
dest.SetString(string(buf))
return nil
case byteArrayType:
dest.SetBytes(buf)
return nil
case emptyType:
// ignore the value
return nil
}
return fmt.Errorf("unable to capture into %s", dest.Type().String())
}
示例13: bytesValue
func (d *Decoder) bytesValue(value reflect.Value) error {
v, err := d.DecodeBytes()
if err != nil {
return err
}
value.SetBytes(v)
return nil
}
示例14: encrypt
func (ep entityProtector) encrypt(field reflect.Value) {
switch field.Interface().(type) {
case []byte:
field.SetBytes(ep.cipher.Encrypt(field.Bytes()))
case string:
field.SetString(string(ep.cipher.Encrypt([]byte(field.String()))))
}
}
示例15: decodeByteSlice
func decodeByteSlice(s *Stream, val reflect.Value) error {
b, err := s.Bytes()
if err != nil {
return wrapStreamError(err, val.Type())
}
val.SetBytes(b)
return nil
}