aboutsummaryrefslogtreecommitdiffstats
path: root/formats/config/section.go
blob: 6d6a891b57bb63845c2447fb379a60e107107816 (plain) (blame)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package config

import "strings"

type Section struct {
	Name        string
	Options     Options
	Subsections Subsections
}

type Subsection struct {
	Name    string
	Options Options
}

type Sections []*Section

type Subsections []*Subsection

func (s *Section) IsName(name string) bool {
	return strings.ToLower(s.Name) == strings.ToLower(name)
}

func (s *Subsection) IsName(name string) bool {
	return s.Name == name
}

func (s *Section) Option(key string) string {
	return s.Options.Get(key)
}

func (s *Subsection) Option(key string) string {
	return s.Options.Get(key)
}

func (s *Section) AddOption(key string, value string) *Section {
	s.Options = s.Options.withAddedOption(key, value)
	return s
}

func (s *Subsection) AddOption(key string, value string) *Subsection {
	s.Options = s.Options.withAddedOption(key, value)
	return s
}

func (s *Section) SetOption(key string, value string) *Section {
	s.Options = s.Options.withSettedOption(key, value)
	return s
}

func (s *Section) RemoveOption(key string) *Section {
	s.Options = s.Options.withoutOption(key)
	return s
}

func (s *Subsection) SetOption(key string, value string) *Subsection {
	s.Options = s.Options.withSettedOption(key, value)
	return s
}

func (s *Subsection) RemoveOption(key string) *Subsection {
	s.Options = s.Options.withoutOption(key)
	return s
}

func (s *Section) Subsection(name string) *Subsection {
	for i := len(s.Subsections) - 1; i >= 0; i-- {
		ss := s.Subsections[i]
		if ss.IsName(name) {
			return ss
		}
	}
	ss := &Subsection{Name: name}
	s.Subsections = append(s.Subsections, ss)
	return ss
}