From 4b45aeb96cb9f23bf3ddc04fcaf1fb9e4e7d3dfd Mon Sep 17 00:00:00 2001 From: Daniel <24758309+leedaniil@users.noreply.github.com> Date: Wed, 24 Nov 2021 20:39:08 +0300 Subject: [PATCH] Refactoring (#9) * Refactor code * Refactor code --- .github/workflows/ci.yml | 2 +- configs/example.config.yaml | 14 +++++------ pkg/file/cache.go | 10 ++++---- pkg/file/file.go | 22 +++++----------- pkg/file/path.go | 18 ++++++------- pkg/validation/address.go | 4 +-- pkg/validation/errors.go | 13 +++++----- pkg/validation/folders.go | 32 ++++++------------------ pkg/validation/info/fields_validators.go | 14 ++++------- pkg/validation/names.go | 4 +-- src/config/config.go | 2 +- src/config/validators.go | 10 ++++---- src/validator/service.go | 7 ++---- src/validator/validators.go | 2 +- 14 files changed, 60 insertions(+), 94 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index aeae497..2f1906d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,7 +11,7 @@ jobs: name: Unit runs-on: ubuntu-latest steps: - - name: Set up Go 1.x + - name: Set up Go 1.17 uses: actions/setup-go@v2 with: go-version: 1.17 diff --git a/configs/example.config.yaml b/configs/example.config.yaml index 8bab863..30ea9fd 100644 --- a/configs/example.config.yaml +++ b/configs/example.config.yaml @@ -6,17 +6,12 @@ client_urls: validators_settings: image_file: - size: 100 # file size in KB + size_kb: 100 max_w: 512 max_h: 512 min_w: 50 min_h: 50 - asset_folder: - allowed_files: - - "logo.png" - - "info.json" - root_folder: allowed_files: - ".github" @@ -50,11 +45,16 @@ validators_settings: - "validators" - "info" + asset_folder: + allowed_files: + - "logo.png" + - "info.json" + chain_info_folder: has_files: - "logo.png" - "info.json" - + chain_validators_asset_folder: has_files: - "logo.png" diff --git a/pkg/file/cache.go b/pkg/file/cache.go index 51f3177..0abe9f8 100644 --- a/pkg/file/cache.go +++ b/pkg/file/cache.go @@ -13,8 +13,8 @@ func NewFileService(filePaths ...string) *FileService { var filesMap = make(map[string]*AssetFile) for _, path := range filePaths { - assetF := newAssetFile(path) - filesMap[path] = assetF + assetFile := NewAssetFile(path) + filesMap[path] = assetFile } return &FileService{ @@ -32,7 +32,7 @@ func (f *FileService) GetAssetFile(path string) (*AssetFile, error) { func (f *FileService) getFile(path string) (*AssetFile, error) { if file, exists := f.cache[path]; exists { - err := file.open() + err := file.Open() if err != nil { return nil, err } @@ -40,10 +40,10 @@ func (f *FileService) getFile(path string) (*AssetFile, error) { return file, nil } - assetF := newAssetFile(path) + assetF := NewAssetFile(path) f.cache[path] = assetF - err := assetF.open() + err := assetF.Open() if err != nil { return nil, err } diff --git a/pkg/file/file.go b/pkg/file/file.go index ea7f666..49fcd31 100644 --- a/pkg/file/file.go +++ b/pkg/file/file.go @@ -7,25 +7,14 @@ import ( ) type AssetFile struct { - Info *AssetInfo *os.File -} - -func Open(path string) (*AssetFile, error) { - file := newAssetFile(path) - err := file.open() - if err != nil { - return nil, err - } - return file, nil + Info *AssetInfo } -func newAssetFile(path string) *AssetFile { - p := NewPath(path) - +func NewAssetFile(path string) *AssetFile { info := AssetInfo{ - path: p, + path: NewPath(path), } return &AssetFile{ @@ -33,7 +22,7 @@ func newAssetFile(path string) *AssetFile { } } -func (f *AssetFile) open() error { +func (f *AssetFile) Open() error { file, err := os.Open(f.Info.Path()) if err != nil { return err @@ -53,8 +42,9 @@ func (i *AssetInfo) Path() string { } func (i *AssetInfo) Type() string { - return i.path.type_ + return i.path.fileType } + func (i *AssetInfo) Chain() coin.Coin { return i.path.chain } diff --git a/pkg/file/path.go b/pkg/file/path.go index 6ab4b35..84a162c 100644 --- a/pkg/file/path.go +++ b/pkg/file/path.go @@ -73,10 +73,10 @@ var regexes = map[string]*regexp.Regexp{ } type Path struct { - path string - chain coin.Coin - asset string - type_ string + path string + chain coin.Coin + asset string + fileType string } func NewPath(path string) *Path { @@ -84,16 +84,16 @@ func NewPath(path string) *Path { path: path, } - type_, reg := defineFileType(path) + fileType, reg := defineFileType(path) if reg == nil { - p.type_ = TypeUnknown + p.fileType = TypeUnknown return &p } match := reg.FindStringSubmatch(path) - if type_ != TypeUnknown { - p.type_ = type_ + if fileType != TypeUnknown { + p.fileType = fileType } if len(match) >= 2 { @@ -113,7 +113,7 @@ func NewPath(path string) *Path { } func (p Path) Type() string { - return p.type_ + return p.fileType } func (p Path) String() string { diff --git a/pkg/validation/address.go b/pkg/validation/address.go index 1caea60..0d391f2 100644 --- a/pkg/validation/address.go +++ b/pkg/validation/address.go @@ -59,7 +59,7 @@ func ValidateValidatorsAddress(chain coin.Coin, address string) error { func ValidateWavesAddress(addr string) error { condition := strings.HasPrefix(addr, "3P") && len(addr) == 35 && !pkg.IsLowerCase(addr) && !pkg.IsUpperCase(addr) if !condition { - return fmt.Errorf("%w, %s - should be Waves address", ErrInvalidAddress, addr) + return fmt.Errorf("%w: should be Waves address", ErrInvalidAddress) } return nil @@ -67,7 +67,7 @@ func ValidateWavesAddress(addr string) error { func ValidateTezosAddress(addr string) error { if !strings.HasPrefix(addr, "tz") { - return fmt.Errorf("%w, shoud be valid tezos address", ErrInvalidAddress) + return fmt.Errorf("%w: shoud be valid tezos address", ErrInvalidAddress) } return nil diff --git a/pkg/validation/errors.go b/pkg/validation/errors.go index 1f8976a..649ba81 100644 --- a/pkg/validation/errors.go +++ b/pkg/validation/errors.go @@ -6,16 +6,15 @@ import ( ) var ( - ErrMissingFile = errors.New("missing file") - ErrNotAllowedFile = errors.New("file is not allowed") - ErrInvalidJson = errors.New("invalid json") - ErrInvalidImgDimension = errors.New("invalid file dimension") - + ErrMissingFile = errors.New("missing file") + ErrNotAllowedFile = errors.New("file is not allowed") + ErrInvalidAddress = errors.New("invalid address") + ErrInvalidJson = errors.New("invalid json") + ErrInvalidImgDimension = errors.New("invalid file dimension") ErrInvalidFileCase = errors.New("invalid file name case") ErrInvalidFileExt = errors.New("invalid file extension") ErrInvalidFileSize = errors.New("invalid file size") ErrInvalidFileNameLength = errors.New("invalid file name length") - ErrInvalidAddress = errors.New("invalid address") ) func NewErrComposite() *ErrComposite { @@ -33,7 +32,7 @@ func (e *ErrComposite) Len() int { func (e *ErrComposite) Error() string { var msg string for _, err := range e.errors { - msg = fmt.Sprintf("%s - validation error: %s \n", msg, err.Error()) + msg += fmt.Sprintf("- %s\n", err.Error()) } return msg diff --git a/pkg/validation/folders.go b/pkg/validation/folders.go index 6bc177f..579b0ab 100644 --- a/pkg/validation/folders.go +++ b/pkg/validation/folders.go @@ -8,43 +8,26 @@ import ( ) func ValidateHasFiles(files []fs.DirEntry, fileNames []string) error { - if len(files) < len(fileNames) { - return fmt.Errorf("%w, folders length shorter then needed", ErrMissingFile) - } - compErr := NewErrComposite() -OutLoop: - for _, fName := range fileNames { - for _, dirF := range files { - if dirF.Name() == fName { - continue OutLoop - } - } - compErr.Append(fmt.Errorf("%w %s", ErrMissingFile, fName)) - } + if len(files) < len(fileNames) { + compErr.Append(fmt.Errorf("%w: this folder must have more files", ErrMissingFile)) - if compErr.Len() > 0 { return compErr } - return nil -} - -func ValidateFilesNotInList(files []fs.DirEntry, fileList []string) error { - compErr := NewErrComposite() - - for _, dir := range files { + for _, fName := range fileNames { var found bool - for _, f := range fileList { - if dir.Name() == f { + + for _, dirF := range files { + if dirF.Name() == fName { found = true break } } if !found { - compErr.Append(fmt.Errorf("%w: %s", ErrMissingFile, dir.Name())) + compErr.Append(fmt.Errorf("%w: %s", ErrMissingFile, fName)) } } @@ -57,6 +40,7 @@ func ValidateFilesNotInList(files []fs.DirEntry, fileList []string) error { func ValidateAllowedFiles(files []fs.DirEntry, allowedFiles []string) error { compErr := NewErrComposite() + for _, f := range files { if !pkg.Contains(f.Name(), allowedFiles) { compErr.Append(fmt.Errorf("%w: %s", ErrNotAllowedFile, f.Name())) diff --git a/pkg/validation/info/fields_validators.go b/pkg/validation/info/fields_validators.go index 28bbd1a..5cbc538 100644 --- a/pkg/validation/info/fields_validators.go +++ b/pkg/validation/info/fields_validators.go @@ -4,8 +4,6 @@ import ( "fmt" "strings" - log "github.com/sirupsen/logrus" - "github.com/trustwallet/assets-go-libs/pkg" "github.com/trustwallet/assets-go-libs/pkg/validation" "github.com/trustwallet/go-primitives/coin" @@ -47,7 +45,7 @@ func ValidateAssetRequiredKeys(a AssetModel) error { } if len(fields) != len(requiredAssetFields) { - return fmt.Errorf("missing or empty required fields \n-%s", + return fmt.Errorf("missing or empty required fields\n-%s", strings.Join(difference(requiredAssetFields, fields), "\n")) } @@ -57,7 +55,7 @@ func ValidateAssetRequiredKeys(a AssetModel) error { func ValidateAssetType(type_ string, chain coin.Coin) error { chainFromType, err := types.GetChainFromAssetType(type_) if err != nil { - return fmt.Errorf("invalid value of field - type, %w", err) + return fmt.Errorf("invalid type field: %w", err) } if chainFromType != chain { @@ -73,10 +71,8 @@ func ValidateAssetType(type_ string, chain coin.Coin) error { func ValidateAssetID(id string, address string) error { if id != address { - log.Debugf("id - %s, addr - %s", id, address) - if !strings.EqualFold(id, address) { - return fmt.Errorf("invalid value for id field") + return fmt.Errorf("invalid id field") } return fmt.Errorf("invalid casing for id field") @@ -87,7 +83,7 @@ func ValidateAssetID(id string, address string) error { func ValidateAssetDecimalsAccordingType(type_ string, decimals int) error { if type_ == "BEP2" && decimals != 8 { - return fmt.Errorf("invalid value for field - decimals, BEP2 tokens have 8 decimals") + return fmt.Errorf("invalid decimals field, BEP2 tokens have 8 decimals") } return nil @@ -123,7 +119,7 @@ func ValidateCoinRequiredKeys(c CoinModel) error { } if len(fields) != len(requiredCoinFields) { - return fmt.Errorf("missing or empty required fields \n-%s", + return fmt.Errorf("missing or empty required fields\n-%s", strings.Join(difference(requiredCoinFields, fields), "\n")) } diff --git a/pkg/validation/names.go b/pkg/validation/names.go index f78347a..e5a0329 100644 --- a/pkg/validation/names.go +++ b/pkg/validation/names.go @@ -9,7 +9,7 @@ import ( func ValidateLowercase(name string) error { if !pkg.IsLowerCase(name) { - return fmt.Errorf("%w: file - %s should be in lowercase", ErrInvalidFileCase, name) + return fmt.Errorf("%w: it should be in lowercase", ErrInvalidFileCase) } return nil @@ -18,7 +18,7 @@ func ValidateLowercase(name string) error { func ValidateExtension(name, ext string) error { fileExtension := filepath.Ext(name) if fileExtension != ext { - return fmt.Errorf("%w: %s, only .png allowed in folder", ErrInvalidFileExt, name) + return fmt.Errorf("%w: only %s allowed in folder", ErrInvalidFileExt, ext) } return nil diff --git a/src/config/config.go b/src/config/config.go index 0d5fe30..2afd469 100644 --- a/src/config/config.go +++ b/src/config/config.go @@ -23,9 +23,9 @@ type ( ValidatorsSettings struct { ImageFile ImageFile `mapstructure:"image_file"` - AssetFolder AssetFolder `mapstructure:"asset_folder"` RootFolder RootFolder `mapstructure:"root_folder"` ChainFolder ChainFolder `mapstructure:"chain_folder"` + AssetFolder AssetFolder `mapstructure:"asset_folder"` ChainInfoFolder ChainInfoFolder `mapstructure:"chain_info_folder"` ChainValidatorsAssetFolder ChainValidatorsAssetFolder `mapstructure:"chain_validators_asset_folder"` DappsFolder DappsFolder `mapstructure:"dapps_folder"` diff --git a/src/config/validators.go b/src/config/validators.go index 9b631a7..6ab8917 100644 --- a/src/config/validators.go +++ b/src/config/validators.go @@ -1,17 +1,13 @@ package config type ImageFile struct { - Size int `mapstructure:"size,omitempty"` + Size int `mapstructure:"size_kb,omitempty"` MaxH int `mapstructure:"max_h,omitempty"` MaxW int `mapstructure:"max_w,omitempty"` MinH int `mapstructure:"min_h,omitempty"` MinW int `mapstructure:"min_w,omitempty"` } -type AssetFolder struct { - AllowedFiles []string `mapstructure:"allowed_files,omitempty"` -} - type RootFolder struct { AllowedFiles []string `mapstructure:"allowed_files,omitempty"` SkipFiles []string `mapstructure:"skip_files,omitempty"` @@ -21,6 +17,10 @@ type ChainFolder struct { AllowedFiles []string `mapstructure:"allowed_files,omitempty"` } +type AssetFolder struct { + AllowedFiles []string `mapstructure:"allowed_files,omitempty"` +} + type ChainInfoFolder struct { HasFiles []string `mapstructure:"has_files,omitempty"` } diff --git a/src/validator/service.go b/src/validator/service.go index 17ff203..7ff2ad4 100644 --- a/src/validator/service.go +++ b/src/validator/service.go @@ -8,10 +8,8 @@ type Service struct { fileService *file.FileService } -func NewService(fileProvider *file.FileService) (*Service, error) { - return &Service{ - fileService: fileProvider, - }, nil +func NewService(fileProvider *file.FileService) *Service { + return &Service{fileService: fileProvider} } // nolint:funlen @@ -94,7 +92,6 @@ func (s *Service) GetFixer(f *file.AssetFile) *Validator { fileType := f.Info.Type() switch fileType { - } return nil diff --git a/src/validator/validators.go b/src/validator/validators.go index ec651c9..2197a73 100644 --- a/src/validator/validators.go +++ b/src/validator/validators.go @@ -274,7 +274,7 @@ func (s *Service) ValidateValidatorsListFile(file *file.AssetFile) error { return err } - err = validation.ValidateFilesNotInList(dirAssetFolderFiles, listIDs) + err = validation.ValidateAllowedFiles(dirAssetFolderFiles, listIDs) if err != nil { return err }