Skip to content

Commit

Permalink
fix: Applying review comments: removing section struct and renaming P…
Browse files Browse the repository at this point in the history
…opulateINI method
  • Loading branch information
RawanMostafa08 committed Sep 24, 2024
1 parent a3c15af commit b35523d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 48 deletions.
33 changes: 14 additions & 19 deletions pkg/iniparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,22 @@ var ErrNoKey = errors.New("key not found")
var ErrNoSection = errors.New("section not found")
var ErrNotINI = errors.New("this is not an ini file")

// The section acts as a type representing the value of the iniparser map
type section struct {
map_ map[string]string
}

// The Parser acts as the data structure storing all of the parsed sections
type Parser struct {
sections map[string]section
sections map[string]map[string]string
}

// NewParser returns a Parser type object
// NewParser is an essential call to get a parser to be able to access its APIs
func NewParser() Parser {
return Parser{
make(map[string]section),
make(map[string]map[string]string),
}
}

func (i Parser) populateINI(lines []string) {
func (i Parser) parse(lines []string) {
var title string
var sec section
var sec map[string]string
for _, line := range lines {

line = strings.TrimSpace(line)
Expand All @@ -47,14 +42,14 @@ func (i Parser) populateINI(lines []string) {
i.sections[title] = sec
}
title = strings.Trim(line, "[]")
sec = section{map_: make(map[string]string)}
sec = make(map[string]string)

} else if strings.Contains(line, "=") {

parts := strings.SplitN(line, "=", 2)
key := strings.TrimSpace(parts[0])
value := strings.TrimSpace(parts[1])
sec.map_[key] = value
sec[key] = value
}
}
if title != "" {
Expand All @@ -70,7 +65,7 @@ func (i Parser) populateINI(lines []string) {
func (i Parser) LoadFromString(data string) {

lines := strings.Split(data, "\n")
i.populateINI(lines)
i.parse(lines)
}

// LoadFromFile is a method that takes a path to an ini file and parses it into the caller Parser object
Expand Down Expand Up @@ -108,9 +103,9 @@ func (i Parser) GetSectionNames() ([]string, error) {

// GetSections is a method that returns a map[string]section representing
// the data structure of the caller Parser object and an error in case of empty sections
func (i Parser) GetSections() (map[string]section, error) {
func (i Parser) GetSections() (map[string]map[string]string, error) {
if len(i.sections) == 0 {
return make(map[string]section, 0), nil
return make(map[string]map[string]string, 0), nil
}
return i.sections, nil
}
Expand All @@ -119,7 +114,7 @@ func (i Parser) GetSections() (map[string]section, error) {
// and returns the value of this key and a boolean to indicate if found or not
func (i Parser) Get(sectionName string, key string) (string, bool) {

val, exists := i.sections[sectionName].map_[key]
val, exists := i.sections[sectionName][key]
return val, exists
}

Expand All @@ -134,10 +129,10 @@ func (i Parser) Set(sectionName string, key string, value string) error {
if _, ok := i.sections[sectionName]; !ok {
return ErrNoSection
}
if _, ok := i.sections[sectionName].map_[key]; !ok {
if _, ok := i.sections[sectionName][key]; !ok {
return ErrNoKey
}
i.sections[sectionName].map_[key] = value
i.sections[sectionName][key] = value
return nil
}

Expand All @@ -155,12 +150,12 @@ func (i Parser) String() string {
for _, sectionName := range sectionNames {
keys := make([]string, 0)
result += fmt.Sprintf("[%s]\n", sectionName)
for key := range i.sections[sectionName].map_ {
for key := range i.sections[sectionName] {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
result += fmt.Sprintf("%s = %s\n", key, i.sections[sectionName].map_[key])
result += fmt.Sprintf("%s = %s\n", key, i.sections[sectionName][key])
}
}
return fmt.Sprint(result)
Expand Down
48 changes: 19 additions & 29 deletions pkg/iniparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,43 +22,33 @@ port = 143
key0 = val0
key1 = val1`

func populateExpectedNormal(t *testing.T) map[string]section {
func populateExpectedNormal(t *testing.T) map[string]map[string]string {
t.Helper()
expected := map[string]section{
"owner": {
map_: map[string]string{
"name": "John Doe",
"organization": "Acme Widgets Inc.",
},
expected := map[string]map[string]string{
"owner": map[string]string{
"name": "John Doe",
"organization": "Acme Widgets Inc.",
},
"database": {
map_: map[string]string{
"server": "192.0.2.62",
"port": "143",
},
"database": map[string]string{
"server": "192.0.2.62",
"port": "143",
},
"section": {
map_: map[string]string{
"key0": "val0",
"key1": "val1",
},
"section": map[string]string{
"key0": "val0",
"key1": "val1",
},
}
return expected
}

func populateExpectedEmptySection(t *testing.T) map[string]section {
func populateExpectedEmptySection(t *testing.T) map[string]map[string]string {
t.Helper()
expected := map[string]section{
"owner": {
map_: map[string]string{
"name": "John Doe",
"organization": "Acme Widgets Inc.",
},
},
"database": {
map_: map[string]string{},
expected := map[string]map[string]string{
"owner": map[string]string{
"name": "John Doe",
"organization": "Acme Widgets Inc.",
},
"database": map[string]string{},
}
return expected
}
Expand Down Expand Up @@ -118,7 +108,7 @@ func TestLoadFromFile(t *testing.T) {
testcases := []struct {
testcaseName string
filePath string
expected map[string]section
expected map[string]map[string]string
err string
}{
{
Expand Down Expand Up @@ -275,7 +265,7 @@ func TestSet(t *testing.T) {
}
err = parser.Set(testcase.sectionName, testcase.key, testcase.value)
if testcase.err == "" {
value := parser.sections[testcase.sectionName].map_[testcase.key]
value := parser.sections[testcase.sectionName][testcase.key]
assertEquality(t, testcase.value, value)
} else {
assertEquality(t, testcase.err, err.Error())
Expand Down

0 comments on commit b35523d

Please sign in to comment.