Skip to content

Commit c848085

Browse files
authored
chore: add some utils (#228)
* chore: add some utils * chore: add tests
1 parent 2573556 commit c848085

File tree

4 files changed

+137
-1
lines changed

4 files changed

+137
-1
lines changed

core/util/http.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* Copyright © INFINI LTD. All rights reserved.
2+
* Web: https://infinilabs.com
3+
* Email: hello#infini.ltd */
4+
5+
package util
6+
7+
import (
8+
"bytes"
9+
"infini.sh/framework/core/errors"
10+
"io/ioutil"
11+
"net/http"
12+
)
13+
14+
func ReadBody(r *http.Request) ([]byte, error) {
15+
if r.ContentLength > 0 && r.Body != nil {
16+
content, err := ioutil.ReadAll(r.Body)
17+
_ = r.Body.Close()
18+
if err != nil {
19+
return nil, err
20+
}
21+
if len(content) == 0 {
22+
return nil, errors.NewWithCode(err, errors.BodyEmpty, r.URL.String())
23+
}
24+
25+
// Replace r.Body so it can be read again later
26+
r.Body = ioutil.NopCloser(bytes.NewBuffer(content))
27+
28+
return content, nil
29+
}
30+
return nil, errors.Error("request body is nil")
31+
}

core/util/set.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* Copyright © INFINI LTD. All rights reserved.
2+
* Web: https://infinilabs.com
3+
* Email: hello#infini.ltd */
4+
5+
package util
6+
7+
import "github.com/emirpasic/gods/sets/hashset"
8+
9+
func IsSuperset(a, b *hashset.Set) bool {
10+
for _, item := range b.Values() {
11+
if !a.Contains(item) {
12+
return false
13+
}
14+
}
15+
return true
16+
}

core/util/set_test.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/* Copyright © INFINI LTD. All rights reserved.
2+
* Web: https://infinilabs.com
3+
* Email: hello#infini.ltd */
4+
5+
package util
6+
7+
import (
8+
"github.com/emirpasic/gods/sets/hashset"
9+
"testing"
10+
)
11+
12+
func newSet(values ...interface{}) *hashset.Set {
13+
s := hashset.New()
14+
s.Add(values...)
15+
return s
16+
}
17+
18+
func TestIsSuperset(t *testing.T) {
19+
tests := []struct {
20+
name string
21+
a *hashset.Set
22+
b *hashset.Set
23+
expected bool
24+
}{
25+
{
26+
name: "both empty",
27+
a: newSet(),
28+
b: newSet(),
29+
expected: true, // empty set is superset of empty
30+
},
31+
{
32+
name: "a empty, b non-empty",
33+
a: newSet(),
34+
b: newSet("x"),
35+
expected: false,
36+
},
37+
{
38+
name: "a non-empty, b empty",
39+
a: newSet("x", "y"),
40+
b: newSet(),
41+
expected: true,
42+
},
43+
{
44+
name: "a == b",
45+
a: newSet(1, 2, 3),
46+
b: newSet(1, 2, 3),
47+
expected: true,
48+
},
49+
{
50+
name: "a larger than b (true superset)",
51+
a: newSet(1, 2, 3, 4),
52+
b: newSet(2, 3),
53+
expected: true,
54+
},
55+
{
56+
name: "a missing one element from b",
57+
a: newSet(1, 2),
58+
b: newSet(1, 2, 3),
59+
expected: false,
60+
},
61+
{
62+
name: "different types - string elements",
63+
a: newSet("a", "b", "c"),
64+
b: newSet("b", "c"),
65+
expected: true,
66+
},
67+
{
68+
name: "b contains element not in a",
69+
a: newSet("a", "b"),
70+
b: newSet("a", "b", "c"),
71+
expected: false,
72+
},
73+
{
74+
name: "subset partially overlapping",
75+
a: newSet("x", "y"),
76+
b: newSet("y", "z"),
77+
expected: false,
78+
},
79+
}
80+
81+
for _, tt := range tests {
82+
t.Run(tt.name, func(t *testing.T) {
83+
result := IsSuperset(tt.a, tt.b)
84+
if result != tt.expected {
85+
t.Errorf("IsSuperset(%v, %v) = %v; want %v", tt.a.Values(), tt.b.Values(), result, tt.expected)
86+
}
87+
})
88+
}
89+
}

core/util/stringutil.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ const (
619619
lowercaseChars = "abcdefghijklmnopqrstuvwxyz"
620620
uppercaseChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
621621
digitChars = "0123456789"
622-
specialChars = "!@#%^&*_+-=?"
622+
specialChars = "!@#%^&$*_+-=?,.~`;:|()<>"
623623

624624
allChars = lowercaseChars + uppercaseChars + digitChars + specialChars
625625
simpleChars = lowercaseChars + digitChars

0 commit comments

Comments
 (0)