georgysavva / scany

Library for scanning data from a database into Go structs and more
MIT License
1.27k stars 67 forks source link

Scanning into a pointer to string inside struct #23

Closed pragyanaryal closed 3 years ago

pragyanaryal commented 3 years ago

Hello, I read through the note in doc, but i need to implement some columns as null-able field. Is there any workaround to scan pointer to string using pgxscan ?

My Struct is as such image

It is able to scan to pointer in nested struct, which is of composite type, but is not able to scan to plain pointer to nil. Am I missing something ?

image

georgysavva commented 3 years ago

Hi. Thanks for the issue. What you are asking for should work by default, without any workarounds. What exactly "note in the docs" you are talking about, can you give me the link?

BTW, can you send me the full code version with the struct definition, pgxscan call and the full error.

pragyanaryal commented 3 years ago

Hello, Sorry, It was my fault . Data type mismatch between db and go's code was causing the problem. I had implemented int on the database but the field was string on the go's side, upon changing, it behaves fine. It scans fine now as intended. The link in docs I was pointing towards was: https://pkg.go.dev/github.com/georgysavva/scany/pgxscan#hdr-Note_about_pgx_custom_types

image Upon reading, it cannot scan into pointer, I was deviated.

But, I was able to scan even to the pointer inside custom type. I have a nested struct like this, inside which a Location is a custom type: image

Upon implementing, decoder and encoder to the Location struct like this: image

And, specifying pgxscan to use binary format for the given oid, it successfully scanned into the struct, image

And gave result like this: image

Thanks, this is really nice package. :)