This repository has been archived by the owner on May 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.go
119 lines (105 loc) · 2.73 KB
/
utils.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
package bucket
import (
"encoding/base64"
"errors"
"log"
"net/http"
"reflect"
"strings"
"time"
)
// NullTimeout is the library's built in NullTimeout type for Opts
type NullTimeout struct {
valid bool
Value time.Duration
}
// NullTimeoutMillisec creates a NullTimeout with the given millisec
func NullTimeoutMillisec(dur uint64) NullTimeout {
return NullTimeout{
valid: true,
Value: time.Duration(dur) * time.Millisecond,
}
}
// NullTimeoutSec creates a NullTimeout with the given sec
func NullTimeoutSec(dur uint64) NullTimeout {
return NullTimeout{
valid: true,
Value: time.Duration(dur) * time.Second,
}
}
// NullTimeoutFrom creates a NullTimeout with the given time.Duration
func NullTimeoutFrom(dur time.Duration) NullTimeout {
return NullTimeout{
valid: true,
Value: dur,
}
}
func basicAuth(username, password string) string {
auth := username + ":" + password
return base64.StdEncoding.EncodeToString([]byte(auth))
}
func setupBasicAuth(req *http.Request) {
req.Header.Add("Authorization", "Basic "+basicAuth("Administrator", "password"))
}
func defaultHandler() *Handler {
h, err := New(&Configuration{
Username: "Administrator",
Password: "password",
BucketName: bucketName,
BucketPassword: "",
Separator: "::",
})
if err != nil {
log.Fatal(err)
}
return h
}
func removeOmitempty(tag string) string {
if strings.Contains(tag, ",omitempty") {
tag = strings.Replace(tag, ",omitempty", "", -1)
}
return tag
}
func getDocumentTypes(value interface{}) ([]string, error) {
if reflect.ValueOf(value).Kind() != reflect.Ptr {
return nil, ErrInvalidGetDocumentTypesParam
}
var typs []string
var val = reflect.New(reflect.ValueOf(value).Type().Elem())
if val.Kind() == reflect.Ptr {
val = reflect.Indirect(val)
}
typ := val.Type()
if typ.Kind() != reflect.Struct {
return nil, errors.New("value argument must be a struct")
}
for i := 0; i < typ.NumField(); i++ {
typeField := typ.Field(i)
structField := val.Field(i)
if val, ok := typeField.Tag.Lookup(tagReferenced); ok {
typs = append(typs, val)
if structField.IsNil() && structField.CanSet() {
structField.Set(reflect.New(structField.Type().Elem()))
}
moreTypes, err := getDocumentTypes(structField.Interface())
if err != nil {
return nil, err
}
typs = append(typs, moreTypes...)
}
}
return typs, nil
}
func getStructAddressableSubfields(value reflect.Value) map[string]interface{} {
if value.Kind() == reflect.Ptr {
value = reflect.Indirect(value)
}
var result = make(map[string]interface{})
typ := value.Type()
for i := 0; i < typ.NumField(); i++ {
if tag, ok := typ.Field(i).Tag.Lookup(tagReferenced); ok && tag != "" {
result[tag] = value.Field(i).Addr().Interface()
}
}
return result
}