PageRenderTime 11ms CodeModel.GetById 1ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 1ms

/src/marshall.go

http://bone-aap.googlecode.com/
Go | 69 lines | 58 code | 11 blank | 0 comment | 4 complexity | 2fd95837465f947239581b71f13bc3b1 MD5 | raw file
 1package aal_net
 2
 3import (
 4	"reflect"
 5	"bytes"
 6	"encoding/binary"
 7)
 8
 9type Marshallable interface {
10	Marshall(interface{}) []byte
11	Unmarshall([]byte) interface{}
12}
13
14type MarshallableThings struct {}
15
16func (MarshallableThings) Marshall(t interface{}) []byte {
17	tValue := reflect.ValueOf(t)
18	buf := bytes.NewBuffer([]byte{})
19	
20	for i := 0; i < tValue.NumField(); i += 1 {
21		vField := tValue.Field(i)
22		
23		switch vField.Kind() {
24		case reflect.Int:
25			binary.Write(buf, binary.LittleEndian, int32(vField.Int()))
26		case reflect.Int8:
27			binary.Write(buf, binary.LittleEndian, int8(vField.Int()))
28		case reflect.Int16:
29			binary.Write(buf, binary.LittleEndian, int16(vField.Int()))
30		case reflect.Int32:
31			binary.Write(buf, binary.LittleEndian, int32(vField.Int()))
32		case reflect.Int64:
33			binary.Write(buf, binary.LittleEndian, int64(vField.Int()))
34		case reflect.Uint:
35			binary.Write(buf, binary.LittleEndian, uint32(vField.Int()))
36		case reflect.Uint8:
37			binary.Write(buf, binary.LittleEndian, uint8(vField.Int()))
38		case reflect.Uint16:
39			binary.Write(buf, binary.LittleEndian, uint16(vField.Int()))
40		case reflect.Uint32:
41			binary.Write(buf, binary.LittleEndian, uint32(vField.Int()))
42		case reflect.Uint64:
43			binary.Write(buf, binary.LittleEndian, uint64(vField.Int()))
44		case reflect.Bool:
45			if vField.Bool() {
46				binary.Write(buf, binary.LittleEndian, uint8(1))
47			} else {
48				binary.Write(buf, binary.LittleEndian, uint8(0))
49			}
50		case reflect.String:
51			s := vField.String()
52			binary.Write(buf, binary.LittleEndian, uint16(len(s)))
53			binary.Write(buf, binary.LittleEndian, []byte(s))
54		}
55	}
56	
57	return buf.Bytes()
58}
59
60func (MarshallableThings) Unmarshall([]byte) interface{} {
61	return nil
62}
63
64func marshallNum(result []byte, num interface{}) {
65	
66}
67
68func marshallString(result []byte) {
69}