forked from porthos-rpc/porthos-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform.go
40 lines (31 loc) · 802 Bytes
/
form.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
package porthos
import (
"bytes"
"encoding/json"
"fmt"
)
// Form represents a request form where data are retrieved through indexes.
type Form interface {
// GetArg returns an argument giving the index.
GetArg(index int) Argument
}
type form struct {
args []interface{}
}
// NewForm creates a new form to retrieve values from its index.
func NewForm(contentType string, body []byte) (Form, error) {
if contentType != "application/json" {
return nil, fmt.Errorf("Invalid content type, got: %s", contentType)
}
decoder := json.NewDecoder(bytes.NewReader(body))
decoder.UseNumber()
args := new([]interface{})
err := decoder.Decode(args)
if err != nil {
return nil, err
}
return &form{*args}, nil
}
func (r *form) GetArg(index int) Argument {
return &argument{r.args[index]}
}