vendor/gopkg.in/ini.v1/section.go
changeset 256 6d9efbef00a9
parent 251 1c52a0eeb952
child 260 445e01aede7e
equal deleted inserted replaced
255:4f153a23adab 256:6d9efbef00a9
    64 
    64 
    65 // NewKey creates a new key to given section.
    65 // NewKey creates a new key to given section.
    66 func (s *Section) NewKey(name, val string) (*Key, error) {
    66 func (s *Section) NewKey(name, val string) (*Key, error) {
    67 	if len(name) == 0 {
    67 	if len(name) == 0 {
    68 		return nil, errors.New("error creating new key: empty key name")
    68 		return nil, errors.New("error creating new key: empty key name")
    69 	} else if s.f.options.Insensitive {
    69 	} else if s.f.options.Insensitive || s.f.options.InsensitiveKeys {
    70 		name = strings.ToLower(name)
    70 		name = strings.ToLower(name)
    71 	}
    71 	}
    72 
    72 
    73 	if s.f.BlockMode {
    73 	if s.f.BlockMode {
    74 		s.f.lock.Lock()
    74 		s.f.lock.Lock()
   107 // GetKey returns key in section by given name.
   107 // GetKey returns key in section by given name.
   108 func (s *Section) GetKey(name string) (*Key, error) {
   108 func (s *Section) GetKey(name string) (*Key, error) {
   109 	if s.f.BlockMode {
   109 	if s.f.BlockMode {
   110 		s.f.lock.RLock()
   110 		s.f.lock.RLock()
   111 	}
   111 	}
   112 	if s.f.options.Insensitive {
   112 	if s.f.options.Insensitive || s.f.options.InsensitiveKeys {
   113 		name = strings.ToLower(name)
   113 		name = strings.ToLower(name)
   114 	}
   114 	}
   115 	key := s.keys[name]
   115 	key := s.keys[name]
   116 	if s.f.BlockMode {
   116 	if s.f.BlockMode {
   117 		s.f.lock.RUnlock()
   117 		s.f.lock.RUnlock()
   119 
   119 
   120 	if key == nil {
   120 	if key == nil {
   121 		// Check if it is a child-section.
   121 		// Check if it is a child-section.
   122 		sname := s.name
   122 		sname := s.name
   123 		for {
   123 		for {
   124 			if i := strings.LastIndex(sname, "."); i > -1 {
   124 			if i := strings.LastIndex(sname, s.f.options.ChildSectionDelimiter); i > -1 {
   125 				sname = sname[:i]
   125 				sname = sname[:i]
   126 				sec, err := s.f.GetSection(sname)
   126 				sec, err := s.f.GetSection(sname)
   127 				if err != nil {
   127 				if err != nil {
   128 					continue
   128 					continue
   129 				}
   129 				}
   130 				return sec.GetKey(name)
   130 				return sec.GetKey(name)
   131 			}
   131 			}
   132 			break
   132 			break
   133 		}
   133 		}
   134 		return nil, fmt.Errorf("error when getting key of section '%s': key '%s' not exists", s.name, name)
   134 		return nil, fmt.Errorf("error when getting key of section %q: key %q not exists", s.name, name)
   135 	}
   135 	}
   136 	return key, nil
   136 	return key, nil
   137 }
   137 }
   138 
   138 
   139 // HasKey returns true if section contains a key with given name.
   139 // HasKey returns true if section contains a key with given name.
   186 // ParentKeys returns list of keys of parent section.
   186 // ParentKeys returns list of keys of parent section.
   187 func (s *Section) ParentKeys() []*Key {
   187 func (s *Section) ParentKeys() []*Key {
   188 	var parentKeys []*Key
   188 	var parentKeys []*Key
   189 	sname := s.name
   189 	sname := s.name
   190 	for {
   190 	for {
   191 		if i := strings.LastIndex(sname, "."); i > -1 {
   191 		if i := strings.LastIndex(sname, s.f.options.ChildSectionDelimiter); i > -1 {
   192 			sname = sname[:i]
   192 			sname = sname[:i]
   193 			sec, err := s.f.GetSection(sname)
   193 			sec, err := s.f.GetSection(sname)
   194 			if err != nil {
   194 			if err != nil {
   195 				continue
   195 				continue
   196 			}
   196 			}
   243 
   243 
   244 // ChildSections returns a list of child sections of current section.
   244 // ChildSections returns a list of child sections of current section.
   245 // For example, "[parent.child1]" and "[parent.child12]" are child sections
   245 // For example, "[parent.child1]" and "[parent.child12]" are child sections
   246 // of section "[parent]".
   246 // of section "[parent]".
   247 func (s *Section) ChildSections() []*Section {
   247 func (s *Section) ChildSections() []*Section {
   248 	prefix := s.name + "."
   248 	prefix := s.name + s.f.options.ChildSectionDelimiter
   249 	children := make([]*Section, 0, 3)
   249 	children := make([]*Section, 0, 3)
   250 	for _, name := range s.f.sectionList {
   250 	for _, name := range s.f.sectionList {
   251 		if strings.HasPrefix(name, prefix) {
   251 		if strings.HasPrefix(name, prefix) {
   252 			children = append(children, s.f.sections[name])
   252 			children = append(children, s.f.sections[name]...)
   253 		}
   253 		}
   254 	}
   254 	}
   255 	return children
   255 	return children
   256 }
   256 }