-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathform.go
86 lines (80 loc) · 2.19 KB
/
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
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
package bqwt
import (
"fmt"
"github.com/viant/toolbox"
"net/http"
"strings"
)
type FormFields struct {
Meta string
Method string
Dataset string
Mode string
Match string
Location string
Expr string
Loopback string
Prune string
AbsoluteExpr string
StorageRegion string
}
func (f *FormFields) Validate() error {
if f.Meta == "" {
return fmt.Errorf("meta is empty")
}
if f.Dataset == "" {
return fmt.Errorf("dataset is empty")
}
return nil
}
func (f *FormFields) AsRequest() (*Request, error) {
var err error
defer func() {
if r := recover(); r != nil {
JSON, _ := toolbox.AsJSONText(f)
err = fmt.Errorf("failed to create request from %v %v", JSON, r)
}
}()
request := &Request{}
request.MetaURL = f.Meta
request.DatasetID = f.Dataset
request.Mode = f.Mode
request.MatchingTables = strings.Split(f.Match, ",")
request.Location = f.Location
request.Method = f.Method
request.Expression = toolbox.AsBoolean(f.Expr)
request.AbsoluteExpression = toolbox.AsBoolean(f.AbsoluteExpr)
request.StorageRegion = f.StorageRegion
if f.Loopback != "" {
if request.LoopbackWindowInSec, err = toolbox.ToInt(f.Loopback); err != nil {
return nil, fmt.Errorf("invalid loopback %v, %v", f.Loopback, err)
}
}
if f.Prune != "" {
if request.PruneThresholdInSec, err = toolbox.ToInt(f.Prune); err != nil {
return nil, fmt.Errorf("invalid loopback %v, %v", f.Prune, err)
}
}
return request, err
}
func NewFormFields(request *http.Request) (*FormFields, error) {
if err := request.ParseForm(); err != nil {
return nil, err
}
if len(request.Form) == 0 {
return nil, fmt.Errorf("form field were empty")
}
return &FormFields{
Method: request.Form.Get("method"),
Meta: request.Form.Get("meta"),
Dataset: request.Form.Get("dataset"),
Mode: request.Form.Get("mode"),
Match: request.Form.Get("match"),
Location: request.Form.Get("location"),
Expr: request.Form.Get("expr"),
Loopback: request.Form.Get("loopback"),
Prune: request.Form.Get("prune"),
AbsoluteExpr: request.Form.Get("absExpr"),
StorageRegion: request.Form.Get("storage"),
}, nil
}