summaryrefslogtreecommitdiffstats
path: root/vendor/gopkg.in/ini.v1
diff options
context:
space:
mode:
authorWim <wim@42.be>2020-08-10 00:29:54 +0200
committerGitHub <noreply@github.com>2020-08-10 00:29:54 +0200
commit4e50fd864921c556988c919269448efdb90fa961 (patch)
treea3625f03f8de3c4f3841364000a4ea3aa42c1533 /vendor/gopkg.in/ini.v1
parentdfdffa0027334e55ce213fc6eb62206dbf48baf6 (diff)
downloadmatterbridge-msglm-4e50fd864921c556988c919269448efdb90fa961.tar.gz
matterbridge-msglm-4e50fd864921c556988c919269448efdb90fa961.tar.bz2
matterbridge-msglm-4e50fd864921c556988c919269448efdb90fa961.zip
Use mattermost v5 module (#1192)
Diffstat (limited to 'vendor/gopkg.in/ini.v1')
-rw-r--r--vendor/gopkg.in/ini.v1/.travis.yml5
-rw-r--r--vendor/gopkg.in/ini.v1/Makefile2
-rw-r--r--vendor/gopkg.in/ini.v1/README.md2
-rw-r--r--vendor/gopkg.in/ini.v1/data_source.go2
-rw-r--r--vendor/gopkg.in/ini.v1/file.go153
-rw-r--r--vendor/gopkg.in/ini.v1/ini.go10
-rw-r--r--vendor/gopkg.in/ini.v1/parser.go4
-rw-r--r--vendor/gopkg.in/ini.v1/section.go4
-rw-r--r--vendor/gopkg.in/ini.v1/struct.go188
9 files changed, 290 insertions, 80 deletions
diff --git a/vendor/gopkg.in/ini.v1/.travis.yml b/vendor/gopkg.in/ini.v1/.travis.yml
index 149b7249..4db2e661 100644
--- a/vendor/gopkg.in/ini.v1/.travis.yml
+++ b/vendor/gopkg.in/ini.v1/.travis.yml
@@ -1,5 +1,6 @@
-sudo: false
language: go
+os: linux
+dist: xenial
go:
- 1.6.x
- 1.7.x
@@ -9,7 +10,7 @@ go:
- 1.11.x
- 1.12.x
- 1.13.x
-
+ - 1.14.x
install: skip
script:
- go get golang.org/x/tools/cmd/cover
diff --git a/vendor/gopkg.in/ini.v1/Makefile b/vendor/gopkg.in/ini.v1/Makefile
index af27ff07..f3b0dae2 100644
--- a/vendor/gopkg.in/ini.v1/Makefile
+++ b/vendor/gopkg.in/ini.v1/Makefile
@@ -6,7 +6,7 @@ test:
go test -v -cover -race
bench:
- go test -v -cover -race -test.bench=. -test.benchmem
+ go test -v -cover -test.bench=. -test.benchmem
vet:
go vet
diff --git a/vendor/gopkg.in/ini.v1/README.md b/vendor/gopkg.in/ini.v1/README.md
index 3d6d3cfc..783eb06a 100644
--- a/vendor/gopkg.in/ini.v1/README.md
+++ b/vendor/gopkg.in/ini.v1/README.md
@@ -8,7 +8,7 @@ Package ini provides INI file read and write functionality in Go.
## Features
-- Load from multiple data sources(`[]byte`, file and `io.ReadCloser`) with overwrites.
+- Load from multiple data sources(file, `[]byte`, `io.Reader` and `io.ReadCloser`) with overwrites.
- Read with recursion values.
- Read with parent-child sections.
- Read with auto-increment key names.
diff --git a/vendor/gopkg.in/ini.v1/data_source.go b/vendor/gopkg.in/ini.v1/data_source.go
index dc0277ec..bbedf361 100644
--- a/vendor/gopkg.in/ini.v1/data_source.go
+++ b/vendor/gopkg.in/ini.v1/data_source.go
@@ -66,6 +66,8 @@ func parseDataSource(source interface{}) (dataSource, error) {
return sourceFile{s}, nil
case []byte:
return &sourceData{s}, nil
+ case io.Reader:
+ return &sourceReadCloser{ioutil.NopCloser(s)}, nil
case io.ReadCloser:
return &sourceReadCloser{s}, nil
default:
diff --git a/vendor/gopkg.in/ini.v1/file.go b/vendor/gopkg.in/ini.v1/file.go
index 017b77c8..f95606f9 100644
--- a/vendor/gopkg.in/ini.v1/file.go
+++ b/vendor/gopkg.in/ini.v1/file.go
@@ -25,7 +25,7 @@ import (
"sync"
)
-// File represents a combination of a or more INI file(s) in memory.
+// File represents a combination of one or more INI files in memory.
type File struct {
options LoadOptions
dataSources []dataSource
@@ -36,8 +36,12 @@ type File struct {
// To keep data in order.
sectionList []string
+ // To keep track of the index of a section with same name.
+ // This meta list is only used with non-unique section names are allowed.
+ sectionIndexes []int
+
// Actual data is stored here.
- sections map[string]*Section
+ sections map[string][]*Section
NameMapper
ValueMapper
@@ -48,27 +52,37 @@ func newFile(dataSources []dataSource, opts LoadOptions) *File {
if len(opts.KeyValueDelimiters) == 0 {
opts.KeyValueDelimiters = "=:"
}
+ if len(opts.KeyValueDelimiterOnWrite) == 0 {
+ opts.KeyValueDelimiterOnWrite = "="
+ }
+
return &File{
BlockMode: true,
dataSources: dataSources,
- sections: make(map[string]*Section),
- sectionList: make([]string, 0, 10),
+ sections: make(map[string][]*Section),
options: opts,
}
}
// Empty returns an empty file object.
-func Empty() *File {
- // Ignore error here, we sure our data is good.
- f, _ := Load([]byte(""))
+func Empty(opts ...LoadOptions) *File {
+ var opt LoadOptions
+ if len(opts) > 0 {
+ opt = opts[0]
+ }
+
+ // Ignore error here, we are sure our data is good.
+ f, _ := LoadSources(opt, []byte(""))
return f
}
// NewSection creates a new section.
func (f *File) NewSection(name string) (*Section, error) {
if len(name) == 0 {
- return nil, errors.New("error creating new section: empty section name")
- } else if f.options.Insensitive && name != DefaultSection {
+ return nil, errors.New("empty section name")
+ }
+
+ if f.options.Insensitive && name != DefaultSection {
name = strings.ToLower(name)
}
@@ -77,13 +91,20 @@ func (f *File) NewSection(name string) (*Section, error) {
defer f.lock.Unlock()
}
- if inSlice(name, f.sectionList) {
- return f.sections[name], nil
+ if !f.options.AllowNonUniqueSections && inSlice(name, f.sectionList) {
+ return f.sections[name][0], nil
}
f.sectionList = append(f.sectionList, name)
- f.sections[name] = newSection(f, name)
- return f.sections[name], nil
+
+ // NOTE: Append to indexes must happen before appending to sections,
+ // otherwise index will have off-by-one problem.
+ f.sectionIndexes = append(f.sectionIndexes, len(f.sections[name]))
+
+ sec := newSection(f, name)
+ f.sections[name] = append(f.sections[name], sec)
+
+ return sec, nil
}
// NewRawSection creates a new section with an unparseable body.
@@ -110,6 +131,16 @@ func (f *File) NewSections(names ...string) (err error) {
// GetSection returns section by given name.
func (f *File) GetSection(name string) (*Section, error) {
+ secs, err := f.SectionsByName(name)
+ if err != nil {
+ return nil, err
+ }
+
+ return secs[0], err
+}
+
+// SectionsByName returns all sections with given name.
+func (f *File) SectionsByName(name string) ([]*Section, error) {
if len(name) == 0 {
name = DefaultSection
}
@@ -122,11 +153,12 @@ func (f *File) GetSection(name string) (*Section, error) {
defer f.lock.RUnlock()
}
- sec := f.sections[name]
- if sec == nil {
- return nil, fmt.Errorf("section '%s' does not exist", name)
+ secs := f.sections[name]
+ if len(secs) == 0 {
+ return nil, fmt.Errorf("section %q does not exist", name)
}
- return sec, nil
+
+ return secs, nil
}
// Section assumes named section exists and returns a zero-value when not.
@@ -141,6 +173,19 @@ func (f *File) Section(name string) *Section {
return sec
}
+// SectionWithIndex assumes named section exists and returns a new section when not.
+func (f *File) SectionWithIndex(name string, index int) *Section {
+ secs, err := f.SectionsByName(name)
+ if err != nil || len(secs) <= index {
+ // NOTE: It's OK here because the only possible error is empty section name,
+ // but if it's empty, this piece of code won't be executed.
+ newSec, _ := f.NewSection(name)
+ return newSec
+ }
+
+ return secs[index]
+}
+
// Sections returns a list of Section stored in the current instance.
func (f *File) Sections() []*Section {
if f.BlockMode {
@@ -150,7 +195,7 @@ func (f *File) Sections() []*Section {
sections := make([]*Section, len(f.sectionList))
for i, name := range f.sectionList {
- sections[i] = f.sections[name]
+ sections[i] = f.sections[name][f.sectionIndexes[i]]
}
return sections
}
@@ -167,24 +212,70 @@ func (f *File) SectionStrings() []string {
return list
}
-// DeleteSection deletes a section.
+// DeleteSection deletes a section or all sections with given name.
func (f *File) DeleteSection(name string) {
- if f.BlockMode {
- f.lock.Lock()
- defer f.lock.Unlock()
+ secs, err := f.SectionsByName(name)
+ if err != nil {
+ return
+ }
+
+ for i := 0; i < len(secs); i++ {
+ // For non-unique sections, it is always needed to remove the first one so
+ // in the next iteration, the subsequent section continue having index 0.
+ // Ignoring the error as index 0 never returns an error.
+ _ = f.DeleteSectionWithIndex(name, 0)
+ }
+}
+
+// DeleteSectionWithIndex deletes a section with given name and index.
+func (f *File) DeleteSectionWithIndex(name string, index int) error {
+ if !f.options.AllowNonUniqueSections && index != 0 {
+ return fmt.Errorf("delete section with non-zero index is only allowed when non-unique sections is enabled")
}
if len(name) == 0 {
name = DefaultSection
}
+ if f.options.Insensitive {
+ name = strings.ToLower(name)
+ }
+
+ if f.BlockMode {
+ f.lock.Lock()
+ defer f.lock.Unlock()
+ }
+
+ // Count occurrences of the sections
+ occurrences := 0
+
+ sectionListCopy := make([]string, len(f.sectionList))
+ copy(sectionListCopy, f.sectionList)
+
+ for i, s := range sectionListCopy {
+ if s != name {
+ continue
+ }
- for i, s := range f.sectionList {
- if s == name {
+ if occurrences == index {
+ if len(f.sections[name]) <= 1 {
+ delete(f.sections, name) // The last one in the map
+ } else {
+ f.sections[name] = append(f.sections[name][:index], f.sections[name][index+1:]...)
+ }
+
+ // Fix section lists
f.sectionList = append(f.sectionList[:i], f.sectionList[i+1:]...)
- delete(f.sections, name)
- return
+ f.sectionIndexes = append(f.sectionIndexes[:i], f.sectionIndexes[i+1:]...)
+
+ } else if occurrences > index {
+ // Fix the indices of all following sections with this name.
+ f.sectionIndexes[i-1]--
}
+
+ occurrences++
}
+
+ return nil
}
func (f *File) reload(s dataSource) error {
@@ -203,7 +294,7 @@ func (f *File) Reload() (err error) {
if err = f.reload(s); err != nil {
// In loose mode, we create an empty default section for nonexistent files.
if os.IsNotExist(err) && f.options.Loose {
- f.parse(bytes.NewBuffer(nil))
+ _ = f.parse(bytes.NewBuffer(nil))
continue
}
return err
@@ -230,16 +321,16 @@ func (f *File) Append(source interface{}, others ...interface{}) error {
}
func (f *File) writeToBuffer(indent string) (*bytes.Buffer, error) {
- equalSign := DefaultFormatLeft + "=" + DefaultFormatRight
+ equalSign := DefaultFormatLeft + f.options.KeyValueDelimiterOnWrite + DefaultFormatRight
if PrettyFormat || PrettyEqual {
- equalSign = " = "
+ equalSign = fmt.Sprintf(" %s ", f.options.KeyValueDelimiterOnWrite)
}
// Use buffer to make sure target is safe until finish encoding.
buf := bytes.NewBuffer(nil)
for i, sname := range f.sectionList {
- sec := f.Section(sname)
+ sec := f.SectionWithIndex(sname, f.sectionIndexes[i])
if len(sec.Comment) > 0 {
// Support multiline comments
lines := strings.Split(sec.Comment, LineBreak)
@@ -282,7 +373,7 @@ func (f *File) writeToBuffer(indent string) (*bytes.Buffer, error) {
}
// Count and generate alignment length and buffer spaces using the
- // longest key. Keys may be modifed if they contain certain characters so
+ // longest key. Keys may be modified if they contain certain characters so
// we need to take that into account in our calculation.
alignLength := 0
if PrettyFormat {
diff --git a/vendor/gopkg.in/ini.v1/ini.go b/vendor/gopkg.in/ini.v1/ini.go
index 945fc00c..9f28cb31 100644
--- a/vendor/gopkg.in/ini.v1/ini.go
+++ b/vendor/gopkg.in/ini.v1/ini.go
@@ -29,14 +29,8 @@ const (
// Maximum allowed depth when recursively substituing variable names.
depthValues = 99
- version = "1.51.0"
)
-// Version returns current package version literal.
-func Version() string {
- return version
-}
-
var (
// LineBreak is the delimiter to determine or compose a new line.
// This variable will be changed to "\r\n" automatically on Windows at package init time.
@@ -109,12 +103,16 @@ type LoadOptions struct {
UnparseableSections []string
// KeyValueDelimiters is the sequence of delimiters that are used to separate key and value. By default, it is "=:".
KeyValueDelimiters string
+ // KeyValueDelimiters is the delimiter that are used to separate key and value output. By default, it is "=".
+ KeyValueDelimiterOnWrite string
// PreserveSurroundedQuote indicates whether to preserve surrounded quote (single and double quotes).
PreserveSurroundedQuote bool
// DebugFunc is called to collect debug information (currently only useful to debug parsing Python-style multiline values).
DebugFunc DebugFunc
// ReaderBufferSize is the buffer size of the reader in bytes.
ReaderBufferSize int
+ // AllowNonUniqueSections indicates whether to allow sections with the same name multiple times.
+ AllowNonUniqueSections bool
}
// DebugFunc is the type of function called to log parse events.
diff --git a/vendor/gopkg.in/ini.v1/parser.go b/vendor/gopkg.in/ini.v1/parser.go
index 53ab45c4..f023db59 100644
--- a/vendor/gopkg.in/ini.v1/parser.go
+++ b/vendor/gopkg.in/ini.v1/parser.go
@@ -181,7 +181,7 @@ func (p *parser) readMultilines(line, val, valQuote string) (string, error) {
}
val += next
if p.isEOF {
- return "", fmt.Errorf("missing closing key quote from '%s' to '%s'", line, next)
+ return "", fmt.Errorf("missing closing key quote from %q to %q", line, next)
}
}
return val, nil
@@ -453,7 +453,7 @@ func (f *File) parse(reader io.Reader) (err error) {
section.Comment = strings.TrimSpace(p.comment.String())
- // Reset aotu-counter and comments
+ // Reset auto-counter and comments
p.comment.Reset()
p.count = 1
diff --git a/vendor/gopkg.in/ini.v1/section.go b/vendor/gopkg.in/ini.v1/section.go
index 0bd3e130..6ba5ac29 100644
--- a/vendor/gopkg.in/ini.v1/section.go
+++ b/vendor/gopkg.in/ini.v1/section.go
@@ -131,7 +131,7 @@ func (s *Section) GetKey(name string) (*Key, error) {
}
break
}
- return nil, fmt.Errorf("error when getting key of section '%s': key '%s' not exists", s.name, name)
+ return nil, fmt.Errorf("error when getting key of section %q: key %q not exists", s.name, name)
}
return key, nil
}
@@ -249,7 +249,7 @@ func (s *Section) ChildSections() []*Section {
children := make([]*Section, 0, 3)
for _, name := range s.f.sectionList {
if strings.HasPrefix(name, prefix) {
- children = append(children, s.f.sections[name])
+ children = append(children, s.f.sections[name]...)
}
}
return children
diff --git a/vendor/gopkg.in/ini.v1/struct.go b/vendor/gopkg.in/ini.v1/struct.go
index 6bc70e4d..6b958496 100644
--- a/vendor/gopkg.in/ini.v1/struct.go
+++ b/vendor/gopkg.in/ini.v1/struct.go
@@ -183,6 +183,10 @@ func setWithProperType(t reflect.Type, key *Key, field reflect.Value, delim stri
if vt.Name() == "Duration" {
durationVal, err := key.Duration()
if err != nil {
+ if intVal, err := key.Int64(); err == nil {
+ field.SetInt(intVal)
+ return nil
+ }
return wrapStrictError(err, isStrict)
}
if isPtr {
@@ -254,13 +258,13 @@ func setWithProperType(t reflect.Type, key *Key, field reflect.Value, delim stri
case reflect.Slice:
return setSliceWithProperType(key, field, delim, allowShadow, isStrict)
default:
- return fmt.Errorf("unsupported type '%s'", t)
+ return fmt.Errorf("unsupported type %q", t)
}
return nil
}
-func parseTagOptions(tag string) (rawName string, omitEmpty bool, allowShadow bool) {
- opts := strings.SplitN(tag, ",", 3)
+func parseTagOptions(tag string) (rawName string, omitEmpty bool, allowShadow bool, allowNonUnique bool) {
+ opts := strings.SplitN(tag, ",", 4)
rawName = opts[0]
if len(opts) > 1 {
omitEmpty = opts[1] == "omitempty"
@@ -268,10 +272,13 @@ func parseTagOptions(tag string) (rawName string, omitEmpty bool, allowShadow bo
if len(opts) > 2 {
allowShadow = opts[2] == "allowshadow"
}
- return rawName, omitEmpty, allowShadow
+ if len(opts) > 3 {
+ allowNonUnique = opts[3] == "nonunique"
+ }
+ return rawName, omitEmpty, allowShadow, allowNonUnique
}
-func (s *Section) mapTo(val reflect.Value, isStrict bool) error {
+func (s *Section) mapToField(val reflect.Value, isStrict bool) error {
if val.Kind() == reflect.Ptr {
val = val.Elem()
}
@@ -286,7 +293,7 @@ func (s *Section) mapTo(val reflect.Value, isStrict bool) error {
continue
}
- rawName, _, allowShadow := parseTagOptions(tag)
+ rawName, _, allowShadow, allowNonUnique := parseTagOptions(tag)
fieldName := s.parseFieldName(tpField.Name, rawName)
if len(fieldName) == 0 || !field.CanSet() {
continue
@@ -301,55 +308,92 @@ func (s *Section) mapTo(val reflect.Value, isStrict bool) error {
if isAnonymous || isStruct || isStructPtr {
if sec, err := s.f.GetSection(fieldName); err == nil {
- // Only set the field to non-nil struct value if we have
- // a section for it. Otherwise, we end up with a non-nil
- // struct ptr even though there is no data.
+ // Only set the field to non-nil struct value if we have a section for it.
+ // Otherwise, we end up with a non-nil struct ptr even though there is no data.
if isStructPtr && field.IsNil() {
field.Set(reflect.New(tpField.Type.Elem()))
}
- if err = sec.mapTo(field, isStrict); err != nil {
- return fmt.Errorf("error mapping field(%s): %v", fieldName, err)
+ if err = sec.mapToField(field, isStrict); err != nil {
+ return fmt.Errorf("map to field %q: %v", fieldName, err)
}
continue
}
}
+
+ // Map non-unique sections
+ if allowNonUnique && tpField.Type.Kind() == reflect.Slice {
+ newField, err := s.mapToSlice(fieldName, field, isStrict)
+ if err != nil {
+ return fmt.Errorf("map to slice %q: %v", fieldName, err)
+ }
+
+ field.Set(newField)
+ continue
+ }
+
if key, err := s.GetKey(fieldName); err == nil {
delim := parseDelim(tpField.Tag.Get("delim"))
if err = setWithProperType(tpField.Type, key, field, delim, allowShadow, isStrict); err != nil {
- return fmt.Errorf("error mapping field(%s): %v", fieldName, err)
+ return fmt.Errorf("set field %q: %v", fieldName, err)
}
}
}
return nil
}
-// MapTo maps section to given struct.
-func (s *Section) MapTo(v interface{}) error {
+// mapToSlice maps all sections with the same name and returns the new value.
+// The type of the Value must be a slice.
+func (s *Section) mapToSlice(secName string, val reflect.Value, isStrict bool) (reflect.Value, error) {
+ secs, err := s.f.SectionsByName(secName)
+ if err != nil {
+ return reflect.Value{}, err
+ }
+
+ typ := val.Type().Elem()
+ for _, sec := range secs {
+ elem := reflect.New(typ)
+ if err = sec.mapToField(elem, isStrict); err != nil {
+ return reflect.Value{}, fmt.Errorf("map to field from section %q: %v", secName, err)
+ }
+
+ val = reflect.Append(val, elem.Elem())
+ }
+ return val, nil
+}
+
+// mapTo maps a section to object v.
+func (s *Section) mapTo(v interface{}, isStrict bool) error {
typ := reflect.TypeOf(v)
val := reflect.ValueOf(v)
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
val = val.Elem()
} else {
- return errors.New("cannot map to non-pointer struct")
+ return errors.New("not a pointer to a struct")
}
- return s.mapTo(val, false)
+ if typ.Kind() == reflect.Slice {
+ newField, err := s.mapToSlice(s.name, val, isStrict)
+ if err != nil {
+ return err
+ }
+
+ val.Set(newField)
+ return nil
+ }
+
+ return s.mapToField(val, isStrict)
+}
+
+// MapTo maps section to given struct.
+func (s *Section) MapTo(v interface{}) error {
+ return s.mapTo(v, false)
}
// StrictMapTo maps section to given struct in strict mode,
// which returns all possible error including value parsing error.
func (s *Section) StrictMapTo(v interface{}) error {
- typ := reflect.TypeOf(v)
- val := reflect.ValueOf(v)
- if typ.Kind() == reflect.Ptr {
- typ = typ.Elem()
- val = val.Elem()
- } else {
- return errors.New("cannot map to non-pointer struct")
- }
-
- return s.mapTo(val, true)
+ return s.mapTo(v, true)
}
// MapTo maps file to given struct.
@@ -427,7 +471,7 @@ func reflectSliceWithProperType(key *Key, field reflect.Value, delim string, all
if i == 0 {
keyWithShadows = newKey(key.s, key.name, val)
} else {
- keyWithShadows.AddShadow(val)
+ _ = keyWithShadows.AddShadow(val)
}
}
key = keyWithShadows
@@ -480,7 +524,7 @@ func reflectWithProperType(t reflect.Type, key *Key, field reflect.Value, delim
return reflectWithProperType(t.Elem(), key, field.Elem(), delim, allowShadow)
}
default:
- return fmt.Errorf("unsupported type '%s'", t)
+ return fmt.Errorf("unsupported type %q", t)
}
return nil
}
@@ -508,6 +552,11 @@ func isEmptyValue(v reflect.Value) bool {
return false
}
+// StructReflector is the interface implemented by struct types that can extract themselves into INI objects.
+type StructReflector interface {
+ ReflectINIStruct(*File) error
+}
+
func (s *Section) reflectFrom(val reflect.Value) error {
if val.Kind() == reflect.Ptr {
val = val.Elem()
@@ -523,11 +572,15 @@ func (s *Section) reflectFrom(val reflect.Value) error {
continue
}
- rawName, omitEmpty, allowShadow := parseTagOptions(tag)
+ rawName, omitEmpty, allowShadow, allowNonUnique := parseTagOptions(tag)
if omitEmpty && isEmptyValue(field) {
continue
}
+ if r, ok := field.Interface().(StructReflector); ok {
+ return r.ReflectINIStruct(s.f)
+ }
+
fieldName := s.parseFieldName(tpField.Name, rawName)
if len(fieldName) == 0 || !field.CanSet() {
continue
@@ -548,12 +601,41 @@ func (s *Section) reflectFrom(val reflect.Value) error {
}
if err = sec.reflectFrom(field); err != nil {
- return fmt.Errorf("error reflecting field (%s): %v", fieldName, err)
+ return fmt.Errorf("reflect from field %q: %v", fieldName, err)
+ }
+ continue
+ }
+
+ if allowNonUnique && tpField.Type.Kind() == reflect.Slice {
+ slice := field.Slice(0, field.Len())
+ if field.Len() == 0 {
+ return nil
+ }
+ sliceOf := field.Type().Elem().Kind()
+
+ for i := 0; i < field.Len(); i++ {
+ if sliceOf != reflect.Struct && sliceOf != reflect.Ptr {
+ return fmt.Errorf("field %q is not a slice of pointer or struct", fieldName)
+ }
+
+ sec, err := s.f.NewSection(fieldName)
+ if err != nil {
+ return err
+ }
+
+ // Add comment from comment tag
+ if len(sec.Comment) == 0 {
+ sec.Comment = tpField.Tag.Get("comment")
+ }
+
+ if err := sec.reflectFrom(slice.Index(i)); err != nil {
+ return fmt.Errorf("reflect from field %q: %v", fieldName, err)
+ }
}
continue
}
- // Note: Same reason as secion.
+ // Note: Same reason as section.
key, err := s.GetKey(fieldName)
if err != nil {
key, _ = s.NewKey(fieldName, "")
@@ -564,23 +646,59 @@ func (s *Section) reflectFrom(val reflect.Value) error {
key.Comment = tpField.Tag.Get("comment")
}
- if err = reflectWithProperType(tpField.Type, key, field, parseDelim(tpField.Tag.Get("delim")), allowShadow); err != nil {
- return fmt.Errorf("error reflecting field (%s): %v", fieldName, err)
+ delim := parseDelim(tpField.Tag.Get("delim"))
+ if err = reflectWithProperType(tpField.Type, key, field, delim, allowShadow); err != nil {
+ return fmt.Errorf("reflect field %q: %v", fieldName, err)
}
}
return nil
}
-// ReflectFrom reflects secion from given struct.
+// ReflectFrom reflects section from given struct. It overwrites existing ones.
func (s *Section) ReflectFrom(v interface{}) error {
typ := reflect.TypeOf(v)
val := reflect.ValueOf(v)
+
+ if s.name != DefaultSection && s.f.options.AllowNonUniqueSections &&
+ (typ.Kind() == reflect.Slice || typ.Kind() == reflect.Ptr) {
+ // Clear sections to make sure none exists before adding the new ones
+ s.f.DeleteSection(s.name)
+
+ if typ.Kind() == reflect.Ptr {
+ sec, err := s.f.NewSection(s.name)
+ if err != nil {
+ return err
+ }
+ return sec.reflectFrom(val.Elem())
+ }
+
+ slice := val.Slice(0, val.Len())
+ sliceOf := val.Type().Elem().Kind()
+ if sliceOf != reflect.Ptr {
+ return fmt.Errorf("not a slice of pointers")
+ }
+
+ for i := 0; i < slice.Len(); i++ {
+ sec, err := s.f.NewSection(s.name)
+ if err != nil {
+ return err
+ }
+
+ err = sec.reflectFrom(slice.Index(i))
+ if err != nil {
+ return fmt.Errorf("reflect from %dth field: %v", i, err)
+ }
+ }
+
+ return nil
+ }
+
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
val = val.Elem()
} else {
- return errors.New("cannot reflect from non-pointer struct")
+ return errors.New("not a pointer to a struct")
}
return s.reflectFrom(val)