From f85dbab551175359cd58e27606e0a41b6b048dce Mon Sep 17 00:00:00 2001 From: Simon Martens Date: Thu, 13 Mar 2025 18:31:09 +0100 Subject: [PATCH] BUGFIX: read XML data --- .gitignore | 1 + config.dev.json | 5 +++++ config/config.go | 16 +++++++++++----- config/public.go | 6 ++++-- git/git.go | 2 +- lenz.go | 33 ++++++++++++++++++++++++++++++++- xml/helpers.go | 4 +++- xmlmodels/library.go | 38 ++++++++++++++++++++++++++++++++++++-- xmlmodels/roots.go | 28 ++++++++++++---------------- 9 files changed, 105 insertions(+), 28 deletions(-) create mode 100644 .gitignore create mode 100644 config.dev.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e934adf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +cache/ diff --git a/config.dev.json b/config.dev.json new file mode 100644 index 0000000..e17b1ac --- /dev/null +++ b/config.dev.json @@ -0,0 +1,5 @@ +{ + "debug": true, + "watch": true, + "git_url": "git@github.com:Theodor-Springmann-Stiftung/lenz-briefe.git" +} diff --git a/config/config.go b/config/config.go index b0aecf6..4586acd 100644 --- a/config/config.go +++ b/config/config.go @@ -17,8 +17,10 @@ const ( DEFAULT_SEARCH_CACHE_DIR = "search" DEFAULT_IMG_DIR = "data_bilder" + DEFAULT_BRANCH = "main" + DEFAULT_PORT = "8080" - DEFAULT_ADDR = "localhost" + DEFAULT_ADDR = "127.0.0.1" DEFAULT_HTTPS = false ENV_PREFIX = "KGPZ" @@ -55,7 +57,7 @@ func NewConfigProvider(files []string) *ConfigProvider { func (c *ConfigProvider) Read() error { c.Config = &Config{} for _, file := range c.Files { - c.Config = readSettingsFile(c.Config, file) + _, _ = readSettingsFile(c.Config, file) } c.Config = readSettingsEnv(c.Config) c.Config = readDefaults(c.Config) @@ -69,17 +71,17 @@ func (c *ConfigProvider) Validate() error { return nil } -func readSettingsFile(cfg *Config, path string) *Config { +func readSettingsFile(cfg *Config, path string) (*Config, error) { f, err := os.Open(path) if err != nil { - return cfg + return cfg, err } defer f.Close() dec := json.NewDecoder(f) err = dec.Decode(cfg) - return cfg + return cfg, err } func readSettingsEnv(cfg *Config) *Config { @@ -92,6 +94,10 @@ func readDefaults(cfg *Config) *Config { cfg.BaseDIR = DEFAULT_DIR } + if strings.TrimSpace(cfg.GitBranch) == "" { + cfg.GitBranch = DEFAULT_BRANCH + } + if strings.TrimSpace(cfg.GITPath) == "" { cfg.GITPath = DEFAULT_GIT_CACHE_DIR } diff --git a/config/public.go b/config/public.go index 65fae63..a56136e 100644 --- a/config/public.go +++ b/config/public.go @@ -5,14 +5,16 @@ import "sync" var cp *ConfigProvider var mu = sync.Mutex{} -func Get() Config { +func Get() (Config, error) { mu.Lock() defer mu.Unlock() if cp == nil { cp = NewConfigProvider([]string{"config.dev.json", "config.json"}) + return *cp.Config, cp.Read() } - return *cp.Config + + return *cp.Config, nil } func Set(config Config) { diff --git a/git/git.go b/git/git.go index 45152fd..c7f64bd 100644 --- a/git/git.go +++ b/git/git.go @@ -1,4 +1,4 @@ -package providers +package gitprovider import ( "errors" diff --git a/lenz.go b/lenz.go index 16e2b22..ec713b9 100644 --- a/lenz.go +++ b/lenz.go @@ -1,5 +1,36 @@ package main +import ( + "fmt" + "log/slog" + "path/filepath" + + "github.com/Theodor-Springmann-Stiftung/lenz-web/config" + gitprovider "github.com/Theodor-Springmann-Stiftung/lenz-web/git" + xmlparsing "github.com/Theodor-Springmann-Stiftung/lenz-web/xml" + "github.com/Theodor-Springmann-Stiftung/lenz-web/xmlmodels" +) + func main() { - println("Hello, World!") + cfg, err := config.Get() + if err != nil { + panic(err) + } + + if cfg.Debug { + slog.SetLogLoggerLevel(slog.LevelDebug) + } + + dir := filepath.Join(cfg.BaseDIR, cfg.GITPath) + + gp, err := gitprovider.NewGitProvider(cfg.GitURL, dir, cfg.GitBranch) + + if err != nil { + panic(err) + } + + lib := xmlmodels.NewLibrary() + lib.Parse(xmlparsing.Commit, dir, gp.Commit) + + fmt.Println("Library: ", lib) } diff --git a/xml/helpers.go b/xml/helpers.go index 293c761..efea695 100644 --- a/xml/helpers.go +++ b/xml/helpers.go @@ -3,11 +3,13 @@ package xmlparsing import ( "encoding/xml" "io" + "log/slog" "os" "path/filepath" ) func UnmarshalFile[T any](filename string, data T) error { + slog.Debug("Unmarshalling file: ", "file", filename) xmlFile, err := os.Open(filename) if err != nil { return err @@ -18,8 +20,8 @@ func UnmarshalFile[T any](filename string, data T) error { if err != nil { return err } - err = xml.Unmarshal(byteValue, &data) + err = xml.Unmarshal(byteValue, &data) if err != nil { return err } diff --git a/xmlmodels/library.go b/xmlmodels/library.go index a0c1ee5..4422af4 100644 --- a/xmlmodels/library.go +++ b/xmlmodels/library.go @@ -2,7 +2,9 @@ package xmlmodels import ( "fmt" + "log/slog" "path/filepath" + "strconv" "strings" "sync" "time" @@ -11,7 +13,7 @@ import ( ) const ( - REFERENCES_PATH = "data/xml/akteure.xml" + REFERENCES_PATH = "data/xml/references.xml" LETTERS_PATH = "data/xml/briefe.xml" META_PATH = "data/xml/meta.xml" TRADITIONS_PATH = "data/xml/traditions.xml" @@ -32,7 +34,33 @@ type Library struct { func (l *Library) String() string { // TODO: - return "" + sb := strings.Builder{} + + sb.WriteString("Persons: ") + sb.WriteString(strconv.Itoa(len(l.Persons.Array))) + sb.WriteString("\n") + + sb.WriteString("Places: ") + sb.WriteString(strconv.Itoa(len(l.Places.Array))) + sb.WriteString("\n") + + sb.WriteString("AppDefs: ") + sb.WriteString(strconv.Itoa(len(l.AppDefs.Array))) + sb.WriteString("\n") + + sb.WriteString("Letters: ") + sb.WriteString(strconv.Itoa(len(l.Letters.Array))) + sb.WriteString("\n") + + sb.WriteString("Traditions: ") + sb.WriteString(strconv.Itoa(len(l.Traditions.Array))) + sb.WriteString("\n") + + sb.WriteString("Metas: ") + sb.WriteString(strconv.Itoa(len(l.Metas.Array))) + sb.WriteString("\n") + + return sb.String() } // INFO: this is the only place where the providers are created. There is no need for locking on access. @@ -68,6 +96,7 @@ func (l *Library) Parse(source xmlparsing.ParseSource, baseDir, commit string) e err := l.Persons.Serialize(&PersonDefs{}, filepath.Join(meta.BaseDir, REFERENCES_PATH), meta) if err != nil { metamu.Lock() + slog.Error("Failed to serialize persons:", "error", err) meta.FailedPaths = append(meta.FailedPaths, filepath.Join(meta.BaseDir, REFERENCES_PATH)) metamu.Unlock() } @@ -79,6 +108,7 @@ func (l *Library) Parse(source xmlparsing.ParseSource, baseDir, commit string) e err := l.Places.Serialize(&LocationDefs{}, filepath.Join(meta.BaseDir, REFERENCES_PATH), meta) if err != nil { metamu.Lock() + slog.Error("Failed to serialize places:", "error", err) meta.FailedPaths = append(meta.FailedPaths, filepath.Join(meta.BaseDir, REFERENCES_PATH)) metamu.Unlock() } @@ -90,6 +120,7 @@ func (l *Library) Parse(source xmlparsing.ParseSource, baseDir, commit string) e err := l.AppDefs.Serialize(&AppDefs{}, filepath.Join(meta.BaseDir, REFERENCES_PATH), meta) if err != nil { metamu.Lock() + slog.Error("Failed to serialize appdefs:", "error", err) meta.FailedPaths = append(meta.FailedPaths, filepath.Join(meta.BaseDir, REFERENCES_PATH)) metamu.Unlock() } @@ -101,6 +132,7 @@ func (l *Library) Parse(source xmlparsing.ParseSource, baseDir, commit string) e err := l.Letters.Serialize(&DocumentsRoot{}, filepath.Join(meta.BaseDir, LETTERS_PATH), meta) if err != nil { metamu.Lock() + slog.Error("Failed to serialize letters:", "error", err) meta.FailedPaths = append(meta.FailedPaths, filepath.Join(meta.BaseDir, LETTERS_PATH)) metamu.Unlock() } @@ -112,6 +144,7 @@ func (l *Library) Parse(source xmlparsing.ParseSource, baseDir, commit string) e err := l.Traditions.Serialize(&TraditionsRoot{}, filepath.Join(meta.BaseDir, TRADITIONS_PATH), meta) if err != nil { metamu.Lock() + slog.Error("Failed to serialize traditions:", "error", err) meta.FailedPaths = append(meta.FailedPaths, filepath.Join(meta.BaseDir, TRADITIONS_PATH)) metamu.Unlock() } @@ -123,6 +156,7 @@ func (l *Library) Parse(source xmlparsing.ParseSource, baseDir, commit string) e err := l.Metas.Serialize(&MetaRoot{}, filepath.Join(meta.BaseDir, META_PATH), meta) if err != nil { metamu.Lock() + slog.Error("Failed to serialize meta:", "error", err) meta.FailedPaths = append(meta.FailedPaths, filepath.Join(meta.BaseDir, META_PATH)) metamu.Unlock() } diff --git a/xmlmodels/roots.go b/xmlmodels/roots.go index 213e20a..243edd8 100644 --- a/xmlmodels/roots.go +++ b/xmlmodels/roots.go @@ -4,22 +4,16 @@ import "encoding/xml" type MetaRoot struct { XMLName xml.Name `xml:"opus"` - Metas []Meta `xml:"letterDesc"` + Metas []Meta `xml:"descriptions>letterDesc"` } func (m MetaRoot) Children() []Meta { return m.Metas } -type DefinitionsRoot struct { - XMLName xml.Name `xml:"definitions"` - Persons PersonDefs `xml:"personDefs"` - Locations LocationDefs `xml:"locationDefs"` - Apparatuses AppDefs `xml:"appDefs"` -} - type PersonDefs struct { - Persons []PersonDef `xml:"personDef"` + xml.Name `xml:"opus"` + Persons []PersonDef `xml:"definitions>personDefs>personDef"` } func (p PersonDefs) Children() []PersonDef { @@ -27,7 +21,8 @@ func (p PersonDefs) Children() []PersonDef { } type LocationDefs struct { - Locations []LocationDef `xml:"locationDef"` + xml.Name `xml:"opus"` + Locations []LocationDef `xml:"definitions>locationDefs>locationDef"` } func (l LocationDefs) Children() []LocationDef { @@ -35,7 +30,8 @@ func (l LocationDefs) Children() []LocationDef { } type AppDefs struct { - Apps []AppDef `xml:"appDef"` + xml.Name `xml:"opus"` + Apps []AppDef `xml:"definitions>appDefs>appDef"` } func (a AppDefs) Children() []AppDef { @@ -43,8 +39,8 @@ func (a AppDefs) Children() []AppDef { } type TraditionsRoot struct { - XMLName xml.Name `xml:"traditions"` - Traditions []Tradition `xml:"tradition"` + xml.Name `xml:"opus"` + Traditions []Tradition `xml:"traditions>letterTradition"` } func (t TraditionsRoot) Children() []Tradition { @@ -52,10 +48,10 @@ func (t TraditionsRoot) Children() []Tradition { } type DocumentsRoot struct { - XMLName xml.Name `xml:"document"` - Documents []Letter `xml:"letterText"` + xml.Name `xml:"opus"` + Document []Letter `xml:"document>letterText"` } func (d DocumentsRoot) Children() []Letter { - return d.Documents + return d.Document }