-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibcobhandemo.go
More file actions
109 lines (86 loc) · 2.24 KB
/
libcobhandemo.go
File metadata and controls
109 lines (86 loc) · 2.24 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
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
package main
import (
"C"
"encoding/base64"
"math"
"strings"
"time"
"unsafe"
"github.com/godaddy/cobhan-go"
)
func main() {
}
//export sleepTest
func sleepTest(seconds int32) {
time.Sleep(time.Duration(seconds) * time.Second)
}
//export addInt32
func addInt32(x int32, y int32) int32 {
sum := x + y
//Integers wrap in go, let's use MaxInt32 as a special case value to indicate overflow
if x >= 0 && sum < y {
return math.MaxInt32
}
//Integers wrap in go, let's use MinInt32 as a special case value to indicate underflow
if x < 0 && sum > y {
return math.MinInt32
}
return sum
}
//export addInt64
func addInt64(x int64, y int64) int64 {
sum := x + y
//Integers wrap in go, let's use MaxInt64 as a special case value to indicate overflow
if x >= 0 && sum < y {
return math.MaxInt64
}
//Integers wrap in go, let's use MinInt64 as a special case value to indicate underflow
if x < 0 && sum > y {
return math.MinInt64
}
return sum
}
//export addDouble
func addDouble(x float64, y float64) float64 {
return x + y
}
//export toUpper
func toUpper(input unsafe.Pointer, output unsafe.Pointer) int32 {
str, result := cobhan.BufferToString(input)
if result != 0 {
return result
}
str = strings.ToUpper(str)
return cobhan.StringToBuffer(str, output)
}
//export filterJson
func filterJson(input unsafe.Pointer, disallowedValue unsafe.Pointer, output unsafe.Pointer) int32 {
jsonList, result := cobhan.BufferToJson(input)
if result != 0 {
return result
}
disallowedVal, result := cobhan.BufferToString(disallowedValue)
if result != 0 {
return result
}
for key := range jsonList {
switch val := jsonList[key].(type) {
case string:
if strings.Contains(val, disallowedVal) {
delete(jsonList, key)
}
}
}
return cobhan.JsonToBuffer(jsonList, output)
}
//export base64Encode
func base64Encode(input unsafe.Pointer, output unsafe.Pointer) int32 {
inputBytes, result := cobhan.BufferToBytes(input)
if result != 0 {
return result
}
outputLen := base64.StdEncoding.EncodedLen(len(inputBytes))
outputBytes := make([]byte, outputLen)
base64.StdEncoding.Encode(outputBytes, inputBytes)
return cobhan.BytesToBuffer(outputBytes, output)
}