From e31f5ad2042ce18599f39473887c8c99b17c4f3a Mon Sep 17 00:00:00 2001 From: Aykhan Shahsuvarov Date: Wed, 19 Mar 2025 04:06:10 +0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20'AppendByKey'=20method=20o?= =?UTF-8?q?f=20the=20'[]KeyValue[string,=20[]string]'=20types?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 3 +++ output.txt | 0 types/cookies.go | 16 ++++++++-------- types/headers.go | 16 ++++++++-------- types/params.go | 16 ++++++++-------- 5 files changed, 27 insertions(+), 24 deletions(-) create mode 100644 output.txt diff --git a/main.go b/main.go index 5e99a86..da7a240 100644 --- a/main.go +++ b/main.go @@ -36,6 +36,9 @@ func main() { utils.PrintErrAndExit(errors.Join(errs...)) } + // for _, param := range conf.Params { + // fmt.Printf("%s: %v\n", param.Key, param.Value) + // } requestConf := config.NewRequestConfig(conf) requestConf.Print() diff --git a/output.txt b/output.txt new file mode 100644 index 0000000..e69de29 diff --git a/types/cookies.go b/types/cookies.go index ecc3a62..3a1fba8 100644 --- a/types/cookies.go +++ b/types/cookies.go @@ -21,7 +21,7 @@ func (cookies Cookies) String() string { displayLimit := 3 - for i, item := range cookies { + for i, item := range cookies[:min(len(cookies), displayLimit)] { if i > 0 { buffer.WriteString(",\n") } @@ -96,18 +96,18 @@ func (cookies *Cookies) Set(value string) error { return nil } -func (cookies *Cookies) AppendByKey(key string, value string) { - if existingValue := cookies.GetValue(key); existingValue != nil { - *cookies = append(*cookies, KeyValue[string, []string]{Key: key, Value: append(existingValue, value)}) +func (cookies *Cookies) AppendByKey(key, value string) { + if item := cookies.GetValue(key); item != nil { + *item = append(*item, value) } else { *cookies = append(*cookies, KeyValue[string, []string]{Key: key, Value: []string{value}}) } } -func (cookies *Cookies) GetValue(key string) []string { - for _, cookie := range *cookies { - if cookie.Key == key { - return cookie.Value +func (cookies Cookies) GetValue(key string) *[]string { + for i := range cookies { + if cookies[i].Key == key { + return &cookies[i].Value } } return nil diff --git a/types/headers.go b/types/headers.go index c266dac..2968db3 100644 --- a/types/headers.go +++ b/types/headers.go @@ -21,7 +21,7 @@ func (headers Headers) String() string { displayLimit := 3 - for i, item := range headers { + for i, item := range headers[:min(len(headers), displayLimit)] { if i > 0 { buffer.WriteString(",\n") } @@ -96,18 +96,18 @@ func (headers *Headers) Set(value string) error { return nil } -func (headers *Headers) AppendByKey(key string, value string) { - if existingValue := headers.GetValue(key); existingValue != nil { - *headers = append(*headers, KeyValue[string, []string]{Key: key, Value: append(existingValue, value)}) +func (headers *Headers) AppendByKey(key, value string) { + if item := headers.GetValue(key); item != nil { + *item = append(*item, value) } else { *headers = append(*headers, KeyValue[string, []string]{Key: key, Value: []string{value}}) } } -func (headers *Headers) GetValue(key string) []string { - for _, header := range *headers { - if header.Key == key { - return header.Value +func (headers Headers) GetValue(key string) *[]string { + for i := range headers { + if headers[i].Key == key { + return &headers[i].Value } } return nil diff --git a/types/params.go b/types/params.go index 9b9960e..fd4b60d 100644 --- a/types/params.go +++ b/types/params.go @@ -21,7 +21,7 @@ func (params Params) String() string { displayLimit := 3 - for i, item := range params { + for i, item := range params[:min(len(params), displayLimit)] { if i > 0 { buffer.WriteString(",\n") } @@ -96,18 +96,18 @@ func (params *Params) Set(value string) error { return nil } -func (params *Params) AppendByKey(key string, value string) { - if existingValue := params.GetValue(key); existingValue != nil { - *params = append(*params, KeyValue[string, []string]{Key: key, Value: append(existingValue, value)}) +func (params *Params) AppendByKey(key, value string) { + if item := params.GetValue(key); item != nil { + *item = append(*item, value) } else { *params = append(*params, KeyValue[string, []string]{Key: key, Value: []string{value}}) } } -func (params *Params) GetValue(key string) []string { - for _, param := range *params { - if param.Key == key { - return param.Value +func (params Params) GetValue(key string) *[]string { + for i := range params { + if params[i].Key == key { + return ¶ms[i].Value } } return nil