🇧🇷 CPF validation in Go

Moves to Tangled

+26 -42
-30
.github/workflows/tests.yml
··· 1 - name: Tests 2 - 3 - on: [pull_request, push] 4 - 5 - jobs: 6 - 7 - build: 8 - name: Build 9 - runs-on: ubuntu-latest 10 - steps: 11 - 12 - - name: Set up Go 1.14 13 - uses: actions/setup-go@v1 14 - with: 15 - go-version: 1.14 16 - id: go 17 - 18 - - name: Check out code into the Go module directory 19 - uses: actions/checkout@v2 20 - 21 - - name: Get dependencies 22 - run: | 23 - go get -v -t -d ./... 24 - if [ -f Gopkg.toml ]; then 25 - curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh 26 - dep ensure 27 - fi 28 - 29 - - name: Build 30 - run: go test
+15
.tangled/workflows/tests.yaml
··· 1 + when: 2 + - event: ["push"] 3 + branch: ["main"] 4 + engine: "nixery" 5 + dependencies: 6 + nixpkgs: 7 + - go 8 + - golangci-lint 9 + steps: 10 + - name: Formatting 11 + command: if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then exit 1; fi 12 + - name: Linter 13 + command: golangci-lint run ./... 14 + - name: Tests 15 + command: go test -v -race ./...
+2 -2
README.md
··· 1 - # Go CPF [![Tests](https://github.com/cuducos/go-cpf/workflows/Tests/badge.svg)]()[![GoDoc](https://godoc.org/github.com/cuducos/go-cpf?status.svg)](https://godoc.org/github.com/cuducos/go-cpf)![Go version](https://img.shields.io/github/go-mod/go-version/cuducos/go-cpf) 1 + # Go CPF [![GoDoc](https://godoc.org/tangled.org/cuducos.me/go-cpf?status.svg)](https://godoc.org/tangled.org/cuducos.me/go-cpf)![Go version](https://img.shields.io/github/go-mod/go-version/cuducos/go-cpf) 2 2 3 3 A Go module to validate CPF numbers (Brazilian unique identifier for the Federal Revenue). 4 4 5 5 ```go 6 6 package main 7 7 8 - import "github.com/cuducos/go-cpf" 8 + import "tangled.org/cuducos.me/go-cpf" 9 9 10 10 11 11 func main() {
+7 -8
cpf.go
··· 7 7 "strings" 8 8 ) 9 9 10 - //These are auxiliar type and variable to build a set 10 + // These are auxiliar type and variable to build a set 11 11 type void struct{} 12 12 13 13 var member void 14 - 15 14 16 15 func checksum(ds []int64) int64 { 17 16 var s int64 ··· 25 24 return r 26 25 } 27 26 28 - //IsValid checks whether CPF number is valid or not 29 - func IsValid(n string) bool { 27 + // IsValid checks whether CPF number is valid or not 28 + func IsValid(n string) bool { 30 29 u := Unmask(n) 31 30 32 31 if len(u) != 11 { ··· 52 51 return checksum(ds[:9]) == ds[9] && checksum(ds[:10]) == ds[10] 53 52 } 54 53 55 - //Mask returns the CPF number formatted 56 - func Mask(n string) string { 54 + // Mask returns the CPF number formatted 55 + func Mask(n string) string { 57 56 u := Unmask(n) 58 57 if len(u) != 11 { 59 58 return n ··· 61 60 return fmt.Sprintf("%s.%s.%s-%s", u[:3], u[3:6], u[6:9], u[9:]) 62 61 } 63 62 64 - //Unmask removes any non-digit (numeric) from the CPF number 65 - func Unmask(n string) string { 63 + // Unmask removes any non-digit (numeric) from the CPF number 64 + func Unmask(n string) string { 66 65 return regexp.MustCompile(`\D`).ReplaceAllString(n, "") 67 66 }
+1 -1
cpf_test.go
··· 21 21 } 22 22 23 23 func TestUnmask(t *testing.T) { 24 - if got := Unmask("111.111.111-11"); "11111111111" != got { 24 + if got := Unmask("111.111.111-11"); got != "11111111111" { 25 25 t.Errorf("Unmask(\"111.111.111-11\") = %v; want 11111111111", got) 26 26 } 27 27 }
+1 -1
go.mod
··· 1 - module github.com/cuducos/go-cpf 1 + module tangled.org/cuducos.me/go-cpf 2 2 3 3 go 1.14