Added custom encoding/decoding

This commit is contained in:
Eduard Urbach 2023-07-07 14:38:41 +02:00
parent bb3a571b01
commit 8b3914eba5
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
3 changed files with 26 additions and 3 deletions

View File

@ -1,7 +1,6 @@
package ocean package ocean
import ( import (
"encoding/json"
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
@ -168,7 +167,7 @@ func (c *collection[T]) loadFileFromDisk(fileName string) error {
} }
value := new(T) value := new(T)
decoder := json.NewDecoder(file) decoder := NewDecoder(file)
err = decoder.Decode(value) err = decoder.Decode(value)
if err != nil { if err != nil {
@ -190,7 +189,7 @@ func (c *collection[T]) writeFileToDisk(key string, value *T) error {
return err return err
} }
encoder := json.NewEncoder(file) encoder := NewEncoder(file)
err = encoder.Encode(value) err = encoder.Encode(value)
if err != nil { if err != nil {

12
Decoder.go Normal file
View File

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

12
Encoder.go Normal file
View File

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