From 4603dc00bc05f4104719f64695f2207e48b25eb5 Mon Sep 17 00:00:00 2001 From: Konstantin Zhernosenko Date: Fri, 19 Feb 2021 19:07:43 +0300 Subject: [PATCH] Nesting level initialisation for root children is fixed (#8) Co-authored-by: Konstantin Zhernosenko --- lib/tree/tree.go | 14 +++++++++++--- lib/tree/yaml.go | 6 ------ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/tree/tree.go b/lib/tree/tree.go index 988d8ac..0989dbe 100644 --- a/lib/tree/tree.go +++ b/lib/tree/tree.go @@ -60,7 +60,7 @@ func NewSubTree(name, parentFullKey string) *Tree { Content: make(map[string]Marshalable), Name: name, FullKey: MakeFullKey(parentFullKey, name), - nestingLevel: strings.Count(parentFullKey, sep) + 2, + nestingLevel: initNestingLevel(parentFullKey), } } @@ -69,7 +69,7 @@ func NewBranch(name, parentFullKey string) *Branch { Content: []Marshalable{}, Name: name, FullKey: MakeFullKey(parentFullKey, name), - nestingLevel: strings.Count(parentFullKey, sep) + 2, + nestingLevel: initNestingLevel(parentFullKey), } } @@ -77,7 +77,7 @@ func NewLeaf(name, parentFullKey string) *Leaf { return &Leaf{ Name: name, FullKey: MakeFullKey(parentFullKey, name), - nestingLevel: strings.Count(parentFullKey, sep) + 2, + nestingLevel: initNestingLevel(parentFullKey), } } @@ -477,6 +477,14 @@ func (ml *Leaf) GetNestingLevel() int { return ml.nestingLevel } +func initNestingLevel(parentFullKey string) int { + if len(parentFullKey) == 0 { + return 1 + } + + return strings.Count(parentFullKey, sep) + 2 +} + func (mt *Tree) clearValues() { if len(mt.Content) > 0 { mt.Content = make(map[string]Marshalable) diff --git a/lib/tree/yaml.go b/lib/tree/yaml.go index b0c6c72..7e6b16f 100644 --- a/lib/tree/yaml.go +++ b/lib/tree/yaml.go @@ -110,12 +110,6 @@ func (ml *Leaf) UnmarshalYAML(node *yaml.Node) error { switch node.Kind { case yaml.ScalarNode: ml.Value = node.Value - case yaml.SequenceNode: - leafValue, err := yaml.Marshal(node) - if err != nil { - return fmt.Errorf("marshal sequence %q: %w", ml.FullKey, err) - } - ml.Value = string(leafValue) default: return fmt.Errorf("unprocessable content type `%v` for leaf %q", node.Kind, ml.FullKey) }