add documentation for functions

This commit is contained in:
Shane C. 2024-11-18 17:00:58 -05:00
parent 9614599eb5
commit 5ce1b86da4
Signed by: Shane C.
GPG key ID: E46B5FEA35B22FF9
2 changed files with 20 additions and 4 deletions

View file

@ -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 {

View file

@ -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",