From 74db0f579707f757397e34da2a18c81ce3b34e22 Mon Sep 17 00:00:00 2001 From: Alessio Date: Wed, 23 Oct 2024 21:50:54 -0700 Subject: [PATCH] Remove sqlx mapper function; just map all fields manually --- pkg/persistence/profile.go | 4 ---- pkg/persistence/profile_test.go | 9 --------- pkg/persistence/utils.go | 9 --------- pkg/scraper/space.go | 14 +++++++------- 4 files changed, 7 insertions(+), 29 deletions(-) diff --git a/pkg/persistence/profile.go b/pkg/persistence/profile.go index 38542be..add90a0 100644 --- a/pkg/persistence/profile.go +++ b/pkg/persistence/profile.go @@ -7,7 +7,6 @@ import ( "path/filepath" sql "github.com/jmoiron/sqlx" - "github.com/jmoiron/sqlx/reflectx" _ "github.com/mattn/go-sqlite3" "gitlab.com/offline-twitter/twitter_offline_engine/pkg/scraper" @@ -58,8 +57,6 @@ func NewProfile(target_dir string) (Profile, error) { fmt.Printf("Creating............. %s\n", sqlite_file) db := sql.MustOpen("sqlite3", sqlite_file+"?_foreign_keys=on") db.MustExec(sql_init) - InitializeDatabaseVersion(db) - db.Mapper = reflectx.NewMapperFunc("db", ToSnakeCase) // Create `profile_images` fmt.Printf("Creating............. %s/\n", profile_images_dir) @@ -119,7 +116,6 @@ func LoadProfile(profile_dir string) (Profile, error) { } db := sql.MustOpen("sqlite3", fmt.Sprintf("%s?_foreign_keys=on&_journal_mode=WAL", sqlite_file)) - db.Mapper = reflectx.NewMapperFunc("db", ToSnakeCase) ret := Profile{ ProfileDir: profile_dir, diff --git a/pkg/persistence/profile_test.go b/pkg/persistence/profile_test.go index 9acb103..d5828b4 100644 --- a/pkg/persistence/profile_test.go +++ b/pkg/persistence/profile_test.go @@ -103,12 +103,3 @@ func TestLoadProfile(t *testing.T) { assert.Equal(t, profile_path, profile.ProfileDir) } - -// Test the ToSnakeCase implementation -func TestSnakeCase(t *testing.T) { - assert := assert.New(t) - - assert.Equal("tweet_id", persistence.ToSnakeCase("TweetID")) - assert.Equal("i_am_a_computer", persistence.ToSnakeCase("IAmAComputer")) - assert.Equal("choice1_votes", persistence.ToSnakeCase("Choice1_Votes")) -} diff --git a/pkg/persistence/utils.go b/pkg/persistence/utils.go index 8595da9..83ff068 100644 --- a/pkg/persistence/utils.go +++ b/pkg/persistence/utils.go @@ -3,8 +3,6 @@ package persistence import ( "errors" "os" - "regexp" - "strings" ) var ErrNotInDatabase = errors.New("not in database") @@ -20,10 +18,3 @@ func file_exists(path string) bool { panic(err) } } - -// https://stackoverflow.com/questions/56616196/how-to-convert-camel-case-string-to-snake-case#56616250 -func ToSnakeCase(str string) string { - snake := regexp.MustCompile("(.)_?([A-Z][a-z]+)").ReplaceAllString(str, "${1}_${2}") - snake = regexp.MustCompile("([a-z0-9])_?([A-Z])").ReplaceAllString(snake, "${1}_${2}") - return strings.ToLower(snake) -} diff --git a/pkg/scraper/space.go b/pkg/scraper/space.go index 2056310..6392e73 100644 --- a/pkg/scraper/space.go +++ b/pkg/scraper/space.go @@ -12,18 +12,18 @@ type Space struct { State string `db:"state"` Title string `db:"title"` CreatedAt Timestamp `db:"created_at"` - StartedAt Timestamp + StartedAt Timestamp `db:"started_at"` EndedAt Timestamp `db:"ended_at"` - UpdatedAt Timestamp - IsAvailableForReplay bool - ReplayWatchCount int - LiveListenersCount int + UpdatedAt Timestamp `db:"updated_at"` + IsAvailableForReplay bool `db:"is_available_for_replay"` + ReplayWatchCount int `db:"replay_watch_count"` + LiveListenersCount int `db:"live_listeners_count"` ParticipantIds []UserID - CreatedById UserID + CreatedById UserID `db:"created_by_id"` TweetID TweetID - IsDetailsFetched bool + IsDetailsFetched bool `db:"is_details_fetched"` } func (space Space) FormatDuration() string {