Compare commits

...

3 Commits

Author SHA1 Message Date
b92b2541e0 fixup path filling
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-05-04 13:41:59 +03:00
9d955fc079 lower deps
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-05-04 09:18:37 +03:00
1be75ea264 fill nested fields in path
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-05-02 14:40:12 +03:00
4 changed files with 69 additions and 18 deletions

4
go.mod
View File

@@ -1,8 +1,8 @@
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 (
golang.org/x/sys v0.19.0 // indirect

4
go.sum
View File

@@ -1,6 +1,6 @@
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.64/go.mod h1:erMgt3Bl7vQQ0e9UpQyR5NlLiZ9pKeEJ9+1tfYFaqUg=
go.unistack.org/micro/v3 v3.10.67 h1:Bk2VXsmMoKS2xkiultQS5WQCzsQlinYY+cAk4mAXi0U=
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/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=

View File

@@ -6,7 +6,8 @@ import (
"testing"
)
type Request struct {
type request struct {
Nested *request `json:"nested"`
Name string `json:"name"`
Field1 string `json:"field1"`
ClientID string
@@ -14,8 +15,23 @@ type Request struct {
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) {
req := &Request{Name: "vtolstov", Field1: "field1", ClientID: "1234567890"}
req := &request{Name: "vtolstov", Field1: "field1", ClientID: "1234567890"}
p, m, err := newPathRequest(
"/api/v1/test?Name={name}&Field1={field1}",
"POST",
@@ -40,7 +56,7 @@ func TestPathWithHeader(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)
if err != nil {
t.Fatal(err)
@@ -56,7 +72,7 @@ func TestPathValues(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)
if err != nil {
t.Fatal(err)
@@ -73,9 +89,9 @@ func TestValidPath(t *testing.T) {
}
func TestInvalidPath(t *testing.T) {
req := &Request{Name: "vtolstov", Field1: "field1", Field2: "field2", Field3: 10}
_, _, err := newPathRequest("/api/v1/{xname}/list", "GET", "", req, nil, nil)
req := &request{Name: "vtolstov", Field1: "field1", Field2: "field2", Field3: 10}
s, _, err := newPathRequest("/api/v1/{xname}/list", "GET", "", req, nil, nil)
if err == nil {
t.Fatal("path param must not be filled")
t.Fatalf("path param must not be filled: %s", s)
}
}

35
util.go
View File

@@ -87,6 +87,7 @@ func newPathRequest(path string, method string, body string, msg interface{}, ta
values := url.Values{}
// copy cycle
for i := 0; i < tmsg.NumField(); i++ {
val := tmsg.Field(i)
if val.IsZero() {
@@ -121,6 +122,7 @@ func newPathRequest(path string, method string, body string, msg interface{}, ta
default:
t = &tag{key: tn, name: tp[0]}
}
if t.name != "" {
break
}
@@ -159,6 +161,38 @@ func newPathRequest(path string, method string, body string, msg interface{}, ta
tnmsg.Field(i).Set(val)
}
} else {
isSet := false
for k, v := range fieldsmap {
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)))
@@ -168,6 +202,7 @@ func newPathRequest(path string, method string, body string, msg interface{}, ta
}
}
}
}
// check not filled stuff
for k, v := range fieldsmap {