mirror of
https://github.com/Instadapp/trustwallet-assets.git
synced 2024-07-29 22:37:31 +00:00
e7f9efcd2c
* Add tokenlist validation * Fix according to review
102 lines
2.2 KiB
Go
102 lines
2.2 KiB
Go
package service
|
|
|
|
import (
|
|
"github.com/trustwallet/assets-go-libs/validation"
|
|
"github.com/trustwallet/assets/internal/file"
|
|
"github.com/trustwallet/assets/internal/processor"
|
|
"github.com/trustwallet/assets/internal/report"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
type Service struct {
|
|
fileService *file.Service
|
|
processorService *processor.Service
|
|
reportService *report.Service
|
|
}
|
|
|
|
func NewService(fs *file.Service, cs *processor.Service, rs *report.Service) *Service {
|
|
return &Service{
|
|
fileService: fs,
|
|
processorService: cs,
|
|
reportService: rs,
|
|
}
|
|
}
|
|
|
|
func (s *Service) RunJob(paths []string, job func(*file.AssetFile)) {
|
|
for _, path := range paths {
|
|
f := s.fileService.GetAssetFile(path)
|
|
job(f)
|
|
s.reportService.IncTotalFiles()
|
|
}
|
|
}
|
|
|
|
func (s *Service) Check(f *file.AssetFile) {
|
|
validators := s.processorService.GetValidator(f)
|
|
|
|
for _, validator := range validators {
|
|
if err := validator.Run(f); err != nil {
|
|
s.handleError(err, f, validator.Name)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (s *Service) Fix(f *file.AssetFile) {
|
|
fixers := s.processorService.GetFixers(f)
|
|
|
|
for _, fixer := range fixers {
|
|
if err := fixer.Run(f); err != nil {
|
|
s.handleError(err, f, fixer.Name)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (s *Service) RunUpdateAuto() {
|
|
updaters := s.processorService.GetUpdatersAuto()
|
|
s.runUpdaters(updaters)
|
|
}
|
|
|
|
func (s *Service) RunUpdateManual() {
|
|
updaters := s.processorService.GetUpdatersManual()
|
|
s.runUpdaters(updaters)
|
|
}
|
|
|
|
func (s *Service) runUpdaters(updaters []processor.Updater) {
|
|
for _, updater := range updaters {
|
|
err := updater.Run()
|
|
if err != nil {
|
|
log.WithError(err).Error()
|
|
}
|
|
}
|
|
}
|
|
|
|
func (s *Service) handleError(err error, info *file.AssetFile, valName string) {
|
|
errors := UnwrapComposite(err)
|
|
|
|
for _, err := range errors {
|
|
log.WithFields(log.Fields{
|
|
"type": info.Type(),
|
|
"chain": info.Chain().Handle,
|
|
"asset": info.Asset(),
|
|
"path": info.Path(),
|
|
"validation": valName,
|
|
}).Error(err)
|
|
|
|
s.reportService.IncErrors()
|
|
}
|
|
}
|
|
|
|
func UnwrapComposite(err error) []error {
|
|
compErr, ok := err.(*validation.ErrComposite)
|
|
if !ok {
|
|
return []error{err}
|
|
}
|
|
|
|
var errors []error
|
|
for _, e := range compErr.GetErrors() {
|
|
errors = append(errors, UnwrapComposite(e)...)
|
|
}
|
|
|
|
return errors
|
|
}
|