From 5ce1b86da4010ddc282067860e56c417b89646dd Mon Sep 17 00:00:00 2001 From: Shane C Date: Mon, 18 Nov 2024 17:00:58 -0500 Subject: [PATCH] add documentation for functions --- storage.go | 20 ++++++++++++++++++-- storage_test.go | 4 ++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/storage.go b/storage.go index 6c87c61..e6a903a 100644 --- a/storage.go +++ b/storage.go @@ -21,12 +21,13 @@ var ( ) type Storage struct { - config *Config + config Config s3Client *minio.Client ctx context.Context } type Config struct { + // Type of storage, can be "local" or "s3" Type string Path string @@ -41,7 +42,10 @@ type Config struct { S3BucketName string } -func New(config *Config) (*Storage, error) { +// New creates a new storage interface +// It takes Config as its parameter. +// The function returns the Storage interface, and an error, if any. +func New(config Config) (*Storage, error) { newStorage := new(Storage) newStorage.config = config newStorage.ctx = context.Background() @@ -85,6 +89,9 @@ func New(config *Config) (*Storage, error) { return newStorage, nil } +// Open opens a file within the Storage interface. +// It takes a file name as its parameter. +// The function returns a File and an error, if any. func (s *Storage) Open(name string) (*File, error) { returnFile := new(File) returnFile.storage = s @@ -132,6 +139,9 @@ func (s *Storage) Open(name string) (*File, error) { return returnFile, nil } +// Read opens a file within the Storage interfaces and reads the contents. +// It takes a file name as its parameter. +// The function returns a []byte, or an error, if any. func (s *Storage) Read(name string) ([]byte, error) { if s.s3Client != nil { object, err := s.s3Client.GetObject(s.ctx, s.config.S3BucketName, name, minio.GetObjectOptions{}) @@ -159,6 +169,9 @@ type WriteOptions struct { S3Tags map[string]string } +// Write writes to a file within the Storage interface. +// It takes a name, a []byte, and WriteOptions as its parameters. +// The function returns an error, if any. func (s *Storage) Write(name string, data []byte, opts WriteOptions) error { if s.s3Client != nil { if _, err := s.s3Client.PutObject(s.ctx, s.config.S3BucketName, name, bytes.NewReader(data), int64(len(data)), minio.PutObjectOptions{ @@ -172,6 +185,9 @@ func (s *Storage) Write(name string, data []byte, opts WriteOptions) error { } } +// Delete deleted a file within the storage interface. +// It takes a file name as its parameter. +// The function returns an error, if any. func (s *Storage) Delete(name string) error { if s.s3Client != nil { if err := s.s3Client.RemoveObject(s.ctx, s.config.S3BucketName, name, minio.RemoveObjectOptions{}); err != nil { diff --git a/storage_test.go b/storage_test.go index 700aa9e..3fb3c0d 100644 --- a/storage_test.go +++ b/storage_test.go @@ -8,7 +8,7 @@ import ( func TestStorageLocal(t *testing.T) { - stor, err := New(&Config{ + stor, err := New(Config{ Type: "local", Path: "./test/local", }) @@ -43,7 +43,7 @@ func TestStorageLocal(t *testing.T) { func TestStorageS3(t *testing.T) { - stor, err := New(&Config{ + stor, err := New(Config{ Type: "s3", S3BucketName: "test", S3AccessID: "root",