zek
zek copied to clipboard
added named struct extraction flag
Hello!
Here's my suggestion for a flag allowing non-nested structs, discussed in #14 . The diff is a bit noisy, but essentially it takes in a slice of names in the flag, then the structwriter check if the node that is about to be written as an anonymous struct is contained in this slice, and just writes the capitalized version of the name instead.
Let me know what you think!
Thanks for the PR and sorry for the long response time - I'll look at it shortly.
I think better create flag with name "inline" that by default will be true like now;
Like in this solution JSON-to-Go it is called "Inline type definitions";
@miku you agree with "inline" flag?
Hi!
Sorry for the (very long!) delay,
@YaroslavPodorvanov 's suggestion makes sense, happy to change the PR to implement it.
That being said, the current PR allows for the extraction of just specific structs. If we change it to a boolean flag, that means that it either inlines all structs, or outlines (?) all structs, with no in-between or granular control.
I could imagine having both flags existing side by side, e.g.:
- if
inlineStructs
is true andreplaceStructs
is empty: default behaviour, everything inlined - if
inlineStructs
is true andreplaceStructs
is not empty: extracts the structs specified in thereplaceStructs
- if
inlineStructs
is false andreplaceStructs
is empty: extracts all structs - if
inlineStructs
is false andreplaceStructs
is not empty: extracts only the struct
But maybe I'm making it more complicated than it should be, so perhaps we should go for the boolean flag.
Any thoughts?
@periode make sense with tests
Thanks for looking into this. With the -r
(and the existing -t
) flag it would be possible to do the following now:
$ curl -s https://raw.githubusercontent.com/miku/zek/master/fixtures/e.xml | ./zek -e -t channel
// Channel was generated 2024-01-02 21:48:18 by tir on reka.
type Channel struct {
XMLName xml.Name `xml:"channel"`
Text string `xml:",chardata"`
Title string `xml:"title"` // ESS New Releases (Display...
Link string `xml:"link"` // http://tinyurl.com/ESSNew...
Description string `xml:"description"` // New releases from the Ear...
LastBuildDate string `xml:"lastBuildDate"` // Mon, 27 Nov 2017 00:06:35...
Item []struct {
Text string `xml:",chardata"`
...
Readme string `xml:"readme"` // readme | https://geoscan....
PPIid string `xml:"PPIid"` // 34532, 35096, 35438, 2563...
} `xml:"item"`
}
$ curl -s https://raw.githubusercontent.com/miku/zek/master/fixtures/e.xml | ./zek -e -r Channel
// Rss was generated 2024-01-02 21:47:53 by tir on reka.
type Rss struct {
XMLName xml.Name `xml:"rss"`
Text string `xml:",chardata"`
Rdf string `xml:"rdf,attr"`
Dc string `xml:"dc,attr"`
Geoscan string `xml:"geoscan,attr"`
Media string `xml:"media,attr"`
Gml string `xml:"gml,attr"`
Taxo string `xml:"taxo,attr"`
Georss string `xml:"georss,attr"`
Content string `xml:"content,attr"`
Geo string `xml:"geo,attr"`
Version string `xml:"version,attr"`
Channel Channel `xml:"channel"`
}
But maybe I'm making it more complicated than it should be, so perhaps we should go for the boolean flag.
I believe there are two things you want to do: 1) read XML only, 2) read and write XML.
If you only read XML, then inline is nicer to read in code as it's reducing jumps and somewhat reveals the XML tree in the struct itself (I like the compact output from zek more than some of the other generators I tried).
If you want to read and write XML, you probably need all structs expanded anyway, as you need to build the XML yourself.
Therefore, I'd favor a single boolean flag to expand all nested structs.
@miku yes, my use case is to manipulate the structs from the XML, so read/write.
I guess I was trying to have the best of both worlds by only extracting the structs I need to process, and keep the rest for readability, but for now the boolean flag to expand nested structs instead of inlining them should do.
It would be something like this:
cat "<a><b>Berlin</b></a>" | ./zek -I
// A was as generated 2024-01-03 18:47:20 by pierre on archpierre.
type A struct {
XMLName xml.Name `xml:"a"`
Text string `xml:",chardata"`
B B `xml:"b"`
}
type B struct {
Text string `xml:",chardata"` // Berlin
}