Compare commits

...

2 Commits

Author SHA1 Message Date
2225b95e3e try to fixup
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-05-20 08:55:24 +03:00
b76b8d3114 fixup path filling
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-05-04 15:05:21 +03:00
5 changed files with 75 additions and 30 deletions

2
go.mod
View File

@@ -2,7 +2,7 @@ module go.unistack.org/micro-client-http/v3
go 1.20 go 1.20
require go.unistack.org/micro/v3 v3.10.67 require go.unistack.org/micro/v3 v3.10.69
require ( require (
golang.org/x/sys v0.19.0 // indirect 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= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
go.unistack.org/micro/v3 v3.10.67 h1:Bk2VXsmMoKS2xkiultQS5WQCzsQlinYY+cAk4mAXi0U= go.unistack.org/micro/v3 v3.10.69 h1:V4g9LqUhzGab73U2aevnjtffCdJqGTuMfWY7fy4qGwI=
go.unistack.org/micro/v3 v3.10.67/go.mod h1:erMgt3Bl7vQQ0e9UpQyR5NlLiZ9pKeEJ9+1tfYFaqUg= go.unistack.org/micro/v3 v3.10.69/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=

View File

@@ -1,23 +1,16 @@
package http package http
import ( import (
"encoding/json"
"fmt"
"net/url" "net/url"
"strings" "strings"
"testing" "testing"
) )
type request struct { func TestNestedPathPost(t *testing.T) {
Nested *request `json:"nested"` req := &request{Name: "first", Field1: "fieldval"}
Name string `json:"name"` p, m, err := newPathRequest("/api/v1/xxxx", "POST", "*", req, []string{"json", "protobuf"}, nil)
Field1 string `json:"field1"`
ClientID string
Field2 string
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 { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -25,9 +18,39 @@ func TestNestedPath(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if s := u.String(); s != "/api/v1/first/second?field1=fieldval" { if s := u.String(); s != "/api/v1/xxxx" {
t.Fatalf("nested path error %s", s) t.Fatalf("nested path error %s", s)
} }
_ = m
}
type request struct {
NestedTest *request `json:"nested_test,omitempty"`
Name string `json:"name,omitempty"`
Field1 string `json:"field1,omitempty"`
ClientID string `json:",omitempty"`
Field2 string `json:",omitempty"`
Field3 int64 `json:",omitempty"`
}
func TestNestedPath(t *testing.T) {
req := &request{Name: "first", NestedTest: &request{Name: "second"}, Field1: "fieldval"}
p, m, err := newPathRequest("/api/v1/{name}/{nested_test.name}", "PUT", "*", 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" {
t.Fatalf("nested path error %s", s)
}
b, err := json.Marshal(m)
if err != nil {
t.Fatal(err)
}
fmt.Printf("m %#+v %s\n", m, b)
} }
func TestPathWithHeader(t *testing.T) { func TestPathWithHeader(t *testing.T) {

35
util.go
View File

@@ -88,6 +88,7 @@ func newPathRequest(path string, method string, body string, msg interface{}, ta
values := url.Values{} values := url.Values{}
// copy cycle // copy cycle
cleanPath := make(map[string]bool)
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() {
@@ -156,29 +157,28 @@ func newPathRequest(path string, method string, body string, msg interface{}, ta
default: default:
fieldsmap[t.name] = getParam(val) fieldsmap[t.name] = getParam(val)
} }
} else if (body == "*" || body == t.name) && method != http.MethodGet {
if tnmsg.Field(i).CanSet() {
tnmsg.Field(i).Set(val)
}
} else { } else {
isSet := false
for k, v := range fieldsmap { for k, v := range fieldsmap {
isSet := false
if v != "" { if v != "" {
continue continue
} }
var clean []string
fld := msg fld := msg
parts := strings.Split(k, ".") parts := strings.Split(k, ".")
for idx := 0; idx < len(parts); idx++ { for idx := 0; idx < len(parts); idx++ {
var nfld interface{} var nfld interface{}
var name string
if tags == nil { if tags == nil {
tags = []string{"json"} tags = []string{"json"}
} }
tagsloop: tagsloop:
for i := 0; i < len(tags); i++ { for ti := 0; ti < len(tags); ti++ {
nfld, err = rutil.StructFieldByTag(fld, tags[i], parts[idx]) name, nfld, err = rutil.StructFieldNameByTag(fld, tags[ti], parts[idx])
if err == nil { if err == nil {
clean = append(clean, name)
break tagsloop break tagsloop
} }
} }
@@ -187,20 +187,33 @@ func newPathRequest(path string, method string, body string, msg interface{}, ta
if len(parts)-1 == idx { if len(parts)-1 == idx {
isSet = true isSet = true
fieldsmap[k] = fmt.Sprintf("%v", fld) fieldsmap[k] = fmt.Sprintf("%v", fld)
} }
} }
} }
if isSet {
cleanPath[strings.Join(clean, ".")] = true
}
} }
for k := range cleanPath {
if !isSet { if err = rutil.ZeroFieldByPath(nmsg, k); err != nil {
return "", nil, err
}
}
if (body == "*" || body == t.name) && method != http.MethodGet {
if tnmsg.Field(i).CanSet() {
tnmsg.Field(i).Set(val)
}
} else if method == http.MethodGet {
if val.Type().Kind() == reflect.Slice { if val.Type().Kind() == reflect.Slice {
for idx := 0; idx < val.Len(); idx++ { for idx := 0; idx < val.Len(); idx++ {
values.Add(t.name, getParam(val.Index(idx))) values.Add(t.name, getParam(val.Index(idx)))
} }
} else { } else if !rutil.IsEmpty(val) {
values.Add(t.name, getParam(val)) values.Add(t.name, getParam(val))
} }
} }
} }
} }
@@ -252,6 +265,8 @@ func newPathRequest(path string, method string, body string, msg interface{}, ta
_, _ = b.WriteString(values.Encode()) _, _ = b.WriteString(values.Encode())
} }
// rutil.ZeroEmpty(tnmsg.Interface())
if rutil.IsZero(nmsg) && !isEmptyStruct(nmsg) { if rutil.IsZero(nmsg) && !isEmptyStruct(nmsg) {
return b.String(), nil, nil return b.String(), nil, nil
} }

View File

@@ -1,6 +1,7 @@
package http package http
import ( import (
"net/http"
"net/url" "net/url"
"testing" "testing"
) )
@@ -52,10 +53,16 @@ func TestNewPathRequest(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
vals := u.Query() switch m {
if v, ok := vals["name"]; !ok || v[0] != "test_name" { case http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete:
t.Fatalf("invalid path: %v nmsg: %v", path, nmsg) break
case http.MethodGet:
vals := u.Query()
if v, ok := vals["name"]; !ok || v[0] != "test_name" {
t.Fatalf("%s invalid path: %v nmsg: %v", m, path, nmsg)
}
} }
} }
} }
@@ -119,7 +126,7 @@ func TestNewPathVarRequest(t *testing.T) {
t.Fatalf("invalid nmsg: %#+v\n", nmsg) t.Fatalf("invalid nmsg: %#+v\n", nmsg)
} }
if nmsg.(*Message).Name != "test_name" { if nmsg.(*Message).Name != "test_name" {
t.Fatalf("invalid nmsg: %v nmsg: %v", path, nmsg) t.Fatalf("invalid path: %v nmsg: %#+v", path, nmsg)
} }
} else { } else {
vals := u.Query() vals := u.Query()