Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
add serialize and save methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Timo972 committed Aug 22, 2021
1 parent 6d1ca9d commit a6dae05
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 9 deletions.
34 changes: 25 additions & 9 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package cfg_reader
import "errors"

type Config struct {
Node Dict
Node *Node
Name string
}

func NewConfig(file string) *Config {
Expand All @@ -12,12 +13,8 @@ func NewConfig(file string) *Config {
panic(err)
}
config := &Config{}
dict, ok := node.ToDict()
if !ok {
println("could not convert to dict")
return nil
}
config.Node = dict
config.Node = node
config.Name = file
return config
}

Expand Down Expand Up @@ -65,7 +62,13 @@ func Convert(node *Node) interface{} {
}

func (c Config) Get(key string) (interface{}, error) {
node := c.Node[key]

dict, ok := c.Node.ToDict()
if !ok {
return nil, errors.New("could not convert to dict")
}

node := dict[key]

if node == nil {
return nil, errors.New("key not found")
Expand All @@ -77,5 +80,18 @@ func (c Config) Get(key string) (interface{}, error) {
}

func (c Config) Set(key string, value interface{}) {
c.Node[key] = NewNode(value)
c.Node.Value.(Dict)[key] = NewNode(value)
}

func (c Config) Serialize(useCommas bool, useApostrophes bool) string {
emitter := NewEmitter()
emitter.Emit(c.Node, 0, true, useCommas, useApostrophes)
return emitter.String()
}

func (c Config) Save(useCommas bool, useApostrophes bool) error {
emitter := NewEmitter()
emitter.Emit(c.Node, 0, true, useCommas, useApostrophes)
err := WriteFile(c.Name, emitter.String())
return err
}
82 changes: 82 additions & 0 deletions emitter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package cfg_reader

import (
"errors"
"strings"
)

type Emitter struct {
Stream string
}

func NewEmitter() *Emitter {
return &Emitter{Stream: ""}
}

func (e *Emitter) Emit(node *Node, indent uint, isLast bool, useCommas bool, useApostrophes bool) error {
_indent := strings.Repeat(" ", int(indent * 2))

if node.IsScalar() {
str, ok := node.ToString()
if !ok {
return errors.New("failed to emit")
}
specialChars := strings.Count(str, " ")
if useApostrophes || specialChars > 0 {
e.Stream += "'" + Escape(str) + "'\n"
} else {
e.Stream += Escape(str) + "\n"
}
} else if node.IsList() {
e.Stream += "[\n"

list, ok := node.ToList()
if !ok {
return errors.New("failed to emit list")
}
end := len(list) - 1
for i, node := range list {
e.Stream += _indent
e.Emit(node, indent + 1, i == end, useCommas, useApostrophes)
}
e.Stream += strings.Repeat(" ", int((indent - 1) * 2))
if isLast || !useCommas {
e.Stream += "]\n"
} else {
e.Stream += "],\n"
}
} else if node.IsDict() {
if indent > 0 {
e.Stream += "{\n"
}

dict, ok := node.ToDict()
if !ok {
return errors.New("failed to emit dict")
}
i := 0
endIdx := len(dict) - 1
for key, node := range dict {
if node.IsNone() {
continue
}
e.Stream += _indent + key + ":"
e.Emit(node, indent + 1, i == endIdx, useCommas, useApostrophes)
i++
}

if indent > 0 {
e.Stream += strings.Repeat(" ", int((indent - 1) * 2))
if isLast || !useCommas {
e.Stream += "}\n"
} else {
e.Stream += "},\n"
}
}
}
return nil
}

func (e *Emitter) String() string {
return e.Stream
}
5 changes: 5 additions & 0 deletions read.go → io.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ func ReadFile(fileName string) (*Node, error) {
}

return node, err
}

func WriteFile(fileName string, content string) error {
err := ioutil.WriteFile(fileName, []byte(content), 0644)
return err
}

0 comments on commit a6dae05

Please sign in to comment.