本文整理汇总了Golang中reflect.Value.Bytes方法的典型用法代码示例。如果您正苦于以下问题:Golang Value.Bytes方法的具体用法?Golang Value.Bytes怎么用?Golang Value.Bytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类reflect.Value
的用法示例。
在下文中一共展示了Value.Bytes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: binaryEncoder
func binaryEncoder(e *encodeState, k string, v reflect.Value) {
//type(1) | klen(1) | vlen(4) | key(len(k)) | 0x00 | value
e.resizeIfNeeded(1 + 1 + 4 + len(k) + 1 + v.Len())
e.data[e.off] = MCPACKV2_BINARY
e.off++
if len(k) > 0 { // key len
e.data[e.off] = byte(len(k) + 1)
} else {
e.data[e.off] = 0
}
e.off++
vlenpos := e.off
e.off += 4 // content length
if len(k) > 0 { // key and 0x00
e.off += copy(e.data[e.off:], k)
e.data[e.off] = 0
e.off++
}
vpos := e.off
e.off += copy(e.data[e.off:], v.Bytes()) //value
PutInt32(e.data[vlenpos:], int32(e.off-vpos))
}
示例2: Pack
func (f *Field) Pack(buf []byte, val reflect.Value, length int) error {
if f.Type == Pad {
for i := 0; i < length; i++ {
buf[i] = 0
}
return nil
}
if f.Slice {
// special case byte slices for performance
if !f.Array && f.Type == Uint8 && f.defType == Uint8 {
copy(buf, val.Bytes())
return nil
}
pos := 0
for i := 0; i < length; i++ {
if err := f.packVal(buf[pos:], val.Index(i), 1); err != nil {
return err
}
pos += f.Type.Size()
}
return nil
} else {
return f.packVal(buf, val, length)
}
}
示例3: defaultReturnHandler
func defaultReturnHandler() ReturnHandler {
return func(ctx *Context, vals []reflect.Value) {
rv := ctx.GetVal(inject.InterfaceOf((*http.ResponseWriter)(nil)))
resp := rv.Interface().(http.ResponseWriter)
var respVal reflect.Value
if len(vals) > 1 && vals[0].Kind() == reflect.Int {
resp.WriteHeader(int(vals[0].Int()))
respVal = vals[1]
} else if len(vals) > 0 {
respVal = vals[0]
if isError(respVal) {
err := respVal.Interface().(error)
if err != nil {
ctx.internalServerError(ctx, err)
}
return
} else if canDeref(respVal) {
if respVal.IsNil() {
return // Ignore nil error
}
}
}
if canDeref(respVal) {
respVal = respVal.Elem()
}
if isByteSlice(respVal) {
resp.Write(respVal.Bytes())
} else {
resp.Write([]byte(respVal.String()))
}
}
}
示例4: 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)
}
}
示例5: encodePacketValue
func encodePacketValue(buf []byte, v reflect.Value) (int, error) {
n := 0
switch v.Kind() {
default:
return n, ErrUnhandledFieldType
case reflect.Struct:
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
n2, err := encodePacketValue(buf[n:], field)
n += n2
if err != nil {
return n, err
}
}
case reflect.Bool:
if v.Bool() {
buf[n] = 1
} else {
buf[n] = 0
}
n += 1
case reflect.Int32:
binary.BigEndian.PutUint32(buf[n:n+4], uint32(v.Int()))
n += 4
case reflect.Int64:
binary.BigEndian.PutUint64(buf[n:n+8], uint64(v.Int()))
n += 8
case reflect.String:
str := v.String()
binary.BigEndian.PutUint32(buf[n:n+4], uint32(len(str)))
copy(buf[n+4:n+4+len(str)], []byte(str))
n += 4 + len(str)
case reflect.Slice:
switch v.Type().Elem().Kind() {
default:
count := v.Len()
startN := n
n += 4
for i := 0; i < count; i++ {
n2, err := encodePacketValue(buf[n:], v.Index(i))
n += n2
if err != nil {
return n, err
}
}
binary.BigEndian.PutUint32(buf[startN:startN+4], uint32(count))
case reflect.Uint8:
if v.IsNil() {
binary.BigEndian.PutUint32(buf[n:n+4], uint32(0xffffffff))
n += 4
} else {
bytes := v.Bytes()
binary.BigEndian.PutUint32(buf[n:n+4], uint32(len(bytes)))
copy(buf[n+4:n+4+len(bytes)], bytes)
n += 4 + len(bytes)
}
}
}
return n, nil
}
示例6: 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))
}
}
示例7: indexBytes
func indexBytes(typ reflect.Type, value reflect.Value) (b []byte, err error) {
switch typ.Kind() {
case reflect.String:
b = []byte(value.String())
case reflect.Int:
buf := new(bytes.Buffer)
if err = binary.Write(buf, binary.BigEndian, value.Int()); err != nil {
return
}
b = buf.Bytes()
case reflect.Slice:
switch typ.Elem().Kind() {
case reflect.Uint8:
b = value.Bytes()
default:
err = fmt.Errorf("%v is not an indexable type", typ)
}
case reflect.Bool:
if value.Bool() {
b = []byte{1}
} else {
b = []byte{0}
}
default:
err = fmt.Errorf("%v is not an indexable type", typ)
return
}
if len(b) == 0 {
b = []byte{0}
}
return
}
示例8: encode
func encode(b []byte, rv reflect.Value) ([]byte, error) {
switch rk := rv.Kind(); rk {
case reflect.Bool:
b = encodeBool(b, rv.Bool())
case reflect.Int, reflect.Int8, reflect.Int64, reflect.Int32, reflect.Int16:
b = encodeInt(b, rv.Int())
case reflect.Uint8, reflect.Uint64, reflect.Uint, reflect.Uint32, reflect.Uint16:
b = encodeInt(b, int64(rv.Uint()))
case reflect.String:
b = encodeString(b, rv.String())
case reflect.Array, reflect.Slice:
if rv.Len() == 0 && rv.Type().Elem().Kind() == reflect.Uint8 {
b = encodeBytes(b, rv.Bytes())
} else if rv.Index(0).Kind() == reflect.Uint8 {
b = encodeBytes(b, rv.Bytes())
} else {
b = encodeArray(b, rv)
}
case reflect.Map:
b = encodeMap(b, rv)
case reflect.Struct:
b = encodeStruct(b, rv)
default:
panic("no support for type")
}
return b, nil
}
示例9: marshalBytesValue
func marshalBytesValue(value reflect.Value) *dynamodb.AttributeValue {
if value.IsNil() {
return makeNullAttrValue()
}
return &dynamodb.AttributeValue{B: value.Bytes()}
}
示例10: Subscription
/*
Subscription will return a subscription with name, watching changes to objects with the same type and id as obj.
It will watch for operations matching the ops, and send the events to the subscriber.
*/
func (self *DB) Subscription(name string, obj interface{}, ops Operation, subscriber Subscriber) (result *Subscription, err error) {
var wantedValue reflect.Value
var wantedId reflect.Value
if wantedValue, wantedId, err = identify(obj); err != nil {
return
}
wantedType := wantedValue.Type()
wantedBytes := make([]byte, len(wantedId.Bytes()))
copy(wantedBytes, wantedId.Bytes())
result = &Subscription{
name: name,
db: self,
matcher: func(tx *TX, typ reflect.Type, value reflect.Value) (result bool, err error) {
if typ.Name() != wantedType.Name() {
return
}
if bytes.Compare(value.FieldByName(idField).Bytes(), wantedBytes) != 0 {
return
}
result = true
return
},
subscriber: subscriber,
ops: ops,
typ: wantedType,
}
return
}
示例11: 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)
}
}
示例12: encodeScalar
func (e *Encoder) encodeScalar(by []byte, rv reflect.Value, isKeyOrClass bool, strTable map[string]int, ptrTable map[uintptr]int) []byte {
for rv.Kind() == reflect.Interface {
rv = rv.Elem()
}
switch rv.Kind() {
case reflect.Array, reflect.Slice:
if rv.Type().Elem().Kind() == reflect.Uint8 {
by = e.encodeBytes(by, rv.Bytes(), isKeyOrClass, strTable)
} else {
by = append(by, typeREFN)
by = e.encodeArray(by, rv, strTable, ptrTable)
}
case reflect.Map:
by = append(by, typeREFN)
by = e.encodeMap(by, rv, strTable, ptrTable)
case reflect.String:
by = e.encodeString(by, rv.String(), true, strTable)
default:
by, _ = e.encode(by, rv, isKeyOrClass, strTable, ptrTable)
}
return by
}
示例13: marshalSimple
func (p *printer) marshalSimple(typ reflect.Type, val reflect.Value) error {
// Normally we don't see structs, but this can happen for an attribute.
if val.Type() == timeType {
p.WriteString(val.Interface().(time.Time).Format(time.RFC3339Nano))
return nil
}
switch val.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
p.WriteString(strconv.FormatInt(val.Int(), 10))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
p.WriteString(strconv.FormatUint(val.Uint(), 10))
case reflect.Float32, reflect.Float64:
p.WriteString(strconv.FormatFloat(val.Float(), 'g', -1, 64))
case reflect.String:
// TODO: Add EscapeString.
Escape(p, []byte(val.String()))
case reflect.Bool:
p.WriteString(strconv.FormatBool(val.Bool()))
case reflect.Array:
// will be [...]byte
bytes := make([]byte, val.Len())
for i := range bytes {
bytes[i] = val.Index(i).Interface().(byte)
}
Escape(p, bytes)
case reflect.Slice:
// will be []byte
Escape(p, val.Bytes())
default:
return &UnsupportedTypeError{typ}
}
return p.cachedWriteError()
}
示例14: defaultReturnHandler
func defaultReturnHandler() ReturnHandler {
return func(ctx Context, vals []reflect.Value) {
rv := ctx.Get(inject.InterfaceOf((*http.ResponseWriter)(nil)))
res := rv.Interface().(http.ResponseWriter)
var responseVal reflect.Value
if len(vals) > 1 && vals[0].Kind() == reflect.Int {
res.WriteHeader(int(vals[0].Int()))
responseVal = vals[1]
} else if len(vals) > 0 {
responseVal = vals[0]
}
if canDeref(responseVal) {
responseVal = responseVal.Elem()
}
if isByteSlice(responseVal) {
res.Write(responseVal.Bytes())
} else {
jsonBody, err := json.Marshal(responseVal.Interface())
if err != nil {
res.Write([]byte(responseVal.String()))
} else {
res.Write(jsonBody)
}
}
}
}
示例15: marshalSimple
func (p *printer) marshalSimple(typ reflect.Type, val reflect.Value) (string, []byte, error) {
switch val.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return strconv.FormatInt(val.Int(), 10), nil, nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return strconv.FormatUint(val.Uint(), 10), nil, nil
case reflect.Float32, reflect.Float64:
return strconv.FormatFloat(val.Float(), 'g', -1, val.Type().Bits()), nil, nil
case reflect.String:
return val.String(), nil, nil
case reflect.Bool:
return strconv.FormatBool(val.Bool()), nil, nil
case reflect.Array:
if typ.Elem().Kind() != reflect.Uint8 {
break
}
// [...]byte
var bytes []byte
if val.CanAddr() {
bytes = val.Slice(0, val.Len()).Bytes()
} else {
bytes = make([]byte, val.Len())
reflect.Copy(reflect.ValueOf(bytes), val)
}
return "", bytes, nil
case reflect.Slice:
if typ.Elem().Kind() != reflect.Uint8 {
break
}
// []byte
return "", val.Bytes(), nil
}
return "", nil, &UnsupportedTypeError{typ}
}