Skip to content

Commit

Permalink
fix: better error handling techniques
Browse files Browse the repository at this point in the history
  • Loading branch information
RawanMostafa08 committed Sep 11, 2024
1 parent 73e5e03 commit 5f01597
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions pkg/iniparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"os"
"reflect"
"sort"
"strings"
)
Expand Down Expand Up @@ -130,10 +129,10 @@ func (i iniParser) GetSections() (map[string]section, error) {
// else --> nil
func (i iniParser) Get(sectionName string, key string) (string, error) {

if reflect.DeepEqual(i.sections[sectionName], section{}) {
if _, ok := i.sections[sectionName]; !ok {
return "", errors.New("section not found")
}
if i.sections[sectionName].map_[key] == "" {
if _, ok := i.sections[sectionName].map_[key]; !ok {
return "", errors.New("key not found")
}
return i.sections[sectionName].map_[key], nil
Expand All @@ -148,10 +147,10 @@ func (i iniParser) Get(sectionName string, key string) (string, error) {
// If the key passed isn't found in the passed section --> "key not found"
// else --> nil
func (i *iniParser) Set(sectionName string, key string, value string) error {
if reflect.DeepEqual(i.sections[sectionName], section{}) {
if _, ok := i.sections[sectionName]; !ok {
return errors.New("section not found")
}
if i.sections[sectionName].map_[key] == "" {
if _, ok := i.sections[sectionName].map_[key]; !ok {
return errors.New("key not found")
}
i.sections[sectionName].map_[key] = value
Expand Down

0 comments on commit 5f01597

Please sign in to comment.