fill nested fields in path

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2024-05-02 14:40:12 +03:00
parent d4a07099a2
commit 1be75ea264
2 changed files with 53 additions and 12 deletions

View File

@ -6,7 +6,8 @@ import (
"testing" "testing"
) )
type Request struct { type request struct {
Nested *request `json:"nested"`
Name string `json:"name"` Name string `json:"name"`
Field1 string `json:"field1"` Field1 string `json:"field1"`
ClientID string ClientID string
@ -14,8 +15,23 @@ type Request struct {
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 u.String() != "/api/v1/first/second?field1=fieldval" {
t.Fatal("nested path error")
}
}
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,7 +89,7 @@ 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) _, _, 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.Fatal("path param must not be filled")

25
util.go
View File

@ -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,6 +161,28 @@ func newPathRequest(path string, method string, body string, msg interface{}, ta
tnmsg.Field(i).Set(val) tnmsg.Field(i).Set(val)
} }
} else { } else {
isSet := false
for k, v := range fieldsmap {
if v != "" {
continue
}
fld := msg
parts := strings.Split(k, ".")
for idx := 0; idx < len(parts); idx++ {
name := strings.Title(parts[idx])
fld, err = rutil.StructFieldByName(fld, name)
if err != nil {
break
}
if len(parts)-1 == idx {
isSet = true
fieldsmap[k] = fmt.Sprintf("%v", fld)
}
}
}
if !isSet {
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)))
@ -168,6 +192,7 @@ func newPathRequest(path string, method string, body string, msg interface{}, ta
} }
} }
} }
}
// check not filled stuff // check not filled stuff
for k, v := range fieldsmap { for k, v := range fieldsmap {