From a26b61975dca2a4d78f4132681596b697fc253b9 Mon Sep 17 00:00:00 2001 From: amorozov Date: Tue, 25 Jun 2024 22:18:44 +0300 Subject: [PATCH] feature: added array list marshalling --- pkg/collections/array_list/array_list_json.go | 20 +++++++++++++++++++ pkg/collections/array_list/array_list_xml.go | 20 +++++++++++++++++++ pkg/collections/array_list/array_list_yaml.go | 20 +++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 pkg/collections/array_list/array_list_json.go create mode 100644 pkg/collections/array_list/array_list_xml.go create mode 100644 pkg/collections/array_list/array_list_yaml.go diff --git a/pkg/collections/array_list/array_list_json.go b/pkg/collections/array_list/array_list_json.go new file mode 100644 index 0000000..77222dd --- /dev/null +++ b/pkg/collections/array_list/array_list_json.go @@ -0,0 +1,20 @@ +package array_list + +import ( + "encoding/json" +) + +func (a *ArrayList[T]) MarshalJSON() ([]byte, error) { + return json.Marshal(a.content[:a.size]) +} + +func (a *ArrayList[T]) UnmarshalJSON(data []byte) error { + var content []T + if err := json.Unmarshal(data, &content); err != nil { + return err + } + + a.content = content + a.size = len(content) + return nil +} diff --git a/pkg/collections/array_list/array_list_xml.go b/pkg/collections/array_list/array_list_xml.go new file mode 100644 index 0000000..3650195 --- /dev/null +++ b/pkg/collections/array_list/array_list_xml.go @@ -0,0 +1,20 @@ +package array_list + +import ( + "encoding/json" +) + +func (a *ArrayList[T]) MarshalXML() ([]byte, error) { + return json.Marshal(a.content[:a.size]) +} + +func (a *ArrayList[T]) UnmarshalXML(data []byte) error { + var content []T + if err := json.Unmarshal(data, &content); err != nil { + return err + } + + a.content = content + a.size = len(content) + return nil +} diff --git a/pkg/collections/array_list/array_list_yaml.go b/pkg/collections/array_list/array_list_yaml.go new file mode 100644 index 0000000..0437e38 --- /dev/null +++ b/pkg/collections/array_list/array_list_yaml.go @@ -0,0 +1,20 @@ +package array_list + +import ( + "encoding/json" +) + +func (a *ArrayList[T]) MarshalYAML() ([]byte, error) { + return json.Marshal(a.content[:a.size]) +} + +func (a *ArrayList[T]) UnmarshalYAML(data []byte) error { + var content []T + if err := json.Unmarshal(data, &content); err != nil { + return err + } + + a.content = content + a.size = len(content) + return nil +}