code quality improvements

This commit is contained in:
Pavel 2019-12-12 10:08:26 +03:00
parent 6290301815
commit adccc7b3b6
5 changed files with 53 additions and 34 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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
}

View File

@ -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
}