sections.go raw

   1  package ini
   2  
   3  import (
   4  	"sort"
   5  )
   6  
   7  // Sections is a map of Section structures that represent
   8  // a configuration.
   9  type Sections struct {
  10  	container map[string]Section
  11  }
  12  
  13  // NewSections returns empty ini Sections
  14  func NewSections() Sections {
  15  	return Sections{
  16  		container: make(map[string]Section, 0),
  17  	}
  18  }
  19  
  20  // GetSection will return section p. If section p does not exist,
  21  // false will be returned in the second parameter.
  22  func (t Sections) GetSection(p string) (Section, bool) {
  23  	v, ok := t.container[p]
  24  	return v, ok
  25  }
  26  
  27  // HasSection denotes if Sections consist of a section with
  28  // provided name.
  29  func (t Sections) HasSection(p string) bool {
  30  	_, ok := t.container[p]
  31  	return ok
  32  }
  33  
  34  // SetSection sets a section value for provided section name.
  35  func (t Sections) SetSection(p string, v Section) Sections {
  36  	t.container[p] = v
  37  	return t
  38  }
  39  
  40  // DeleteSection deletes a section entry/value for provided section name./
  41  func (t Sections) DeleteSection(p string) {
  42  	delete(t.container, p)
  43  }
  44  
  45  // values represents a map of union values.
  46  type values map[string]Value
  47  
  48  // List will return a list of all sections that were successfully
  49  // parsed.
  50  func (t Sections) List() []string {
  51  	keys := make([]string, len(t.container))
  52  	i := 0
  53  	for k := range t.container {
  54  		keys[i] = k
  55  		i++
  56  	}
  57  
  58  	sort.Strings(keys)
  59  	return keys
  60  }
  61  
  62  // Section contains a name and values. This represent
  63  // a sectioned entry in a configuration file.
  64  type Section struct {
  65  	// Name is the Section profile name
  66  	Name string
  67  
  68  	// values are the values within parsed profile
  69  	values values
  70  
  71  	// Errors is the list of errors
  72  	Errors []error
  73  
  74  	// Logs is the list of logs
  75  	Logs []string
  76  
  77  	// SourceFile is the INI Source file from where this section
  78  	// was retrieved. They key is the property, value is the
  79  	// source file the property was retrieved from.
  80  	SourceFile map[string]string
  81  }
  82  
  83  // NewSection returns an initialize section for the name
  84  func NewSection(name string) Section {
  85  	return Section{
  86  		Name:       name,
  87  		values:     values{},
  88  		SourceFile: map[string]string{},
  89  	}
  90  }
  91  
  92  // List will return a list of all
  93  // services in values
  94  func (t Section) List() []string {
  95  	keys := make([]string, len(t.values))
  96  	i := 0
  97  	for k := range t.values {
  98  		keys[i] = k
  99  		i++
 100  	}
 101  
 102  	sort.Strings(keys)
 103  	return keys
 104  }
 105  
 106  // UpdateSourceFile updates source file for a property to provided filepath.
 107  func (t Section) UpdateSourceFile(property string, filepath string) {
 108  	t.SourceFile[property] = filepath
 109  }
 110  
 111  // UpdateValue updates value for a provided key with provided value
 112  func (t Section) UpdateValue(k string, v Value) error {
 113  	t.values[k] = v
 114  	return nil
 115  }
 116  
 117  // Has will return whether or not an entry exists in a given section
 118  func (t Section) Has(k string) bool {
 119  	_, ok := t.values[k]
 120  	return ok
 121  }
 122  
 123  // ValueType will returned what type the union is set to. If
 124  // k was not found, the NoneType will be returned.
 125  func (t Section) ValueType(k string) (ValueType, bool) {
 126  	v, ok := t.values[k]
 127  	return v.Type, ok
 128  }
 129  
 130  // Bool returns a bool value at k
 131  func (t Section) Bool(k string) (bool, bool) {
 132  	return t.values[k].BoolValue()
 133  }
 134  
 135  // Int returns an integer value at k
 136  func (t Section) Int(k string) (int64, bool) {
 137  	return t.values[k].IntValue()
 138  }
 139  
 140  // Map returns a map value at k
 141  func (t Section) Map(k string) map[string]string {
 142  	return t.values[k].MapValue()
 143  }
 144  
 145  // Float64 returns a float value at k
 146  func (t Section) Float64(k string) (float64, bool) {
 147  	return t.values[k].FloatValue()
 148  }
 149  
 150  // String returns the string value at k
 151  func (t Section) String(k string) string {
 152  	_, ok := t.values[k]
 153  	if !ok {
 154  		return ""
 155  	}
 156  	return t.values[k].StringValue()
 157  }
 158