Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change switch case to maps #6

Merged
merged 1 commit into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.23'
go-version: '1.22'

- name: Build
run: go build -v ./...
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module verifycat

go 1.23
go 1.22

require github.com/gin-gonic/gin v1.9.1

Expand Down
43 changes: 21 additions & 22 deletions validate/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,35 @@ type CreditCardResult struct {
Brand string `json:"brand,omitempty"`
}

// Mapeamento de tipos para funções de validação
var validationFuncMap = map[string]func(string) (bool, string){
"cpf": func(value string) (bool, string) { return IsValidCPF(value), "" },
"cnpj": func(value string) (bool, string) { return IsValidCNPJ(value), "" },
"url": func(value string) (bool, string) { return IsValidURL(value), "" },
"creditcard": ValidateCreditCard,
"email": func(value string) (bool, string) { return IsValidEmail(value), "" },
}

func ValidateHandler(c *gin.Context) {
var req ValidationRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

validationFunc, exists := validationFuncMap[req.Type]
if !exists {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid validation type"})
return
}

var isValid bool
var message string
var brand string

switch req.Type {
case "cpf":
isValid = IsValidCPF(req.Value)
message = "CPF"
case "cnpj":
isValid = IsValidCNPJ(req.Value)
message = "CNPJ"
case "url":
isValid = IsValidURL(req.Value)
message = "URL"
case "creditcard":
isValid, brand = ValidateCreditCard(req.Value)
message = "Credit Card"
case "email":
isValid = IsValidEmail(req.Value)
message = "Email"
default:
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid validation type"})
return
if req.Type == "creditcard" {
isValid, brand = validationFunc(req.Value)
} else {
isValid, _ = validationFunc(req.Value)
}

var result interface{}
Expand All @@ -60,7 +59,7 @@ func ValidateHandler(c *gin.Context) {
}{
ValidationResult: ValidationResult{
IsValid: isValid,
Message: message,
Message: req.Type,
},
CreditCard: CreditCardResult{
Brand: brand,
Expand All @@ -69,7 +68,7 @@ func ValidateHandler(c *gin.Context) {
} else {
result = ValidationResult{
IsValid: isValid,
Message: message,
Message: req.Type,
}
}

Expand Down
Loading