diff --git a/http_test.go b/http_test.go index fb8e4d6..14c8ea6 100644 --- a/http_test.go +++ b/http_test.go @@ -1,18 +1,36 @@ package http import ( + "encoding/json" + "fmt" "net/url" "strings" "testing" ) +func TestNestedPathPost(t *testing.T) { + req := &request{Name: "first", Field1: "fieldval"} + p, m, err := newPathRequest("/api/v1/xxxx", "POST", "*", 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/xxxx" { + t.Fatalf("nested path error %s", s) + } + _ = m +} + type request struct { - NestedTest *request `json:"nested_test"` - Name string `json:"name"` - Field1 string `json:"field1"` - ClientID string - Field2 string - Field3 int64 + 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) { @@ -25,10 +43,14 @@ func TestNestedPath(t *testing.T) { if err != nil { t.Fatal(err) } - if s := u.String(); s != "/api/v1/first/second?field1=fieldval" { + if s := u.String(); s != "/api/v1/first/second" { t.Fatalf("nested path error %s", s) } - _ = m + b, err := json.Marshal(m) + if err != nil { + t.Fatal(err) + } + fmt.Printf("m %#+v %s\n", m, b) } func TestPathWithHeader(t *testing.T) { diff --git a/util.go b/util.go index 3e6f221..95e0e6b 100644 --- a/util.go +++ b/util.go @@ -195,24 +195,23 @@ func newPathRequest(path string, method string, body string, msg interface{}, ta cleanPath[strings.Join(clean, ".")] = true } } - - if (body == "*" || body == t.name) && method != http.MethodGet { - if tnmsg.Field(i).CanSet() { - tnmsg.Field(i).Set(val) - } - } for k := range cleanPath { if err = rutil.ZeroFieldByPath(nmsg, k); err != nil { return "", nil, err } } - - if val.Type().Kind() == reflect.Slice { - for idx := 0; idx < val.Len(); idx++ { - values.Add(t.name, getParam(val.Index(idx))) + 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 { + for idx := 0; idx < val.Len(); idx++ { + values.Add(t.name, getParam(val.Index(idx))) + } + } else if !rutil.IsEmpty(val) { + values.Add(t.name, getParam(val)) } - } else if !rutil.IsEmpty(val) { - values.Add(t.name, getParam(val)) } } @@ -266,6 +265,8 @@ func newPathRequest(path string, method string, body string, msg interface{}, ta _, _ = b.WriteString(values.Encode()) } + // rutil.ZeroEmpty(tnmsg.Interface()) + if rutil.IsZero(nmsg) && !isEmptyStruct(nmsg) { return b.String(), nil, nil } diff --git a/util_test.go b/util_test.go index 24d7f2f..c84e2a2 100644 --- a/util_test.go +++ b/util_test.go @@ -1,6 +1,7 @@ package http import ( + "net/http" "net/url" "testing" ) @@ -52,10 +53,16 @@ func TestNewPathRequest(t *testing.T) { if err != nil { t.Fatal(err) } - vals := u.Query() - if v, ok := vals["name"]; !ok || v[0] != "test_name" { - t.Fatalf("invalid path: %v nmsg: %v", path, nmsg) + switch m { + case http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete: + 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) + } } + } }