mirror of
https://github.com/duke-git/lancet.git
synced 2026-03-01 00:35:28 +08:00
Compare commits
3 Commits
71aa91a58d
...
51a6912eb3
| Author | SHA1 | Date | |
|---|---|---|---|
| 51a6912eb3 | |||
| 28d0428b50 | |||
| 6a9eb645bb |
+13
-5
@@ -591,20 +591,28 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
type TodoQuery struct {
|
type TodoQuery struct {
|
||||||
Id int `json:"id"`
|
Id int `json:"id"`
|
||||||
Name string `json:"name"`
|
UserId int `json:"userId"`
|
||||||
|
Name string `json:"name,omitempty"`
|
||||||
|
Status string
|
||||||
}
|
}
|
||||||
todoQuery := TodoQuery{
|
item := TodoQuery{
|
||||||
Id: 1,
|
Id: 1,
|
||||||
Name: "Test",
|
UserId: 123,
|
||||||
|
Name: "test",
|
||||||
|
Status: "completed",
|
||||||
}
|
}
|
||||||
todoValues := netutil.StructToUrlValues(todoQuery)
|
queryValues := netutil.StructToUrlValues(item)
|
||||||
|
|
||||||
fmt.Println(todoValues.Get("id"))
|
fmt.Println(todoValues.Get("id"))
|
||||||
|
fmt.Println(todoValues.Get("userId"))
|
||||||
fmt.Println(todoValues.Get("name"))
|
fmt.Println(todoValues.Get("name"))
|
||||||
|
fmt.Println(todoValues.Get("status"))
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 1
|
// 1
|
||||||
// Test
|
// 123
|
||||||
|
// test
|
||||||
|
//
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
+13
-5
@@ -593,20 +593,28 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
type TodoQuery struct {
|
type TodoQuery struct {
|
||||||
Id int `json:"id"`
|
Id int `json:"id"`
|
||||||
Name string `json:"name"`
|
UserId int `json:"userId"`
|
||||||
|
Name string `json:"name,omitempty"`
|
||||||
|
Status string
|
||||||
}
|
}
|
||||||
todoQuery := TodoQuery{
|
item := TodoQuery{
|
||||||
Id: 1,
|
Id: 1,
|
||||||
Name: "Test",
|
UserId: 123,
|
||||||
|
Name: "test",
|
||||||
|
Status: "completed",
|
||||||
}
|
}
|
||||||
todoValues := netutil.StructToUrlValues(todoQuery)
|
queryValues := netutil.StructToUrlValues(item)
|
||||||
|
|
||||||
fmt.Println(todoValues.Get("id"))
|
fmt.Println(todoValues.Get("id"))
|
||||||
|
fmt.Println(todoValues.Get("userId"))
|
||||||
fmt.Println(todoValues.Get("name"))
|
fmt.Println(todoValues.Get("name"))
|
||||||
|
fmt.Println(todoValues.Get("status"))
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 1
|
// 1
|
||||||
// Test
|
// 123
|
||||||
|
// test
|
||||||
|
//
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -183,3 +183,36 @@ func Average[T constraints.Integer | constraints.Float](numbers ...T) T {
|
|||||||
}
|
}
|
||||||
return sum / n
|
return sum / n
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Range creates a slice of numbers from start with specified count, element step is 1.
|
||||||
|
// Play: todo
|
||||||
|
func Range[T constraints.Integer | constraints.Float](start T, count int) []T {
|
||||||
|
size := count
|
||||||
|
if count < 0 {
|
||||||
|
size = -count
|
||||||
|
}
|
||||||
|
|
||||||
|
result := make([]T, size)
|
||||||
|
|
||||||
|
for i, j := 0, start; i < size; i, j = i+1, j+1 {
|
||||||
|
result[i] = j
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// RangeWithStep creates a slice of numbers from start to end with specified step.
|
||||||
|
// Play: todo
|
||||||
|
func RangeWithStep[T constraints.Integer | constraints.Float](start, end, step T) []T {
|
||||||
|
result := []T{}
|
||||||
|
|
||||||
|
if start >= end || step == 0 {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := start; i < end; i += step {
|
||||||
|
result = append(result, i)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|||||||
@@ -185,3 +185,39 @@ func ExampleMinBy() {
|
|||||||
// ab
|
// ab
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleRange() {
|
||||||
|
result1 := Range(1, 4)
|
||||||
|
result2 := Range(1, -4)
|
||||||
|
result3 := Range(-4, 4)
|
||||||
|
result4 := Range(1.0, 4)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [1 2 3 4]
|
||||||
|
// [1 2 3 4]
|
||||||
|
// [-4 -3 -2 -1]
|
||||||
|
// [1 2 3 4]
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleRangeWithStep() {
|
||||||
|
result1 := RangeWithStep(1, 4, 1)
|
||||||
|
result2 := RangeWithStep(1, -1, 0)
|
||||||
|
result3 := RangeWithStep(-4, 1, 2)
|
||||||
|
result4 := RangeWithStep(1.0, 4.0, 1.1)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [1 2 3]
|
||||||
|
// []
|
||||||
|
// [-4 -2 0]
|
||||||
|
// [1 2.1 3.2]
|
||||||
|
}
|
||||||
|
|||||||
@@ -137,3 +137,33 @@ func TestMinBy(t *testing.T) {
|
|||||||
})
|
})
|
||||||
assert.Equal("", res3)
|
assert.Equal("", res3)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRange(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "Range")
|
||||||
|
|
||||||
|
result1 := Range(1, 4)
|
||||||
|
result2 := Range(1, -4)
|
||||||
|
result3 := Range(-4, 4)
|
||||||
|
result4 := Range(1.0, 4)
|
||||||
|
result5 := Range(1, 0)
|
||||||
|
|
||||||
|
assert.Equal([]int{1, 2, 3, 4}, result1)
|
||||||
|
assert.Equal([]int{1, 2, 3, 4}, result2)
|
||||||
|
assert.Equal([]int{-4, -3, -2, -1}, result3)
|
||||||
|
assert.Equal([]float64{1.0, 2.0, 3.0, 4.0}, result4)
|
||||||
|
assert.Equal([]int{}, result5)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRangeWithStep(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "Range")
|
||||||
|
|
||||||
|
result1 := RangeWithStep(1, 4, 1)
|
||||||
|
result2 := RangeWithStep(1, -1, 0)
|
||||||
|
result3 := RangeWithStep(-4, 1, 2)
|
||||||
|
result4 := RangeWithStep(1.0, 4.0, 1.1)
|
||||||
|
|
||||||
|
assert.Equal([]int{1, 2, 3}, result1)
|
||||||
|
assert.Equal([]int{}, result2)
|
||||||
|
assert.Equal([]int{-4, -2, 0}, result3)
|
||||||
|
assert.Equal([]float64{1.0, 2.1, 3.2}, result4)
|
||||||
|
}
|
||||||
|
|||||||
@@ -297,7 +297,11 @@ func StructToUrlValues(targetStruct any) url.Values {
|
|||||||
for i := 0; i < fieldNum; i++ {
|
for i := 0; i < fieldNum; i++ {
|
||||||
name := rt.Field(i).Name
|
name := rt.Field(i).Name
|
||||||
tag := rt.Field(i).Tag.Get("json")
|
tag := rt.Field(i).Tag.Get("json")
|
||||||
|
// if regex.MatchString(name) && tag != "" && !strings.Contains(tag, "omitempty"){
|
||||||
if regex.MatchString(name) && tag != "" {
|
if regex.MatchString(name) && tag != "" {
|
||||||
|
if strings.Contains(tag, "omitempty") {
|
||||||
|
tag = strings.Split(tag, ",")[0]
|
||||||
|
}
|
||||||
result.Add(tag, fmt.Sprintf("%v", rv.Field(i).Interface()))
|
result.Add(tag, fmt.Sprintf("%v", rv.Field(i).Interface()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+18
-17
@@ -219,28 +219,29 @@ func TestStructToUrlValues(t *testing.T) {
|
|||||||
type TodoQuery struct {
|
type TodoQuery struct {
|
||||||
Id int `json:"id"`
|
Id int `json:"id"`
|
||||||
UserId int `json:"userId"`
|
UserId int `json:"userId"`
|
||||||
|
Name string `json:"name,omitempty"`
|
||||||
|
Status string
|
||||||
}
|
}
|
||||||
todoQuery := TodoQuery{
|
item1 := TodoQuery{
|
||||||
Id: 1,
|
Id: 1,
|
||||||
UserId: 1,
|
UserId: 123,
|
||||||
|
Name: "test",
|
||||||
|
Status: "completed",
|
||||||
}
|
}
|
||||||
todoValues := StructToUrlValues(todoQuery)
|
queryValues1 := StructToUrlValues(item1)
|
||||||
|
|
||||||
assert.Equal("1", todoValues.Get("id"))
|
assert.Equal("1", queryValues1.Get("id"))
|
||||||
assert.Equal("1", todoValues.Get("userId"))
|
assert.Equal("123", queryValues1.Get("userId"))
|
||||||
|
assert.Equal("test", queryValues1.Get("name"))
|
||||||
|
assert.Equal("", queryValues1.Get("status"))
|
||||||
|
|
||||||
request := &HttpRequest{
|
item2 := TodoQuery{
|
||||||
RawURL: "https://jsonplaceholder.typicode.com/todos",
|
Id: 2,
|
||||||
Method: "GET",
|
UserId: 456,
|
||||||
QueryParams: todoValues,
|
|
||||||
}
|
}
|
||||||
|
queryValues2 := StructToUrlValues(item2)
|
||||||
|
|
||||||
httpClient := NewHttpClient()
|
assert.Equal("2", queryValues2.Get("id"))
|
||||||
resp, err := httpClient.SendRequest(request)
|
assert.Equal("456", queryValues2.Get("userId"))
|
||||||
if err != nil || resp.StatusCode != 200 {
|
assert.Equal("", queryValues2.Get("name"))
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
body, _ := io.ReadAll(resp.Body)
|
|
||||||
t.Log("response: ", string(body))
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -124,20 +124,41 @@ func ExampleHttpClient_DecodeResponse() {
|
|||||||
func ExampleStructToUrlValues() {
|
func ExampleStructToUrlValues() {
|
||||||
type TodoQuery struct {
|
type TodoQuery struct {
|
||||||
Id int `json:"id"`
|
Id int `json:"id"`
|
||||||
Name string `json:"name"`
|
UserId int `json:"userId"`
|
||||||
|
Name string `json:"name,omitempty"`
|
||||||
|
Status string
|
||||||
}
|
}
|
||||||
todoQuery := TodoQuery{
|
item1 := TodoQuery{
|
||||||
Id: 1,
|
Id: 1,
|
||||||
Name: "Test",
|
UserId: 123,
|
||||||
|
Name: "test",
|
||||||
|
Status: "completed",
|
||||||
}
|
}
|
||||||
todoValues := StructToUrlValues(todoQuery)
|
queryValues1 := StructToUrlValues(item1)
|
||||||
|
|
||||||
fmt.Println(todoValues.Get("id"))
|
item2 := TodoQuery{
|
||||||
fmt.Println(todoValues.Get("name"))
|
Id: 2,
|
||||||
|
UserId: 456,
|
||||||
|
}
|
||||||
|
queryValues2 := StructToUrlValues(item2)
|
||||||
|
|
||||||
|
fmt.Println(queryValues1.Get("id"))
|
||||||
|
fmt.Println(queryValues1.Get("userId"))
|
||||||
|
fmt.Println(queryValues1.Get("name"))
|
||||||
|
fmt.Println(queryValues1.Get("status"))
|
||||||
|
|
||||||
|
fmt.Println(queryValues2.Get("id"))
|
||||||
|
fmt.Println(queryValues2.Get("userId"))
|
||||||
|
fmt.Println(queryValues2.Get("name"))
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 1
|
// 1
|
||||||
// Test
|
// 123
|
||||||
|
// test
|
||||||
|
//
|
||||||
|
// 2
|
||||||
|
// 456
|
||||||
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleConvertMapToQueryString() {
|
func ExampleConvertMapToQueryString() {
|
||||||
|
|||||||
Reference in New Issue
Block a user