Skip to content

Commit

Permalink
add collection
Browse files Browse the repository at this point in the history
  • Loading branch information
Matrix-X committed Jun 11, 2021
1 parent a2b42d6 commit 18c8016
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 19 deletions.
112 changes: 112 additions & 0 deletions object/collection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package object

import "strings"

type Collection struct {
items HashMap
}

func NewCollection(items *HashMap) *Collection {
return &Collection{
items: *items,
}
}

func (c *Collection) All() HashMap {
return c.items
}

func (c *Collection) Only(keys []string) (result *HashMap) {

result = &HashMap{}

for key, value := range c.items {
value = c.Get(key, nil)
if value != nil {
(*result)[key] = value
}
}

return result
}

func (c *Collection) Except(keys []string) HashMap {
return nil
}

func (c *Collection) Merge(items *HashMap) HashMap {
return nil
}

func (c *Collection) Has(key string) bool {
return false
}

func (c *Collection) First() interface{} {
return nil
}

func (c *Collection) Last() interface{} {
return nil
}
func (c *Collection) Add(key string, value interface{}) {

}

func (c *Collection) Set(key string, value interface{}) {

}

// Get an item from an hashMap using "dot" notation.
func (c *Collection) Get(key string, defaultValue interface{}) interface{} {

hashedObject := c.items

if key == "" {
return &hashedObject
}

if hashedObject[key] != nil {
return hashedObject[key]
}

segments := strings.Split(key, ".")
for _, segment := range segments {
if hashedObject[segment] != nil {
return defaultValue
} else {
hashedObject = hashedObject[segment].(HashMap)
}
}

return hashedObject
}

func (c *Collection)Forget(key string){

}

func (c *Collection)ToHashMap() HashMap{
return c.All()
}

func (c *Collection)ToJson(option int) string {
return ""
}
func (c *Collection)ToString() string {
return c.ToJson(0)
}

func (c *Collection)Count() int {
return len(c.items)
}

func (c *Collection)Unserialize(serialized string) HashMap {


return c.items
}




23 changes: 4 additions & 19 deletions object/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ type HashMap map[string]interface{}
type StringMap map[string]string

func MergeHashMap(toMap *HashMap, subMaps ...*HashMap) *HashMap {
if toMap == nil{
if toMap == nil {
toMap = &HashMap{}
}
for _, subMap := range subMaps {
if subMap!=nil{
if subMap != nil {
for k, v := range *subMap {
(*toMap)[k] = v
}
Expand All @@ -23,11 +23,11 @@ func MergeHashMap(toMap *HashMap, subMaps ...*HashMap) *HashMap {
}

func MergeStringMap(toMap *HashMap, subMaps ...*HashMap) *HashMap {
if toMap == nil{
if toMap == nil {
toMap = &HashMap{}
}
for _, subMap := range subMaps {
if subMap!=nil{
if subMap != nil {
for k, v := range *subMap {
(*toMap)[k] = v
}
Expand All @@ -43,18 +43,3 @@ func ConvertStringMapToString(m *StringMap) string {
}
return b.String()
}

// Get an item from an hashMap using "dot" notation.
func Get(hashedObject HashMap, key string, defaultValue interface{}) interface{} {
if key==""{
return &hashedObject
}

if hashedObject[key] !=nil{
return hashedObject[key]
}



return nil
}

0 comments on commit 18c8016

Please sign in to comment.