-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata.go
58 lines (51 loc) · 1.32 KB
/
data.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
package main
import (
"encoding/json"
"errors"
"github.com/valyala/fasthttp"
"log"
"strings"
)
func dumpPOST(ctx *fasthttp.RequestCtx) {
log.Println(string(ctx.PostBody()))
}
func unmarshal(body []byte, value interface{}) (error) {
// https://gist.github.com/aodin/9493190
// unmarshal
err := json.Unmarshal(body, &value)
if err != nil {
return err
}
// check for 'null'
// https://golang.org/pkg/encoding/json/#Unmarshal
// The JSON null value unmarshals into an interface, map, pointer, or slice
// by setting that Go value to nil. Because null is often used in JSON to mean
// “not present,” unmarshaling a JSON null into any other Go type has no effect
// on the value and produces no error.
if strings.Contains(string(body), ": null") {
return errors.New("null found")
}
return nil
}
// https://stackoverflow.com/a/39444005
func WriteInt(ctx *fasthttp.RequestCtx, n int) {
buf := [11]byte{}
pos := len(buf)
i := int64(n)
signed := i < 0
if signed {
i = -i
}
for {
pos--
buf[pos], i = '0'+byte(i%10), i/10
if i == 0 {
if signed {
pos--
buf[pos] = '-'
}
ctx.Write(buf[pos:])
return
}
}
}