Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use struct tags match Params when using UnmarshalParams #43

Open
stevvooe opened this issue Nov 28, 2010 · 0 comments
Open

Use struct tags match Params when using UnmarshalParams #43

stevvooe opened this issue Nov 28, 2010 · 0 comments

Comments

@stevvooe
Copy link

To deal with the naming constraints of a struct, the json module allows one too specify tags to guide unmarshaling/marshaling:

type MyParams struct {
    FooBar string "foo_bar"
}

This would serialize the contents of FooBar with the key "foo_bar" and deserialize such that an item with the "foo_bar" will be placed into FooBar.

I added this modification to allow web.go use these tags to guide parameter unmarshaling in the same manner:

diff --git a/request.go b/request.go
index 0e2fc4c..9df8837 100644
--- a/request.go
+++ b/request.go
@@ -328,17 +328,33 @@ func (r *Request) writeToContainer(val reflect.Value) os.Error {
             v.SetElem(mk, mv)
         }
     case *reflect.StructValue:
+        // A little preprocessing to make this faster
+        tagMap := map[string](reflect.Value) {}
+        nameMap := map[string](reflect.Value) {}
+
+        ty := v.Type().(*reflect.StructType)
+        for i := v.NumField(); i >= 0; i-- {
+            field := ty.Field(i)
+            if len(field.Tag) > 0 {
+                tagMap[strings.ToLower(field.Tag)] = v.Field(i)
+            }
+
+            if len(field.Name) > 0 {
+                nameMap[strings.ToLower(field.Name)] = v.Field(i)
+            }
+        }
+
         for pk, pv := range r.Params {
-            //try case sensitive match
-            field := v.FieldByName(pk)
-            if field != nil {
+            // try struct tag matching first
+            if field, ok := tagMap[strings.ToLower(pk)]; ok {
                 writeTo(pv, field)
+                continue
             }

-            //try case insensitive matching
-            field = v.FieldByNameFunc(func(s string) bool { return matchName(pk, s) })
-            if field != nil {
+            // now try case-insensitive
+            if field, ok := nameMap[strings.ToLower(pk)]; ok {
                 writeTo(pv, field)
+                continue
             }

         }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant