georgysavva / scany

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

Improve quickstart documentation #26

Closed nigelainscoe closed 2 years ago

nigelainscoe commented 3 years ago

Please make clear the use of the db tag on fields within structs in the README doc. It is not at all obvious. E.g.

type Simple struct { Email stringdb:"Email" }

Is required where the name of the field being returned from the database is Email as opposed to email. It took me a long time to debug why dbscan was throwing scany: column: 'Email': no corresponding field found, or it's unexported when I could clearly see "Email" as a field in my struct and a corresponding "Email" field in my select statement.

I know it's alluded to in the pkg.go.dev docs but it's not obvious even there.

Or maybe I should just do a PR?

georgysavva commented 3 years ago

Hello and thank you for opening this issue. I am sad to hear that it took you a while to debug. Let's improve it together if possible.

Here are my considerations:

  1. I decided to use the README file only for show casting, to hook the potential users of the library. All details documentation and manual live in pkg.go.dev
  2. pkg.go.dev already contains a dedicated paragraph for struct field mapping: https://pkg.go.dev/github.com/georgysavva/scany/dbscan#hdr-Mapping_struct_field_to_database_column: By default, to get the corresponding database column dbscan translates struct field name to snake case. To override this behavior, specify the column name in thedbfield tag. In the example above User struct is mapped to the following columns: "user_id", "first_name", "email". The docs contain an example that even includes an Email field just as you provided.
  3. A similar major project sqlx works exactly the same in case you provided.

I totally understand that even with all those counts that I listed above it might be not clear in some cases. I would be positive if you submit a PR where you update the documentation in pkg.go.dev in your words.