Skip to content

Commit

Permalink
Merge pull request #24 from tmontdev/improve/docs
Browse files Browse the repository at this point in the history
adding struct method
  • Loading branch information
tmontdev authored Nov 27, 2023
2 parents b303aa4 + ac67db9 commit e7aed86
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
11 changes: 11 additions & 0 deletions hashmap.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package collections

import "encoding/json"

// MapFrom returns a new HashMap from the given built-in source map.
// Changes in the returned HashMap will not affect the source map
func MapFrom[K comparable, V any](source map[K]V) Map[K, V] {
Expand Down Expand Up @@ -144,3 +146,12 @@ func (m HashMap[K, V]) Builtin() map[K]V {
func (m HashMap[K, V]) HashMap() HashMap[K, V] {
return m
}

func (m HashMap[K, V]) Struct(str any) error {
bytes, err := json.Marshal(m)
if err != nil {
return err
}
err = json.Unmarshal(bytes, str)
return err
}
2 changes: 2 additions & 0 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,6 @@ type Map[K comparable, V any] interface {
Builtin() map[K]V

HashMap() HashMap[K, V]

Struct(str any) error
}

0 comments on commit e7aed86

Please sign in to comment.