forked from bluenviron/gomavlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage.go
More file actions
66 lines (60 loc) · 2.15 KB
/
message.go
File metadata and controls
66 lines (60 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package gomavlib
import (
"errors"
"reflect"
)
// Message is the interface that all Mavlink messages must implements.
// Furthermore, message structs must be labeled MessageNameOfMessage.
type Message interface {
GetId() uint32
SetField(field string, value interface{}) error
}
// MessageRaw is a special struct that contains a byte-encoded message,
// available in Content. It is used:
//
// * as intermediate step in the encoding/decoding process
//
// * when the parser receives an unknown message
//
type MessageRaw struct {
Id uint32
Content []byte
}
// GetId implements the message interface.
func (m *MessageRaw) GetId() uint32 {
return m.Id
}
func (m *MessageRaw) SetField(field string, value interface{}) error {
return errors.New("cannot set fields in raw message")
}
func SetMessageField(m Message, field string, value interface{}) error {
switch reflect.ValueOf(value).Kind() {
case reflect.Int:
reflect.ValueOf(m).Elem().FieldByName(field).SetInt(int64(value.(int)))
case reflect.Int8:
reflect.ValueOf(m).Elem().FieldByName(field).SetInt(int64(value.(int8)))
case reflect.Int16:
reflect.ValueOf(m).Elem().FieldByName(field).SetInt(int64(value.(int16)))
case reflect.Int32:
reflect.ValueOf(m).Elem().FieldByName(field).SetInt(int64(value.(int32)))
case reflect.Int64:
reflect.ValueOf(m).Elem().FieldByName(field).SetInt(int64(value.(int64)))
case reflect.Uint:
reflect.ValueOf(m).Elem().FieldByName(field).SetUint(uint64(value.(uint)))
case reflect.Uint8:
reflect.ValueOf(m).Elem().FieldByName(field).SetUint(uint64(value.(uint8)))
case reflect.Uint16:
reflect.ValueOf(m).Elem().FieldByName(field).SetUint(uint64(value.(uint16)))
case reflect.Uint32:
reflect.ValueOf(m).Elem().FieldByName(field).SetUint(uint64(value.(uint32)))
case reflect.Uint64:
reflect.ValueOf(m).Elem().FieldByName(field).SetUint(uint64(value.(uint64)))
case reflect.Float32:
reflect.ValueOf(m).Elem().FieldByName(field).SetFloat(float64(value.(float32)))
case reflect.Float64:
reflect.ValueOf(m).Elem().FieldByName(field).SetFloat(float64(value.(float64)))
default:
reflect.ValueOf(m).Elem().FieldByName(field).Set(reflect.ValueOf(value))
}
return nil
}