Git Product home page Git Product logo

go-types's Introduction

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/v2

Usage:

package main

import (
	"encoding/json"
	"fmt"
	"log"

	validation "github.com/go-ozzo/ozzo-validation/v4"
	"github.com/mikekonan/go-types/v2/country"
	"github.com/mikekonan/go-types/v2/country/alpha2"
	"github.com/mikekonan/go-types/v2/country/alpha3"
	"github.com/mikekonan/go-types/v2/language"
	"github.com/mikekonan/go-types/v2/country/name"
	"github.com/mikekonan/go-types/v2/currency"
	"github.com/mikekonan/go-types/v2/currency/code"
	"github.com/mikekonan/go-types/v2/phone"
	"github.com/mikekonan/go-types/v2/postal_code"
)

// 1. use in your structs
type User struct {
	Name            string                `json:"name" db:"name"`
	Country         country.Alpha2Code    `json:"country" db:"country"`
	Currency        currency.Code         `json:"currency" db:"currency"`
	Language        language.Alpha2Code   `json:"language" db:"language"`
	Phone           phone.Number          `json:"phone" db:"phone"`
	CountryDialCode phone.DialCode        `json:"dialCode" db:"dialCode"`
	PostalCode      postalcode.PostalCode `json:"postalCode" db:"postalCode"`
}

func main() {
	// 2. use in your wire
	user := User{}
	_ = json.Unmarshal([]byte(`{"name":"name", "country": "CA", "currency": "CAD", "language": "fr", "phone": "123456789", "dialCode": "1"}`), &user)

	// 3. check is set
	user.Country.IsSet()
	user.Currency.IsSet()
	user.Language.IsSet()

	// 4. validate using ozzo-validation
	if err := validation.ValidateStruct(&user, validation.Field(&user.Country), validation.Field(&user.Currency)); 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. lookup by 2 and 3 char codes, language name
	if userLanguage, ok := language.ByAlpha2Code(user.Language); ok {
            fmt.Printf("language name - '%s', alpha-2 - '%s', alpha-3 - '%s'", userLanguage.Name(), userLanguage.Alpha2Code(), userLanguage.Alpha3Code())
	}

	// 7. lookup by country dial code
	if phoneCountries, ok := phone.CountriesByDialCode(user.CountryDialCode); ok {
            for _, phoneCountry := range phoneCountries {
                fmt.Printf("country by dial code - '%s'", phoneCountry)
            }
	}

	// 8. lookup by country
	if dialCode, ok := phone.DialByAlpha2Code(user.Country); ok {
            fmt.Printf("'%s' dial code is '%s'", user.Country, dialCode)
        }

	// 9. lookup by currency code
	if userCurrency, ok := currency.ByCode(user.Currency); ok {
            fmt.Printf("currency name - '%s', code - '%s', number - '%s', countries - '%s', decimal places - '%d'",
                userCurrency.Currency(), userCurrency.Code(), userCurrency.Number(), userCurrency.Countries(), userCurrency.DecimalPlaces())
	}

	// 10. store in db
	fmt.Println(user.Country.Value())  //prints 'CA'
	fmt.Println(user.Currency.Value()) //prints 'CAN'
	fmt.Println(user.Language.Value()) //prints 'fr'

	// 11. use specific country constants
	fmt.Println(country.Canada.Alpha2Code())
	fmt.Println("name:", name.Canada)
	fmt.Println("alpha-2:", alpha2.CA)
	fmt.Println("alpha-3:", alpha3.CAN)

	// 12. use specific currency codes
	fmt.Println(code.CAD)
}

Links:

go-types's People

Contributors

asechiho avatar avelino avatar glebbeloded avatar ilemiashonak avatar lstmix avatar mikekonan avatar vadim-mve avatar valichek avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

go-types's Issues

Compilation error with ozzo validator.

cannot user.Country (variable of type country.Alpha2Code) as validation.Rule value in argument to validation.Field: wrong type for method Validate (have func() (err error), want func(value interface{}) error)

My Code

package foo

import (
    validation "github.com/go-ozzo/ozzo-validation"
    "github.com/mikekonan/go-types/country"
)

type Foo struct {
    CountryCode country.Alpha2Code
}
func (r *Foo) Validate() error {
    return validation.ValidateStruct(r,
        validation.Field(r.Country, validation.Required, r.Country),
    ),
}

Type Date must be string according to the OpenAPI specification

Underlying type of Date is null.Time and this causes problems with go-oas3 which generates invalid code like this

func (body Payer) Validate() error {
	return validation.ValidateStruct(&body,
		validation.Field(&body.BirthDate, validation.Skip.When(body.BirthDate == ""), validation.RuneLength(10, 10)))
}

validation.Skip tries to compare null.Time to ""

Invalid code generated here:
https://github.com/mikekonan/go-oas3/blob/main/generator/generator.go#L580

According to specification underlining type of Date must be string

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.