Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
fe3fdcb
refactor: standardize error constants to follow Go conventions
wellfernandes Jun 23, 2025
28780c3
feat: added new error constant
wellfernandes Jun 23, 2025
bc054e8
refactor: replaced fatal error with a custom error
wellfernandes Jun 23, 2025
b6b4366
refactor: code formatting
wellfernandes Jun 23, 2025
50e01c2
refactor: improved func return readability
wellfernandes Jun 24, 2025
cd6006d
remove: removed files after refactoring
wellfernandes Jun 25, 2025
63e5d2c
feat: added cli constants
wellfernandes Jun 25, 2025
96010dd
feat: added utils
wellfernandes Jun 25, 2025
d59ee3a
feat: added main for running examples
wellfernandes Jun 25, 2025
45f9f3f
docs: updated readme
wellfernandes Jun 25, 2025
7c28184
docs: updated readme (pt)
wellfernandes Jun 25, 2025
a75ce35
fix: fixed inconsistent usage of error variable
wellfernandes Jun 26, 2025
536ea47
remove: removed after package reorganization
wellfernandes Jun 28, 2025
3bae813
refactor: simplified func return
wellfernandes Jun 28, 2025
51f5c02
refactor: standardized custom error and return
wellfernandes Jun 28, 2025
e74f478
refactor: changed return of the gender entity
wellfernandes Jun 28, 2025
e253084
refactor: reorganized examples of usage
wellfernandes Jun 28, 2025
bbd88b6
feat: added simplified usage example
wellfernandes Jun 28, 2025
2cc3d8a
feat: added simplified usage option
wellfernandes Jun 28, 2025
81f7ce6
docs: updated readme
wellfernandes Jun 28, 2025
a485e83
docs: updated readme (pt)
wellfernandes Jun 28, 2025
0abebcd
refactor: added error handling
wellfernandes Jun 28, 2025
7426a12
fix: fixed error return when creating a mocker
wellfernandes Jun 28, 2025
77419ff
docs: updated readme with fixed code example
wellfernandes Jun 28, 2025
cee1791
docs: updated readme (pt) with fixed code example
wellfernandes Jun 28, 2025
8469a85
refactor: removed unnecessary field
wellfernandes Jun 28, 2025
1cb26d4
docs: replaced tabs with spaces in code blocks
wellfernandes Jun 29, 2025
c4e2ef8
refactor: fixed import after restructuring
wellfernandes Jul 10, 2025
d661acc
fix: added internal directory and reorganized files
wellfernandes Jul 10, 2025
d6bd9cc
remove: files removed after fix
wellfernandes Jul 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func GenerateMockExample() {
```

### Examples
The ***examples*** folder contains samples of how to use the library. To run the examples, navigate to the root folder and execute:
The ***examples*** folder contains samples of how to use the library. To run the examples, navigate to the *examples* folder and execute:

```
go run main.go
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
package constants

import (
"github.com/brazzcore/mocai/pkg/mocai"
)
package mocai
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As coisas dentro de cmd são sempre package main e não pode ser importadas em outros lugares

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfeito! Criei um diretório internal pra isso!


// ANSI Escape Codes
var (
Expand All @@ -22,7 +18,7 @@ var (

func init() {
// Checks if the terminal supports ANSI
if mocai.SupportsANSI() {
if SupportsANSI() {
CyanBold = "\033[1;36m"
BlueBold = "\033[1;34m"
Italic = "\033[3m"
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion docs/localization/pt/README-PT.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func GenerateMockExample() {
```

### Exemplos
A pasta ***examples*** contém exemplos de como usar a biblioteca. Para executar os exemplos, navegue até a pasta raiz e execute:
A pasta ***examples*** contém exemplos de como usar a biblioteca. Para executar os exemplos, navegue até a pasta *examples* e execute:

```
go run main.go
Expand Down
12 changes: 6 additions & 6 deletions examples/mock_example.go → examples/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package examples
package main

import (
"fmt"

"github.com/brazzcore/mocai/pkg/mocai/constants"
"github.com/brazzcore/mocai/cmd/mocai"
"github.com/brazzcore/mocai/pkg/mocai/entities/address"
"github.com/brazzcore/mocai/pkg/mocai/entities/certificate"
"github.com/brazzcore/mocai/pkg/mocai/entities/company"
Expand All @@ -14,7 +14,7 @@ import (
"github.com/brazzcore/mocai/pkg/mocai/translations"
)

func GenerateMockExample() {
func main() {
// Set the language to pt-BR
translations.SetLanguage("ptbr")

Expand Down Expand Up @@ -61,8 +61,8 @@ func GenerateMockExample() {
return
}

fmt.Println(constants.HeaderMain)
fmt.Println(constants.SubHeader)
fmt.Println(mocai.HeaderMain)
fmt.Println(mocai.SubHeader)

fmt.Printf("Person: %s %s, %s, %d years old, CPF: %s\n",
p.FirstNameMale, p.LastName, p.Gender, p.Age, p.CPF)
Expand Down Expand Up @@ -92,5 +92,5 @@ func GenerateMockExample() {
vr.BrazilianVoteRegistration.Section, vr.BrazilianVoteRegistration.Zone,
vr.BrazilianVoteRegistration.Number)

fmt.Println(constants.Footer)
fmt.Println(mocai.Footer)
}
8 changes: 0 additions & 8 deletions main.go

This file was deleted.

3 changes: 1 addition & 2 deletions pkg/mocai/entities/company/countries/brazilian_company.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package countries

import (
"errors"
"fmt"
"math/rand"
"strings"
Expand All @@ -27,7 +26,7 @@ func GenerateBrazilianCompany(formatted bool) (*BrazilianCompany, error) {

// Validate data
if len(companyNames) == 0 {
return nil, errors.New("no company names available")
return nil, ErrNoCompanyNamesAvailable
}

// Choose a random company name
Expand Down
1 change: 1 addition & 0 deletions pkg/mocai/entities/company/countries/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ import "errors"
var (
ErrGeneratingBrazilianCompany = errors.New("error generating brazilian company")
ErrGeneratingCNPJ = errors.New("error generating CNPJ")
ErrNoCompanyNamesAvailable = errors.New("no company names available")
)
4 changes: 2 additions & 2 deletions pkg/mocai/entities/company/countries/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func TestGenerateCompany(t *testing.T) {
company, err := GenerateBrazilianCompany(false)
if err != nil {
t.Fatalf("Failed to generate company: %v", err)
t.Errorf("Failed to generate company: %v", err)
}

if company.CompanyName == "" || company.CNPJ == "" {
Expand All @@ -20,7 +20,7 @@ func TestGenerateCompany(t *testing.T) {
func TestIfCompanyHasAValidCNPJ(t *testing.T) {
company, err := GenerateBrazilianCompany(false)
if err != nil {
t.Fatalf("Failed to generate company: %v", err)
t.Errorf("Failed to generate company: %v", err)
}

if !cnpj.ValidateCNPJ(company.CNPJ) {
Expand Down
1 change: 0 additions & 1 deletion pkg/mocai/entities/company/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ type Company struct {
// GenerateCompany generates all companies available.
func GenerateCompany(formatted bool) (*Company, error) {
createdCompanyBrazilian, err := countries.GenerateBrazilianCompany(formatted)

if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/mocai/entities/cpf/generator.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cpf

import (
"errors"
"fmt"
"math/rand"
"strings"
Expand Down Expand Up @@ -34,7 +33,7 @@ func GenerateCPF(formatted bool) (string, error) {
// Format the CPF if requested
if formatted {
if len(cpf) != 11 {
return "", errors.New(ErrInvalidCPF.Error())
return "", ErrInvalidCPF
}
return cpf[:3] + "." + cpf[3:6] + "." + cpf[6:9] + "-" + cpf[9:], nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func GenerateBrazilianNationalID(formatted bool) (*RG, error) {
if formatted {
rgNumber = formatRG(rgNumber)
}

return &RG{
Number: rgNumber,
State: "SP",
Expand Down Expand Up @@ -64,14 +65,15 @@ func calculateSPRGDigit() (string, error) {
} else {
dvStr = strconv.Itoa(checkDigit)
}

full := fmt.Sprintf("%s%s", baseStr, dvStr)

return full, nil
}

func formatRG(rgNumber string) string {
if len(rgNumber) != 9 {
return rgNumber
}

return fmt.Sprintf("%s.%s.%s-%s", rgNumber[0:3], rgNumber[3:6], rgNumber[6:8], rgNumber[8:])
}
10 changes: 6 additions & 4 deletions pkg/mocai/entities/person/errors.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package person

import "errors"

// Error constants for person-related data generation failures.
// These errors represent specific failure scenarios and should be wrapped with additional
// context when returned.
const (
ERROR_GENERATING_PERSON = "error generating person"
ERROR_NO_FIRST_NAMES = "no data available for first names"
ERROR_NO_LAST_NAMES = "no data available for last names"
var (
ErrGeneratingPerson = errors.New("error generating person")
ErrNoFirstNames = errors.New("no data available for first names")
ErrNoLastNames = errors.New("no data available for last names")
)
13 changes: 5 additions & 8 deletions pkg/mocai/entities/person/generator.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package person

import (
"errors"
"fmt"
"math/rand"
"strings"
Expand Down Expand Up @@ -33,11 +32,11 @@ func GeneratePerson() (*Person, error) {

// Validate data
if len(firstNamesMale) == 0 || len(firstNamesFemale) == 0 {
return nil, errors.New(ERROR_NO_FIRST_NAMES)
return nil, ErrNoFirstNames
}

if len(lastNames) == 0 {
return nil, errors.New(ERROR_NO_LAST_NAMES)
return nil, ErrNoLastNames
}

// Choose random values
Expand All @@ -60,17 +59,15 @@ func GeneratePerson() (*Person, error) {
// Validate required fields
if firstNameMale == "" || firstNameFemale == "" || lastName == "" {
return nil, fmt.Errorf("%s: missing required data (firstNameMale: %s, firstNameFemale: %s, lastName: %s)",
ERROR_GENERATING_PERSON, firstNameMale, firstNameFemale, lastName)
ErrGeneratingPerson, firstNameMale, firstNameFemale, lastName)
}

createdPerson := &Person{
return &Person{
FirstNameMale: firstNameMale,
FirstNameFemale: firstNameFemale,
LastName: lastName,
Gender: gender,
Age: rand.Intn(80) + 18,
CPF: cpf,
}

return createdPerson, nil
}, nil
}
8 changes: 5 additions & 3 deletions pkg/mocai/entities/phone/errors.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package phone

import "errors"

// Error constants used for phone number generation failures.
// These errors represent specific failure scenarios and should be wrapped with additional
// context when returned.
const (
ERROR_GENERATING_PHONE = "error generating phone"
ERROR_NO_AREA_CODES = "no data available for area codes"
var (
ErrGeneratingPhone = errors.New("error generating phone")
ErrNoAreaCodes = errors.New("no data available for area codes")
)
12 changes: 5 additions & 7 deletions pkg/mocai/entities/phone/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ func GeneratePhone() (*Phone, error) {
// Get the list of area codes
areaCodeStr := translations.Get(lang, "phone_area_code")
if areaCodeStr == "" {
return nil, fmt.Errorf(ERROR_NO_AREA_CODES+" for: %s", lang)
return nil, fmt.Errorf(ErrNoAreaCodes.Error()+" for: %s", lang)
}
areaCodes := strings.Split(areaCodeStr, ",")

// Validate data
if len(areaCodes) == 0 {
return nil, fmt.Errorf(ERROR_NO_AREA_CODES+" for: %s", lang)
return nil, fmt.Errorf(ErrNoAreaCodes.Error()+" for: %s", lang)
}
areaCode := areaCodes[rand.Intn(len(areaCodes))]

Expand All @@ -37,13 +37,11 @@ func GeneratePhone() (*Phone, error) {

if areaCode == "" || number == "" {
return nil, fmt.Errorf("%s: missing required data (areaCode: %s, number: %s)",
ERROR_GENERATING_PHONE, areaCode, number)
ErrGeneratingPhone, areaCode, number)
}

createdPhone := &Phone{
return &Phone{
AreaCode: areaCode,
Number: number,
}

return createdPhone, nil
}, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,11 @@ func GenerateBrazilianVoteRegistration(formatted bool) (*BrazilianVoteRegistrati
number = fmt.Sprintf("%s %s %s", number[:4], number[4:8], number[8:])
}

createdBrazilianVoteRegistration := &BrazilianVoteRegistration{
return &BrazilianVoteRegistration{
Section: section,
Zone: zone,
Number: number,
}

return createdBrazilianVoteRegistration, nil
}, nil
}

// randomInt generates a random integer between min and max
Expand Down
Loading