-
Notifications
You must be signed in to change notification settings - Fork 0
/
section.go
69 lines (57 loc) · 1.87 KB
/
section.go
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
// Copyright 2014 Charles Gentry All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gofig
import (
"strings"
// "fmt"
)
func conformSectionName( sectionName string ) string {
return strings.TrimSpace( sectionName )
}
// IsSection will return true if a section name exists in the map
func (config *Configuration ) IsSection( sectionName string ) bool {
_ , found := config.ConfigMap[sectionName]
return found
}
// GetSection will return a map for a given section and true or return nil and false
func (config *Configuration) GetSection(sectionName string) (ConfigOption, bool) {
mm, found := config.ConfigMap[conformSectionName( sectionName )]
return mm, found
}
// AddSection will return a map for a given section. If the section
// doesn't exist, it will be created
func (config *Configuration) AddSection(sectionName string) ConfigOption {
sectionName = conformSectionName( sectionName )
mm, ok := config.ConfigMap[sectionName]
if !ok {
mm = make(ConfigOption, 10)
config.ConfigMap[sectionName] = mm
config.Sections++
}
return mm
}
// DeleteSection Delete all of the entries in a section. If the map doesn't exist, ignore it
func (config *Configuration) DeleteSection( sectionName string ) *Configuration {
sectionName = conformSectionName( sectionName )
if _ , found := config.ConfigMap[sectionName] ; found {
for opt,_ := range config.ConfigMap[sectionName] {
delete( config.ConfigMap[sectionName] , opt )
}
delete( config.ConfigMap , sectionName)
config.Sections--
}
return config
}
// GetSectionNames will return a complete list of all of the sections defined
func (config *Configuration) GetSectionNames() []string {
names := make( []string , len(config.ConfigMap ) )
i := 0
for name := range config.ConfigMap {
if name != "" {
names[i] = name
i++
}
}
return names
}