summaryrefslogtreecommitdiffstats
path: root/lib/xdsconfig/foldersconfig.go
blob: 4ad16dfa674e08d6dedd670b4422251802f4c6d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package xdsconfig

import (
	"fmt"
)

// FoldersConfig contains all the folder configurations
type FoldersConfig []FolderConfig

// GetIdx returns the index of the folder matching id in FoldersConfig array
func (c FoldersConfig) GetIdx(id string) int {
	for i := range c {
		if id == c[i].ID {
			return i
		}
	}
	return -1
}

// Update is used to fully update or add a new FolderConfig
func (c FoldersConfig) Update(newCfg FoldersConfig) FoldersConfig {
	for i := range newCfg {
		found := false
		for j := range c {
			if newCfg[i].ID == c[j].ID {
				c[j] = newCfg[i]
				found = true
				break
			}
		}
		if !found {
			c = append(c, newCfg[i])
		}
	}
	return c
}

// Delete is used to delete a folder matching id in FoldersConfig array
func (c FoldersConfig) Delete(id string) (FoldersConfig, FolderConfig, error) {
	if idx := c.GetIdx(id); idx != -1 {
		f := c[idx]
		c = append(c[:idx], c[idx+1:]...)
		return c, f, nil
	}

	return c, FolderConfig{}, fmt.Errorf("invalid id")
}