Skip to content

Commit

Permalink
BE-2593: IsEmpty method is added and deletion now working as recursiv…
Browse files Browse the repository at this point in the history
…e. (#9)

Co-authored-by: Konstantin Zhernosenko <konstantin.zhernosenko@humans.net>
  • Loading branch information
kainobor and Konstantin Zhernosenko authored Apr 1, 2021
1 parent 4603dc0 commit d0330ab
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions lib/tree/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Marshalable interface {
GetName() string
GetNestingLevel() int
Delete(string) error
IsEmpty() bool
}

type Tree struct {
Expand Down Expand Up @@ -227,7 +228,12 @@ func (mt *Tree) Delete(fullKey string) error {
continue
}
if !isFinal {
return v.Delete(fullKey)
if err := v.Delete(fullKey); err != nil {
return err
}
if !v.IsEmpty() {
return nil
}
}
deletedName = v.GetName()
delete(mt.Content, deletedName)
Expand Down Expand Up @@ -273,7 +279,12 @@ func (mb *Branch) Delete(fullKey string) error {
}

if !isFinal {
return mb.Content[searchIdx].Delete(fullKey)
if err := mb.Content[searchIdx].Delete(fullKey); err != nil {
return err
}
if !mb.Content[searchIdx].IsEmpty() {
return nil
}
}

copy(mb.Content[searchIdx:], mb.Content[searchIdx+1:]) // Shift a[i+1:] left one index
Expand All @@ -283,8 +294,13 @@ func (mb *Branch) Delete(fullKey string) error {
return nil
}

func (ml *Leaf) Delete(_ string) error {
return ErrorUnsupported
func (ml *Leaf) Delete(fullKey string) error {
if ml.FullKey != fullKey {
return fmt.Errorf("incorrect key for leaf deletion: %q", fullKey)
}
ml.Value = nil

return nil
}

func (mt *Tree) AddOrReplaceDirectly(name string, value Marshalable) {
Expand Down Expand Up @@ -405,10 +421,6 @@ func (ml *Leaf) DeepClone() *Leaf {
}
}

func (mt *Tree) IsEmpty() bool {
return len(mt.Order) == 0
}

func (ml *Leaf) SetYamlMarshalStyle(s yaml.Style) {
ml.yamlMarshalStyle = s
}
Expand Down Expand Up @@ -453,6 +465,10 @@ func (mb *Branch) GetNestingLevel() int {
return mb.nestingLevel
}

func (mb *Branch) IsEmpty() bool {
return len(mb.Content) == 0
}

func (mt *Tree) GetName() string {
return mt.Name
}
Expand All @@ -465,6 +481,10 @@ func (mt *Tree) GetNestingLevel() int {
return mt.nestingLevel
}

func (mt *Tree) IsEmpty() bool {
return len(mt.Content) == 0
}

func (ml *Leaf) GetName() string {
return ml.Name
}
Expand All @@ -477,6 +497,10 @@ func (ml *Leaf) GetNestingLevel() int {
return ml.nestingLevel
}

func (ml *Leaf) IsEmpty() bool {
return ml.Value == nil
}

func initNestingLevel(parentFullKey string) int {
if len(parentFullKey) == 0 {
return 1
Expand Down

0 comments on commit d0330ab

Please sign in to comment.