Skip to content

Commit 6fbf0d0

Browse files
committed
add -c for more compact representation
1 parent de13c56 commit 6fbf0d0

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

cmd/zek/main.go

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var (
2626
exampleMaxChars = flag.Int("x", 25, "max chars for example")
2727
version = flag.Bool("version", false, "show version")
2828
structName = flag.String("n", "", "use a different name for the top-level struct")
29+
compact = flag.Bool("c", false, "emit more compact struct")
2930
)
3031

3132
func main() {
@@ -78,6 +79,7 @@ func main() {
7879
sw.WithJSONTags = *withJSONTags
7980
sw.Strict = *strict
8081
sw.ExampleMaxChars = *exampleMaxChars
82+
sw.Compact = *compact
8183

8284
if err := sw.WriteNode(root); err != nil {
8385
log.Fatal(err)
@@ -117,6 +119,7 @@ func main() {
117119
sw.WithComments = *withComments
118120
sw.Strict = *strict
119121
sw.ExampleMaxChars = *exampleMaxChars
122+
sw.Compact = *compact
120123

121124
if err := sw.WriteNode(root); err != nil {
122125
log.Fatal(err)

structwriter.go

+7
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ type StructWriter struct {
8989
ExampleMaxChars int // Max length of example comment.
9090
Strict bool // Whether to ignore implementation holes.
9191
WithJSONTags bool // Include JSON struct tags.
92+
Compact bool // Emit more compact struct.
9293
}
9394

9495
// NewStructWriter can write a node to a given writer. Default list of
@@ -212,6 +213,12 @@ func (sw *StructWriter) writeNode(node *Node, top bool) (err error) {
212213
if node.IsMultivalued() && !top {
213214
io.WriteString(sew, "[]")
214215
}
216+
217+
if sw.Compact && len(node.Children) == 0 && len(node.Attr) == 0 {
218+
fmt.Fprintf(sew, "string `xml:\"%s\"`\n", node.Name.Local)
219+
return nil
220+
}
221+
215222
io.WriteString(sew, "struct {\n")
216223
if top {
217224
sw.writeNameField(sew, node)

0 commit comments

Comments
 (0)