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 +}