-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdk.go
162 lines (129 loc) · 4.11 KB
/
sdk.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package sdk
import (
"errors"
)
var (
ErrNoInputBuffer = errors.New("no input buffer")
ErrInvalidInputBufferLength = errors.New("expected input buffer length and actual buffer length are different")
ErrNoTagValue = errors.New("no tag value")
ErrInvalidTagValueLength = errors.New("expected tag value buffer length and actual buffer length are different")
ErrNoSourceValue = errors.New("no source value")
ErrInvalidSourceValueLength = errors.New("expected source value buffer length and actual buffer length are different")
ErrNoLocationInformation = errors.New("there is no location information")
)
// ErrorCode represents the error code for Soralet.
type ErrorCode int32
// Location is a struct that contains the longitude and latitude information.
//go:generate json-ice --type=Location
type Location struct {
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
}
func (l *Location) MarshalJSON() ([]byte, error) {
return MarshalLocationAsJSON(l)
}
// Following functions are provided by orbit runtime. {{{
//go:wasm-module env
//export orbit_log
func orbitLog(string)
//go:wasm-module env
//export orbit_get_input_buffer
func orbitGetInputBuffer(*byte, int32) int32
//go:wasm-module env
//export orbit_get_input_buffer_len
func orbitGetInputBufferLen() int32
//go:wasm-module env
//export orbit_set_output
func orbitSetOutput(string)
//go:wasm-module env
//export orbit_set_output_content_type
func orbitSetOutputContentType(string)
//go:wasm-module env
//export orbit_get_tag_value
func orbitGetTagValue(string, *byte, int32) int32
//go:wasm-module env
//export orbit_get_tag_value_len
func orbitGetTagValueLen(string) int32
//go:wasm-module env
//export orbit_get_source_value
func orbitGetSourceValue(string, *byte, int32) int32
//go:wasm-module env
//export orbit_get_source_value_len
func orbitGetSourceValueLen(string) int32
//go:wasm-module env
//export orbit_has_location
func orbitHasLocation() int32
//go:wasm-module env
//export orbit_get_location_lat
func orbitGetLocationLat() float64
//go:wasm-module env
//export orbit_get_location_lon
func orbitGetLocationLon() float64
//go:wasm-module env
//export orbit_get_timestamp
func orbitGetTimestamp() int64
// }}}
// Log outputs a log entry.
func Log(msg string) {
orbitLog(msg)
}
// GetInputBuffer retrieves an input value from the orbit backend.
func GetInputBuffer() ([]byte, error) {
bufferLen := orbitGetInputBufferLen()
if bufferLen <= 0 {
return nil, ErrNoInputBuffer
}
buff := make([]byte, bufferLen, bufferLen)
actualLen := orbitGetInputBuffer(&buff[0], bufferLen)
if bufferLen != actualLen {
return nil, ErrInvalidInputBufferLength
}
return buff, nil
}
// GetTagValue retrieves tag value according to the given name.
func GetTagValue(name string) ([]byte, error) {
bufferLen := orbitGetTagValueLen(name)
if bufferLen <= 0 {
return nil, ErrNoTagValue
}
buff := make([]byte, bufferLen, bufferLen)
actualLen := orbitGetTagValue(name, &buff[0], bufferLen)
if bufferLen != actualLen {
return nil, ErrInvalidTagValueLength
}
return buff, nil
}
// GetSourceValue retrieves source value according to the given name.
func GetSourceValue(name string) ([]byte, error) {
bufferLen := orbitGetSourceValueLen(name)
if bufferLen <= 0 {
return nil, ErrNoSourceValue
}
buff := make([]byte, bufferLen, bufferLen)
actualLen := orbitGetSourceValue(name, &buff[0], bufferLen)
if bufferLen != actualLen {
return nil, ErrInvalidSourceValueLength
}
return buff, nil
}
// GetLocation retrieves location information if that exists.
// If the location information doesn't exist, this function returns a nil struct and "no-location" error.
func GetLocation() (*Location, error) {
if orbitHasLocation() == 0 {
return nil, ErrNoLocationInformation
}
return &Location{
Lat: orbitGetLocationLat(),
Lon: orbitGetLocationLon(),
}, nil
}
// GetTimestamp returns the timestamp.
func GetTimestamp() int64 {
return orbitGetTimestamp()
}
// SetOutputJSON sets a JSON string as the output for the orbit backend.
func SetOutputJSON(out string) {
contentType := "application/json"
orbitSetOutputContentType(contentType)
orbitSetOutput(out)
}