Skip to content

Commit

Permalink
Add flag -u to make examples unique
Browse files Browse the repository at this point in the history
  • Loading branch information
matbur committed Nov 22, 2018
1 parent efc50bf commit 876cc1a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/zek/main.go
Expand Up @@ -27,6 +27,7 @@ var (
version = flag.Bool("version", false, "show version")
structName = flag.String("n", "", "use a different name for the top-level struct")
compact = flag.Bool("c", false, "emit more compact struct")
uniqueExamples = flag.Bool("u", false, "filter out duplicated examples")
)

func main() {
Expand Down Expand Up @@ -80,6 +81,7 @@ func main() {
sw.Strict = *strict
sw.ExampleMaxChars = *exampleMaxChars
sw.Compact = *compact
sw.UniqueExamples = *uniqueExamples

if err := sw.WriteNode(root); err != nil {
log.Fatal(err)
Expand Down
22 changes: 22 additions & 0 deletions structwriter.go
Expand Up @@ -90,6 +90,7 @@ type StructWriter struct {
Strict bool // Whether to ignore implementation holes.
WithJSONTags bool // Include JSON struct tags.
Compact bool // Emit more compact struct.
UniqueExamples bool // Filter out duplicated examples
}

// NewStructWriter can write a node to a given writer. Default list of
Expand Down Expand Up @@ -176,6 +177,11 @@ func (sw *StructWriter) writeChardataField(w io.Writer, node *Node) (int, error)
} else {
s = fmt.Sprintf("%s string `xml:\",chardata\"`", textFieldName)
}

if sw.UniqueExamples {
node.Examples = uniqueStrings(node.Examples)
}

if sw.WithComments && len(node.Examples) > 0 {
examples := strings.Replace(strings.Join(node.Examples, ", "), "\n", " ", -1)
s = fmt.Sprintf("%s // %s", s, truncateString(examples, sw.ExampleMaxChars, "..."))
Expand Down Expand Up @@ -215,6 +221,10 @@ func (sw *StructWriter) writeNode(node *Node, top bool) (err error) {
io.WriteString(sew, "[]")
}

if sw.UniqueExamples {
node.Examples = uniqueStrings(node.Examples)
}

if sw.Compact && len(node.Children) == 0 && len(node.Attr) == 0 {
s := fmt.Sprintf("string `xml:\"%s\"`", node.Name.Local)
if sw.WithComments && len(node.Examples) > 0 {
Expand Down Expand Up @@ -282,3 +292,15 @@ func (sw *StructWriter) writeNode(node *Node, top bool) (err error) {
io.WriteString(sew, "\n")
return err
}

func uniqueStrings(ss []string) []string {
uniq := make([]string, 0, len(ss))
m := map[string]struct{}{}
for _, s := range ss {
if _, ok := m[s]; !ok {
uniq = append(uniq, s)
m[s] = struct{}{}
}
}
return uniq
}

0 comments on commit 876cc1a

Please sign in to comment.