1
// Copyright 2014 Unknwon
3
// Licensed under the Apache License, Version 2.0 (the "License"): you may
4
// not use this file except in compliance with the License. You may obtain
5
// a copy of the License at
7
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
// License for the specific language governing permissions and limitations
23
// Section represents a config section.
30
keysHash map[string]string
33
func newSection(f *File, name string) *Section {
34
return &Section{f, "", name, make(map[string]*Key), make([]string, 0, 10), make(map[string]string)}
37
// Name returns name of Section.
38
func (s *Section) Name() string {
42
// NewKey creates a new key to given section.
43
func (s *Section) NewKey(name, val string) (*Key, error) {
45
return nil, errors.New("error creating new key: empty key name")
50
defer s.f.lock.Unlock()
53
if inSlice(name, s.keyList) {
54
s.keys[name].value = val
55
return s.keys[name], nil
58
s.keyList = append(s.keyList, name)
59
s.keys[name] = &Key{s, "", name, val, false}
60
s.keysHash[name] = val
61
return s.keys[name], nil
64
// GetKey returns key in section by given name.
65
func (s *Section) GetKey(name string) (*Key, error) {
66
// FIXME: change to section level lock?
76
// Check if it is a child-section.
79
if i := strings.LastIndex(sname, "."); i > -1 {
81
sec, err := s.f.GetSection(sname)
85
return sec.GetKey(name)
90
return nil, fmt.Errorf("error when getting key of section '%s': key '%s' not exists", s.name, name)
95
// HasKey returns true if section contains a key with given name.
96
func (s *Section) HasKey(name string) bool {
97
key, _ := s.GetKey(name)
101
// Haskey is a backwards-compatible name for HasKey.
102
func (s *Section) Haskey(name string) bool {
103
return s.HasKey(name)
106
// HasValue returns true if section contains given raw value.
107
func (s *Section) HasValue(value string) bool {
110
defer s.f.lock.RUnlock()
113
for _, k := range s.keys {
114
if value == k.value {
121
// Key assumes named Key exists in section and returns a zero-value when not.
122
func (s *Section) Key(name string) *Key {
123
key, err := s.GetKey(name)
125
// It's OK here because the only possible error is empty key name,
126
// but if it's empty, this piece of code won't be executed.
127
key, _ = s.NewKey(name, "")
133
// Keys returns list of keys of section.
134
func (s *Section) Keys() []*Key {
135
keys := make([]*Key, len(s.keyList))
136
for i := range s.keyList {
137
keys[i] = s.Key(s.keyList[i])
142
// KeyStrings returns list of key names of section.
143
func (s *Section) KeyStrings() []string {
144
list := make([]string, len(s.keyList))
145
copy(list, s.keyList)
149
// KeysHash returns keys hash consisting of names and values.
150
func (s *Section) KeysHash() map[string]string {
153
defer s.f.lock.RUnlock()
156
hash := map[string]string{}
157
for key, value := range s.keysHash {
163
// DeleteKey deletes a key from section.
164
func (s *Section) DeleteKey(name string) {
167
defer s.f.lock.Unlock()
170
for i, k := range s.keyList {
172
s.keyList = append(s.keyList[:i], s.keyList[i+1:]...)