-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add embedded struct support + options
- Loading branch information
Dean Karn
committed
Jul 30, 2017
1 parent
20d0406
commit 920e9aa
Showing
12 changed files
with
256 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/url" | ||
|
||
"github.com/go-playground/form" | ||
) | ||
|
||
// A ... | ||
type A struct { | ||
Field string | ||
} | ||
|
||
// B ... | ||
type B struct { | ||
A | ||
Field string | ||
} | ||
|
||
// use a single instance of Decoder, it caches struct info | ||
var decoder *form.Decoder | ||
|
||
func main() { | ||
decoder = form.NewDecoder() | ||
|
||
// this simulates the results of http.Request's ParseForm() function | ||
values := parseFormB() | ||
|
||
var b B | ||
|
||
// must pass a pointer | ||
err := decoder.Decode(&b, values) | ||
if err != nil { | ||
log.Panic(err) | ||
} | ||
|
||
fmt.Printf("%#v\n", b) | ||
|
||
values = parseFormAB() | ||
|
||
// must pass a pointer | ||
err = decoder.Decode(&b, values) | ||
if err != nil { | ||
log.Panic(err) | ||
} | ||
|
||
fmt.Printf("%#v\n", b) | ||
} | ||
|
||
// this simulates the results of http.Request's ParseForm() function | ||
func parseFormB() url.Values { | ||
return url.Values{ | ||
"Field": []string{"B FieldVal"}, | ||
} | ||
} | ||
|
||
// this simulates the results of http.Request's ParseForm() function | ||
func parseFormAB() url.Values { | ||
return url.Values{ | ||
"Field": []string{"B FieldVal"}, | ||
"A.Field": []string{"A FieldVal"}, | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/go-playground/form" | ||
) | ||
|
||
// A ... | ||
type A struct { | ||
Field string | ||
} | ||
|
||
// B ... | ||
type B struct { | ||
A | ||
Field string | ||
} | ||
|
||
// use a single instance of Encoder, it caches struct info | ||
var encoder *form.Encoder | ||
|
||
func main() { | ||
|
||
type A struct { | ||
Field string | ||
} | ||
|
||
type B struct { | ||
A | ||
Field string | ||
} | ||
|
||
b := B{ | ||
A: A{ | ||
Field: "A Val", | ||
}, | ||
Field: "B Val", | ||
} | ||
|
||
encoder = form.NewEncoder() | ||
|
||
v, err := encoder.Encode(b) | ||
if err != nil { | ||
log.Panic(err) | ||
} | ||
|
||
fmt.Printf("%#v\n", v) | ||
|
||
encoder.SetAnonymousMode(form.AnonymousSeparate) | ||
v, err = encoder.Encode(b) | ||
if err != nil { | ||
log.Panic(err) | ||
} | ||
|
||
fmt.Printf("%#v\n", v) | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters