/src/marshall.go
http://bone-aap.googlecode.com/ · Go · 69 lines · 58 code · 11 blank · 0 comment · 4 complexity · 2fd95837465f947239581b71f13bc3b1 MD5 · raw file
- package aal_net
- import (
- "reflect"
- "bytes"
- "encoding/binary"
- )
- type Marshallable interface {
- Marshall(interface{}) []byte
- Unmarshall([]byte) interface{}
- }
- type MarshallableThings struct {}
- func (MarshallableThings) Marshall(t interface{}) []byte {
- tValue := reflect.ValueOf(t)
- buf := bytes.NewBuffer([]byte{})
-
- for i := 0; i < tValue.NumField(); i += 1 {
- vField := tValue.Field(i)
-
- switch vField.Kind() {
- case reflect.Int:
- binary.Write(buf, binary.LittleEndian, int32(vField.Int()))
- case reflect.Int8:
- binary.Write(buf, binary.LittleEndian, int8(vField.Int()))
- case reflect.Int16:
- binary.Write(buf, binary.LittleEndian, int16(vField.Int()))
- case reflect.Int32:
- binary.Write(buf, binary.LittleEndian, int32(vField.Int()))
- case reflect.Int64:
- binary.Write(buf, binary.LittleEndian, int64(vField.Int()))
- case reflect.Uint:
- binary.Write(buf, binary.LittleEndian, uint32(vField.Int()))
- case reflect.Uint8:
- binary.Write(buf, binary.LittleEndian, uint8(vField.Int()))
- case reflect.Uint16:
- binary.Write(buf, binary.LittleEndian, uint16(vField.Int()))
- case reflect.Uint32:
- binary.Write(buf, binary.LittleEndian, uint32(vField.Int()))
- case reflect.Uint64:
- binary.Write(buf, binary.LittleEndian, uint64(vField.Int()))
- case reflect.Bool:
- if vField.Bool() {
- binary.Write(buf, binary.LittleEndian, uint8(1))
- } else {
- binary.Write(buf, binary.LittleEndian, uint8(0))
- }
- case reflect.String:
- s := vField.String()
- binary.Write(buf, binary.LittleEndian, uint16(len(s)))
- binary.Write(buf, binary.LittleEndian, []byte(s))
- }
- }
-
- return buf.Bytes()
- }
- func (MarshallableThings) Unmarshall([]byte) interface{} {
- return nil
- }
- func marshallNum(result []byte, num interface{}) {
-
- }
- func marshallString(result []byte) {
- }