Skip to content

Commit

Permalink
Merge pull request #29 from Sovietaced/docs
Browse files Browse the repository at this point in the history
Document marshal interface and implementations
  • Loading branch information
Sovietaced authored Dec 21, 2023
2 parents c9e21f3 + 7fb2dda commit 0dc5a8c
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ err = mutex.Unlock(ctx)

### Distributed Map

The `Mapp` struct aims to provide similar semantics to native Go map.
The `Map` struct aims to provide similar semantics to a native Go map.

```go
ctx := context.Background()
Expand Down
3 changes: 3 additions & 0 deletions marshal/json_marshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"encoding/json"
)

// JsonMarshaler is an implementation of Marshaler that uses JSON.
type JsonMarshaler[T any] struct {
}

// Marshal marshals a go struct into a JSON string.
func (jm *JsonMarshaler[T]) Marshal(ctx context.Context, value T) (string, error) {
bytes, err := json.Marshal(value)
if err != nil {
Expand All @@ -17,6 +19,7 @@ func (jm *JsonMarshaler[T]) Marshal(ctx context.Context, value T) (string, error
return string(bytes), nil
}

// Unmarshal unmarshals a go struct from a JSON string into a go struct.
func (jm *JsonMarshaler[T]) Unmarshal(ctx context.Context, valueString string, value *T) error {
return json.Unmarshal([]byte(valueString), value)
}
3 changes: 3 additions & 0 deletions marshal/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import (
"context"
)

// Marshaler is an interface contract for marshalling values from Go structs into strings that can be stored in Redis.
type Marshaler[T any] interface {
// Marshal marshals a go struct into a string.
Marshal(ctx context.Context, value T) (string, error)
// Unmarshal unmarshals a string into a go struct.
Unmarshal(ctx context.Context, valueString string, value *T) error
}

0 comments on commit 0dc5a8c

Please sign in to comment.