jonbodner / proteus

A simple tool for generating an application's data access layer.
MIT License
209 stars 16 forks source link

Support custom Scanner slices (like JSONB columns with Arrays) #11

Open slaskis opened 7 years ago

slaskis commented 7 years ago

This project seems great! While giving it a go with a project I'm doing in which, among a lot of things, I use a JSONB column in which I store an array of elements.

I've been using sqlx for named parameters so far which works well but is getting a bit out of hand which is why this "magic" DAO style seems very interesting.

The problem I'm facing is that the array of elements are a type which in turn is a slice which implement the Scanner interface to marshal as JSON.

Something like this:

type Records []*Record

func(r *Records) Scan(value interface{}) error {
    if row, ok := value.([]byte); ok {
        return json.Unmarshal(row, a)
    }
    return nil
}
func (r Records) Value() (driver.Value, error) {
    return json.Marshal(r)
}

When using proteus it expands the slice into multiple variables and my vague guess is that the issue might be that it checks if it's a slice before it checks if it implements the Scanner interface.

I might be able to write a test and a PR but I'm currently choked so I just wanted to give you a heads up and check if it sounds like the right path first.

Thanks!

jonbodner commented 7 years ago

Hi Robert,

Thanks for finding this! If you can give me a test case, I can work on a fix.

jonbodner commented 7 years ago

This is still on my radar; as soon as I get a chance, I will resolve this.

slaskis commented 7 years ago

Cool!

I'm sorry for still not providing any tests yet. I've left this experiment for now but I'll be back as soon as we launch.

Cheers!

jonbodner commented 6 years ago

I think I fixed this in Release v0.5.1; if anyone is still interested in this issue, please create a slice type that implements Scanner and Valuer and see if it does the right thing.