From 61f5f79d5e394b76f259d4bae83080f30eef331a Mon Sep 17 00:00:00 2001 From: "Marcel M. Otte" Date: Sat, 13 May 2023 21:42:50 +0200 Subject: [PATCH] proper structure... --- .../protocoldictionary.go | 70 +++++++++---------- .../protocoldictionary_test.go | 2 +- 2 files changed, 36 insertions(+), 36 deletions(-) rename protocoldb/protocoldb.go => dop/protocoldictionary.go (61%) rename protocoldb/protocoldb_test.go => dop/protocoldictionary_test.go (73%) diff --git a/protocoldb/protocoldb.go b/dop/protocoldictionary.go similarity index 61% rename from protocoldb/protocoldb.go rename to dop/protocoldictionary.go index 6b1cc5c..9af71b7 100644 --- a/protocoldb/protocoldb.go +++ b/dop/protocoldictionary.go @@ -1,4 +1,4 @@ -package protocoldb +package dop import ( "fmt" @@ -15,19 +15,19 @@ import ( "gitea.mmo.to/ppForge/ppforge/protocolctl" ) -// PPFDatabases is a list of all databases, with the most simple interface -type PPFDatabases struct { - Databases []ProtocolDatabaseProvider +// ProtocolDictionaryList is a list of all databases, with the most simple interface +type ProtocolDictionaryList struct { + PDs []ProtocolDictionaryProvider } -// ProtocolDatabaseProvider is a simple protocol database interface, which only provides open/close and retrieving the contents -type ProtocolDatabaseProvider interface { +// ProtocolDictionaryProvider is a simple protocol database interface, which only provides open/close and retrieving the contents +type ProtocolDictionaryProvider interface { // Open opens a new database Open(path string) error // Close closes a database (only internal flag set) Close() error // Protocols returns the content of the database as map, an error if the first read of the database failed - Protocols() ([]DatabaseEntry, error) + Protocols() ([]ProtocolDictionaryEntry, error) } // ProtocolDatabaseManager is a complex protocol database interface, which allows adding, updating and refreshing of the content @@ -38,16 +38,16 @@ type ProtocolDatabaseManager interface { UpdateDatabase() error // scans for new protocols } -// DatabaseEntry is a single entry in the file database, additionally to the meta data the path is required -type DatabaseEntry struct { +// ProtocolDictionaryEntry is a single entry in the file database, additionally to the meta data the path is required +type ProtocolDictionaryEntry struct { Path string Protocol protocol.DOPMeta } // Init initializes the databases, opens the default one and checks for others if available -func Init() *PPFDatabases { +func Init() *ProtocolDictionaryList { // initialize main list - databases := PPFDatabases{[]ProtocolDatabaseProvider{}} + databases := ProtocolDictionaryList{[]ProtocolDictionaryProvider{}} // initialize default db user, err := user.Current() if err != nil { @@ -55,41 +55,41 @@ func Init() *PPFDatabases { log.Fatal("Impossible to open default database") } pathlist := []string{user.HomeDir, ".config", "ppforge"} - fdb := FileDatabase{path.Join(pathlist...), map[string]DatabaseEntry{}, false} + fdb := FileDOP{path.Join(pathlist...), map[string]ProtocolDictionaryEntry{}, false} fdb.Open(path.Join(pathlist...)) fdb.UpdateDatabase() - databases.Databases = append(databases.Databases, fdb) - log.Printf("Amount of databases available %d", len(databases.Databases)) + databases.PDs = append(databases.PDs, fdb) + log.Printf("Amount of databases available %d", len(databases.PDs)) return &databases } // Get Retrieves the full set of databases -func (ppfdb *PPFDatabases) Get() []ProtocolDatabaseProvider { - return ppfdb.Databases +func (ppfdb *ProtocolDictionaryList) Get() []ProtocolDictionaryProvider { + return ppfdb.PDs } // Get a full protocol from a database entry -func (de *DatabaseEntry) Get() (*protocol.ProtocolStructure, error) { +func (de *ProtocolDictionaryEntry) Get() (*protocol.ProtocolStructure, error) { return protocolctl.LoadNew(de.Path) } -// FileDatabase implements ProtocolDatabaseProvider and ProtocolDatabaseManager -type FileDatabase struct { +// FileDOP implements ProtocolDatabaseProvider and ProtocolDatabaseManager +type FileDOP struct { Path string - protocols map[string]DatabaseEntry + protocols map[string]ProtocolDictionaryEntry closed bool } -// FileDatabaseFromGitRemote implements retrieving the latest stuff from an external git remote additionally to the FileDatabase functionality +// FileDOPFromGitRemote implements retrieving the latest stuff from an external git remote additionally to the FileDatabase functionality // TODO -type FileDatabaseFromGitRemote struct { - FileDatabase +type FileDOPFromGitRemote struct { + FileDOP RemoteGitRepository string gitRepo git.Repository } // Open the database -func (fd FileDatabase) Open(path string) error { +func (fd FileDOP) Open(path string) error { fileinfo, err := os.Stat(path) if err == os.ErrNotExist { return err @@ -103,7 +103,7 @@ func (fd FileDatabase) Open(path string) error { } // Close the database and free the data -func (fd FileDatabase) Close() error { +func (fd FileDOP) Close() error { for k := range fd.protocols { delete(fd.protocols, k) } @@ -112,17 +112,17 @@ func (fd FileDatabase) Close() error { } // Protocols returns the map of protocols or an error if the first read failed -func (fd FileDatabase) Protocols() ([]DatabaseEntry, error) { - entries := []DatabaseEntry{} +func (fd FileDOP) Protocols() ([]ProtocolDictionaryEntry, error) { + entries := []ProtocolDictionaryEntry{} if !fd.closed && len(fd.protocols) == 0 { - fd.protocols = map[string]DatabaseEntry{} + fd.protocols = map[string]ProtocolDictionaryEntry{} // recurse recursively through the path err := filepath.Walk(fd.Path, func(path string, info fs.FileInfo, err error) error { if !info.IsDir() { prot, err := protocolctl.LoadNew(path) if err == nil { // add to map - entry := DatabaseEntry{path, prot.Metadata} + entry := ProtocolDictionaryEntry{path, prot.Metadata} fd.protocols[prot.Metadata.Name] = entry entries = append(entries, entry) @@ -135,7 +135,7 @@ func (fd FileDatabase) Protocols() ([]DatabaseEntry, error) { } } if entries == nil || len(entries) == 0 { - entries = make([]DatabaseEntry, len(fd.protocols)) + entries = make([]ProtocolDictionaryEntry, len(fd.protocols)) for _, v := range fd.protocols { entries = append(entries, v) } @@ -144,17 +144,17 @@ func (fd FileDatabase) Protocols() ([]DatabaseEntry, error) { } // Add puts a new protocol into the path of the file database -func (fd *FileDatabase) Add(prot *protocol.ProtocolStructure) error { +func (fd *FileDOP) Add(prot *protocol.ProtocolStructure) error { p := path.Join(fd.Path, prot.Metadata.Name, ".protocoljson") err := protocolctl.Save(prot, p) if err == nil { - fd.protocols[prot.Metadata.Name] = DatabaseEntry{p, prot.Metadata} + fd.protocols[prot.Metadata.Name] = ProtocolDictionaryEntry{p, prot.Metadata} } return err } // Update updates an existing entry through saving the contents of the protocol to the file, or just adding it anew -func (fd *FileDatabase) Update(prot *protocol.ProtocolStructure) error { +func (fd *FileDOP) Update(prot *protocol.ProtocolStructure) error { if v, ok := fd.protocols[prot.Metadata.Name]; ok { v.Protocol = prot.Metadata return protocolctl.Save(prot, v.Path) @@ -163,14 +163,14 @@ func (fd *FileDatabase) Update(prot *protocol.ProtocolStructure) error { } // UpdateDatabase just rereads all files -func (fd *FileDatabase) UpdateDatabase() error { +func (fd *FileDOP) UpdateDatabase() error { // recurse recursively through the path err := filepath.Walk(fd.Path, func(path string, info fs.FileInfo, err error) error { if !info.IsDir() { prot, err := protocolctl.LoadNew(path) if err == nil { // add to map - fd.protocols[prot.Metadata.Name] = DatabaseEntry{path, prot.Metadata} + fd.protocols[prot.Metadata.Name] = ProtocolDictionaryEntry{path, prot.Metadata} } } return nil diff --git a/protocoldb/protocoldb_test.go b/dop/protocoldictionary_test.go similarity index 73% rename from protocoldb/protocoldb_test.go rename to dop/protocoldictionary_test.go index 16e945f..447e2bb 100644 --- a/protocoldb/protocoldb_test.go +++ b/dop/protocoldictionary_test.go @@ -1,4 +1,4 @@ -package protocoldb +package dop import ( "testing"