Skip to content

Commit

Permalink
Nesting level initialisation for root children is fixed (#8)
Browse files Browse the repository at this point in the history
Co-authored-by: Konstantin Zhernosenko <konstantin.zhernosenko@humans.net>
  • Loading branch information
kainobor and Konstantin Zhernosenko authored Feb 19, 2021
1 parent 71d1d1f commit 4603dc0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
14 changes: 11 additions & 3 deletions lib/tree/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand All @@ -69,15 +69,15 @@ func NewBranch(name, parentFullKey string) *Branch {
Content: []Marshalable{},
Name: name,
FullKey: MakeFullKey(parentFullKey, name),
nestingLevel: strings.Count(parentFullKey, sep) + 2,
nestingLevel: initNestingLevel(parentFullKey),
}
}

func NewLeaf(name, parentFullKey string) *Leaf {
return &Leaf{
Name: name,
FullKey: MakeFullKey(parentFullKey, name),
nestingLevel: strings.Count(parentFullKey, sep) + 2,
nestingLevel: initNestingLevel(parentFullKey),
}
}

Expand Down Expand Up @@ -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)
Expand Down
6 changes: 0 additions & 6 deletions lib/tree/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 4603dc0

Please sign in to comment.