Skip to content

Commit

Permalink
Ajustes sugeridos pelo linter #2
Browse files Browse the repository at this point in the history
  • Loading branch information
dude333 committed Oct 5, 2023
1 parent 64510a3 commit fc5e43f
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 35 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ debug.test
target/*
on
*.xlsx
golangci*.y*ml
8 changes: 4 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import (
)

var flags = struct {
debug bool
trace bool
cfgFile string
atualizar flagsAtualizar
relatorio flagsRelatorio
dataSrc string // banco de dados sqlite (ex.: "file:/var/local/rapina.db")
tempDir string // arquivos temporários
relatorio flagsRelatorio
atualizar flagsAtualizar
debug bool
trace bool
}{}

const (
Expand Down
37 changes: 19 additions & 18 deletions pkg/contabil/dominio/contabil.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,42 @@ type DemonstraçãoFinanceira struct {
Contas []Conta
}

func (d DemonstraçãoFinanceira) Válida() bool {
return len(d.CNPJ) == len("17.836.901/0001-10") &&
len(d.Nome) > 0 &&
d.Ano >= 2000 && d.Ano < 2221 && // 2 séculos de rapina :)
len(d.Contas) > 0
func (df *DemonstraçãoFinanceira) Válida() bool {
return len(df.CNPJ) == len("17.836.901/0001-10") &&
len(df.Nome) > 0 &&
df.Ano >= 2000 && df.Ano < 2221 && // 2 séculos de rapina :)
len(df.Contas) > 0
}

// Conta com os dados das Demonstrações Financeiras Padronizadas (DFP) ou
// com as Informações Trimestrais (ITR).
type Conta struct {
Código string // 1, 1.01, 1.02...
Descr string
Consolidado bool // Individual ou Consolidado
Grupo string // BPA, BPP, DRE, DFC...
DataIniExerc string // AAAA-MM-DD
DataFimExerc string // AAAA-MM-DD
Meses int // Meses acumulados desde o início do período
OrdemExerc string // ÚLTIMO ou PENÚLTIMO
Total rapina.Dinheiro
Código string // 1, 1.01, 1.02...
Descr string // Descrição
Grupo string // BPA, BPP, DRE, DFC...
DataIniExerc string // AAAA-MM-DD
DataFimExerc string // AAAA-MM-DD
OrdemExerc string // ÚLTIMO ou PENÚLTIMO
Total rapina.Dinheiro // $
Meses int // Meses acumulados desde o início do período
Consolidado bool // Individual ou Consolidado
}

// Válida retorna verdadeiro se os dados da conta são válidos. Ignora os registros
// do penúltimo ano, com exceção de 2009, uma vez que a CVM só disponibliza (pelo
// menos em 2021) dados até 2010.
func (c Conta) Válida() bool {
func (c *Conta) Válida() bool {
return len(c.Código) > 0 &&
len(c.Descr) > 0 &&
len(c.DataFimExerc) == len("AAAA-MM-DD") &&
(c.OrdemExerc == "ÚLTIMO" ||
(c.OrdemExerc == "PENÚLTIMO" && strings.HasPrefix(c.DataFimExerc, "2009")))
}

func (df DemonstraçãoFinanceira) String() string {
func (df *DemonstraçãoFinanceira) String() string {
var contasStr []string
for _, conta := range df.Contas {
for i := range df.Contas {
conta := &df.Contas[i]
contaStr := fmt.Sprintf(
"Código: %s\nDescr: %s\nConsolidado: %t\nGrupo: %s\nDataIniExerc: %s\nDataFimExerc: %s\nMeses: %d\nOrdemExerc: %s\nTotal: %v\n",
conta.Código, conta.Descr, conta.Consolidado, conta.Grupo, conta.DataIniExerc, conta.DataFimExerc,
Expand Down Expand Up @@ -102,9 +103,9 @@ type ConfigConta struct {
// -- REPOSITÓRIO & SERVIÇO --

type Resultado struct {
Error error
Empresa *DemonstraçãoFinanceira
Hash string
Error error
}

type Serviço interface {
Expand Down
16 changes: 9 additions & 7 deletions pkg/contabil/repositorio/repositorio_cvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ func (c *CVM) Importar(ctx context.Context, ano int, trimestral bool) <-chan dom

url := urlArquivo(ano, trimestral)

arquivos, err := c.infra.DownloadAndUnzip(url, filtros())
arquivos, err := c.DownloadAndUnzip(url, filtros())
if err != nil {
results <- dominio.Resultado{Error: err}
return
}
defer func() {
_ = c.infra.Cleanup(arquivos)
_ = c.Cleanup(arquivos)
}()

for _, arquivo := range arquivos {
Expand Down Expand Up @@ -170,10 +170,12 @@ func filtros() []string {
}

for _, t := range tipo {
filtros = append(filtros, "dfp_cia_aberta_"+t+"_con")
filtros = append(filtros, "dfp_cia_aberta_"+t+"_ind")
filtros = append(filtros, "itr_cia_aberta_"+t+"_con")
filtros = append(filtros, "itr_cia_aberta_"+t+"_ind")
filtros = append(filtros,
"dfp_cia_aberta_"+t+"_con",
"dfp_cia_aberta_"+t+"_ind",
"itr_cia_aberta_"+t+"_con",
"itr_cia_aberta_"+t+"_ind",
)
}

return filtros
Expand All @@ -188,7 +190,7 @@ func urlArquivo(ano int, trimestral bool) string {
return `http://dados.cvm.gov.br/dados/CIA_ABERTA/DOC/` + tipo + `/DADOS/` + zip
}

func processarArquivoDFP(ctx context.Context, arquivo Arquivo, results chan<- dominio.Resultado) error {
func processarArquivoDFP(_ context.Context, arquivo Arquivo, results chan<- dominio.Resultado) error {
fh, err := os.Open(arquivo.path)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion pkg/contabil/repositorio/repositorio_erros.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
var (
ErrArquivoInválido = errors.New("arquivo inválido")
ErrDFPInválida = errors.New("DFP inválida")
ErrSemDados = errors.New("Sem dados")
ErrSemDados = errors.New("sem dados")

ErrAnoInválidoFn = func(ano int) error { return fmt.Errorf("ano inválido: %d", ano) }
)
4 changes: 2 additions & 2 deletions pkg/contabil/repositorio/repositorio_sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,9 @@ func inserirContas(ctx context.Context, db *sqlx.DB, id int, contas []dominio.Co
if sqliteErr.Code != sqlite3.ErrConstraint {
_ = tx.Rollback()
return err
} else {
progress.ErrorMsg("%s: %d, %s, %#v", err, id, nome, contas[i])
}
progress.ErrorMsg("%s: %d, %s, %#v", err, id, nome, contas[i])

}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/infra/infra_unzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func Unzip(src, dest string, filters []string, verbose bool) ([]string, error) {

for _, f := range r.File {

// https://stackoverflow.com/a/45617791/276311
// deferInLoop: stackoverflow.com/a/45617791/276311
ff, err := func() ([]string, error) {
if !matchFilter(f.Name, filters) {
return filenames, nil // continue
Expand Down
3 changes: 1 addition & 2 deletions rapina.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,8 @@ func equalizarValores(ano int, v1, v2 ValoresTrimestrais) (ValoresTrimestrais, b
}
if v1Tn != 0.0 && v2Tn == 0.0 {
return v1Tn, true
} else {
return v2Tn, true
}
return v2Tn, true
}

v.T1, ok = check(v1.T1, v2.T1)
Expand Down

0 comments on commit fc5e43f

Please sign in to comment.