From 209b1dafb9e6f0a58a9cb5f1e6368b6667c88fb9 Mon Sep 17 00:00:00 2001 From: Zachary Scott Date: Thu, 21 Jun 2018 00:48:37 +0900 Subject: [PATCH] Add docs and re-organize functions to their respective places. This was simpler than refactoring any further. --- filetree/filetree.go | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/filetree/filetree.go b/filetree/filetree.go index 5a421e1ee..ab2dd0967 100644 --- a/filetree/filetree.go +++ b/filetree/filetree.go @@ -12,6 +12,26 @@ import ( yaml "gopkg.in/yaml.v2" ) +// This is a quick hack of a function to convert interfaces +// into to map[string]interface{} and combine them. +func mergeTree(trees ...interface{}) map[string]interface{} { + result := make(map[string]interface{}) + for _, tree := range trees { + kvp := make(map[string]interface{}) + if err := mapstructure.Decode(tree, &kvp); err != nil { + panic(err) + } + for k, v := range kvp { + result[k] = v + } + } + return result +} + +// SpecialCase is a function you can pass to NewTree +// in order to override the behavior when marshalling a node. +var SpecialCase func(path string) bool + // Node represents a leaf in the filetree type Node struct { FullPath string `json:"full_path"` @@ -20,7 +40,7 @@ type Node struct { Parent *Node `json:"-"` } -// MarshalYaml serializes the tree into YAML +// MarshalYAML serializes the tree into YAML func (n Node) MarshalYAML() (interface{}, error) { if len(n.Children) == 0 { return n.marshalLeaf() @@ -40,22 +60,11 @@ func (n Node) rootFile() bool { return n.Info.Mode().IsRegular() && n.root() == n.Parent } -func mergeTree(trees ...interface{}) map[string]interface{} { - result := make(map[string]interface{}) - for _, tree := range trees { - kvp := make(map[string]interface{}) - if err := mapstructure.Decode(tree, &kvp); err != nil { - panic(err) - } - for k, v := range kvp { - result[k] = v - } - } - return result +func (n Node) notYaml() bool { + re := regexp.MustCompile(`.+\.(yml|yaml)$`) + return !re.MatchString(n.FullPath) } -var SpecialCase func(path string) bool - func (n Node) marshalParent() (interface{}, error) { tree := map[string]interface{}{} for _, child := range n.Children { @@ -109,11 +118,6 @@ func (n Node) marshalLeaf() (interface{}, error) { return content, err } -func (n Node) notYaml() bool { - re := regexp.MustCompile(`.+\.(yml|yaml)$`) - return !re.MatchString(n.FullPath) -} - func dotfile(path string) bool { re := regexp.MustCompile(`^\..+`) return re.MatchString(path)