From 8b3914eba5909f4785d908dbb3925fa2c0110b7d Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Fri, 7 Jul 2023 14:38:41 +0200 Subject: [PATCH] Added custom encoding/decoding --- Collection.go | 5 ++--- Decoder.go | 12 ++++++++++++ Encoder.go | 12 ++++++++++++ 3 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 Decoder.go create mode 100644 Encoder.go diff --git a/Collection.go b/Collection.go index 42b7076..b1a3add 100644 --- a/Collection.go +++ b/Collection.go @@ -1,7 +1,6 @@ package ocean import ( - "encoding/json" "os" "path/filepath" "reflect" @@ -168,7 +167,7 @@ func (c *collection[T]) loadFileFromDisk(fileName string) error { } value := new(T) - decoder := json.NewDecoder(file) + decoder := NewDecoder(file) err = decoder.Decode(value) if err != nil { @@ -190,7 +189,7 @@ func (c *collection[T]) writeFileToDisk(key string, value *T) error { return err } - encoder := json.NewEncoder(file) + encoder := NewEncoder(file) err = encoder.Encode(value) if err != nil { diff --git a/Decoder.go b/Decoder.go new file mode 100644 index 0000000..84f9c89 --- /dev/null +++ b/Decoder.go @@ -0,0 +1,12 @@ +package ocean + +import ( + "encoding/json" + "io" +) + +var NewDecoder = func(r io.Reader) Decoder { return json.NewDecoder(r) } + +type Decoder interface { + Decode(any) error +} diff --git a/Encoder.go b/Encoder.go new file mode 100644 index 0000000..40c5940 --- /dev/null +++ b/Encoder.go @@ -0,0 +1,12 @@ +package ocean + +import ( + "encoding/json" + "io" +) + +var NewEncoder = func(w io.Writer) Encoder { return json.NewEncoder(w) } + +type Encoder interface { + Encode(any) error +}