forked from go-routeros/routeros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reply.go
115 lines (97 loc) · 2.23 KB
/
reply.go
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package routeros
import (
"bytes"
"fmt"
"reflect"
"github.com/pawelsocha/routeros/proto"
)
// Reply has all the sentences from a reply.
type Reply struct {
Re []*proto.Sentence
Done *proto.Sentence
}
func (r *Reply) String() string {
b := &bytes.Buffer{}
for _, re := range r.Re {
fmt.Fprintf(b, "%s\n", re)
}
fmt.Fprintf(b, "%s", r.Done)
return b.String()
}
// readReply reads one reply synchronously. It returns the reply.
func (c *Client) readReply() (*Reply, error) {
r := &Reply{}
for {
sen, err := c.r.ReadSentence()
if err != nil {
return nil, err
}
done, err := r.processSentence(sen)
if err != nil {
return nil, err
}
if done {
return r, nil
}
}
}
func (r *Reply) processSentence(sen *proto.Sentence) (bool, error) {
switch sen.Word {
case "!re":
r.Re = append(r.Re, sen)
case "!done":
r.Done = sen
return true, nil
case "!trap", "!fatal":
return true, &DeviceError{sen}
case "":
// API docs say that empty sentences should be ignored
default:
return true, &UnknownReplyError{sen}
}
return false, nil
}
func (r *Reply) fillRow(record *proto.Sentence, value reflect.Value) reflect.Value {
typ := value.Type()
for i := 0; i < value.NumField(); i++ {
tag := typ.Field(i).Tag.Get("routeros")
if tag == "" {
continue
}
if data, ok := record.Map[tag]; ok {
value.FieldByName(typ.Field(i).Name).SetString(data)
}
}
return value
}
func Generator(r *Reply) chan *proto.Sentence {
ret := make(chan *proto.Sentence, len(r.Re))
defer close(ret)
for _, data := range r.Re {
ret <- data
}
return ret
}
//Fetch map data to struct
func (r *Reply) Fetch(out interface{}) error {
if reflect.TypeOf(out).Kind() != reflect.Ptr {
return fmt.Errorf("Out variable is not a pointer. Type: %v", reflect.TypeOf(out).Kind())
}
if len(r.Re) < 1 {
return fmt.Errorf("Empty data returned from routeros")
}
value := reflect.ValueOf(out).Elem()
switch value.Kind() {
case reflect.Struct:
if len(r.Re) > 1 {
return fmt.Errorf("Too many records returned from routeros")
}
value.Set(r.fillRow(r.Re[0], value))
case reflect.Slice:
newobj := reflect.New(value.Type())
for _, data := range r.Re {
value.Set(reflect.Append(value, r.fillRow(data, newobj)))
}
}
return nil
}