Skip to content

Commit

Permalink
doc changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mikekonan committed Oct 31, 2020
1 parent 614d87a commit 5c59db8
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 14 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,40 @@ Created with a codegen - check generator branch.

# Installation
```go get github.com/mikekonan/go-countries```

# 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, validation.Required, 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'", userCountry.Name(), userCountry.Alpha2Code(), userCountry.Alpha3Code())
}

//6. store in db
fmt.Println(user.Country.Value()) //prints 'CA'

//7. use specific countries
fmt.Println(country.Canada.Alpha2Code())
}


# API
### Lookup:
country.ByNameStrErr()
Expand Down
43 changes: 29 additions & 14 deletions country.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Constants have been generated with code generation(github.com/mikekonan/go-count
The source of truth is https://www.iso.org/iso-3166-country-codes.html.
Lookups methods by alpha2 and alpha3 are case robust.
Author Mikalai Konan(mikalai.konan@icloud.com).
*/
package country
Expand Down Expand Up @@ -49,11 +51,13 @@ type Alpha2Code string

//Value implementation of driver.Valuer
func (code Alpha2Code) Value() (value driver.Value, err error) {
if _, err = ByAlpha2CodeErr(code); err != nil {
var country Country

if country, err = ByAlpha2CodeErr(code); err != nil {
return nil, err
}

return code.String(), nil
return country.Alpha2Code().String(), nil
}

//Validate implementation of ozzo-validation Validate interface
Expand All @@ -73,16 +77,22 @@ func (code Alpha2Code) String() string {
return string(code)
}

func (code Alpha2Code) toUpper() Alpha2Code {
return Alpha2Code(strings.ToUpper(code.String()))
}

//Alpha3Code represents alpha-3 code
type Alpha3Code string

//Value implementation of driver.Valuer
func (code Alpha3Code) Value() (value driver.Value, err error) {
if _, err = ByAlpha3CodeErr(code); err != nil {
var country Country

if country, err = ByAlpha3CodeErr(code); err != nil {
return nil, err
}

return code.String(), nil
return country.Alpha3Code().String(), nil
}

//Validate implementation of ozzo-validation Validate interface
Expand All @@ -102,6 +112,10 @@ func (code Alpha3Code) String() string {
return string(code)
}

func (code Alpha3Code) toUpper() Alpha3Code {
return Alpha3Code(strings.ToUpper(code.String()))
}

//Country represents country entity according to ISO-3166.
type Country struct {
name Name
Expand Down Expand Up @@ -129,13 +143,14 @@ func (country Country) Alpha3CodeStr() string { return country.alpha3.String() }

//ByAlpha3Code lookup for country by alpha-3 code
func ByAlpha3Code(code Alpha3Code) (result Country, ok bool) {
result, ok = countryByAlpha3[code]
result, ok = countryByAlpha3[code.toUpper()]

return
}

//ByAlpha3CodeStr lookup for country by alpha-3 code string. Lowercase robust
//ByAlpha3CodeStr lookup for country by alpha-3 code string
func ByAlpha3CodeStr(code string) (result Country, ok bool) {
return ByAlpha3Code(Alpha3Code(strings.ToUpper(code)))
return ByAlpha3Code(Alpha3Code(code))
}

//ByAlpha3CodeErr lookup for country by alpha-3 code with error return type
Expand All @@ -149,20 +164,20 @@ func ByAlpha3CodeErr(code Alpha3Code) (result Country, err error) {
return
}

//ByAlpha3CodeStrErr lookup for country by alpha-3 code string with error return type. Lowercase robust
//ByAlpha3CodeStrErr lookup for country by alpha-3 code string with error return type
func ByAlpha3CodeStrErr(code string) (result Country, err error) {
return ByAlpha3CodeErr(Alpha3Code(strings.ToUpper(code)))
return ByAlpha3CodeErr(Alpha3Code(code))
}

//ByAlpha2Code lookup for country by alpha-2 code
func ByAlpha2Code(code Alpha2Code) (result Country, ok bool) {
result, ok = countryByAlpha2[code]
result, ok = countryByAlpha2[code.toUpper()]
return
}

//ByAlpha2CodeStr lookup for country by alpha-2 code string. Lowercase robust
//ByAlpha2CodeStr lookup for country by alpha-2 code string
func ByAlpha2CodeStr(code string) (result Country, ok bool) {
return ByAlpha2Code(Alpha2Code(strings.ToUpper(code)))
return ByAlpha2Code(Alpha2Code(code))
}

//ByAlpha2CodeErr lookup for country by alpha-2 code with error return type
Expand All @@ -176,9 +191,9 @@ func ByAlpha2CodeErr(code Alpha2Code) (result Country, err error) {
return
}

//ByAlpha2CodeStrErr lookup for country by alpha-2 code string with error return type. Lowercase robust
//ByAlpha2CodeStrErr lookup for country by alpha-2 code string with error return type
func ByAlpha2CodeStrErr(code string) (result Country, err error) {
return ByAlpha2CodeErr(Alpha2Code(strings.ToUpper(code)))
return ByAlpha2CodeErr(Alpha2Code(code))
}

//ByName lookup for country by name
Expand Down

0 comments on commit 5c59db8

Please sign in to comment.