From abada5d00a3480b3c6714e287fd724696e355317 Mon Sep 17 00:00:00 2001 From: beebeeoii Date: Sat, 22 Jan 2022 15:55:32 +0800 Subject: [PATCH] moved validator function to response file --- FyneApp.toml | 2 +- pkg/api/files.go | 27 ++++----------------------- pkg/api/response.go | 16 ++++++++++++++++ 3 files changed, 21 insertions(+), 24 deletions(-) diff --git a/FyneApp.toml b/FyneApp.toml index 85d04d5..3f6be58 100644 --- a/FyneApp.toml +++ b/FyneApp.toml @@ -5,4 +5,4 @@ Website = "https://github.com/beebeeoii/lominus" Name = "Lominus" ID = "com.beebeeoii.lominus" Version = "1.2.0" - Build = 120 + Build = 121 diff --git a/pkg/api/files.go b/pkg/api/files.go index b661c95..9a44346 100644 --- a/pkg/api/files.go +++ b/pkg/api/files.go @@ -10,7 +10,6 @@ import ( "time" "github.com/beebeeoii/lominus/internal/file" - logs "github.com/beebeeoii/lominus/internal/log" ) // Folder struct is the datapack for containing details about a Folder @@ -39,13 +38,13 @@ const FILE_URL_ENDPOINT = "https://luminus.nus.edu.sg/v2/api/files/%s/file?popul const DOWNLOAD_URL_ENDPOINT = "https://luminus.nus.edu.sg/v2/api/files/file/%s/downloadurl" // getFolderFieldsRequired is a helper function that returns a constant array with fields that a Folder response -// returned by Luminus needs +// returned by Luminus needs. func getFolderFieldsRequired() []string { return []string{"access", "id", "name", "isActive", "allowUpload", "subFolderCount"} } // getFileFieldsRequired is a helper function that returns a constant array with fields that a File response -// returned by Luminus needs +// returned by Luminus needs. func getFileFieldsRequired() []string { return []string{"id", "name", "lastUpdatedDate"} } @@ -68,7 +67,7 @@ func (req DocumentRequest) GetAllFolders() ([]Folder, error) { } for _, content := range rawResponse.Data { - if !isResponseValid(getFolderFieldsRequired(), content) { + if !IsResponseValid(getFolderFieldsRequired(), content) { continue } @@ -104,7 +103,7 @@ func (req DocumentRequest) GetAllFiles() ([]File, error) { } for _, content := range rawResponse.Data { - if !isResponseValid(getFileFieldsRequired(), content) { + if !IsResponseValid(getFileFieldsRequired(), content) { continue } @@ -215,21 +214,3 @@ func (req DocumentRequest) Download(filePath string) error { return nil } - -// isResponseValid is a helper function that checks if a Folder/File response is valid. -// It checks if the response contains the required fields required. -func isResponseValid(fieldsRequired []string, response map[string]interface{}) bool { - isValid := true - for _, field := range fieldsRequired { - _, exists := response[field] - - if !exists { - isValid = false - break - } - } - - logs.Logger.Debugln(isValid, fieldsRequired) - - return isValid -} diff --git a/pkg/api/response.go b/pkg/api/response.go index 7f592b4..27192d4 100644 --- a/pkg/api/response.go +++ b/pkg/api/response.go @@ -50,3 +50,19 @@ func (req Request) GetRawResponse(res interface{}) error { return err } + +// isResponseValid is a helper function that checks if a Folder/File response is valid. +// It checks if the response contains the required fields required. +func IsResponseValid(fieldsRequired []string, response map[string]interface{}) bool { + isValid := true + for _, field := range fieldsRequired { + _, exists := response[field] + + if !exists { + isValid = false + break + } + } + + return isValid +}