From e5bdaa67f6b2ad812b8aa5674316af02ae87e652 Mon Sep 17 00:00:00 2001 From: Steven Tracey Date: Wed, 3 May 2023 08:57:32 -0400 Subject: [PATCH] Update ini.go --- .idea/vcs.xml | 6 +++++ ini.go | 65 ++++++++++++++++++++++----------------------------- 2 files changed, 34 insertions(+), 37 deletions(-) create mode 100644 .idea/vcs.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ini.go b/ini.go index 73b036b..898c8ad 100644 --- a/ini.go +++ b/ini.go @@ -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(",") }