Skip to content
This repository has been archived by the owner on Jan 14, 2022. It is now read-only.

Handling JSON arrays #29

Closed
ysimonson opened this issue Dec 15, 2014 · 4 comments
Closed

Handling JSON arrays #29

ysimonson opened this issue Dec 15, 2014 · 4 comments
Labels

Comments

@ysimonson
Copy link

Is it possible to bind from a JSON array to an array of structs implementing FieldMapper? It doesn't seem like it from browsing the source.

@mholt mholt added question and removed question labels Dec 15, 2014
@mholt
Copy link
Owner

mholt commented Dec 15, 2014

(Strange - why does GitHub say I both added and removed the 'question' label? It still shows up for me...)

Not directly, but you can type alias the array/slice like:

type MyTypes []MyType

Where MyTypes implements FieldMapper.

By the way, if you have thoughts about improving this package (related to this question or other things), feel free to comment here or in #28.

@ysimonson
Copy link
Author

But doesn't FieldMap only allow you to define logic for deserializing an object? What would the FieldMapper definition look like for an array?

@mholt
Copy link
Owner

mholt commented Dec 27, 2014

You can either implement FieldMapper and define your own Binder func, or you can implement the Binder interface directly (as long as you defined the type in the same package):

func (t *MyTypes) Bind(fieldName string, strVals []string, errs binding.Errors) binding.Errors {
    for _, v := range strVals {
        *t = append(t, v)
    }
    return errs
}

Or something like that. Since you're deserializing into a slice of structs, you would probably initialize a struct for each iteration of the for loop. Is that what you're asking for?

@ysimonson
Copy link
Author

Yep. Thanks @mholt!

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

No branches or pull requests

2 participants