Skip to content

Commit

Permalink
Merge pull request #10 from spekary/MergeMap
Browse files Browse the repository at this point in the history
Merge map
  • Loading branch information
spekary authored Jul 4, 2019
2 parents b5d445b + acd363b commit a8ba763
Show file tree
Hide file tree
Showing 24 changed files with 461 additions and 79 deletions.
19 changes: 19 additions & 0 deletions pkg/maps/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,25 @@ func (o *Map) Merge(i MapI) {
})
}

// MergeMap merges the given standard map with the current one. The given one takes precedent on collisions.
func (o *Map) MergeMap(m map[string]interface{}) {
if m == nil {
return
}

if o == nil {
panic("The map must be created before being used.")
}

if o.items == nil {
o.items = make(map[string]interface{}, len(m))
}
for k,v := range m {
o.items[k] = v
}
}


// Equals returns true if all the keys in the given map exist in this map, and the values are the same
func (o *Map) Equals(i MapI) bool {
len := o.Len()
Expand Down
26 changes: 23 additions & 3 deletions pkg/maps/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,32 @@ func ExampleMap_Merge() {

n := NewMap()
n.Set("D",5)
m.Merge(n)
n.Merge(m)

fmt.Println(m.Get("D"))
//Output: 5
fmt.Println(n.Get("C"))
fmt.Println(n.Get("D"))
// Output: Other
// 5
}

func ExampleMap_MergeMap() {
m := map[string]interface{} {
"B": "This",
"A": "That",
"C": 6.1,
}

n := NewMap()
n.Set("D","Last")
n.MergeMap(m)

fmt.Println(n.Get("C"))
fmt.Println(n.Get("D"))
// Output: 6.1
// Last
}


func ExampleNewMapFrom() {
n := NewMap()
n.Set("a", "this")
Expand Down
12 changes: 0 additions & 12 deletions pkg/maps/mapi2.go

This file was deleted.

21 changes: 21 additions & 0 deletions pkg/maps/safemap.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,27 @@ func (o *SafeMap) Merge(i MapI) {
})
}

// MergeMap merges the given standard map with the current one. The given one takes precedent on collisions.
func (o *SafeMap) MergeMap(m map[string]interface{}) {
if m == nil {
return
}

if o == nil {
panic("The map must be created before being used.")
}
o.Lock()
defer o.Unlock()

if o.items == nil {
o.items = make(map[string]interface{}, len(m))
}
for k,v := range m {
o.items[k] = v
}
}


// Equals returns true if all the keys in the given map exist in this map, and the values are the same
func (o *SafeMap) Equals(i MapI) bool {
len := o.Len()
Expand Down
26 changes: 23 additions & 3 deletions pkg/maps/safemap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,32 @@ func ExampleSafeMap_Merge() {

n := NewSafeMap()
n.Set("D",5)
m.Merge(n)
n.Merge(m)

fmt.Println(m.Get("D"))
//Output: 5
fmt.Println(n.Get("C"))
fmt.Println(n.Get("D"))
// Output: Other
// 5
}

func ExampleSafeMap_MergeMap() {
m := map[string]interface{} {
"B": "This",
"A": "That",
"C": 6.1,
}

n := NewSafeMap()
n.Set("D","Last")
n.MergeMap(m)

fmt.Println(n.Get("C"))
fmt.Println(n.Get("D"))
// Output: 6.1
// Last
}


func ExampleNewSafeMapFrom() {
n := NewSafeMap()
n.Set("a", "this")
Expand Down
19 changes: 17 additions & 2 deletions pkg/maps/safeslicemap.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func NewSafeSliceMapFromMap(i map[string]interface{}) *SafeSliceMap {
// on an ongoing basis. Normally, items will iterate in the order they were added.
// The sort function is a Less function, that returns true when item 1 is "less" than item 2.
// The sort function receives both the keys and values, so it can use either to decide how to sort.
func (o *SafeSliceMap) SetSortFunc(f func(key1,key2 string, val1, val2 interface{}) bool) {
func (o *SafeSliceMap) SetSortFunc(f func(key1,key2 string, val1, val2 interface{}) bool) *SafeSliceMap {
o.Lock()
o.lessF = f
if f != nil && len(o.order) > 0 {
Expand All @@ -66,11 +66,14 @@ func (o *SafeSliceMap) SetSortFunc(f func(key1,key2 string, val1, val2 interface
})
}
o.Unlock()

return o
}

// SortByKeys sets up the map to have its sort order sort by keys, lowest to highest
func (o *SafeSliceMap) SortByKeys() {
func (o *SafeSliceMap) SortByKeys() *SafeSliceMap {
o.SetSortFunc(keySortSafeSliceMap)
return o
}

func keySortSafeSliceMap(key1, key2 string, val1, val2 interface{}) bool {
Expand Down Expand Up @@ -434,6 +437,18 @@ func (o *SafeSliceMap) Merge(i MapI) {
}
}

// MergeMap merges the given standard map with the current one. The given one takes precedent on collisions.
func (o *SafeSliceMap) MergeMap(m map[string]interface{}) {
if m == nil {
return
}

for k,v := range m {
o.Set(k, v)
}
}


// Range will call the given function with every key and value in the order
// they were placed in the map, or in if you sorted the map, in your custom order.
// If f returns false, it stops the iteration. This pattern is taken from sync.Map.
Expand Down
28 changes: 23 additions & 5 deletions pkg/maps/safeslicemap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,34 @@ func ExampleSafeSliceMap_Merge() {

m.Set("B", "This")
m.Set("A", "That")
m.Set("C", "Other")
m.Set("C", 5)

n := new (Map)
n := new (SafeSliceMap)
n.SortByKeys()
n.Set("D", "Last")
m.Merge(n)
n.Merge(m)
values := n.Values()
fmt.Println(values)
//Output: [That This 5 Last]
}

fmt.Println(m.GetAt(3))
//Output: Last
func ExampleSafeSliceMap_MergeMap() {
m := map[string]interface{} {
"B": "This",
"A": "That",
"C": 5,
}

n := NewSafeSliceMap()
n.SortByKeys()
n.Set("D","Last")
n.MergeMap(m)
values := n.Values()
fmt.Println(values)
// Output: [That This 5 Last]
}


func ExampleSafeSliceMap_Values() {
m := new (SafeSliceMap)
m.Set("B", "This")
Expand Down
21 changes: 21 additions & 0 deletions pkg/maps/safestrmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,27 @@ func (o *SafeStringMap) Merge(i StringMapI) {
})
}

// MergeMap merges the given standard map with the current one. The given one takes precedent on collisions.
func (o *SafeStringMap) MergeMap(m map[string]string) {
if m == nil {
return
}

if o == nil {
panic("The map must be created before being used.")
}
o.Lock()
defer o.Unlock()

if o.items == nil {
o.items = make(map[string]string, len(m))
}
for k,v := range m {
o.items[k] = v
}
}


// Equals returns true if all the keys in the given map exist in this map, and the values are the same
func (o *SafeStringMap) Equals(i StringMapI) bool {
len := o.Len()
Expand Down
26 changes: 23 additions & 3 deletions pkg/maps/safestrmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,32 @@ func ExampleSafeStringMap_Merge() {

n := NewSafeStringMap()
n.Set("D","Last")
m.Merge(n)
n.Merge(m)

fmt.Println(m.Get("D"))
//Output: Last
fmt.Println(n.Get("C"))
fmt.Println(n.Get("D"))
// Output: Other
// Last
}

func ExampleSafeStringMap_MergeMap() {
m := map[string]string {
"B": "This",
"A": "That",
"C": "Other",
}

n := NewSafeStringMap()
n.Set("D","Last")
n.MergeMap(m)

fmt.Println(n.Get("C"))
fmt.Println(n.Get("D"))
// Output: Other
// Last
}


func ExampleNewSafeStringMapFrom() {
n:= NewSafeStringMap()
n.Set("a", "this")
Expand Down
19 changes: 17 additions & 2 deletions pkg/maps/safestrslicemap.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func NewSafeStringSliceMapFromMap(i map[string]string) *SafeStringSliceMap {
// on an ongoing basis. Normally, items will iterate in the order they were added.
// The sort function is a Less function, that returns true when item 1 is "less" than item 2.
// The sort function receives both the keys and values, so it can use either to decide how to sort.
func (o *SafeStringSliceMap) SetSortFunc(f func(key1,key2 string, val1, val2 string) bool) {
func (o *SafeStringSliceMap) SetSortFunc(f func(key1,key2 string, val1, val2 string) bool) *SafeStringSliceMap {
o.Lock()
o.lessF = f
if f != nil && len(o.order) > 0 {
Expand All @@ -66,11 +66,14 @@ func (o *SafeStringSliceMap) SetSortFunc(f func(key1,key2 string, val1, val2 str
})
}
o.Unlock()

return o
}

// SortByKeys sets up the map to have its sort order sort by keys, lowest to highest
func (o *SafeStringSliceMap) SortByKeys() {
func (o *SafeStringSliceMap) SortByKeys() *SafeStringSliceMap {
o.SetSortFunc(keySortSafeStringSliceMap)
return o
}

func keySortSafeStringSliceMap(key1, key2 string, val1, val2 string) bool {
Expand Down Expand Up @@ -407,6 +410,18 @@ func (o *SafeStringSliceMap) Merge(i StringMapI) {
}
}

// MergeMap merges the given standard map with the current one. The given one takes precedent on collisions.
func (o *SafeStringSliceMap) MergeMap(m map[string]string) {
if m == nil {
return
}

for k,v := range m {
o.Set(k, v)
}
}


// Range will call the given function with every key and value in the order
// they were placed in the map, or in if you sorted the map, in your custom order.
// If f returns false, it stops the iteration. This pattern is taken from sync.Map.
Expand Down
26 changes: 22 additions & 4 deletions pkg/maps/safestrslicemap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,32 @@ func ExampleSafeStringSliceMap_Merge() {
m.Set("A", "That")
m.Set("C", "Other")

n := new (StringMap)
n := new (SafeStringSliceMap)
n.SortByKeys()
n.Set("D", "Last")
m.Merge(n)
n.Merge(m)
values := n.Values()
fmt.Println(values)
//Output: [That This Other Last]
}

fmt.Println(m.GetAt(3))
//Output: Last
func ExampleSafeStringSliceMap_MergeMap() {
m := map[string]string {
"B": "This",
"A": "That",
"C": "Other",
}

n := NewSafeStringSliceMap()
n.SortByKeys()
n.Set("D","Last")
n.MergeMap(m)
values := n.Values()
fmt.Println(values)
// Output: [That This Other Last]
}


func ExampleSafeStringSliceMap_Delete() {
n:= map[string]string{"a":"this","b":"that","c":"other"}
m := NewSafeStringSliceMapFromMap(n)
Expand Down
Loading

0 comments on commit a8ba763

Please sign in to comment.