go-types
This library has been created with the purpose to facilitate the store, validation, and transfer of Go ISO-3166/ISO-4217/timezones/emails/URL types. There is a openapi3 spec of that type and make you able to include it into your spec. All types has own ozzo.Validate, json.Unmarshaler, Stringer and driver.Valuer implementations.
Installation
go get github.com/mikekonan/go-types
Usage:
//1. use in your structs
type User struct {
Name string `json:"name" db:"name"`
Country country.Alpha2Code `json:"country" db:"country"`
}
func main() {
user := User{}
//2. use in your wire
json.Unmarshal([]byte(`{"name":"name", "country": "ca"}`), &user)
//3. check is set
user.Country.IsSet() //check user country is provided
//4. validate using ozzo-validation
if err := validation.ValidateStruct(&user, validation.Field(&user.Country)); err != nil {
log.Fatal(err)
}
//5. lookup by alpha2, alpha3, country name
if userCountry, ok := country.ByAlpha2Code(user.Country); ok {
fmt.Printf("country name - '%s', alpha-2 - '%s', alpha-3 - '%s'", serCountry.Name(), userCountry.Alpha2Code(), userCountry.lpha3Code())
}
//6. store in db
fmt.Println(user.Country.Value()) //prints 'CA'
//7. use specific countries
fmt.Println(country.Canada.Alpha2Code())
}
Links:
- Currency Codes (ISO 4217) - wiki
- Country Codes (ISO 3166) - wiki
- URL(including HttpURL) (standard) - wiki
- Email (part of RFC5322) - wiki
- Timezone (RFC6557 IANA timezones) - wiki