Skip to content

Commit

Permalink
fixed some linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
tomvodi committed Aug 29, 2024
1 parent 07acf24 commit 1f34256
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 90 deletions.
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ require (
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dmarkham/enumer v1.5.10 // indirect
github.com/fatih/color v1.17.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.5 // indirect
Expand Down Expand Up @@ -66,7 +65,6 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/oklog/run v1.1.0 // indirect
github.com/pascaldekloe/name v1.0.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/sagikazarmark/locafero v0.6.0 // indirect
Expand All @@ -82,7 +80,6 @@ require (
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/mod v0.19.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.22.0 // indirect
Expand Down
8 changes: 0 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/denisenkom/go-mssqldb v0.0.0-20190515213511-eb9f6a1743f3/go.mod h1:zAg7JM8CkOJ43xKXIj7eRO9kmWm/TW578qo+oDO6tuM=
github.com/dmarkham/enumer v1.5.10 h1:ygL0L6quiTiH1jpp68DyvsWaea6MaZLZrTTkIS++R0M=
github.com/dmarkham/enumer v1.5.10/go.mod h1:e4VILe2b1nYK3JKJpRmNdl5xbDQvELc6tQ8b+GsGk6E=
github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs=
github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU=
github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I=
Expand Down Expand Up @@ -179,8 +177,6 @@ github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1Cpa
github.com/onsi/gomega v1.34.1 h1:EUMJIKUjM8sKjYbtxQI9A4z2o+rruxnzNvpknOXie6k=
github.com/onsi/gomega v1.34.1/go.mod h1:kU1QgUvBDLXBJq618Xvm2LUX6rSAfRaFRTcdOeDLwwY=
github.com/openzipkin/zipkin-go v0.1.6/go.mod h1:QgAqvLzwWbR/WpD4A3cGpPtJrZXNIiJc5AZX7/PBEpw=
github.com/pascaldekloe/name v1.0.0 h1:n7LKFgHixETzxpRv2R77YgPUFo85QHGZKrdaYm7eY5U=
github.com/pascaldekloe/name v1.0.0/go.mod h1:Z//MfYJnH4jVpQ9wkclwu2I2MkHmXTlT9wR5UZScttM=
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
Expand Down Expand Up @@ -238,8 +234,6 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/tomvodi/limepipes-plugin-api v0.0.0-20240823063106-89f4ecb27e7b h1:vwk1e9k4j4eBZ9s6nEvP1Qyfq7Gva/PZCocQ8ndu8HM=
github.com/tomvodi/limepipes-plugin-api v0.0.0-20240823063106-89f4ecb27e7b/go.mod h1:hJsT7DGbRLZGDavuFG/rNyYC9ofQQfkmP40xJ+BX7f0=
github.com/tomvodi/limepipes-plugin-api v0.0.0-20240823091250-4b97c450b940 h1:sIEr4FP1RQRa/0HTM0whstf8PkoXDiZUQeGsKE1LzpE=
github.com/tomvodi/limepipes-plugin-api v0.0.0-20240823091250-4b97c450b940/go.mod h1:hJsT7DGbRLZGDavuFG/rNyYC9ofQQfkmP40xJ+BX7f0=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
Expand All @@ -262,8 +256,6 @@ golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbR
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/mod v0.19.0 h1:fEdghXQSo20giMthA7cd28ZC+jts4amQ3YMXiP5oMQ8=
golang.org/x/mod v0.19.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
2 changes: 1 addition & 1 deletion internal/api/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ func multipartRequestForFile(
return r
}

func mockJSONPost(c *gin.Context, method string, content interface{}) {
func mockJSONPost(c *gin.Context, method string, content any) {
c.Request = &http.Request{
Method: method,
Header: make(http.Header),
Expand Down
4 changes: 2 additions & 2 deletions internal/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ func FilenameFromPath(file string) string {
return strings.TrimSuffix(onlyFile, filepath.Ext(onlyFile))
}

func HashFromFile(filepath string) (string, error) {
f, err := os.Open(filepath)
func HashFromFile(fp string) (string, error) {
f, err := os.Open(fp)
if err != nil {
return "", err
}
Expand Down
22 changes: 15 additions & 7 deletions internal/config/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,26 @@ func initViperConfigPaths(withParents bool) error {
return err
}

if filepath.Base(wd) != "limepipes" {
for i := 0; i < 3; i++ {
wd = filepath.Dir(wd)
addParentDirsUntilSourceRoot(wd, 3)

if filepath.Base(wd) == "limepipes" {
viper.AddConfigPath(wd)
return nil
}

// addParentDirsUntilSourceRoot goes maxDirUp directories up from the given dir
// until it finds the source root directory called "limepipes" which then
// it adds to the viper config paths.
// If it doesn't find the source root directory, it will not add any paths.
func addParentDirsUntilSourceRoot(dir string, maxDirUp int) {
if filepath.Base(dir) != "limepipes" {
for i := 0; i < maxDirUp; i++ {
dir = filepath.Dir(dir)

if filepath.Base(dir) == "limepipes" {
viper.AddConfigPath(dir)
break
}
}
}

return nil
}

func initViperConfig() {
Expand Down
180 changes: 113 additions & 67 deletions internal/database/db_data_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,47 +40,60 @@ func (d *dbService) Tunes() ([]*apimodel.Tune, error) {
}

func (d *dbService) CreateTune(
tune apimodel.CreateTune,
ct apimodel.CreateTune,
importFile *model.ImportFile,
) (*apimodel.Tune, error) {
if strings.TrimSpace(tune.Title) == "" {
return nil, fmt.Errorf("can't create tune without a title")
if strings.TrimSpace(ct.Title) == "" {
return nil, fmt.Errorf("can'ct create tune without a title")
}

dbTune := model.Tune{}
dbTune, err := d.initDbTuneForCreation(ct, importFile)
if err != nil {
return nil, err
}

if err = d.db.Create(&dbTune).Error; err != nil {
return &apimodel.Tune{}, err
}

apiTune := &apimodel.Tune{}
if err := copier.Copy(apiTune, &dbTune); err != nil {
return nil, err
}
if dbTune.TuneType != nil {
apiTune.Type = dbTune.TuneType.Name
}

return apiTune, nil
}

func (d *dbService) initDbTuneForCreation(
ct apimodel.CreateTune,
importFile *model.ImportFile,
) (*model.Tune, error) {
dbTune := &model.Tune{}
if importFile != nil {
dbTune.ImportFileID = importFile.ID
}
var tuneType *model.TuneType
var err error
if tune.Type != "" {
tuneType, err = d.getOrCreateTuneType(tune.Type)
if ct.Type != "" {
tuneType, err = d.getOrCreateTuneType(ct.Type)
if err != nil {
return &apimodel.Tune{}, err
return nil, err
}
}

err = copier.Copy(&dbTune, &tune)
err = copier.Copy(&dbTune, &ct)
if err != nil {
return &apimodel.Tune{}, fmt.Errorf("could not create db object")
return nil, fmt.Errorf("could not create db tune object")
}
if tuneType != nil {
dbTune.TuneTypeID = &tuneType.ID
dbTune.TuneType = tuneType
}

if err = d.db.Create(&dbTune).Error; err != nil {
return &apimodel.Tune{}, err
}

apiTune := &apimodel.Tune{}
if err := copier.Copy(apiTune, &dbTune); err != nil {
return nil, err
}
if tuneType != nil {
apiTune.Type = tuneType.Name
}

return apiTune, nil
return dbTune, nil
}

func (d *dbService) GetTune(id uuid.UUID) (*apimodel.Tune, error) {
Expand Down Expand Up @@ -158,7 +171,7 @@ func (d *dbService) UpdateTune(id uuid.UUID, updateTune apimodel.UpdateTune) (*a
return nil, common.ErrNotFound
}

var updateVals = map[string]interface{}{}
var updateVals = map[string]any{}
if err := mapstructure.Decode(&updateTune, &updateVals); err != nil {
return nil, err
}
Expand Down Expand Up @@ -392,7 +405,7 @@ func (d *dbService) UpdateMusicSet(id uuid.UUID, updateSet apimodel.UpdateSet) (
return nil, common.ErrNotFound
}

var updateVals = map[string]interface{}{}
var updateVals = map[string]any{}
if err := mapstructure.Decode(&updateSet, &updateVals); err != nil {
return nil, err
}
Expand Down Expand Up @@ -602,26 +615,15 @@ func (d *dbService) ImportTunes(
}

if impTune.TuneFileData != nil {
hasData, err := d.hasSingleFileData(impTune.TuneFileData)
if err != nil {
existingTune, err := d.getExistingImportedTune(
impTune,
)
if err != nil && !errors.Is(err, common.ErrNotFound) {
return err
}

if hasData {
alreadyImportedTune, err := d.getTuneWithSingleFileData(impTune.TuneFileData)
if err != nil {
return err
}

if alreadyImportedTune != nil {
impTune := &apimodel.ImportTune{}
err = copier.Copy(impTune, alreadyImportedTune)
if err != nil {
return fmt.Errorf("failed creating import tune from already imported tune: %s", err.Error())
}
apiTunes = append(apiTunes, impTune)
continue
}
if existingTune != nil {
apiTunes = append(apiTunes, existingTune)
continue
}
}

Expand Down Expand Up @@ -666,30 +668,9 @@ func (d *dbService) ImportTunes(
}

if len(tunes) > 1 {
var apiSet *apimodel.MusicSet
var err error
var tuneIDs []uuid.UUID
for _, apiTune := range apiTunes {
tuneIDs = append(tuneIDs, apiTune.Id)
}

musicSetTitle := musicSetTitleFromTunes(apiTunes)
apiSet, err = d.getMusicSetByTuneIDs(tuneIDs)
if errors.Is(err, common.ErrNotFound) { // music set not yet in db
createSet := apimodel.CreateSet{
Title: musicSetTitle,
Tunes: tuneIDs,
}
apiSet, err = d.CreateMusicSet(createSet, importFile)
if err != nil {
return fmt.Errorf("failed creating set for file %s: %s", fileInfo.Name, err.Error())
}
}

musicSet = &apimodel.BasicMusicSet{}
err = copier.Copy(musicSet, apiSet)
musicSet, err = d.createMusicSetForTunes(apiTunes, importFile)
if err != nil {
return fmt.Errorf("failed creating basic music set from music set")
return err
}
}

Expand All @@ -702,6 +683,71 @@ func (d *dbService) ImportTunes(
return apiTunes, musicSet, nil
}

// getExistingImportedTune returns the imported tune from the database if it already exists.
func (d *dbService) getExistingImportedTune(
impTune *messages.ImportedTune,
) (*apimodel.ImportTune, error) {
hasData, err := d.hasSingleFileData(impTune.TuneFileData)
if err != nil {
return nil, err
}

if hasData {
alreadyImportedTune, err := d.getTuneWithSingleFileData(impTune.TuneFileData)
if err != nil {
return nil, err
}

if alreadyImportedTune != nil {
impTune := &apimodel.ImportTune{}
err = copier.Copy(impTune, alreadyImportedTune)
if err != nil {
return nil, fmt.Errorf("failed creating import tune from already imported tune: %s", err.Error())
}

return impTune, nil
}
}

return nil, common.ErrNotFound
}

func (d *dbService) createMusicSetForTunes(
tunes []*apimodel.ImportTune,
importFile *model.ImportFile,
) (*apimodel.BasicMusicSet, error) {
var apiSet *apimodel.MusicSet
var err error
var tuneIDs []uuid.UUID
for _, t := range tunes {
tuneIDs = append(tuneIDs, t.Id)
}

musicSetTitle := musicSetTitleFromTunes(tunes)
apiSet, err = d.getMusicSetByTuneIDs(tuneIDs)
if errors.Is(err, common.ErrNotFound) { // music set not yet in db
createSet := apimodel.CreateSet{
Title: musicSetTitle,
Tunes: tuneIDs,
}
apiSet, err = d.CreateMusicSet(createSet, importFile)
if err != nil {
return nil, fmt.Errorf("failed creating set for file %s: %s",
importFile.Name,
err.Error(),
)
}
}

musicSet := &apimodel.BasicMusicSet{}
err = copier.Copy(musicSet, apiSet)
if err != nil {
return nil, fmt.Errorf("failed creating basic music set from music set")
}

return musicSet, nil
}

func musicSetTitleFromTunes(tunes []*apimodel.ImportTune) string {
if len(tunes) <= 1 {
log.Error().Msg("not enough tunes given to create a music set title")
Expand Down Expand Up @@ -833,8 +879,8 @@ func (d *dbService) getSingleTuneFileByData(data []byte) (*model.TuneFile, error
return tf, nil
}

func setMessagesToAPITune(apiTune *apimodel.ImportTune, tune *tune.Tune) {
im := tune.ImportMessages()
func setMessagesToAPITune(apiTune *apimodel.ImportTune, modelTune *tune.Tune) {
im := modelTune.ImportMessages()
for _, message := range im {
switch message.Severity {
case measure.Severity_Error:
Expand Down
4 changes: 2 additions & 2 deletions internal/database/model/tune_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ func (t *TuneFile) MusicModelTune() (*tune.Tune, error) {
return tn, nil
}

func TuneFileFromMusicModelTune(tune *tune.Tune) (*TuneFile, error) {
func TuneFileFromMusicModelTune(t *tune.Tune) (*TuneFile, error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)

if err := enc.Encode(tune); err != nil {
if err := enc.Encode(t); err != nil {
return nil, err
}

Expand Down

0 comments on commit 1f34256

Please sign in to comment.