Skip to content

Commit

Permalink
Refactoring (#9)
Browse files Browse the repository at this point in the history
* Refactor code

* Refactor code
  • Loading branch information
unanoc authored Nov 24, 2021
1 parent 3d10894 commit 4b45aeb
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 94 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions configs/example.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
10 changes: 5 additions & 5 deletions pkg/file/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -32,18 +32,18 @@ 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
}

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
}
Expand Down
22 changes: 6 additions & 16 deletions pkg/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,22 @@ 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{
Info: &info,
}
}

func (f *AssetFile) open() error {
func (f *AssetFile) Open() error {
file, err := os.Open(f.Info.Path())
if err != nil {
return err
Expand All @@ -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
}
Expand Down
18 changes: 9 additions & 9 deletions pkg/file/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,27 @@ 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 {
p := 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 {
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/validation/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ 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
}

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
Expand Down
13 changes: 6 additions & 7 deletions pkg/validation/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
32 changes: 8 additions & 24 deletions pkg/validation/folders.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand All @@ -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()))
Expand Down
14 changes: 5 additions & 9 deletions pkg/validation/info/fields_validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"))
}

Expand All @@ -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 {
Expand All @@ -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")
Expand All @@ -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
Expand Down Expand Up @@ -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"))
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/validation/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
10 changes: 5 additions & 5 deletions src/config/validators.go
Original file line number Diff line number Diff line change
@@ -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"`
Expand All @@ -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"`
}
Expand Down
Loading

0 comments on commit 4b45aeb

Please sign in to comment.