Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
b92b2541e0 | |||
9d955fc079 | |||
1be75ea264 |
4
go.mod
4
go.mod
@@ -1,8 +1,8 @@
|
|||||||
module go.unistack.org/micro-client-http/v3
|
module go.unistack.org/micro-client-http/v3
|
||||||
|
|
||||||
go 1.18
|
go 1.20
|
||||||
|
|
||||||
require go.unistack.org/micro/v3 v3.10.64
|
require go.unistack.org/micro/v3 v3.10.67
|
||||||
|
|
||||||
require (
|
require (
|
||||||
golang.org/x/sys v0.19.0 // indirect
|
golang.org/x/sys v0.19.0 // indirect
|
||||||
|
4
go.sum
4
go.sum
@@ -1,6 +1,6 @@
|
|||||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||||
go.unistack.org/micro/v3 v3.10.64 h1:hTRW/4krLjdGb1gDwN8UDub0RmUqrn/oYclL4RXZTOw=
|
go.unistack.org/micro/v3 v3.10.67 h1:Bk2VXsmMoKS2xkiultQS5WQCzsQlinYY+cAk4mAXi0U=
|
||||||
go.unistack.org/micro/v3 v3.10.64/go.mod h1:erMgt3Bl7vQQ0e9UpQyR5NlLiZ9pKeEJ9+1tfYFaqUg=
|
go.unistack.org/micro/v3 v3.10.67/go.mod h1:erMgt3Bl7vQQ0e9UpQyR5NlLiZ9pKeEJ9+1tfYFaqUg=
|
||||||
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
|
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
|
||||||
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
|
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
|
||||||
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
|
34
http_test.go
34
http_test.go
@@ -6,16 +6,32 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Request struct {
|
type request struct {
|
||||||
Name string `json:"name"`
|
Nested *request `json:"nested"`
|
||||||
Field1 string `json:"field1"`
|
Name string `json:"name"`
|
||||||
|
Field1 string `json:"field1"`
|
||||||
ClientID string
|
ClientID string
|
||||||
Field2 string
|
Field2 string
|
||||||
Field3 int64
|
Field3 int64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNestedPath(t *testing.T) {
|
||||||
|
req := &request{Name: "first", Nested: &request{Name: "second"}, Field1: "fieldval"}
|
||||||
|
p, _, err := newPathRequest("/api/v1/{name}/{nested.name}", "GET", "", req, []string{"json", "protobuf"}, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
u, err := url.Parse(p)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if s := u.String(); s != "/api/v1/first/second?field1=fieldval" {
|
||||||
|
t.Fatalf("nested path error %s", s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestPathWithHeader(t *testing.T) {
|
func TestPathWithHeader(t *testing.T) {
|
||||||
req := &Request{Name: "vtolstov", Field1: "field1", ClientID: "1234567890"}
|
req := &request{Name: "vtolstov", Field1: "field1", ClientID: "1234567890"}
|
||||||
p, m, err := newPathRequest(
|
p, m, err := newPathRequest(
|
||||||
"/api/v1/test?Name={name}&Field1={field1}",
|
"/api/v1/test?Name={name}&Field1={field1}",
|
||||||
"POST",
|
"POST",
|
||||||
@@ -40,7 +56,7 @@ func TestPathWithHeader(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestPathValues(t *testing.T) {
|
func TestPathValues(t *testing.T) {
|
||||||
req := &Request{Name: "vtolstov", Field1: "field1"}
|
req := &request{Name: "vtolstov", Field1: "field1"}
|
||||||
p, m, err := newPathRequest("/api/v1/test?Name={name}&Field1={field1}", "POST", "*", req, nil, nil)
|
p, m, err := newPathRequest("/api/v1/test?Name={name}&Field1={field1}", "POST", "*", req, nil, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -56,7 +72,7 @@ func TestPathValues(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestValidPath(t *testing.T) {
|
func TestValidPath(t *testing.T) {
|
||||||
req := &Request{Name: "vtolstov", Field1: "field1", Field2: "field2", Field3: 10}
|
req := &request{Name: "vtolstov", Field1: "field1", Field2: "field2", Field3: 10}
|
||||||
p, m, err := newPathRequest("/api/v1/{name}/list", "GET", "", req, nil, nil)
|
p, m, err := newPathRequest("/api/v1/{name}/list", "GET", "", req, nil, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -73,9 +89,9 @@ func TestValidPath(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestInvalidPath(t *testing.T) {
|
func TestInvalidPath(t *testing.T) {
|
||||||
req := &Request{Name: "vtolstov", Field1: "field1", Field2: "field2", Field3: 10}
|
req := &request{Name: "vtolstov", Field1: "field1", Field2: "field2", Field3: 10}
|
||||||
_, _, err := newPathRequest("/api/v1/{xname}/list", "GET", "", req, nil, nil)
|
s, _, err := newPathRequest("/api/v1/{xname}/list", "GET", "", req, nil, nil)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatal("path param must not be filled")
|
t.Fatalf("path param must not be filled: %s", s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
45
util.go
45
util.go
@@ -87,6 +87,7 @@ func newPathRequest(path string, method string, body string, msg interface{}, ta
|
|||||||
|
|
||||||
values := url.Values{}
|
values := url.Values{}
|
||||||
// copy cycle
|
// copy cycle
|
||||||
|
|
||||||
for i := 0; i < tmsg.NumField(); i++ {
|
for i := 0; i < tmsg.NumField(); i++ {
|
||||||
val := tmsg.Field(i)
|
val := tmsg.Field(i)
|
||||||
if val.IsZero() {
|
if val.IsZero() {
|
||||||
@@ -121,6 +122,7 @@ func newPathRequest(path string, method string, body string, msg interface{}, ta
|
|||||||
default:
|
default:
|
||||||
t = &tag{key: tn, name: tp[0]}
|
t = &tag{key: tn, name: tp[0]}
|
||||||
}
|
}
|
||||||
|
|
||||||
if t.name != "" {
|
if t.name != "" {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -159,12 +161,45 @@ func newPathRequest(path string, method string, body string, msg interface{}, ta
|
|||||||
tnmsg.Field(i).Set(val)
|
tnmsg.Field(i).Set(val)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if val.Type().Kind() == reflect.Slice {
|
isSet := false
|
||||||
for idx := 0; idx < val.Len(); idx++ {
|
for k, v := range fieldsmap {
|
||||||
values.Add(t.name, getParam(val.Index(idx)))
|
if v != "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
fld := msg
|
||||||
|
|
||||||
|
parts := strings.Split(k, ".")
|
||||||
|
|
||||||
|
for idx := 0; idx < len(parts); idx++ {
|
||||||
|
var nfld interface{}
|
||||||
|
if tags == nil {
|
||||||
|
tags = []string{"json"}
|
||||||
|
}
|
||||||
|
tagsloop:
|
||||||
|
for i := 0; i < len(tags); i++ {
|
||||||
|
nfld, err = rutil.StructFieldByTag(fld, tags[i], parts[idx])
|
||||||
|
if err == nil {
|
||||||
|
break tagsloop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err == nil {
|
||||||
|
fld = nfld
|
||||||
|
if len(parts)-1 == idx {
|
||||||
|
isSet = true
|
||||||
|
fieldsmap[k] = fmt.Sprintf("%v", fld)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !isSet {
|
||||||
|
if val.Type().Kind() == reflect.Slice {
|
||||||
|
for idx := 0; idx < val.Len(); idx++ {
|
||||||
|
values.Add(t.name, getParam(val.Index(idx)))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
values.Add(t.name, getParam(val))
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
values.Add(t.name, getParam(val))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user