change docstring formating

This commit is contained in:
2024-12-28 01:28:36 +01:00
parent aeb9de71a1
commit 564b6f815e
3 changed files with 50 additions and 41 deletions

View File

@@ -7,17 +7,16 @@ import (
"encoding/json" "encoding/json"
) )
// A very simple datastructure, implementing the IDatastore interface. It uses
// a simple text file to the data as json.
type FileDatastore struct { type FileDatastore struct {
/* A very simple datastructure, implementing the IDatastore interface. It
* uses a simple text file to the data as json. */
path string path string
} }
// Creates a new FileDatastore object, creating the storage file in the
// process.
func NewFileDatastore(path string) (*FileDatastore, error) { func NewFileDatastore(path string) (*FileDatastore, error) {
/* Creates a new FileDatastore object, creating the storage file in the
* process. */
fds := &FileDatastore{ path: path } fds := &FileDatastore{ path: path }
if _, err := fds.readMapObj(); err != nil { if _, err := fds.readMapObj(); err != nil {
@@ -27,11 +26,10 @@ func NewFileDatastore(path string) (*FileDatastore, error) {
return fds, nil return fds, nil
} }
// Read the contents of the storage file and convert to a map object. May throw
// an error, if the file does not exit or the file content can not be
// converted.
func (fds *FileDatastore) readMapObj() (map[string]string, error) { func (fds *FileDatastore) readMapObj() (map[string]string, error) {
/* Read the contents of the storage file and convert to a map object. May
* throw an error, if the file does not exit or the file content can not be
* converted. */
dat, err := os.ReadFile(fds.path) dat, err := os.ReadFile(fds.path)
if err != nil { return nil, err } if err != nil { return nil, err }
@@ -42,11 +40,9 @@ func (fds *FileDatastore) readMapObj() (map[string]string, error) {
return mapobj, nil return mapobj, nil
} }
// Write the map object to the storage file. Will overwrite the content of the
// file. May throw an error, if the file cannot be created or written to.
func (fds *FileDatastore) writeMapObj(m map[string]string) error { func (fds *FileDatastore) writeMapObj(m map[string]string) error {
/* Write the map object to the storage file. Will overwrite the content of
* the file. May throw an error, if the file cannot be created or written
* to. */
file, err := os.Create(fds.path) file, err := os.Create(fds.path)
if err != nil { return err } if err != nil { return err }
defer file.Close() defer file.Close()
@@ -57,13 +53,13 @@ func (fds *FileDatastore) writeMapObj(m map[string]string) error {
return nil return nil
} }
// --- implement IDatastore interface --- // --- implement IDatastore interface ---
// Sets the key value pair given, overwriting if the key already exists. May
// through an error if the file cannot be opened or the contents cannot be
// decoded correctly.
func (fds *FileDatastore) Set(key string, val string) error { func (fds *FileDatastore) Set(key string, val string) error {
/* Sets the key value pair given, overwriting if the key already exists.
* May through an error if the file cannot be opened or the contents cannot
* be decoded correctly. */
m, err := fds.readMapObj() m, err := fds.readMapObj()
if err != nil { return err } if err != nil { return err }
@@ -75,10 +71,9 @@ func (fds *FileDatastore) Set(key string, val string) error {
return nil return nil
} }
// Check if for the given key a entry does exit. May through an error if the
// file cannot be opened or the contents cannot be decoded correctly.
func (fds *FileDatastore) KeyExists(key string) (bool, error) { func (fds *FileDatastore) KeyExists(key string) (bool, error) {
/* Check if for the given key a entry does exit. May through an error if
* the file cannot be opened or the contents cannot be decoded correctly. */
m, err := fds.readMapObj() m, err := fds.readMapObj()
if err != nil { return false, err } if err != nil { return false, err }
@@ -87,11 +82,10 @@ func (fds *FileDatastore) KeyExists(key string) (bool, error) {
return ok, nil return ok, nil
} }
// Gets the value for the given key. May through an error if the key does not
// exit, the file cannot be opened or the contents cannot be decoded
// correctly.
func (fds *FileDatastore) Get(key string) (string, error) { func (fds *FileDatastore) Get(key string) (string, error) {
/* Gets the value for the given key. May through an error if the key does
* not exit, the file cannot be opened or the contents cannot be decoded
* correctly. */
m, err := fds.readMapObj() m, err := fds.readMapObj()
if err != nil { return "", err } if err != nil { return "", err }
@@ -101,19 +95,17 @@ func (fds *FileDatastore) Get(key string) (string, error) {
return val, nil return val, nil
} }
// Gets all the key value pairs from the file and returns them as a map object.
// May through an error if the file cannot be opened or the contents cannot be
// decoded correctly.
func (fds *FileDatastore) GetAll() (map[string]string, error) { func (fds *FileDatastore) GetAll() (map[string]string, error) {
/* Gets all the key value pairs from the file and returns them as a map
* object. May through an error if the file cannot be opened or the contents
* cannot be decoded correctly. */
return fds.readMapObj() return fds.readMapObj()
} }
// Gets all the key the file and returns them as a map object. May through an
// error if the file cannot be opened or the contents cannot be decoded
// correctly.
func (fds *FileDatastore) GetAllKeys() (map[string]bool, error) { func (fds *FileDatastore) GetAllKeys() (map[string]bool, error) {
/* Gets all the key the file and returns them as a map object. May through
* an error if the file cannot be opened or the contents cannot be decoded
* correctly. */
m, err := fds.readMapObj() m, err := fds.readMapObj()
if err != nil { return nil, err } if err != nil { return nil, err }
@@ -124,3 +116,18 @@ func (fds *FileDatastore) GetAllKeys() (map[string]bool, error) {
return out, nil return out, nil
} }
// Deletes the entry with the given key. May through an error if the file
// cannot be opened or the contents cannot be decoded or encoded correctly.
func (fds *FileDatastore) Delete(key string) error {
m, err := fds.readMapObj()
if err != nil { return err }
delete(m, key)
err = fds.writeMapObj(m)
if err != nil { return err }
return nil
}

View File

@@ -6,4 +6,5 @@ type IDatastore interface {
Get(key string) (string, error) Get(key string) (string, error)
GetAll() (map[string]string, error) GetAll() (map[string]string, error)
GetAllKeys() (map[string]bool, error) GetAllKeys() (map[string]bool, error)
Delete(key string) error
} }

View File

@@ -7,10 +7,10 @@ import (
) )
// An interface to manage generic structure objects persistently. Should use an
// IDatastore as the interface that actually stores and retrieves the data from
// an external source.
type IRepository[T IIdentifiable] interface { type IRepository[T IIdentifiable] interface {
/* An interface to manage generic structure objects persistently. Should
* use an IDatastore as the interface that actually stores and retrieves the
* data from an external source. */
Create(t T) error Create(t T) error
Update(t T) error Update(t T) error
Delete(t T) error Delete(t T) error
@@ -19,6 +19,7 @@ type IRepository[T IIdentifiable] interface {
GetByCriteria(c ISearchCriteria[T]) ([]T, error) GetByCriteria(c ISearchCriteria[T]) ([]T, error)
} }
// --- default implementation --- // --- default implementation ---
type DefaultRepository[T IIdentifiable] struct { type DefaultRepository[T IIdentifiable] struct {
@@ -30,10 +31,10 @@ func NewDefaultRepository[T IIdentifiable](ds IDatastore, prefix string) *Defaul
return &DefaultRepository[T]{ ds: ds, prefix: prefix } return &DefaultRepository[T]{ ds: ds, prefix: prefix }
} }
// Creates a new entry in the repository with the given object t. Throws an
// error if there already exists an entry with the same id, the json encoding
// fails or the connection to the IDatastore fails.
func (repo *DefaultRepository[T]) Create(t T) error { func (repo *DefaultRepository[T]) Create(t T) error {
/* Creates a new entry in the repository with the given object t. Throws an
* error if there already exists an entry with the same id, the json
* encoding fails or the connection to the IDatastore fails. */
key := repo.prefix + ":" + strconv.FormatUint(uint64(t.Id()), 10) key := repo.prefix + ":" + strconv.FormatUint(uint64(t.Id()), 10)
exists, err := repo.ds.KeyExists(key) exists, err := repo.ds.KeyExists(key)
@@ -49,10 +50,10 @@ func (repo *DefaultRepository[T]) Create(t T) error {
return nil return nil
} }
// Updates the entry with the same id as t in the repository with the values of
// t. Trows an error if the json encoding fails or the connection to the
// IDatastore fails.
func (repo *DefaultRepository[T]) Update(t T) error { func (repo *DefaultRepository[T]) Update(t T) error {
/* Updates the entry with the same id as t in the repository with the
* values of t. Trows an error if the json encoding fails or the connection
* to the IDatastore fails. */
key := repo.prefix + ":" + strconv.FormatUint(uint64(t.Id()), 10) key := repo.prefix + ":" + strconv.FormatUint(uint64(t.Id()), 10)
exists, err := repo.ds.KeyExists(key) exists, err := repo.ds.KeyExists(key)