Skip to content

Commit e8d3ca9

Browse files
authored
Simplify onlyContainsPrimitiveTypes and fix isObject nil panic (#332)
1 parent 99502e8 commit e8d3ca9

File tree

6 files changed

+85
-47
lines changed

6 files changed

+85
-47
lines changed

lib/agent/api_discovery/isPrimitiveType.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
package api_discovery
22

3-
func onlyContainsPrimitiveTypes(types interface{}) bool {
4-
switch t := types.(type) {
5-
case string:
6-
return isPrimitiveType(t)
7-
case []string:
8-
for _, item := range t {
9-
if !isPrimitiveType(item) {
10-
return false
11-
}
3+
func onlyContainsPrimitiveTypes(types []string) bool {
4+
for _, item := range types {
5+
if !isPrimitiveType(item) {
6+
return false
127
}
13-
return true
14-
default:
15-
return false
168
}
9+
return true
1710
}
1811

1912
func isPrimitiveType(typeStr string) bool {
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package api_discovery
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestIsPrimitiveType(t *testing.T) {
10+
testCases := []struct {
11+
str string
12+
expected bool
13+
}{
14+
{str: "string", expected: true},
15+
{str: "number", expected: true},
16+
{str: "boolean", expected: true},
17+
{str: "null", expected: true},
18+
{str: "array", expected: false},
19+
{str: "object", expected: false},
20+
}
21+
22+
for _, tc := range testCases {
23+
t.Run(tc.str, func(t *testing.T) {
24+
result := isPrimitiveType(tc.str)
25+
assert.Equal(t, tc.expected, result)
26+
})
27+
}
28+
}
29+
30+
func TestOnlyContainsPrimitiveTypes(t *testing.T) {
31+
testCases := []struct {
32+
name string
33+
types []string
34+
expected bool
35+
}{
36+
{name: "all primitive", types: []string{"string", "number", "boolean"}, expected: true},
37+
{name: "contains object", types: []string{"number", "object"}, expected: false},
38+
{name: "contains array", types: []string{"string", "array"}, expected: false},
39+
{name: "both object and array", types: []string{"object", "array"}, expected: false},
40+
{name: "empty", types: []string{}, expected: true},
41+
{name: "single primitive", types: []string{"string"}, expected: true},
42+
}
43+
44+
for _, tc := range testCases {
45+
t.Run(tc.name, func(t *testing.T) {
46+
result := onlyContainsPrimitiveTypes(tc.types)
47+
assert.Equal(t, tc.expected, result)
48+
})
49+
}
50+
}

lib/request-processor/api_discovery/getApiAuthType.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package api_discovery
33
import (
44
"main/context"
55
"main/ipc/protos"
6+
"slices"
67
"strings"
78
)
89

@@ -98,7 +99,7 @@ func findApiKeys() []*protos.APIAuthType {
9899
if len(cookies) > 0 {
99100
for cookieName := range cookies {
100101
lowerCookieName := strings.ToLower(cookieName)
101-
if contains(commonAuthCookieNames, lowerCookieName) {
102+
if slices.Contains(commonAuthCookieNames, lowerCookieName) {
102103
result = append(result, &protos.APIAuthType{
103104
Type: "apiKey",
104105
In: "cookie",
@@ -111,21 +112,11 @@ func findApiKeys() []*protos.APIAuthType {
111112
return result
112113
}
113114

114-
// contains checks if a string exists in a slice.
115-
func contains(slice []string, item string) bool {
116-
for _, a := range slice {
117-
if a == item {
118-
return true
119-
}
120-
}
121-
return false
122-
}
123-
124115
// isHTTPAuthScheme checks if the given string is a valid HTTP authentication scheme.
125116
// You will need to implement this function similar to the TypeScript helper function.
126117
func isHTTPAuthScheme(scheme string) bool {
127118
// You can add proper logic here to check the scheme, e.g., "basic", "bearer", etc.
128119
// For example:
129120
allowedSchemes := []string{"basic", "bearer"}
130-
return contains(allowedSchemes, strings.ToLower(scheme))
121+
return slices.Contains(allowedSchemes, strings.ToLower(scheme))
131122
}

lib/request-processor/api_discovery/getApiInfo.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"main/context"
66
"main/ipc/protos"
77
"main/log"
8+
"reflect"
89
)
910

1011
func GetApiInfo(server *ServerData) *protos.APISpec {
@@ -57,7 +58,10 @@ func GetApiInfo(server *ServerData) *protos.APISpec {
5758
}
5859

5960
func isObject(data interface{}) bool {
61+
if data == nil {
62+
return false
63+
}
64+
6065
// Helper function to determine if the data is an object (map in Go)
61-
_, ok := data.(map[string]interface{})
62-
return ok
66+
return reflect.TypeOf(data).Kind() == reflect.Map
6367
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package api_discovery
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestIsObject(t *testing.T) {
10+
t.Run("Test isObject", func(t *testing.T) {
11+
assert.Equal(t, false, isObject(""))
12+
assert.Equal(t, false, isObject([]string{"1"}))
13+
assert.Equal(t, false, isObject(nil))
14+
15+
assert.Equal(t, true, isObject(map[string]any{"1": "2"}))
16+
assert.Equal(t, true, isObject(map[string]string{"1": "2"}))
17+
assert.Equal(t, true, isObject(map[string]int{"1": 500}))
18+
assert.Equal(t, true, isObject(map[string][]string{"1": {"2"}}))
19+
assert.Equal(t, true, isObject(map[string][]any{"1": {"2"}}))
20+
})
21+
}

lib/request-processor/api_discovery/isPrimitiveType.go

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)