From 082697ccbbc007ed629d15ec94fe5691ca70b87f Mon Sep 17 00:00:00 2001 From: git-sudo-404 Date: Thu, 1 Oct 2026 18:21:38 +0530 Subject: [PATCH] refactored --- internal/torrent/helper.go | 53 ------------------------------ internal/torrent/metainfoParser.go | 26 +++++++++++++++ 2 files changed, 26 insertions(+), 53 deletions(-) delete mode 100644 internal/torrent/helper.go diff --git a/internal/torrent/helper.go b/internal/torrent/helper.go deleted file mode 100644 index 57d0698..0000000 --- a/internal/torrent/helper.go +++ /dev/null @@ -1,53 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2026 git-sudo-404 - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * Of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * Copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * Copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package torrent - -import "fmt" - -func checkFieldPresenceAndType[T any](dict map[string]any, field string) (T, error) { - var zero T - value, ok := dict[field] - if !ok { - return zero, fmt.Errorf("Meta-Info missing needed field : %s", field) - } - if v, ok := value.(T); !ok { - return zero, fmt.Errorf("Undesired field value type , Field : %s ", field) - } else { - return v, nil - } -} - -func checkOptionalFieldPresenceAndType[T any](dict map[string]any, field string) (T, bool, error) { - var zero T - value, ok := dict[field] - if !ok { - return zero, false, nil - } - if v, ok := value.(T); !ok { - return zero, false, fmt.Errorf("Undesired field value type , Field : %s ", field) - } else { - return v, true, nil - } -} diff --git a/internal/torrent/metainfoParser.go b/internal/torrent/metainfoParser.go index bc845a4..31a65d3 100644 --- a/internal/torrent/metainfoParser.go +++ b/internal/torrent/metainfoParser.go @@ -31,6 +31,32 @@ import ( "os" ) +func checkFieldPresenceAndType[T any](dict map[string]any, field string) (T, error) { + var zero T + value, ok := dict[field] + if !ok { + return zero, fmt.Errorf("Meta-Info missing needed field : %s", field) + } + if v, ok := value.(T); !ok { + return zero, fmt.Errorf("Undesired field value type , Field : %s ", field) + } else { + return v, nil + } +} + +func checkOptionalFieldPresenceAndType[T any](dict map[string]any, field string) (T, bool, error) { + var zero T + value, ok := dict[field] + if !ok { + return zero, false, nil + } + if v, ok := value.(T); !ok { + return zero, false, fmt.Errorf("Undesired field value type , Field : %s ", field) + } else { + return v, true, nil + } +} + // since announce-list is optional this function returns an err if the announce list key is not found in the metaInfoDict func checkAnnounceListPresenceAndType(metaInfoDict map[string]any) ([][]string, bool, error) {