Skip to content

Commit

Permalink
{insert,replace}.go: Simplify type assertion style and update a code …
Browse files Browse the repository at this point in the history
…comment.
  • Loading branch information
umpc committed Jul 3, 2017
1 parent 30b9208 commit 2b0b2aa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
19 changes: 8 additions & 11 deletions insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ func (sm *SortedMap) BatchInsert(recs []Record) []bool {
return results
}

func (sm *SortedMap) batchInsertMapWithInterfaceKeys(v interface{}) error {
m := v.(map[interface{}]interface{})

func (sm *SortedMap) batchInsertMapInterfaceKeys(m map[interface{}]interface{}) error {
for key, val := range m {
if !sm.insert(key, val) {
return fmt.Errorf("Key already exists: %+v", key)
Expand All @@ -38,9 +36,7 @@ func (sm *SortedMap) batchInsertMapWithInterfaceKeys(v interface{}) error {
return nil
}

func (sm *SortedMap) batchInsertMapWithStringKeys(v interface{}) error {
m := v.(map[string]interface{})

func (sm *SortedMap) batchInsertMapStringKeys(m map[string]interface{}) error {
for key, val := range m {
if !sm.insert(key, val) {
return fmt.Errorf("Key already exists: %+v", key)
Expand All @@ -49,17 +45,18 @@ func (sm *SortedMap) batchInsertMapWithStringKeys(v interface{}) error {
return nil
}

// BatchInsertMap adds all map keys and values to the collection and returns a slice containing each record's insert status.
// If a key already exists, the value will not be inserted. Use BatchReplaceMap for the alternative functionality.
// BatchInsertMap adds all map keys and values to the collection.
// If a key already exists, the value will not be inserted and an error will be returned.
// Use BatchReplaceMap for the alternative functionality.
func (sm *SortedMap) BatchInsertMap(v interface{}) error {
const unsupportedTypeErr = "Unsupported type."

switch v.(type) {
switch m := v.(type) {
case map[interface{}]interface{}:
return sm.batchInsertMapWithInterfaceKeys(v)
return sm.batchInsertMapInterfaceKeys(m)

case map[string]interface{}:
return sm.batchInsertMapWithStringKeys(v)
return sm.batchInsertMapStringKeys(m)

default:
return fmt.Errorf("%s", unsupportedTypeErr)
Expand Down
27 changes: 13 additions & 14 deletions replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,47 @@ func (sm *SortedMap) replace(key, val interface{}) {
}

// Replace uses the provided 'less than' function to insert sort.
// Even if the key already exists, the value will be inserted. Use Insert for the alternative functionality.
// Even if the key already exists, the value will be inserted.
// Use Insert for the alternative functionality.
func (sm *SortedMap) Replace(key, val interface{}) {
sm.replace(key, val)
}

// BatchReplace adds all given records to the collection.
// Even if a key already exists, the value will be inserted. Use BatchInsert for the alternative functionality.
// Even if a key already exists, the value will be inserted.
// Use BatchInsert for the alternative functionality.
func (sm *SortedMap) BatchReplace(recs []Record) {
for _, rec := range recs {
sm.replace(rec.Key, rec.Val)
}
}

func (sm *SortedMap) batchReplaceMapWithInterfaceKeys(v interface{}) error {
m := v.(map[interface{}]interface{})

func (sm *SortedMap) batchReplaceMapInterfaceKeys(m map[interface{}]interface{}) {
for key, val := range m {
sm.replace(key, val)
}
return nil
}

func (sm *SortedMap) batchReplaceMapWithStringKeys(v interface{}) error {
m := v.(map[string]interface{})

func (sm *SortedMap) batchReplaceMapStringKeys(m map[string]interface{}) {
for key, val := range m {
sm.replace(key, val)
}
return nil
}

// BatchReplaceMap adds all map keys and values to the collection.
// Even if a key already exists, the value will be inserted. Use BatchInsertMap for the alternative functionality.
// Even if a key already exists, the value will be inserted.
// Use BatchInsertMap for the alternative functionality.
func (sm *SortedMap) BatchReplaceMap(v interface{}) error {
const unsupportedTypeErr = "Unsupported type."

switch v.(type) {
switch m := v.(type) {
case map[interface{}]interface{}:
return sm.batchReplaceMapWithInterfaceKeys(v)
sm.batchReplaceMapInterfaceKeys(m)
return nil

case map[string]interface{}:
return sm.batchReplaceMapWithStringKeys(v)
sm.batchReplaceMapStringKeys(m)
return nil

default:
return fmt.Errorf("%s", unsupportedTypeErr)
Expand Down

0 comments on commit 2b0b2aa

Please sign in to comment.