proteus icon indicating copy to clipboard operation
proteus copied to clipboard

Support custom Scanner slices (like JSONB columns with Arrays)

Open slaskis opened this issue 7 years ago • 4 comments

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!

slaskis avatar Aug 25 '17 07:08 slaskis

Hi Robert,

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

jonbodner avatar Aug 26 '17 19:08 jonbodner

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

jonbodner avatar Sep 03 '17 01:09 jonbodner

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!

slaskis avatar Sep 03 '17 06:09 slaskis

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.

jonbodner avatar May 20 '18 03:05 jonbodner