Skip to content

Commit aa6ec45

Browse files
authored
Merge pull request #95 from aykhans/fix/types
🐛 Fix 'AppendByKey' method of the '[]KeyValue[string, []string]' types
2 parents de9a4bb + e31f5ad commit aa6ec45

File tree

5 files changed

+27
-24
lines changed

5 files changed

+27
-24
lines changed

main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ func main() {
3636
utils.PrintErrAndExit(errors.Join(errs...))
3737
}
3838

39+
// for _, param := range conf.Params {
40+
// fmt.Printf("%s: %v\n", param.Key, param.Value)
41+
// }
3942
requestConf := config.NewRequestConfig(conf)
4043
requestConf.Print()
4144

output.txt

Whitespace-only changes.

types/cookies.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (cookies Cookies) String() string {
2121

2222
displayLimit := 3
2323

24-
for i, item := range cookies {
24+
for i, item := range cookies[:min(len(cookies), displayLimit)] {
2525
if i > 0 {
2626
buffer.WriteString(",\n")
2727
}
@@ -96,18 +96,18 @@ func (cookies *Cookies) Set(value string) error {
9696
return nil
9797
}
9898

99-
func (cookies *Cookies) AppendByKey(key string, value string) {
100-
if existingValue := cookies.GetValue(key); existingValue != nil {
101-
*cookies = append(*cookies, KeyValue[string, []string]{Key: key, Value: append(existingValue, value)})
99+
func (cookies *Cookies) AppendByKey(key, value string) {
100+
if item := cookies.GetValue(key); item != nil {
101+
*item = append(*item, value)
102102
} else {
103103
*cookies = append(*cookies, KeyValue[string, []string]{Key: key, Value: []string{value}})
104104
}
105105
}
106106

107-
func (cookies *Cookies) GetValue(key string) []string {
108-
for _, cookie := range *cookies {
109-
if cookie.Key == key {
110-
return cookie.Value
107+
func (cookies Cookies) GetValue(key string) *[]string {
108+
for i := range cookies {
109+
if cookies[i].Key == key {
110+
return &cookies[i].Value
111111
}
112112
}
113113
return nil

types/headers.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (headers Headers) String() string {
2121

2222
displayLimit := 3
2323

24-
for i, item := range headers {
24+
for i, item := range headers[:min(len(headers), displayLimit)] {
2525
if i > 0 {
2626
buffer.WriteString(",\n")
2727
}
@@ -96,18 +96,18 @@ func (headers *Headers) Set(value string) error {
9696
return nil
9797
}
9898

99-
func (headers *Headers) AppendByKey(key string, value string) {
100-
if existingValue := headers.GetValue(key); existingValue != nil {
101-
*headers = append(*headers, KeyValue[string, []string]{Key: key, Value: append(existingValue, value)})
99+
func (headers *Headers) AppendByKey(key, value string) {
100+
if item := headers.GetValue(key); item != nil {
101+
*item = append(*item, value)
102102
} else {
103103
*headers = append(*headers, KeyValue[string, []string]{Key: key, Value: []string{value}})
104104
}
105105
}
106106

107-
func (headers *Headers) GetValue(key string) []string {
108-
for _, header := range *headers {
109-
if header.Key == key {
110-
return header.Value
107+
func (headers Headers) GetValue(key string) *[]string {
108+
for i := range headers {
109+
if headers[i].Key == key {
110+
return &headers[i].Value
111111
}
112112
}
113113
return nil

types/params.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (params Params) String() string {
2121

2222
displayLimit := 3
2323

24-
for i, item := range params {
24+
for i, item := range params[:min(len(params), displayLimit)] {
2525
if i > 0 {
2626
buffer.WriteString(",\n")
2727
}
@@ -96,18 +96,18 @@ func (params *Params) Set(value string) error {
9696
return nil
9797
}
9898

99-
func (params *Params) AppendByKey(key string, value string) {
100-
if existingValue := params.GetValue(key); existingValue != nil {
101-
*params = append(*params, KeyValue[string, []string]{Key: key, Value: append(existingValue, value)})
99+
func (params *Params) AppendByKey(key, value string) {
100+
if item := params.GetValue(key); item != nil {
101+
*item = append(*item, value)
102102
} else {
103103
*params = append(*params, KeyValue[string, []string]{Key: key, Value: []string{value}})
104104
}
105105
}
106106

107-
func (params *Params) GetValue(key string) []string {
108-
for _, param := range *params {
109-
if param.Key == key {
110-
return param.Value
107+
func (params Params) GetValue(key string) *[]string {
108+
for i := range params {
109+
if params[i].Key == key {
110+
return &params[i].Value
111111
}
112112
}
113113
return nil

0 commit comments

Comments
 (0)