-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstructs.go
44 lines (39 loc) · 1.26 KB
/
structs.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
package libknot
/*
#cgo LDFLAGS: -lknot
#cgo CFLAGS: -I /usr/include/libknot/
#include <dlfcn.h>
#include <libknot.h>
*/
import "C"
// Representation of the types of messages received / sent to the socket as int
const (
END = iota
DATA = iota
EXTRA = iota
BLOCK = iota
)
// Representation of data received / sent by the socket
type KnotCtlData struct {
Command, Flags, Error, Section, Item, Id, Zone, Owner, Ttl, Type, Data, Filter string
}
// ToCtl converts the structure to a structure used to send data to the C library (C.knot_ctl_data_t).
func (obj KnotCtlData) ToCtl() C.knot_ctl_data_t {
var ctlStruct C.knot_ctl_data_t
objs := []string{obj.Command, obj.Flags, obj.Error, obj.Section, obj.Item, obj.Id, obj.Zone, obj.Owner, obj.Ttl, obj.Type, obj.Data, obj.Filter}
for k, v := range objs {
if v != "" {
ctlStruct[k] = C.CString(v)
} else {
ctlStruct[k] = nil
}
}
return ctlStruct
}
// FromCtl transforms the data received by the C library into a struct accessible in Go
func (obj *KnotCtlData) FromCtl(data C.knot_ctl_data_t) {
objs := []*string{&obj.Command, &obj.Flags, &obj.Error, &obj.Section, &obj.Item, &obj.Id, &obj.Zone, &obj.Owner, &obj.Ttl, &obj.Type, &obj.Data, &obj.Filter}
for i, v := range objs {
*v = C.GoString(data[i])
}
}