From ac67db969883150b4fda02693d4c2990a07a88d3 Mon Sep 17 00:00:00 2001 From: Thales Monteiro Date: Mon, 27 Nov 2023 14:54:52 -0300 Subject: [PATCH] adding struct method --- hashmap.go | 11 +++++++++++ map.go | 2 ++ 2 files changed, 13 insertions(+) diff --git a/hashmap.go b/hashmap.go index 807cd5f..1e06cc8 100644 --- a/hashmap.go +++ b/hashmap.go @@ -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] { @@ -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 +} diff --git a/map.go b/map.go index d191f7d..1df9f05 100644 --- a/map.go +++ b/map.go @@ -55,4 +55,6 @@ type Map[K comparable, V any] interface { Builtin() map[K]V HashMap() HashMap[K, V] + + Struct(str any) error }