Update ini.go

This commit is contained in:
Steven Tracey 2023-05-03 08:57:32 -04:00
parent 397365ddd2
commit e5bdaa67f6
2 changed files with 34 additions and 37 deletions

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

65
ini.go
View File

@ -20,7 +20,7 @@ func NewConfiguration(fileLocation string) *Configuration {
return conf
}
func (config *Configuration) getSection(sectionName string) *ini.Section {
func (config *Configuration) GetSection(sectionName string) *ini.Section {
section, err := config.file.GetSection(sectionName)
if err != nil {
fmt.Printf("Error getting section %v in config: %v", sectionName, err)
@ -29,57 +29,48 @@ func (config *Configuration) getSection(sectionName string) *ini.Section {
return section
}
func getChildSection(section *ini.Section, childSectionName string) (childSection *ini.Section, err error) {
func GetChildSection(section *ini.Section, childSectionName string) (childSection *ini.Section, err error) {
childSections := section.ChildSections()
for _, childSection := range childSections {
for _, childSection = range childSections {
if childSection.Name() == childSectionName {
return childSection, nil
return
}
}
return nil, errors.New("subsection " + childSectionName + " not found")
}
func GetChildSections(section *ini.Section) (childSections []*ini.Section) {
return section.ChildSections()
}
func (config *Configuration) GetKey(path string) *ini.Key {
splitPaths := strings.Split(path, ".")
if len(splitPaths) == 1 {
splitPaths = append([]string{"."}, splitPaths...)
}
section := config.getSection(splitPaths[0])
var key *ini.Key
var err error
if len(splitPaths) > 2 {
tempSection := section
for i := 0; i < len(splitPaths)-1; i++ {
tempSection, err = getChildSection(tempSection, splitPaths[i])
if err != nil {
fmt.Printf("Error getting child section %v: %v", splitPaths[i], err)
os.Exit(1)
}
if len(splitPaths) == i+1 {
key, err = tempSection.GetKey(splitPaths[i+1])
if err != nil {
fmt.Printf("Error getting key %v: %v", splitPaths[i+1], err)
}
}
}
} else {
key, err = section.GetKey(splitPaths[1])
if err != nil {
fmt.Printf("Error getting key %v: %v", splitPaths[1], err)
}
sectionMask := strings.Join(splitPaths[0:len(splitPaths)-1], ".")
section := config.GetSection(sectionMask)
keyName := splitPaths[len(splitPaths)-1]
key, err := section.GetKey(keyName)
if err != nil {
fmt.Printf("Error getting key %v: %v", keyName, err)
}
return key
}
func (config *Configuration) GetString(path string) string {
func (config *Configuration) GetAsString(path string) string {
return config.GetKey(path).String()
}
func (config *Configuration) GetStrings(path string) []string {
func (config *Configuration) SetValue(path string, value string) {
config.GetKey(path).SetValue(value)
}
func (config *Configuration) GetAsStrings(path string) []string {
return config.GetKey(path).Strings(",")
}
func (config *Configuration) GetInt(path string) int {
func (config *Configuration) GetAsInt(path string) int {
var i int
var err error
i, err = config.GetKey(path).Int()
@ -90,11 +81,11 @@ func (config *Configuration) GetInt(path string) int {
return i
}
func (config *Configuration) GetInts(path string) []int {
func (config *Configuration) GetAsInts(path string) []int {
return config.GetKey(path).Ints(",")
}
func (config *Configuration) GetUInt(path string) uint {
func (config *Configuration) GetAsUInt(path string) uint {
var i uint
var err error
i, err = config.GetKey(path).Uint()
@ -105,11 +96,11 @@ func (config *Configuration) GetUInt(path string) uint {
return i
}
func (config *Configuration) GetUInts(path string) []uint {
func (config *Configuration) GetAsUInts(path string) []uint {
return config.GetKey(path).Uints(",")
}
func (config *Configuration) GetFloat(path string) float64 {
func (config *Configuration) GetAsFloat(path string) float64 {
var f float64
var err error
f, err = config.GetKey(path).Float64()
@ -120,11 +111,11 @@ func (config *Configuration) GetFloat(path string) float64 {
return f
}
func (config *Configuration) GetFloats(path string) []float64 {
func (config *Configuration) GetAsFloats(path string) []float64 {
return config.GetKey(path).Float64s(",")
}
func (config *Configuration) GetBoolean(path string) bool {
func (config *Configuration) GetAsBoolean(path string) bool {
var b bool
var err error
b, err = config.GetKey(path).Bool()
@ -135,7 +126,7 @@ func (config *Configuration) GetBoolean(path string) bool {
return b
}
func (config *Configuration) GetBooleans(path string) []bool {
func (config *Configuration) GetAsBooleans(path string) []bool {
return config.GetKey(path).Bools(",")
}