From cfc7e5362d2d0a3ef4062c5384a0ed6496e983f3 Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 13 Aug 2024 10:54:21 +0200 Subject: [PATCH] chore: Add comments and improve JSON annotations for better data structuring and clarity across the codebase --- internal/utils/error.go | 13 +++++++- internal/utils/slices.go | 11 ------- internal/utils/slices_test.go | 60 ----------------------------------- internal/utils/tracing.go | 2 ++ pkg/database/client.go | 18 ++++++----- pkg/error/validation.go | 5 --- pkg/models/orm.go | 4 +-- pkg/models/post.go | 2 +- pkg/models/tag.go | 6 ++-- pkg/models/userSource.go | 2 +- 10 files changed, 31 insertions(+), 92 deletions(-) delete mode 100644 internal/utils/slices.go delete mode 100644 internal/utils/slices_test.go diff --git a/internal/utils/error.go b/internal/utils/error.go index f73c2cf..aa80866 100644 --- a/internal/utils/error.go +++ b/internal/utils/error.go @@ -8,7 +8,18 @@ import ( "go.opentelemetry.io/otel/trace" ) -func HandleError(ctx context.Context, span trace.Span, logger *log.Entry, error error) error { +// HandleError logs the provided error, records it in the given trace span, +// sets the span status to error, and returns the error. +// +// Parameters: +// - ctx: context.Context, the context in which the error occurred. +// - span: trace.Span, the trace span where the error will be recorded. +// - logger: *log.Entry, a log entry used to log the error message. +// - error: error, the error to be handled. +// +// Returns: +// - error: The same error that was passed in. +func HandleError(_ context.Context, span trace.Span, logger *log.Entry, error error) error { logger.Error(error) span.RecordError(error) span.SetStatus(codes.Error, error.Error()) diff --git a/internal/utils/slices.go b/internal/utils/slices.go deleted file mode 100644 index dfb5aed..0000000 --- a/internal/utils/slices.go +++ /dev/null @@ -1,11 +0,0 @@ -package utils - -func GetOrDefault(data map[string]any, key string, defaultVal any) any { - val, ok := data[key] - - if !ok { - return defaultVal - } - - return val -} diff --git a/internal/utils/slices_test.go b/internal/utils/slices_test.go deleted file mode 100644 index 9f8cdc6..0000000 --- a/internal/utils/slices_test.go +++ /dev/null @@ -1,60 +0,0 @@ -package utils - -import ( - "reflect" - "testing" -) - -func TestGetOrDefault(t *testing.T) { - type args struct { - data map[string]any - key string - defaultVal any - } - tests := []struct { - name string - args args - want any - }{ - { - name: "Test 1: Nil map", - args: args{ - data: nil, - key: "key1", - defaultVal: "default", - }, - want: "default", - }, - { - name: "Test 2: Existing key", - args: args{ - data: map[string]interface{}{ - "key1": "value1", - "key2": "value2", - }, - key: "key1", - defaultVal: "default", - }, - want: "value1", - }, - { - name: "Test 3: Non-existing key", - args: args{ - data: map[string]interface{}{ - "key1": "value1", - "key2": "value2", - }, - key: "key3", - defaultVal: "default", - }, - want: "default", - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := GetOrDefault(tt.args.data, tt.args.key, tt.args.defaultVal); !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetOrDefault() = %v, want %v", got, tt.want) - } - }) - } -} diff --git a/internal/utils/tracing.go b/internal/utils/tracing.go index 6e83a9f..e71be80 100644 --- a/internal/utils/tracing.go +++ b/internal/utils/tracing.go @@ -7,6 +7,7 @@ import ( "go.opentelemetry.io/otel/trace" ) +// SetupTracing initializes a new trace span and logger for the given context and tracer. func SetupTracing(ctx context.Context, tracer trace.Tracer, tracerName string) (context.Context, trace.Span, *log.Entry) { ctx, span := tracer.Start(ctx, tracerName) localLogger := log.WithContext(ctx) @@ -14,6 +15,7 @@ func SetupTracing(ctx context.Context, tracer trace.Tracer, tracerName string) ( return ctx, span, localLogger } +// HandleEvent logs the provided event name and adds it to the given trace span. func HandleEvent(span trace.Span, logger *log.Entry, eventName string) { logger.Debug(eventName) span.AddEvent(eventName) diff --git a/pkg/database/client.go b/pkg/database/client.go index 2f4c362..c4efd76 100644 --- a/pkg/database/client.go +++ b/pkg/database/client.go @@ -28,9 +28,16 @@ var ( logger = log.New() ) +// Connect to the Database func Connect(ctx context.Context, config models.DatabaseConfig) error { - setupTelemetry() + // Setup open telemetry + tracer = otel.Tracer(tracingName) + + hook := otellogrus.NewHook(tracingName) + logger.AddHook(hook) + + // Debug enabled? if config.Debug { log.SetLevel(log.DebugLevel) } @@ -75,6 +82,7 @@ func Connect(ctx context.Context, config models.DatabaseConfig) error { return nil } +// migrateDatabase handels the migration of ann SQL files in the migrations subfolder func migrateDatabase(ctx context.Context, dbPool *gorm.DB, config models.DatabaseConfig) error { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "migrateDatabase") defer span.End() @@ -114,13 +122,7 @@ func migrateDatabase(ctx context.Context, dbPool *gorm.DB, config models.Databas return nil } -func setupTelemetry() { - tracer = otel.Tracer(tracingName) - - hook := otellogrus.NewHook(tracingName) - logger.AddHook(hook) -} - +// GetGorm returns a ready to use gorm.DB client func GetGorm(ctx context.Context) (*gorm.DB, error) { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetGorm") defer span.End() diff --git a/pkg/error/validation.go b/pkg/error/validation.go index 0e27823..143635e 100644 --- a/pkg/error/validation.go +++ b/pkg/error/validation.go @@ -3,9 +3,6 @@ package error import "fmt" const ( - UserIDIsEmpty = "postID cannot be empty" - UserIDToShort = "postID needs to be 25 characters long" - SourceListIsEmpty = "sourceList cannot be empty" SourceIDIsEmpty = "SourceID cannot be empty" SourceIDToShort = "sourceID needs to be 25 characters long" @@ -15,10 +12,8 @@ const ( UserSourceIDToShort = "userSourceID needs to be 25 characters long" TagListIsEmpty = "tagList cannot be empty" - TagNameIsEmpty = "tagName cannot be empty" TagAliasListIsEmpty = "tagAliasList cannot be empty" - TagAliasNameIsEmpty = "tagAliasName cannot be empty" TagGroupListIsEmpty = "tagGroupList cannot be empty" TagGroupNameIsEmpty = "tagGroupName cannot be empty" diff --git a/pkg/models/orm.go b/pkg/models/orm.go index a4ec52f..2cf6cf5 100644 --- a/pkg/models/orm.go +++ b/pkg/models/orm.go @@ -13,8 +13,8 @@ type ID interface { type BaseModel[T ID] struct { ID T `json:"id" gorm:"primaryKey"` - CreatedAt time.Time `json:"-"` - UpdatedAt time.Time `json:"-"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` DeletedAt gorm.DeletedAt `json:"-" gorm:"index"` } diff --git a/pkg/models/post.go b/pkg/models/post.go index 0d0dda3..479e4cf 100644 --- a/pkg/models/post.go +++ b/pkg/models/post.go @@ -4,7 +4,7 @@ package models type Post struct { BaseModel[PostID] Rating Rating `json:"rating" gorm:"type:enum('safe','questionable','explicit')"` - Tags []Tag `json:"-" gorm:"many2many:post_tags;"` + Tags []Tag `json:"tags,omitempty" gorm:"many2many:post_tags;"` Favorites []UserFavorite `json:"-" gorm:"foreignKey:PostID"` References []PostReference `json:"references" gorm:"foreignKey:PostID"` } diff --git a/pkg/models/tag.go b/pkg/models/tag.go index 53b5c1a..4e87361 100644 --- a/pkg/models/tag.go +++ b/pkg/models/tag.go @@ -4,9 +4,9 @@ package models type Tag struct { Name TagName `json:"name" gorm:"primaryKey"` Type TagType `json:"type" gorm:"column:tag_type"` - Aliases []TagAlias `json:"aliases" gorm:"foreignKey:TagID"` - Groups []TagGroup `json:"groups" gorm:"foreignKey:TagID"` - Posts []Post `json:"posts" gorm:"many2many:post_tags;"` + Aliases []TagAlias `json:"aliases,omitempty" gorm:"foreignKey:TagID"` + Groups []TagGroup `json:"groups,omitempty" gorm:"foreignKey:TagID"` + Posts []Post `json:"posts,omitempty" gorm:"many2many:post_tags;"` } func (Tag) TableName() string { diff --git a/pkg/models/userSource.go b/pkg/models/userSource.go index 187c2a9..171c95e 100644 --- a/pkg/models/userSource.go +++ b/pkg/models/userSource.go @@ -13,7 +13,7 @@ type UserSource struct { AccountID string `json:"account_id"` LastScrapeTime time.Time `json:"last_scrape_time"` AccountValidate bool `json:"account_validate"` - AccountValidationKey string `json:"-"` + AccountValidationKey string `json:"account_validation_key"` } func (UserSource) TableName() string {