-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
88 lines (72 loc) · 2 KB
/
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
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
package util
import (
"errors"
"fmt"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
// GetFromOffset generates a human-readable line/character error from a
// JSON error's offset
func GetFromOffset(input string, offset int) (string, error) {
var line = 1 // Silly humans tending to start counting at 1
var character = 0
lf := rune(0x0A)
if offset > len(input) || offset < 0 {
return "", fmt.Errorf("Offset beyond bounds of input")
}
for pos, char := range input {
if char == lf {
// Newline
character = 0
line++
}
character++
// We're at the problem-causing character
if pos == offset {
break
}
}
return fmt.Sprintf("JSON syntax error at line %d, character %d", line, character), nil
}
// MapStringToInterface converts map[string]string to a map[string]interface{}
func MapStringToInterface(vars map[string]string) map[string]interface{} {
var converted = make(map[string]interface{})
for k, v := range vars {
converted[k] = v
}
return converted
}
// NiceError factors away the erroring of a function into a clean single-line function call
func NiceError(ctx *gin.Context, err error, code int) {
log.Error(err.Error())
errResp := map[string][]string{
"errors": []string{
err.Error(),
},
}
ctx.AbortWithStatusJSON(code, errResp)
}
// GetResourceID tries to extract the ID from data given
func GetResourceID(data interface{}) (string, error) {
var mapped map[string]interface{}
var isMap bool
switch data.(type) {
case []interface{}:
mapped, isMap = data.([]interface{})[0].(map[string]interface{})
if !isMap {
return "", errors.New("Data could not be converted to a map")
}
case map[string]interface{}:
mapped, isMap = data.(map[string]interface{})
if !isMap {
return "", errors.New("Data could not be converted to a map")
}
}
id, ok := mapped["id"].(string)
if id != "" && ok {
return id, nil
} else if !ok {
return "", errors.New("id field is not type string")
}
return "", errors.New("No id field, unable to retrieve the ID")
}