-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.go
51 lines (43 loc) · 1000 Bytes
/
util.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
package gocosmosdb
import (
"encoding/json"
"fmt"
"io"
"math"
"strings"
"time"
"github.com/google/uuid"
)
func genId() string {
return uuid.New().String()
}
// SetTTL takes a duration and sets the field value
func (exp *Expirable) SetTTL(dur time.Duration) {
exp.TTL = int64(math.Round(dur.Seconds()))
}
// path - generates a link
func path(url string, args ...string) (link string) {
args = append([]string{url}, args...)
link = strings.Join(args, "/")
return
}
// readJson - response to given interface(struct, map, ..)
func readJson(reader io.Reader, data interface{}) error {
return json.NewDecoder(reader).Decode(&data)
}
// Stringify query-string as CosmosDB expected
func querify(query string) string {
return fmt.Sprintf(`{ "%s": "%s" }`, "query", query)
}
// Stringify body data
func stringify(body interface{}) (bt []byte, err error) {
switch t := body.(type) {
case string:
bt = []byte(t)
case []byte:
bt = t
default:
bt, err = json.Marshal(t)
}
return
}