Skip to content

Commit 3a42f11

Browse files
committed
add support for innerxml
1 parent fe6f51e commit 3a42f11

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

cmd/zek/main.go

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ var (
3636
packageName = flag.String("P", "", "if set, write out struct within a package with the given name")
3737
fixedBanner = flag.Bool("B", false, "use a fixed banner string (e.g. for CI)")
3838
readAtMost = flag.Int64("S", 0, "read at most this many tags, approximately (0=unlimited)")
39+
useInnerXML = flag.Bool("I", false, "use verbatim innerxml instead of chardata")
3940
)
4041

4142
func main() {
@@ -106,6 +107,7 @@ func main() {
106107
sw.Compact = !*nonCompact
107108
sw.UniqueExamples = *uniqueExamples
108109
sw.OmitEmptyText = *omitEmptyText
110+
sw.UseInnerXML = *useInnerXML
109111
if *fixedBanner {
110112
sw.Banner = fmt.Sprintf(`generated automatically by zek %s. DO NOT EDIT.`, zek.Version)
111113
}

structwriter.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ type StructWriter struct {
127127
Compact bool // Emit more compact struct.
128128
UniqueExamples bool // Filter out duplicated examples
129129
OmitEmptyText bool // Don't generate Text fields if no example elements have chardata.
130+
UseInnerXML bool // Use innerxml instead of chardata for text.
130131
}
131132

132133
// NewStructWriter returns a StructWriter that can write a node to a given
@@ -206,11 +207,15 @@ func (sw *StructWriter) writeChardataField(w io.Writer, node *Node) (int, error)
206207
if !isValidName(textFieldName) {
207208
return 0, fmt.Errorf("name clash, text field")
208209
}
210+
tag := "chardata"
211+
if sw.UseInnerXML {
212+
tag = "innerxml"
213+
}
209214
if sw.WithJSONTags {
210-
s = fmt.Sprintf("%s string `xml:\",chardata\" json:\"%s,omitempty\"`",
211-
textFieldName, strings.ToLower(textFieldName))
215+
s = fmt.Sprintf("%s string `xml:\",%s\" json:\"%s,omitempty\"`",
216+
textFieldName, tag, strings.ToLower(textFieldName))
212217
} else {
213-
s = fmt.Sprintf("%s string `xml:\",chardata\"`", textFieldName)
218+
s = fmt.Sprintf("%s string `xml:\",%s\"`", textFieldName, tag)
214219
}
215220
if sw.UniqueExamples {
216221
node.Examples = uniqueStrings(node.Examples)

0 commit comments

Comments
 (0)