diff --git a/go.mod b/go.mod index e09b5d3..8e2693e 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,7 @@ module go.unistack.org/micro-client-http/v3 go 1.20 -require go.unistack.org/micro/v3 v3.10.67 +require go.unistack.org/micro/v3 v3.10.69 require ( golang.org/x/sys v0.19.0 // indirect diff --git a/go.sum b/go.sum index 9154449..3483f31 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,6 @@ 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.67/go.mod h1:erMgt3Bl7vQQ0e9UpQyR5NlLiZ9pKeEJ9+1tfYFaqUg= +go.unistack.org/micro/v3 v3.10.69 h1:V4g9LqUhzGab73U2aevnjtffCdJqGTuMfWY7fy4qGwI= +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/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= diff --git a/http_test.go b/http_test.go index 52f5b19..fb8e4d6 100644 --- a/http_test.go +++ b/http_test.go @@ -7,17 +7,17 @@ import ( ) type request struct { - Nested *request `json:"nested"` - Name string `json:"name"` - Field1 string `json:"field1"` - ClientID string - Field2 string - Field3 int64 + NestedTest *request `json:"nested_test"` + Name string `json:"name"` + 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) + 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) } @@ -28,6 +28,7 @@ func TestNestedPath(t *testing.T) { if s := u.String(); s != "/api/v1/first/second?field1=fieldval" { t.Fatalf("nested path error %s", s) } + _ = m } func TestPathWithHeader(t *testing.T) { diff --git a/util.go b/util.go index 3a5e68c..3e6f221 100644 --- a/util.go +++ b/util.go @@ -88,6 +88,7 @@ func newPathRequest(path string, method string, body string, msg interface{}, ta values := url.Values{} // copy cycle + cleanPath := make(map[string]bool) for i := 0; i < tmsg.NumField(); i++ { val := tmsg.Field(i) if val.IsZero() { @@ -156,29 +157,28 @@ func newPathRequest(path string, method string, body string, msg interface{}, ta default: 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 { - isSet := false for k, v := range fieldsmap { + isSet := false if v != "" { continue } + var clean []string fld := msg parts := strings.Split(k, ".") for idx := 0; idx < len(parts); idx++ { var nfld interface{} + var name string if tags == nil { tags = []string{"json"} } tagsloop: - for i := 0; i < len(tags); i++ { - nfld, err = rutil.StructFieldByTag(fld, tags[i], parts[idx]) + for ti := 0; ti < len(tags); ti++ { + name, nfld, err = rutil.StructFieldNameByTag(fld, tags[ti], parts[idx]) if err == nil { + clean = append(clean, name) break tagsloop } } @@ -187,20 +187,34 @@ func newPathRequest(path string, method string, body string, msg interface{}, ta 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)) + if isSet { + 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))) + } + } else if !rutil.IsEmpty(val) { + values.Add(t.name, getParam(val)) + } + } } diff --git a/util_test.go b/util_test.go index 1c0b713..24d7f2f 100644 --- a/util_test.go +++ b/util_test.go @@ -119,7 +119,7 @@ func TestNewPathVarRequest(t *testing.T) { t.Fatalf("invalid nmsg: %#+v\n", nmsg) } 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 { vals := u.Query()