br

package module
v0.0.0-...-8b59860 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2023 License: MIT Imports: 14 Imported by: 0

README

br

Note: This project is currently in its early stages and is a work in progress (WIP). Please refrain from using it in a production environment. The API is subject to change without prior notice until the release of v1.0.0.

Goal

The aim of this project is to develop a high-performance Go library tailored for handling Brazilian documents.

Documentation

Overview

Package br provides utility functions and methods for dealing with Brazilian documents.

This package includes validators and formatters for various Brazilian documents, such as CPF and CNPJ, as well as functions for handling Brazilian postal codes (CEP).

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidUF is returned when an invalid state (UF) code is passed.
	ErrInvalidUF = errors.New("br: invalid uf passed")

	// ErrInvalidCEP is returned when an invalid CEP code is passed.
	ErrInvalidCEP = errors.New("br: invalid cep passed")

	// ErrInvalidSerializedAddress is returned when trying to deserialize an
	// invalid string into an Address.
	ErrInvalidSerializedAddress = errors.New(
		"br: invalid serialized address",
	)
)
View Source
var (
	// ErrInvalidCNH is an error returned when an invalid CNH is encountered.
	ErrInvalidCNH = errors.New("br: invalid cnh passed")
)
View Source
var (
	// ErrInvalidCNPJ is an error returned when an invalid CNPJ is encountered.
	ErrInvalidCNPJ = errors.New("br: invalid cnpj passed")
)
View Source
var (
	// ErrInvalidCNS is an error returned when an invalid CNS is encountered.
	ErrInvalidCNS = errors.New("br: invalid cns passed")
)
View Source
var (
	// ErrInvalidCPF is an error returned when an invalid CPF is encountered.
	ErrInvalidCPF = errors.New("br: invalid cpf passed")
)
View Source
var ErrInvalidPlate = errors.New("br: invalid license plate")

ErrInvalidPlate is an error returned when an invalid license plate is encountered.

Functions

This section is empty.

Types

type Address

type Address struct {
	UF          UF     `json:"uf"`
	CEP         CEP    `json:"cep"`
	Localidade  string `json:"localidade"`
	Logradouro  string `json:"logradouroDNEC"`
	Complemento string `json:"complemento"`
	Bairro      string `json:"bairro"`
	NomeUnidade string `json:"nomeUnidade"`
}

Address represents an address associated with a Brazilian CEP.

func (*Address) Deserialize

func (addr *Address) Deserialize(str string) error

Deserialize parses a serialized string and populates the Address fields.

func (*Address) Scan

func (addr *Address) Scan(value any) error

Scan implements the sql.Scanner interface for Address.

func (Address) Serialize

func (addr Address) Serialize() string

Serialize converts the Address instance into a serialized string representation.

This can be used to store the address as a string on a database, for example.

func (*Address) Validate

func (addr *Address) Validate() error

Validate fetches additional address information based on the associated CEP and updates the Address fields.

This method may perform requests to external APIs to retrieve address details.

func (Address) Value

func (addr Address) Value() (driver.Value, error)

Value implements the driver.Valuer interface for Address.

type CEP

type CEP string

CEP represents a Brazilian postal code (Código de Endereçamento Postal).

func NewCEP

func NewCEP(s string) (CEP, error)

NewCEP creates a CEP instance from a string representation.

It verifies the length of the CEP, the digits, and checks the cache for known invalid CEPs. It does not make requests to APIs to validate the corresponding address if the CEP is not in cache.

To check if this CEP actually represents an Address, call the CEP.ToAddress method.

func (CEP) IsValid

func (cep CEP) IsValid() bool

IsValid checks whether the provided CEP is valid based on its length, digits and check the caches for known invalid CEPs.

It does not make requests to APIs to validate the corresponding address.

To check if this CEP actually represents an Address, call the CEP.ToAddress method.

func (CEP) String

func (cep CEP) String() string

String returns the formatted CEP string as XXXXX-XXX.

func (CEP) ToAddress

func (cep CEP) ToAddress() (addr Address, err error)

ToAddress converts a CEP into an Address instance, retrieving address information associated with the CEP.

This method may perform requests to external APIs to fetch address details based on the CEP.

type CNH

type CNH string

CNH represents a Brazilian driver's license number.

func NewCNH

func NewCNH(s string) (CNH, error)

NewCNH creates a new CNH instance from a string representation.

It verifies the CNH's validity using checksum digits.

func (CNH) IsValid

func (cnh CNH) IsValid() bool

IsValid checks whether the provided CNH is valid based on its checksum digits.

func (CNH) String

func (cnh CNH) String() string

String returns the string representation of CNH.

type CNPJ

type CNPJ string

CNPJ represents a Brazilian CNPJ.

func NewCNPJ

func NewCNPJ(s string) (CNPJ, error)

NewCNPJ creates a new CNPJ instance from a string representation.

It verifies the CNPJ's validity using checksum digits.

func (CNPJ) IsValid

func (cnpj CNPJ) IsValid() bool

IsValid checks whether the provided CNPJ is valid based on its checksum digits.

func (CNPJ) String

func (cnpj CNPJ) String() string

String returns the formatted CNPJ string with punctuation as XX.XXX.XXX/XXXX-XX.

type CNS

type CNS string

CNS represents a Brazilian CNS.

func NewCNS

func NewCNS(s string) (CNS, error)

NewCNS creates a new CNS instance from a string representation.

It verifies the CNS's validity using checksum digits.

func (CNS) IsValid

func (cns CNS) IsValid() bool

IsValid checks whether the provided CNS is valid based on its checksum digits.

func (CNS) String

func (cns CNS) String() string

String returns the CNS formatted as XXX XXXX XXXX XXXX.

type CPF

type CPF string

CPF represents a Brazilian CPF.

func NewCPF

func NewCPF(s string) (CPF, error)

NewCPF creates a new CPF instance from a string representation.

It verifies the CPF's validity using checksum digits.

func (CPF) IsValid

func (cpf CPF) IsValid() bool

IsValid checks whether the provided CPF is valid based on its checksum digits.

func (CPF) String

func (cpf CPF) String() string

String returns the formatted CPF string with punctuation as XXX.XXX.XXX-XX.

type Plate

type Plate string

Plate represents a Brazilian vehicle license plate.

func NewPlate

func NewPlate(s string) (Plate, error)

NewPlate creates a new Plate instance from a string representation.

func (Plate) IsValid

func (p Plate) IsValid() bool

IsValid checks whether the provided license plate is valid based on specific formatting rules.

IsValid will return true if the plate if either a MercoSul or a Brazilian type plate.

The formats accepted are: XXXXXXX, XXX-XXXX, XXX.XXXX

func (Plate) String

func (p Plate) String() string

String returns the license plate as an uppercase formatted string.

String always returns the plate in the XXX-XXXX format and in uppercase.

type UF

type UF uint8

UF stands for Unidade Federativa and represents a Brazilian state.

func NewUF

func NewUF(codigo int) (UF, error)

NewUF creates a UF instance from a given state code.

func NewUFFromStr

func NewUFFromStr(uf string) (UF, error)

NewUFFromStr creates a UF instance from a string representation of the state's name or abbreviation.

func (UF) Codigo

func (uf UF) Codigo() int

Codigo returns the numeric code of the UF.

func (UF) MarshalJSON

func (uf UF) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for UF.

func (*UF) Scan

func (uf *UF) Scan(value any) error

Scan implements the sql.Scanner interface for UF.

func (UF) String

func (uf UF) String() string

String returns the abbreviation of the UF, such as RJ and SP.

func (UF) UnidadeFederativa

func (uf UF) UnidadeFederativa() string

UnidadeFederativa returns the full name of the UF, such as Rio de Janeiro.

func (*UF) UnmarshalJSON

func (uf *UF) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for UF.

func (UF) Value

func (uf UF) Value() (driver.Value, error)

Value implements the driver.Valuer interface for UF.

Jump to

Keyboard shortcuts

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