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/cookie.go
Normal file
42
pkg/types/cookie.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package types
|
||||
|
||||
import "strings"
|
||||
|
||||
type Cookie KeyValue[string, []string]
|
||||
|
||||
type Cookies []Cookie
|
||||
|
||||
func (cookies Cookies) GetValue(key string) *[]string {
|
||||
for i := range cookies {
|
||||
if cookies[i].Key == key {
|
||||
return &cookies[i].Value
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cookies *Cookies) Append(cookie Cookie) {
|
||||
if item := cookies.GetValue(cookie.Key); item != nil {
|
||||
*item = append(*item, cookie.Value...)
|
||||
} else {
|
||||
*cookies = append(*cookies, cookie)
|
||||
}
|
||||
}
|
||||
|
||||
func (cookies *Cookies) Parse(rawValues ...string) {
|
||||
for _, rawValue := range rawValues {
|
||||
cookies.Append(*ParseCookie(rawValue))
|
||||
}
|
||||
}
|
||||
|
||||
func ParseCookie(rawValue string) *Cookie {
|
||||
parts := strings.SplitN(rawValue, "=", 2)
|
||||
switch len(parts) {
|
||||
case 1:
|
||||
return &Cookie{Key: parts[0], Value: []string{""}}
|
||||
case 2:
|
||||
return &Cookie{Key: parts[0], Value: []string{parts[1]}}
|
||||
default:
|
||||
return &Cookie{Key: "", Value: []string{""}}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user