go-types

module
v1.0.17 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 23, 2022 License: MIT

README

Go Report Card Build Status codecov

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())
}

Directories

Path Synopsis
Package country provides lookups methods over ISO-3166 countries.
Package country provides lookups methods over ISO-3166 countries.
Package currency provides lookups methods over ISO-4217 currencies.
Package currency provides lookups methods over ISO-4217 currencies.
Package email provides simple Email type.
Package email provides simple Email type.
Package url provides simple URL type.
Package url provides simple URL type.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL