proto-go-sql | Generate SQL Scanner and Valuer implementations | Serialization library
kandi X-RAY | proto-go-sql Summary
kandi X-RAY | proto-go-sql Summary
Generate SQL Scanner and Valuer implementations for your Protobufs.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Msgs iterates through all messages in the given file .
- Generate the plugin
- forEachMessage calls f for each nested message .
- Register an E_All extension .
- NewGenerator returns a new generator
proto-go-sql Key Features
proto-go-sql Examples and Code Snippets
Community Discussions
Trending Discussions on proto-go-sql
QUESTION
Could someone explain how to properly use Scan()
and Value()
in the following example?
I'm trying to make use of the following examples:
- https://github.com/jinzhu/gorm/issues/2017#issuecomment-537627961
- https://github.com/travisjeffery/proto-go-sql/blob/master/_example/person_sql.go
My proto:
...ANSWER
Answered 2019-Nov-28 at 05:37The scanner and valuer interfaces are not really things you'll use yourself, not when it comes to storing custom types in a DB, at least. I'll first cover the use of the Scan()
and Value()
functions, then I'll address your issues.
When you get a sql.Row
result, and want to assign (scan) the values from the result set into your variables of a custom type. The docs show the sql.Row.Scan()
function takes 0 or more arguments of type interface{}
, basically anything. (check docs here).
In the list of supported types into which values can be scanned, the last line is the important one:
any type implementing Scanner (see Scanner docs)
With the function func (ts *Timestamp) Scan(value interface{}) error {
, the Timestamp
type now implements the Scanner
interface, thus allowing sql.Row
to assign values to this type. The documentation for the Scanner
interface is located right below the docs for Scan()
, which I linked above.
Of course, that helps you to read values from the DB, gets you nowhere when it comes to storing these types. For that, you need the Valuer
interface. In case you haven't guessed it yet, the func (ts Timestamp) Value() (driver.Value, error)
function indeed makes it so your Timestamp
type implements this interface. The documentation for the driver.Valuer
interface can be found here, all the way at the bottom.
The point of the Valuer
interface is to allow a way to convert any type to a driver.Value
, that the driver can work with and store in the DB (again: docs here).
First up, I'm goign to have to assume your protoc output is written to the v1
package. If it isn't, it's not going to work very well for you.
The offending line is indeed the one you marked out:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install proto-go-sql
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page