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

Bind form-data request with nested structures #551

Closed
Ganitzsh opened this issue Mar 3, 2016 · 7 comments
Closed

Bind form-data request with nested structures #551

Ganitzsh opened this issue Mar 3, 2016 · 7 comments

Comments

@Ganitzsh
Copy link

Ganitzsh commented Mar 3, 2016

Hi,

I have an struct StructA which contains another struct StructB as an attribute, like this:

type StructB struct {
    FieldB string  `form:"fieldB"`
}

type StructA struct {
    NestedStruct *StructB
    FieldA string `form:"fieldA"`
    ...
}

In my handler I'm Binding the request content to an StructA object:

func MyHandler(c *gin.Context) {
    var tmp StructA
    ...
    c.Bind(&tmp)
    ...
}

My form contains the field fieldA as well as fieldB, but NestedStruct stays <nil> after the Bind call.
Is manual binding the only way to achieve this at the time? Or am I missing something?

Thanks

@misekm
Copy link

misekm commented Oct 7, 2017

Please is there any solution for this? I would like to use nested structs too. Thanks.

@Ganitzsh
Copy link
Author

Ganitzsh commented Oct 7, 2017

I found a trick, using reflection, you can wrap the Bind function. I will provide a sample later.

@Ganitzsh Ganitzsh closed this as completed Oct 7, 2017
@Ganitzsh Ganitzsh reopened this Oct 7, 2017
@misekm
Copy link

misekm commented Oct 7, 2017

Could you please show any sample code?

@qw4n7y
Copy link

qw4n7y commented Nov 22, 2017

any progress?

@thinkerou
Copy link
Member

thinkerou commented Apr 27, 2018

Hi @Ganitzsh @qw4n7y @misekm , gin supports the follow not POINTER:

type StructB struct {
    FieldB string  `form:"fieldB"`
}

type StructA struct {
    NestedStruct StructB
    FieldA string `form:"fieldA"`
    // ...
}

OR

type StructA struct {
    NestedStruct *StructB
    FieldA string `form:"fieldA"`
    // ...
}

@appleboy
Copy link
Member

appleboy commented May 3, 2018

@appleboy appleboy closed this as completed May 3, 2018
@nicolasvasquez
Copy link

I think that assign multiple names for different nested struct is tedious. For example:

type StructB struct {
    Id  int  `form:"structb_id" json:"id"`
    Name  string  `form:"structb_name"  json:"name"`
}
type StructA struct {
    Id  string  `form:"structa_id" json:"id"`
    Name  string  `form:"structa_name" json:"name"`
    NestedStruct *StructB `form:"fieldB" json:"structb"`
}

The json bind for nested struct is more easily than form way. Is possible work with form as the json way and send only like structb.name for assign the name of the second struct??

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

6 participants