mirror of
https://github.com/aykhans/dodo.git
synced 2025-09-05 10:43:37 +00:00
Here we go again...
This commit is contained in:
42
pkg/types/param.go
Normal file
42
pkg/types/param.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package types
|
||||
|
||||
import "strings"
|
||||
|
||||
type Param KeyValue[string, []string]
|
||||
|
||||
type Params []Param
|
||||
|
||||
func (params Params) GetValue(key string) *[]string {
|
||||
for i := range params {
|
||||
if params[i].Key == key {
|
||||
return ¶ms[i].Value
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (params *Params) Append(param Param) {
|
||||
if item := params.GetValue(param.Key); item != nil {
|
||||
*item = append(*item, param.Value...)
|
||||
} else {
|
||||
*params = append(*params, param)
|
||||
}
|
||||
}
|
||||
|
||||
func (params *Params) Parse(rawValues ...string) {
|
||||
for _, rawValue := range rawValues {
|
||||
params.Append(*ParseParam(rawValue))
|
||||
}
|
||||
}
|
||||
|
||||
func ParseParam(rawValue string) *Param {
|
||||
parts := strings.SplitN(rawValue, "=", 2)
|
||||
switch len(parts) {
|
||||
case 1:
|
||||
return &Param{Key: parts[0], Value: []string{""}}
|
||||
case 2:
|
||||
return &Param{Key: parts[0], Value: []string{parts[1]}}
|
||||
default:
|
||||
return &Param{Key: "", Value: []string{""}}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user