biter777 / countries

Countries - ISO-639, ISO-3166 countries codes with subdivisions and names, ISO-4217 currency designators, ITU-T E.164 IDD phone codes, countries capitals, UN M.49 codes, IANA ccTLD countries domains, FIPS, IOC/NOC and FIFA codes, VERY VERY FAST, compatible with Databases/JSON/BSON/GOB/XML/CSV, Emoji countries flags and currencies, Unicode CLDR.
https://pkg.go.dev/github.com/biter777/countries?tab=doc
BSD 2-Clause "Simplified" License
413 stars 76 forks source link

`SubdivisionCode` does not implement sql.Scanner and driver.Valuer interfaces #43

Closed Sophie1142 closed 1 year ago

Sophie1142 commented 1 year ago

First of all, thank you for this amazing package! You've saved me a TON of work.

One call out I have is, this package doesn't seem to implement the database/sql/driver.Valuer and database/sql/driver.Valuer interfaces for SubdivisionCode.

How I arrived at this conclusion: I have an API that takes in POST requests from the client, marshals it into a Go struct, then uses Gorm to store that struct model in a Postgres DB. The struct has a field of type countries.SubdivisionCode, and the package is able to correctly unmarshal it into the struct as a string (i.e. "US-MA" --> "Massachusetts"). It also correctly inserts it into the DB as "Massachusetts." However, when I select that record from the DB and unmarshal it into a Go struct, the subdivision code field reads as "Unknown".

✅ POST request:

image

✅ Unmarshal the Request Body with Fiber into a Struct:

image

✅ Insert Into the DB:

image

❌ Select that Record from DB: Now the field shows up as Unknown.

image

On a related note, what function is causing "US-MA" to be read in as "Massachusetts" and when is that function called? Is there a way to insert it into the DB as a "US-MA"?

EDIT: Using fmt.Printf("%+v") printed the field as "Massachusetts" (I'm guessing the package uses the String() method to do so), but when I just use fmt.Printf("%#v"), it outputs as "US-MA". Still, how is it getting inserted into the DB as "Massachusetts" when it doesn't have an explicit driver.Valuer function?

biter777 commented 1 year ago

Please show your code (insert to db, read from db)

biter777 commented 1 year ago

@Sophie1142 pls reopen issue, if still relevant