proper structure...
Go / lint (push) Successful in 9s Details
Go / build (push) Successful in 2m48s Details

This commit is contained in:
Marcel M. Otte 2023-05-13 21:42:50 +02:00
parent c9fcb3d947
commit 61f5f79d5e
2 changed files with 36 additions and 36 deletions

View File

@ -1,4 +1,4 @@
package protocoldb package dop
import ( import (
"fmt" "fmt"
@ -15,19 +15,19 @@ import (
"gitea.mmo.to/ppForge/ppforge/protocolctl" "gitea.mmo.to/ppForge/ppforge/protocolctl"
) )
// PPFDatabases is a list of all databases, with the most simple interface // ProtocolDictionaryList is a list of all databases, with the most simple interface
type PPFDatabases struct { type ProtocolDictionaryList struct {
Databases []ProtocolDatabaseProvider PDs []ProtocolDictionaryProvider
} }
// ProtocolDatabaseProvider is a simple protocol database interface, which only provides open/close and retrieving the contents // ProtocolDictionaryProvider is a simple protocol database interface, which only provides open/close and retrieving the contents
type ProtocolDatabaseProvider interface { type ProtocolDictionaryProvider interface {
// Open opens a new database // Open opens a new database
Open(path string) error Open(path string) error
// Close closes a database (only internal flag set) // Close closes a database (only internal flag set)
Close() error Close() error
// Protocols returns the content of the database as map, an error if the first read of the database failed // 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 // 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 UpdateDatabase() error // scans for new protocols
} }
// DatabaseEntry is a single entry in the file database, additionally to the meta data the path is required // ProtocolDictionaryEntry is a single entry in the file database, additionally to the meta data the path is required
type DatabaseEntry struct { type ProtocolDictionaryEntry struct {
Path string Path string
Protocol protocol.DOPMeta Protocol protocol.DOPMeta
} }
// Init initializes the databases, opens the default one and checks for others if available // Init initializes the databases, opens the default one and checks for others if available
func Init() *PPFDatabases { func Init() *ProtocolDictionaryList {
// initialize main list // initialize main list
databases := PPFDatabases{[]ProtocolDatabaseProvider{}} databases := ProtocolDictionaryList{[]ProtocolDictionaryProvider{}}
// initialize default db // initialize default db
user, err := user.Current() user, err := user.Current()
if err != nil { if err != nil {
@ -55,41 +55,41 @@ func Init() *PPFDatabases {
log.Fatal("Impossible to open default database") log.Fatal("Impossible to open default database")
} }
pathlist := []string{user.HomeDir, ".config", "ppforge"} 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.Open(path.Join(pathlist...))
fdb.UpdateDatabase() fdb.UpdateDatabase()
databases.Databases = append(databases.Databases, fdb) databases.PDs = append(databases.PDs, fdb)
log.Printf("Amount of databases available %d", len(databases.Databases)) log.Printf("Amount of databases available %d", len(databases.PDs))
return &databases return &databases
} }
// Get Retrieves the full set of databases // Get Retrieves the full set of databases
func (ppfdb *PPFDatabases) Get() []ProtocolDatabaseProvider { func (ppfdb *ProtocolDictionaryList) Get() []ProtocolDictionaryProvider {
return ppfdb.Databases return ppfdb.PDs
} }
// Get a full protocol from a database entry // 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) return protocolctl.LoadNew(de.Path)
} }
// FileDatabase implements ProtocolDatabaseProvider and ProtocolDatabaseManager // FileDOP implements ProtocolDatabaseProvider and ProtocolDatabaseManager
type FileDatabase struct { type FileDOP struct {
Path string Path string
protocols map[string]DatabaseEntry protocols map[string]ProtocolDictionaryEntry
closed bool 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 // TODO
type FileDatabaseFromGitRemote struct { type FileDOPFromGitRemote struct {
FileDatabase FileDOP
RemoteGitRepository string RemoteGitRepository string
gitRepo git.Repository gitRepo git.Repository
} }
// Open the database // Open the database
func (fd FileDatabase) Open(path string) error { func (fd FileDOP) Open(path string) error {
fileinfo, err := os.Stat(path) fileinfo, err := os.Stat(path)
if err == os.ErrNotExist { if err == os.ErrNotExist {
return err return err
@ -103,7 +103,7 @@ func (fd FileDatabase) Open(path string) error {
} }
// Close the database and free the data // Close the database and free the data
func (fd FileDatabase) Close() error { func (fd FileDOP) Close() error {
for k := range fd.protocols { for k := range fd.protocols {
delete(fd.protocols, k) 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 // Protocols returns the map of protocols or an error if the first read failed
func (fd FileDatabase) Protocols() ([]DatabaseEntry, error) { func (fd FileDOP) Protocols() ([]ProtocolDictionaryEntry, error) {
entries := []DatabaseEntry{} entries := []ProtocolDictionaryEntry{}
if !fd.closed && len(fd.protocols) == 0 { if !fd.closed && len(fd.protocols) == 0 {
fd.protocols = map[string]DatabaseEntry{} fd.protocols = map[string]ProtocolDictionaryEntry{}
// recurse recursively through the path // recurse recursively through the path
err := filepath.Walk(fd.Path, func(path string, info fs.FileInfo, err error) error { err := filepath.Walk(fd.Path, func(path string, info fs.FileInfo, err error) error {
if !info.IsDir() { if !info.IsDir() {
prot, err := protocolctl.LoadNew(path) prot, err := protocolctl.LoadNew(path)
if err == nil { if err == nil {
// add to map // add to map
entry := DatabaseEntry{path, prot.Metadata} entry := ProtocolDictionaryEntry{path, prot.Metadata}
fd.protocols[prot.Metadata.Name] = entry fd.protocols[prot.Metadata.Name] = entry
entries = append(entries, entry) entries = append(entries, entry)
@ -135,7 +135,7 @@ func (fd FileDatabase) Protocols() ([]DatabaseEntry, error) {
} }
} }
if entries == nil || len(entries) == 0 { if entries == nil || len(entries) == 0 {
entries = make([]DatabaseEntry, len(fd.protocols)) entries = make([]ProtocolDictionaryEntry, len(fd.protocols))
for _, v := range fd.protocols { for _, v := range fd.protocols {
entries = append(entries, v) 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 // 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") p := path.Join(fd.Path, prot.Metadata.Name, ".protocoljson")
err := protocolctl.Save(prot, p) err := protocolctl.Save(prot, p)
if err == nil { if err == nil {
fd.protocols[prot.Metadata.Name] = DatabaseEntry{p, prot.Metadata} fd.protocols[prot.Metadata.Name] = ProtocolDictionaryEntry{p, prot.Metadata}
} }
return err return err
} }
// Update updates an existing entry through saving the contents of the protocol to the file, or just adding it anew // 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 { if v, ok := fd.protocols[prot.Metadata.Name]; ok {
v.Protocol = prot.Metadata v.Protocol = prot.Metadata
return protocolctl.Save(prot, v.Path) return protocolctl.Save(prot, v.Path)
@ -163,14 +163,14 @@ func (fd *FileDatabase) Update(prot *protocol.ProtocolStructure) error {
} }
// UpdateDatabase just rereads all files // UpdateDatabase just rereads all files
func (fd *FileDatabase) UpdateDatabase() error { func (fd *FileDOP) UpdateDatabase() error {
// recurse recursively through the path // recurse recursively through the path
err := filepath.Walk(fd.Path, func(path string, info fs.FileInfo, err error) error { err := filepath.Walk(fd.Path, func(path string, info fs.FileInfo, err error) error {
if !info.IsDir() { if !info.IsDir() {
prot, err := protocolctl.LoadNew(path) prot, err := protocolctl.LoadNew(path)
if err == nil { if err == nil {
// add to map // add to map
fd.protocols[prot.Metadata.Name] = DatabaseEntry{path, prot.Metadata} fd.protocols[prot.Metadata.Name] = ProtocolDictionaryEntry{path, prot.Metadata}
} }
} }
return nil return nil

View File

@ -1,4 +1,4 @@
package protocoldb package dop
import ( import (
"testing" "testing"