Remove sqlx mapper function; just map all fields manually

This commit is contained in:
Alessio 2024-10-23 21:50:54 -07:00
parent 720c486715
commit 74db0f5797
4 changed files with 7 additions and 29 deletions

View File

@ -7,7 +7,6 @@ import (
"path/filepath" "path/filepath"
sql "github.com/jmoiron/sqlx" sql "github.com/jmoiron/sqlx"
"github.com/jmoiron/sqlx/reflectx"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"gitlab.com/offline-twitter/twitter_offline_engine/pkg/scraper" "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) fmt.Printf("Creating............. %s\n", sqlite_file)
db := sql.MustOpen("sqlite3", sqlite_file+"?_foreign_keys=on") db := sql.MustOpen("sqlite3", sqlite_file+"?_foreign_keys=on")
db.MustExec(sql_init) db.MustExec(sql_init)
InitializeDatabaseVersion(db)
db.Mapper = reflectx.NewMapperFunc("db", ToSnakeCase)
// Create `profile_images` // Create `profile_images`
fmt.Printf("Creating............. %s/\n", profile_images_dir) 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 := sql.MustOpen("sqlite3", fmt.Sprintf("%s?_foreign_keys=on&_journal_mode=WAL", sqlite_file))
db.Mapper = reflectx.NewMapperFunc("db", ToSnakeCase)
ret := Profile{ ret := Profile{
ProfileDir: profile_dir, ProfileDir: profile_dir,

View File

@ -103,12 +103,3 @@ func TestLoadProfile(t *testing.T) {
assert.Equal(t, profile_path, profile.ProfileDir) 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"))
}

View File

@ -3,8 +3,6 @@ package persistence
import ( import (
"errors" "errors"
"os" "os"
"regexp"
"strings"
) )
var ErrNotInDatabase = errors.New("not in database") var ErrNotInDatabase = errors.New("not in database")
@ -20,10 +18,3 @@ func file_exists(path string) bool {
panic(err) 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)
}

View File

@ -12,18 +12,18 @@ type Space struct {
State string `db:"state"` State string `db:"state"`
Title string `db:"title"` Title string `db:"title"`
CreatedAt Timestamp `db:"created_at"` CreatedAt Timestamp `db:"created_at"`
StartedAt Timestamp StartedAt Timestamp `db:"started_at"`
EndedAt Timestamp `db:"ended_at"` EndedAt Timestamp `db:"ended_at"`
UpdatedAt Timestamp UpdatedAt Timestamp `db:"updated_at"`
IsAvailableForReplay bool IsAvailableForReplay bool `db:"is_available_for_replay"`
ReplayWatchCount int ReplayWatchCount int `db:"replay_watch_count"`
LiveListenersCount int LiveListenersCount int `db:"live_listeners_count"`
ParticipantIds []UserID ParticipantIds []UserID
CreatedById UserID CreatedById UserID `db:"created_by_id"`
TweetID TweetID TweetID TweetID
IsDetailsFetched bool IsDetailsFetched bool `db:"is_details_fetched"`
} }
func (space Space) FormatDuration() string { func (space Space) FormatDuration() string {