-
Notifications
You must be signed in to change notification settings - Fork 179
/
helpers.go
70 lines (58 loc) · 1.63 KB
/
helpers.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
package request
import (
"encoding/json"
"errors"
"fmt"
"io"
"strings"
"github.com/onflow/flow-go/model/flow"
)
func parseBody(raw io.Reader, dst interface{}) error {
dec := json.NewDecoder(raw)
dec.DisallowUnknownFields()
err := dec.Decode(&dst)
if err != nil {
var syntaxError *json.SyntaxError
var unmarshalTypeError *json.UnmarshalTypeError
switch {
case errors.As(err, &syntaxError):
return fmt.Errorf("request body contains badly-formed JSON (at position %d)", syntaxError.Offset)
case errors.Is(err, io.ErrUnexpectedEOF):
return fmt.Errorf("request body contains badly-formed JSON")
case errors.As(err, &unmarshalTypeError):
return fmt.Errorf("request body contains an invalid value for the %q field (at position %d)", unmarshalTypeError.Field, unmarshalTypeError.Offset)
case strings.HasPrefix(err.Error(), "json: unknown field "):
fieldName := strings.TrimPrefix(err.Error(), "json: unknown field ")
return fmt.Errorf("request body contains unknown field %s", fieldName)
case errors.Is(err, io.EOF):
return fmt.Errorf("request body must not be empty")
default:
return err
}
}
if dst == nil {
return fmt.Errorf("request body must not be empty")
}
err = dec.Decode(&struct{}{})
if err != io.EOF {
return fmt.Errorf("request body must only contain a single JSON object")
}
return nil
}
type GetByIDRequest struct {
ID flow.Identifier
}
func (g *GetByIDRequest) Build(r *Request) error {
return g.Parse(
r.GetVar(idQuery),
)
}
func (g *GetByIDRequest) Parse(rawID string) error {
var id ID
err := id.Parse(rawID)
if err != nil {
return err
}
g.ID = id.Flow()
return nil
}