Core

/app/controller/cutil/args.go (1.0 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
package cutil

import (
"github.com/samber/lo"
"github.com/valyala/fasthttp"
)

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 map[string]string `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(map[string]string, 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}
}