From 8e9d5e90ba2e53d1f375ecf610854e2ba6d922fd Mon Sep 17 00:00:00 2001 From: THCFree Date: Fri, 11 Oct 2024 17:48:31 +0200 Subject: [PATCH] Add string methods --- models/collection/CollectionHandler.go | 10 ++++++++++ models/history/HistoryHandler.go | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/models/collection/CollectionHandler.go b/models/collection/CollectionHandler.go index b854394..dfeb923 100644 --- a/models/collection/CollectionHandler.go +++ b/models/collection/CollectionHandler.go @@ -22,3 +22,13 @@ func ReadFileAndConvertToCollection(filePath string) (*NML, error) { return &collection, nil } +func CollectionFromString(content string) (*NML, error) { + // Unmarshal the XML content into the Collection struct + var collection NML + err := xml.Unmarshal([]byte(content), &collection) + if err != nil { + return nil, err + } + + return &collection, nil +} diff --git a/models/history/HistoryHandler.go b/models/history/HistoryHandler.go index 8b04bcd..8824ecd 100644 --- a/models/history/HistoryHandler.go +++ b/models/history/HistoryHandler.go @@ -30,6 +30,22 @@ func ReadFileAndConvertToHistory(filePath string) (*NML, error) { return nil, errors.New("file is not a history file") } +func HistoryFromString(content string) (*NML, error) { + // Unmarshal the XML content into the NML struct + var nml NML + err := xml.Unmarshal([]byte(content), &nml) + if err != nil { + return nil, err + } + + // Check if the file is a history file based on the content + if IsHistoryString(content) { + return &nml, nil + } + + return nil, errors.New("file is not a history file") +} + func IsHistoryFile(path string) bool { content, err := os.ReadFile(path) if err != nil { @@ -37,3 +53,7 @@ func IsHistoryFile(path string) bool { } return strings.Contains(string(content), "HistoryData") } + +func IsHistoryString(content string) bool { + return strings.Contains(content, "HistoryData") +}