Skip to content

Commit

Permalink
Merge pull request #21 from ulfox/fix/multi-doc
Browse files Browse the repository at this point in the history
fixes: [docs/convert] methods
  • Loading branch information
ulfox authored Sep 6, 2021
2 parents 8663ea3 + 53d2e41 commit 3e652ca
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 84 deletions.
36 changes: 24 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,13 @@ Next, assert **obj** as `map[string]string`
assertData := db.NewConvertFactory()
assertData.Input(val)
if assertData.Error != nil {
logger.Fatalf(assertData.Error.Error())
if assertData.GetError() != nil {
logger.Fatal(assertData.GetError())
}
vMap, err := assertData.GetMap()
if err != nil {
logger.Fatal(err)
}
vMap := assertData.GetMap()
logger.Info(vMap["key-2"])
```
Expand All @@ -285,10 +288,13 @@ assertData.Input(state.Data).
Key("to").
Key("array-1").
Key("key-1").Index(0)
if assertData.Error != nil {
logger.Fatalf(assertData.Error.Error())
if assertData.GetError() != nil {
logger.Fatal(assertData.GetError())
}
vMap, err := assertData.GetMap()
if err != nil {
logger.Fatal(err)
}
vMap := assertData.GetMap()
logger.Info(vMap["key-2"])
```
Expand Down Expand Up @@ -318,10 +324,13 @@ Next, assert **obj** as `[]string`
assertData := db.NewConvertFactory()
assertData.Input(obj)
if assertData.Error != nil {
logger.Fatalf(assertData.Error.Error())
if assertData.GetError() != nil {
logger.Fatal(assertData.GetError())
}
vArray, err := assertData.GetArray()
if err != nil {
logger.Fatal(err)
}
vArray := assertData.GetArray()
logger.Info(vArray)
```
Expand All @@ -335,10 +344,13 @@ We can get the array manually by using only **Convert** operations
assertData.Input(state.Data).
Key("to").
Key("array-2")
if assertData.Error != nil {
logger.Fatalf(assertData.Error.Error())
if assertData.GetError() != nil {
logger.Fatal(assertData.GetError())
}
vArray, err := assertData.GetArray()
if err != nil {
logger.Fatal(err)
}
vArray := assertData.GetArray()
logger.Info(vArray)
```
Expand Down
5 changes: 5 additions & 0 deletions db/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ func NewCacheFactory() Cache {
return cache
}

// GetError returns cache error field
func (c *Cache) GetError() error {
return c.E
}

// Clear for clearing the cache and setting all
// content to nil
func (c *Cache) Clear() *Cache {
Expand Down
147 changes: 88 additions & 59 deletions db/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,125 +9,154 @@ import (
// AssertData is used to for converting interface objects to
// map of interfaces or array of interfaces
type AssertData struct {
D0 map[string]string
A0 []string
Error error
Cache Cache
d0 map[string]string
s0 *string
s1 []string
i0 *int
i1 []int
cache Cache
}

// NewConvertFactory for initializing AssertData
func NewConvertFactory() *AssertData {
assertData := &AssertData{
D0: make(map[string]string),
A0: make([]string, 0),
Cache: NewCacheFactory(),
ad := &AssertData{
d0: make(map[string]string),
s1: make([]string, 0),
cache: NewCacheFactory(),
}
return assertData
return ad
}

// Clear for resetting AssertData
func (a *AssertData) Clear() {
a.Cache.Clear()
a.D0 = make(map[string]string)
a.A0 = make([]string, 0)
a.cache.Clear()
a.d0 = make(map[string]string)
a.s1 = make([]string, 0)
a.i1 = make([]int, 0)
a.i0 = nil
a.s0 = nil
}

// GetError returns the any error set to AssertData
func (a *AssertData) GetError() error {
return a.cache.GetError()
}

func (a *AssertData) setErr(e ...error) *AssertData {
if len(e) > 0 {
a.cache.E = e[0]
}
return a
}

// Input sets a data source that can be used for assertion
func (a *AssertData) Input(o interface{}) *AssertData {
a.Clear()
a.Cache.V1 = o
a.cache.V1 = o
return a
}

func (a *AssertData) toBytes() {
a.Cache.B, a.Cache.E = yaml.Marshal(a.Cache.V1)
if a.Cache.E != nil {
a.Error = a.Cache.E
// GetString asserts the input as string
func (a *AssertData) GetString() (string, error) {
if a.GetError() != nil {
return "", a.GetError()
}

s, isString := a.cache.V1.(string)
if !isString {
a.setErr(wrapErr(fmt.Errorf(notAType, "string"), getFn()))
return "", a.GetError()
}

return s, nil
}

// GetInt asserts the input as int
func (a *AssertData) GetInt() (int, error) {
if a.GetError() != nil {
return 0, a.GetError()
}

i, isInt := a.cache.V1.(int)
if !isInt {
a.setErr(wrapErr(fmt.Errorf(notAType, "int"), getFn()))
return 0, a.GetError()
}

return i, nil
}

// GetMap for converting a map[interface{}]interface{} into a map[string]string
func (a *AssertData) GetMap() map[string]string {
if a.Cache.E != nil {
a.Error = a.Cache.E
return nil
func (a *AssertData) GetMap() (map[string]string, error) {
if a.GetError() != nil {
return nil, a.GetError()
}

a.toBytes()
if a.Cache.E != nil {
return nil
a.cache.B, a.cache.E = yaml.Marshal(a.cache.V1)
if a.GetError() != nil {
return nil, a.GetError()
}

a.Cache.E = yaml.Unmarshal(a.Cache.B, &a.D0)
if a.Cache.E != nil {
a.Error = a.Cache.E
return nil
a.cache.E = yaml.Unmarshal(a.cache.B, &a.d0)
if a.GetError() != nil {
return nil, a.GetError()
}
return a.D0
return a.d0, nil
}

// GetArray for converting a []interface{} to []string
func (a *AssertData) GetArray() []string {
if a.Cache.E != nil {
a.Error = a.Cache.E
return nil
func (a *AssertData) GetArray() ([]string, error) {
if a.GetError() != nil {
return nil, a.GetError()
}

_, isArray := a.Cache.V1.([]interface{})
_, isArray := a.cache.V1.([]interface{})
if !isArray {
a.Cache.E = wrapErr(fmt.Errorf(notArrayObj), getFn())
a.Error = a.Cache.E
return nil
a.setErr(wrapErr(fmt.Errorf(notArrayObj), getFn()))
return nil, a.GetError()
}

a.toBytes()
if a.Cache.E != nil {
return nil
a.cache.B, a.cache.E = yaml.Marshal(a.cache.V1)
if a.GetError() != nil {
return nil, a.GetError()
}

a.Cache.E = yaml.Unmarshal(a.Cache.B, &a.A0)
if a.Cache.E != nil {
a.Error = a.Cache.E
return nil
a.cache.E = yaml.Unmarshal(a.cache.B, &a.s1)
if a.GetError() != nil {
return nil, a.GetError()
}

return a.A0
return a.s1, nil
}

// Key copies initial interface object and returns a map of interfaces{}
// Used to easily pipe interfaces
func (a *AssertData) Key(k string) *AssertData {
if a.Cache.E != nil {
a.Error = a.Cache.E
if a.GetError() != nil {
return a
}

_, isMap := a.Cache.V1.(map[interface{}]interface{})
_, isMap := a.cache.V1.(map[interface{}]interface{})
if !isMap {
a.Cache.E = wrapErr(fmt.Errorf(notAMap), getFn())
a.Error = a.Cache.E
return a
return a.setErr(wrapErr(fmt.Errorf(notAMap), getFn()))
}

a.Cache.V1 = a.Cache.V1.(map[interface{}]interface{})[k]
a.cache.V1 = a.cache.V1.(map[interface{}]interface{})[k]

return a
}

// Index getting an interface{} from a []interface{}
func (a *AssertData) Index(i int) *AssertData {
if a.Cache.E != nil {
a.Error = a.Cache.E
if a.GetError() != nil {
return a
}

_, isArray := a.Cache.V1.([]interface{})
_, isArray := a.cache.V1.([]interface{})
if !isArray {
a.Cache.E = wrapErr(fmt.Errorf(notArrayObj), getFn())
a.Error = a.Cache.E
return a
return a.setErr(wrapErr(fmt.Errorf(notArrayObj), getFn()))
}
a.Cache.V1 = a.Cache.V1.([]interface{})[i]
a.cache.V1 = a.cache.V1.([]interface{})[i]

return a
}
1 change: 1 addition & 0 deletions db/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
libOutOfIndex = "lib out of index"
docNotExists = "doc [%s] does not exist in lib"
fieldNotString = "[%s] with value [%s] is not a string"
notAType = "value is not a %s"
)

func wrapErr(e error, s string) error {
Expand Down
10 changes: 3 additions & 7 deletions db/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (s *Storage) Switch(i int) error {
// AddDoc will add a new document to the stack and will switch
// Active Document index to that document
func (s *Storage) AddDoc() error {
s.AD++
s.AD = len(s.Data)
s.Data = append(s.Data, make(map[interface{}]interface{}))
return s.stateReload()
}
Expand Down Expand Up @@ -198,14 +198,12 @@ func (s *Storage) Read() error {
s.Data = nil
s.Data = make([]interface{}, 0)

var counter int
var data interface{}
dec := yaml.NewDecoder(bytes.NewReader(f))
for {
s.Data = append(s.Data, data)
err := dec.Decode(&s.Data[counter])
err := dec.Decode(&data)
if err == nil {
counter++
s.Data = append(s.Data, data)
data = nil
continue
}
Expand Down Expand Up @@ -236,8 +234,6 @@ func (s *Storage) Write() error {
for _, j := range s.Data {
if j == nil {
continue
} else if v, ok := j.(map[interface{}]interface{}); ok && len(v) == 0 {
continue
}

err := enc.Encode(j)
Expand Down
Loading

0 comments on commit 3e652ca

Please sign in to comment.