-
Notifications
You must be signed in to change notification settings - Fork 0
/
msibasic_example.go
64 lines (51 loc) · 1.32 KB
/
msibasic_example.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
package main
import (
"encoding/csv"
"github.com/jjacquay712/GoRODS/msi"
"io"
"log"
"strings"
"unsafe"
)
// #cgo CFLAGS: -I/usr/include/irods
// #cgo LDFLAGS: -lirods_server -lirods_common -lpthread
/*
#include "msParam.h"
#include "re_structs.h"
*/
import "C"
//export BasicExample
func BasicExample(inputParam *C.msParam_t, outputParam *C.msParam_t, rei *C.ruleExecInfo_t) int {
// Setup GoRODS/msi
msi.Configure(unsafe.Pointer(rei))
// Convert *C.msParam_t to golang types
inputCSV := msi.ToParam(unsafe.Pointer(inputParam)).String()
outputKVP := msi.ToParam(unsafe.Pointer(outputParam)).ConvertTo(msi.KeyValPair_MS_T)
// Set output KVP
outputKVP.SetKVP(GetKVPMap(inputCSV))
return msi.SUCCESS
}
// Parse CSV and return a map of key value pairs
func GetKVPMap(csvStr string) map[string]string {
kvpMap := make(map[string]string)
reader := csv.NewReader(strings.NewReader(csvStr))
for {
record, err := reader.Read()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
kvpMap[record[0]] = record[1]
}
return kvpMap
}
// Used in testing, since C package can't be imported to test files
func UnsafePtrToC(ptr unsafe.Pointer) *C.msParam_t {
return (*C.msParam_t)(ptr)
}
func main() {
// We need the main function to make possible
// CGO compiler to compile the package as C static archive
}