From adccc7b3b67db3461617cffafe6b8977e9d282b3 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 12 Dec 2019 10:08:26 +0300 Subject: [PATCH] code quality improvements --- core/config.go | 2 +- core/migrate.go | 22 ++++++------- core/template.go | 1 + core/translations_extractor.go | 60 ++++++++++++++++++++++------------ core/utils.go | 2 +- 5 files changed, 53 insertions(+), 34 deletions(-) diff --git a/core/config.go b/core/config.go index fd64441..7206c31 100644 --- a/core/config.go +++ b/core/config.go @@ -18,7 +18,7 @@ var ( markdownSymbols = []string{"*", "_", "`", "["} regCommandName = regexp.MustCompile( `^https://?[\da-z.-]+\.(retailcrm\.(ru|pro|es)|ecomlogic\.com|simlachat\.(com|ru))/?$`) - slashRegex = regexp.MustCompile(`/+$`) + slashRegex = regexp.MustCompile(`/+$`) ) // ConfigInterface settings data structure diff --git a/core/migrate.go b/core/migrate.go index 37b5eac..e6ce331 100644 --- a/core/migrate.go +++ b/core/migrate.go @@ -83,15 +83,15 @@ func (m *Migrate) Rollback() error { return errors.New("abnormal termination: first migration is nil") } - if err := m.GORMigrate.RollbackTo(m.first.ID); err == nil { - if err := m.GORMigrate.RollbackMigration(m.first); err == nil { - return nil - } - - return err - } else { + if err := m.GORMigrate.RollbackTo(m.first.ID); err != nil { return err } + + if err := m.GORMigrate.RollbackMigration(m.first); err != nil { + return err + } + + return nil } // MigrateTo specified version @@ -183,12 +183,12 @@ func (m *Migrate) Current() string { return "0" } - if err := m.db.Last(&migrationInfo).Error; err == nil { - return migrationInfo.ID + if err := m.db.Last(&migrationInfo).Error; err != nil { + fmt.Printf("warning => cannot fetch migration version: %s\n", err.Error()) + return "0" } - fmt.Printf("warning => cannot fetch migration version: %s\n", err.Error()) - return "0" + return migrationInfo.ID } // NextFrom returns next version from passed version diff --git a/core/template.go b/core/template.go index 20e4365..12cdaf8 100644 --- a/core/template.go +++ b/core/template.go @@ -4,6 +4,7 @@ import ( "html/template" "github.com/gin-contrib/multitemplate" + "github.com/gobuffalo/packr/v2" ) // Renderer wraps multitemplate.Renderer in order to make it easier to use diff --git a/core/translations_extractor.go b/core/translations_extractor.go index 3352307..21bcce1 100644 --- a/core/translations_extractor.go +++ b/core/translations_extractor.go @@ -32,45 +32,59 @@ func NewTranslationsExtractor(fileNameTemplate string) *TranslationsExtractor { func (t *TranslationsExtractor) unmarshalToMap(in []byte) (map[string]interface{}, error) { var dataMap map[string]interface{} - if err := yaml.Unmarshal(in, &dataMap); err == nil { - return dataMap, nil - } else { + if err := yaml.Unmarshal(in, &dataMap); err != nil { return dataMap, err } + + return dataMap, nil } // loadYAMLBox loads YAML from box func (t *TranslationsExtractor) loadYAMLBox(fileName string) (map[string]interface{}, error) { - var dataMap map[string]interface{} + var ( + dataMap map[string]interface{} + data []byte + err error + ) - if data, err := t.TranslationsBox.Find(fileName); err != nil { + if data, err = t.TranslationsBox.Find(fileName); err != nil { return dataMap, err - } else { - return t.unmarshalToMap(data) } + + return t.unmarshalToMap(data) } // loadYAMLFile loads YAML from file func (t *TranslationsExtractor) loadYAMLFile(fileName string) (map[string]interface{}, error) { - var dataMap map[string]interface{} + var ( + dataMap map[string]interface{} + info os.FileInfo + err error + ) - if info, err := os.Stat(fileName); err == nil { + if info, err = os.Stat(fileName); err == nil { if !info.IsDir() { - if path, err := filepath.Abs(fileName); err == nil { - if source, err := ioutil.ReadFile(path); err != nil { - return dataMap, err - } else { - return t.unmarshalToMap(source) - } - } else { + var ( + path string + source []byte + err error + ) + + if path, err = filepath.Abs(fileName); err != nil { return dataMap, err } - } else { - return dataMap, errors.New("directory provided instead of file") + + if source, err = ioutil.ReadFile(path); err != nil { + return dataMap, err + } + + return t.unmarshalToMap(source) } - } else { - return dataMap, err + + return dataMap, errors.New("directory provided instead of file") } + + return dataMap, err } // loadYAML loads YAML from filesystem or from packr box - depends on what was configured. Can return error. @@ -106,7 +120,11 @@ func (t *TranslationsExtractor) LoadLocale(locale string) (map[string]interface{ // LoadLocaleKeys returns only sorted keys from translation file func (t *TranslationsExtractor) LoadLocaleKeys(locale string) ([]string, error) { - if data, err := t.LoadLocale(locale); err != nil { + var ( + data map[string]interface{} + err error + ) + if data, err = t.LoadLocale(locale); err != nil { return []string{}, err } diff --git a/core/utils.go b/core/utils.go index aa25a5b..c0161e6 100644 --- a/core/utils.go +++ b/core/utils.go @@ -25,9 +25,9 @@ import ( // Utils service object type Utils struct { IsDebug bool + TokenCounter uint32 ConfigAWS ConfigAWS Logger *logging.Logger - TokenCounter uint32 slashRegex *regexp.Regexp }