Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Optionally generate non-nested structs #14

Open
probonopd opened this issue Nov 16, 2019 · 8 comments
Open

Feature request: Optionally generate non-nested structs #14

probonopd opened this issue Nov 16, 2019 · 8 comments

Comments

@probonopd
Copy link

Right now zek generates nested structs, with one struct being nested in another.

The downside of this is that it is (apparently) not possible to attach methods to the nested struct.

Hence the question, can zek be extended to optionally generate non-nested structs?

Real-life code example:
https://github.com/probonopd/go-scribus/blob/48984ecccda9be0d30a4e7cb5be50670520f7dd2/scribus.go#L740-L748

@probonopd
Copy link
Author

Semi-manual workaround:

# Get names of all structs

STRUCTS=$(/home/me/go/bin/zek /home/me/Document-3.sla  | grep struct | sed -e 's|\t||g' | cut -d " " -f 1)
array=$(echo $STRUCTS | tr "\n" "\n")

# Generate one struct for each name

for STRUCT in $array ; do
  /home/me/go/bin/zek -t "$STRUCT" /home/me/Document-3.sla >> generated.go
  echo "" >> generated.go
done

# Then, by hand, delete the structs that are nested in other structs

@miku
Copy link
Owner

miku commented Nov 17, 2019

Thanks for reporting this issue (and for documenting a workaround).

This is a trade-off, currently - where the emphasis is on reading XML (as opposed to generating it) - and also readability: the complete structure is mirrored in a single struct. It would be nice to have an option to generate separate structs; although I cannot work on it ATM (zek is a prototype and works ok for the use case it covers). I'll leave this issue open, just in case I find time to address this issue.

@NickLarsenNZ
Copy link

I would really like this feature too. Mainly because much of the hierarchy contains stuff I don't need, and I have functions that return a type that is deeply nested (for now I've manually pulled out the sections I need and turned them into named types).

@miku
Copy link
Owner

miku commented Jul 17, 2020

@NickLarsenNZ, thanks for the feedback. Cut and paste is the way to go at the moment, I'm afraid. But as time permits, I'll look into it.

@periode
Copy link

periode commented Mar 21, 2022

Hi @miku , thanks for the package! Would definitely find it great as well to extract nested structs to their own types.

Thanks!

@miku
Copy link
Owner

miku commented Mar 21, 2022

@periode, noted. I'll put this next on my list of improvements for zek.

@periode
Copy link

periode commented Mar 29, 2022

Also happy to offer a PR if you point me in the right direction!

@YaroslavPodorvanov
Copy link
Contributor

In JSON-to-Go it is called "Inline type definitions";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants