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

Vote for New Feature: generate Scan (as specified by sql.Scanner) and driver.Valuer (as specified by driver.Valuer) #108

Closed
pascallouisperez opened this issue Sep 24, 2015 · 13 comments

Comments

@pascallouisperez
Copy link

To store protos in a db (as blobs), it would be great to have an extension generating:

@awalterschulze
Copy link
Member

I am not really a sql user, could you please explain how this would work in a little more detail?
I am also pretty sure that sql does not support nested structures, but protocol buffers do, can the Scanner or Valuer interfaces handle nested structures?

@chancez
Copy link

chancez commented Oct 8, 2015

This would be awesome. @awalterschulze Scanners can handle nested structures, but it does get a bit difficult, you end up calling the Scanner method of the nested type on some bytes. However, even without supporting that, this would be quite valuable. Also, because we know the schema ahead of time, it should be possible to do Scanning without reflection, which would be even better.

@awalterschulze
Copy link
Member

Maybe someone could give me an example of what the generated code would look like for an example structure?

@pascallouisperez
Copy link
Author

Apologies for being MIA despite my initial ask. Providing examples is on my TODO list, but swamped right now!

@tamird
Copy link
Contributor

tamird commented Oct 17, 2015

@awalterschulze if I understand this correctly, the only feasible implementation of Valuer would be a straight delegation to Marshal, and the only reasonable implementation of Scanner would be a straight delegation to Unmarshal (which would assert that src is a []byte). @pascallouisperez did I understand your intent correctly?

@tamird
Copy link
Contributor

tamird commented Oct 17, 2015

@ecnahc515 you cannot implement Scanner in a way that scans multiple fields into a single structure.

@pascallouisperez
Copy link
Author

@tamird Would that support NULL fields?

@tamird
Copy link
Contributor

tamird commented Oct 27, 2015

I don't see why not. The hardest part of all this will be adding tests, since there's no SQL anything in gogoproto right now.

@awalterschulze
Copy link
Member

Is this still a thing you want?

@bir
Copy link

bir commented Jul 3, 2017

I would potentially like to revisit this. Although, I am not in need of complex proto message scan/value support for nesting -> but simply the wrappers defined here.

As an example, adding the following to wrappers.pb.go enables the use of a message with a StringValue to be read directly with a SQL call :

func (s *StringValue) Scan(value interface{}) error {
	if value == nil {
		s.Reset()
		return nil
	}
	if bv, err := driver.String.ConvertValue(value); err == nil {
		if v, ok := bv.(string); ok {
			*s = StringValue{v}
			return nil
		}
	}
	return errors.New("failed to scan StringValue")
}

Without this patch you get the following type of errors:

sql: Scan error on column index 1: unsupported Scan, storing driver.Value type string into type *types.StringValue

Right now I have resolved this by forking the repo and manually adding the Scan and Value methods I need. Happy to create a PR, however, I realize that those files are actually generated and adding support for this to the generator would be the potentially proper solution. If you can point me in the right direction I can dig into that.

@awalterschulze
Copy link
Member

I would recommend building a plugin and then a vanity binary.
That way you can have a decoupled project and still use all the other gogoprotobuf features if you want.
Here are some pointers on how to do that
#304

@travisjeffery
Copy link

https://github.com/travisjeffery/proto-go-sql - generator I wrote that'll implement sql.Scanner and driver.Valuer

@awalterschulze
Copy link
Member

awalterschulze commented Mar 28, 2018 via email

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

No branches or pull requests

6 participants