From e38890b306b5cdabb1af4824a8225445e7d1e210 Mon Sep 17 00:00:00 2001 From: Marcus Carvalho <135276762+gatinhodev@users.noreply.github.com> Date: Sun, 11 Feb 2024 06:06:50 -0300 Subject: [PATCH] change switch case to maps feat: changed the switch case using maps in api.go. fix: version correction in go.mod and go.yml files. --- .github/workflows/go.yml | 2 +- go.mod | 2 +- validate/api.go | 43 ++++++++++++++++++++-------------------- 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 38b2f28..d36ceed 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -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 ./... diff --git a/go.mod b/go.mod index b784b01..1b5cd11 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module verifycat -go 1.23 +go 1.22 require github.com/gin-gonic/gin v1.9.1 diff --git a/validate/api.go b/validate/api.go index 2491a32..f2a7e3f 100644 --- a/validate/api.go +++ b/validate/api.go @@ -20,6 +20,15 @@ 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 { @@ -27,29 +36,19 @@ func ValidateHandler(c *gin.Context) { 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{} @@ -60,7 +59,7 @@ func ValidateHandler(c *gin.Context) { }{ ValidationResult: ValidationResult{ IsValid: isValid, - Message: message, + Message: req.Type, }, CreditCard: CreditCardResult{ Brand: brand, @@ -69,7 +68,7 @@ func ValidateHandler(c *gin.Context) { } else { result = ValidationResult{ IsValid: isValid, - Message: message, + Message: req.Type, } }