-
Notifications
You must be signed in to change notification settings - Fork 4
/
args.go
43 lines (36 loc) · 1.1 KB
/
args.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
// Package cutil - Content managed by Project Forge, see [projectforge.md] for details.
package cutil
import (
"github.com/samber/lo"
"github.com/valyala/fasthttp"
"projectforge.dev/projectforge/app/util"
)
type Arg struct {
Key string `json:"key"`
Title string `json:"title"`
Description string `json:"description,omitempty"`
Type string `json:"type,omitempty"`
Default string `json:"default,omitempty"`
Choices []string `json:"choices,omitempty"`
}
type Args []*Arg
type ArgResults struct {
Args Args `json:"args"`
Values util.ValueMap `json:"values"`
Missing []string `json:"missing,omitempty"`
}
func (a *ArgResults) HasMissing() bool {
return len(a.Missing) > 0
}
func CollectArgs(rc *fasthttp.RequestCtx, args Args) *ArgResults {
ret := make(util.ValueMap, len(args))
var missing []string
lo.ForEach(args, func(arg *Arg, _ int) {
qa := rc.URI().QueryArgs()
if !qa.Has(arg.Key) {
missing = append(missing, arg.Key)
}
ret[arg.Key] = string(qa.Peek(arg.Key))
})
return &ArgResults{Args: args, Values: ret, Missing: missing}
}