diff options
author | xengineering <me@xengineering.eu> | 2023-07-04 16:26:04 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2023-07-04 16:26:04 +0200 |
commit | 7902764e053eb3b8b4d46f8d9caf47d2d5cddd7c (patch) | |
tree | 67bbf5aac4f91406220d2aa5136d6f52799b7b87 | |
parent | 3a5e9fce02264eeed884b15781593479fda9296a (diff) | |
download | limox-7902764e053eb3b8b4d46f8d9caf47d2d5cddd7c.tar limox-7902764e053eb3b8b4d46f8d9caf47d2d5cddd7c.tar.zst limox-7902764e053eb3b8b4d46f8d9caf47d2d5cddd7c.zip |
Implement XML element parsing with Generic
This helps to avoid duplicated code when new XML elements are described
as custom structs.
-rw-r--r-- | xmpp/routing.go | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/xmpp/routing.go b/xmpp/routing.go index e8aa4ed..2b1680f 100644 --- a/xmpp/routing.go +++ b/xmpp/routing.go @@ -8,14 +8,17 @@ import ( func route(s *xml.StartElement, d *xml.Decoder, c chan<- any) { switch (*s).Name { case xml.Name{`http://etherx.jabber.org/streams`, `features`}: - data := streamFeatures{} - err := d.DecodeElement(&data, s) - if err != nil { - log.Printf("Could not decode stream features: %v\n", err) - } else { - log.Print(data) // TODO - } + parse(streamFeatures{}, s, d, c) default: d.Skip() } } + +func parse[T any](data T, s *xml.StartElement, d *xml.Decoder, c chan<- any) { + err := d.DecodeElement(&data, s) + if err != nil { + log.Printf("Could not decode stream features: %v\n", err) + } else { + c <- data + } +} |