sections.go 3.5 KB

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