chore: Add comments and improve JSON annotations for better data structuring and clarity across the codebase
This commit is contained in:
parent
b4e8152cb4
commit
cfc7e5362d
@ -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())
|
||||
|
@ -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
|
||||
}
|
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
@ -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)
|
||||
|
@ -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()
|
||||
|
@ -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"
|
||||
|
@ -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"`
|
||||
}
|
||||
|
||||
|
@ -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"`
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user