Implement note parsing

This commit is contained in:
2024-10-27 17:18:43 +01:00
parent 9f0435057e
commit c31ab6a01d
6 changed files with 95 additions and 4 deletions

2
.gitignore vendored
View File

@@ -1 +1,3 @@
data data
main.log
input

2
db.go
View File

@@ -7,7 +7,7 @@ import (
"os" "os"
"time" "time"
"github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
) )
type DB struct { type DB struct {

2
go.mod
View File

@@ -1,3 +1,5 @@
module stinkinator module stinkinator
go 1.23.2 go 1.23.2
require github.com/mattn/go-sqlite3 v1.14.24

2
go.sum Normal file
View File

@@ -0,0 +1,2 @@
github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM=
github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=

61
main.go
View File

@@ -1,10 +1,13 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
"io" "io"
"log" "log"
"os" "os"
"strings"
"time"
) )
var Error *log.Logger var Error *log.Logger
@@ -28,7 +31,59 @@ func init() {
} }
func main() { func main() {
log.Println("Hello, World!") inputFile := flag.String("if", "input", "Input file")
Warning.Println("Hello, World!") flag.Parse()
Error.Println("Hello, World!") log.Printf("Input file: %s", *inputFile)
db := DB{
path: "data/db.db",
}
err := db.Open()
if err != nil {
Error.Printf("Failed opening database: %v", err)
return
}
defer db.Close()
inputData, err := os.ReadFile(*inputFile)
if err != nil {
Error.Printf("Failed reading input file: %v", err)
return
}
note, err := ParseNote(string(inputData))
if err != nil {
Error.Printf("Failed parsing note: %v", err)
return
}
log.Printf("%#v", note)
}
func ParseNote(data string) (NoteData, error) {
res := NoteData{}
lines := strings.Split(data, "\n")
note := []string{}
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
continue
}
parts := strings.Split(line, ":")
log.Printf("%#v", parts)
switch parts[0] {
case "p":
res.Player = parts[1]
case "d":
var err error
res.Date, err = time.Parse(time.DateOnly, parts[1])
if err != nil {
return res, fmt.Errorf("failed to parse date: %v", err)
}
case "g":
res.Guild = parts[1]
default:
note = append(note, line)
}
}
res.Note = strings.Join(note, "\n")
return res, nil
} }

30
types.go Normal file
View File

@@ -0,0 +1,30 @@
package main
import "time"
type (
Guild struct {
ID int
Name string
}
Stinky struct {
ID int
Name string
Guild Guild
}
Note struct {
ID int
Content string
Timestamp time.Time
Stinky Stinky
}
)
type NoteData struct {
Player string
Date time.Time
Guild string
Note string
}