Repository navigation
Expand file tree
/
Copy pathparse.go
More file actions
55 lines (47 loc) · 1.4 KB
/
Copy pathparse.go
File metadata and controls
55 lines (47 loc) · 1.4 KB
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
package fhttpc
import (
"encoding/xml"
"io"
jsoniter "github.com/json-iterator/go"
"github.com/valyala/fasthttp"
"gopkg.in/yaml.v3"
)
// HTTPError represents an error that occurred while handling a request
// Identical to struct used in labstack/echo
type HTTPError struct {
Code int
Message any
Internal error // Stores the error returned by an external dependency
}
// Set sets the provided byte slice to the response body
func Set(b *[]byte) func(resp *fasthttp.Response) error {
return func(resp *fasthttp.Response) error {
*b = resp.Body()
return nil
}
}
// Copy copies the response body into any io.Writer
func Copy(w io.Writer) func(resp *fasthttp.Response) error {
return func(resp *fasthttp.Response) error {
_, err := w.Write(resp.Body())
return err
}
}
// ParseJSON parses the response body as JSON into a struct
func ParseJSON(v any) func(resp *fasthttp.Response) error {
return func(resp *fasthttp.Response) error {
return jsoniter.Unmarshal(resp.Body(), v)
}
}
// ParseYAML parses the response body as YAML into a struct
func ParseYAML(v any) func(resp *fasthttp.Response) error {
return func(resp *fasthttp.Response) error {
return yaml.Unmarshal(resp.Body(), v)
}
}
// ParseXML parses the response body as XML into a struct
func ParseXML(v any) func(resp *fasthttp.Response) error {
return func(resp *fasthttp.Response) error {
return xml.Unmarshal(resp.Body(), v)
}
}