Bladeren bron

fix mapping inner structs with correct tag (#1718)

Dmitry Kutakov 7 jaren geleden
bovenliggende
commit
49e4b0c60c
2 gewijzigde bestanden met toevoegingen van 24 en 1 verwijderingen
  1. 23 0
      binding/binding_test.go
  2. 1 1
      binding/form_mapping.go

+ 23 - 0
binding/binding_test.go

@@ -11,6 +11,7 @@ import (
 	"io/ioutil"
 	"io/ioutil"
 	"mime/multipart"
 	"mime/multipart"
 	"net/http"
 	"net/http"
+	"strconv"
 	"testing"
 	"testing"
 	"time"
 	"time"
 
 
@@ -690,6 +691,28 @@ func TestUriBinding(t *testing.T) {
 	assert.Equal(t, map[string]interface{}(nil), not.Name)
 	assert.Equal(t, map[string]interface{}(nil), not.Name)
 }
 }
 
 
+func TestUriInnerBinding(t *testing.T) {
+	type Tag struct {
+		Name string `uri:"name"`
+		S    struct {
+			Age int `uri:"age"`
+		}
+	}
+
+	expectedName := "mike"
+	expectedAge := 25
+
+	m := map[string][]string{
+		"name": {expectedName},
+		"age":  {strconv.Itoa(expectedAge)},
+	}
+
+	var tag Tag
+	assert.NoError(t, Uri.BindUri(m, &tag))
+	assert.Equal(t, tag.Name, expectedName)
+	assert.Equal(t, tag.S.Age, expectedAge)
+}
+
 func testFormBinding(t *testing.T, method, path, badPath, body, badBody string) {
 func testFormBinding(t *testing.T, method, path, badPath, body, badBody string) {
 	b := Form
 	b := Form
 	assert.Equal(t, "form", b.Name())
 	assert.Equal(t, "form", b.Name())

+ 1 - 1
binding/form_mapping.go

@@ -55,7 +55,7 @@ func mapFormByTag(ptr interface{}, form map[string][]string, tag string) error {
 				structFieldKind = structField.Kind()
 				structFieldKind = structField.Kind()
 			}
 			}
 			if structFieldKind == reflect.Struct {
 			if structFieldKind == reflect.Struct {
-				err := mapForm(structField.Addr().Interface(), form)
+				err := mapFormByTag(structField.Addr().Interface(), form, tag)
 				if err != nil {
 				if err != nil {
 					return err
 					return err
 				}
 				}