trustwallet-assets/internal/service/service.go

102 lines
2.2 KiB
Go
Raw Permalink Normal View History

package service
import (
2021-12-20 15:03:08 +00:00
"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) {
validator := s.processorService.GetValidator(f)
if validator != nil {
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 {
2021-12-20 15:03:08 +00:00
log.WithFields(log.Fields{
"type": info.Type(),
"chain": info.Chain().Handle,
"asset": info.Asset(),
"path": info.Path(),
"validation": valName,
2021-12-20 15:03:08 +00:00
}).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
}