zek icon indicating copy to clipboard operation
zek copied to clipboard

Feature request: Optionally generate non-nested structs

Open probonopd opened this issue 5 years ago • 8 comments

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 avatar Nov 16 '19 17:11 probonopd

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

probonopd avatar Nov 16 '19 18:11 probonopd

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.

miku avatar Nov 17 '19 21:11 miku

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).

NickLarsenNZ avatar Jul 17 '20 07:07 NickLarsenNZ

@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.

miku avatar Jul 17 '20 07:07 miku

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

Thanks!

periode avatar Mar 21 '22 12:03 periode

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

miku avatar Mar 21 '22 13:03 miku

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

periode avatar Mar 29 '22 15:03 periode

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

YaroslavPodorvanov avatar Aug 23 '22 18:08 YaroslavPodorvanov