Skip to content

Commit

Permalink
Merge pull request #28 from fogfish/object-id-at-gen-interface
Browse files Browse the repository at this point in the history
add object id to gen representation
  • Loading branch information
fogfish authored Feb 8, 2021
2 parents 56fe6d5 + 6e4ffd8 commit e54ab6e
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ type FMap func(Gen) (Thing, error)
//
// Gen is a generic representation of storage type
type Gen interface {
ID() (*ID, error)
To(Thing) error
}

Expand Down
15 changes: 15 additions & 0 deletions ddb.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,21 @@ func (dynamo DB) Update(entity Thing, config ...Config) (err error) {
// dbGen is type alias for generic representation
type dbGen map[string]*dynamodb.AttributeValue

// ID lifts generic representation to its Identity
func (gen dbGen) ID() (*ID, error) {
prefix, isPrefix := gen["prefix"]
suffix, isSuffix := gen["suffix"]
if !isPrefix || !isSuffix {
return nil, errors.New("Invalid DDB schema")
}

pf := aws.StringValue(prefix.S)
sf := aws.StringValue(suffix.S)
id := MkID(curie.New(pf).Join(sf))

return &id, nil
}

// To lifts generic representation to Thing
func (gen dbGen) To(thing Thing) error {
item, err := unmarshal(gen)
Expand Down
10 changes: 10 additions & 0 deletions ddb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ func TestDdbMatchWithFMap(t *testing.T) {
If(pseq).Should().Equal(persons{thing, thing})
}

func TestDdbMatchIDsWithFMap(t *testing.T) {
seq := dynamo.IDs{}
_, err := apiDB().Match(dynamo.NewID("dead:")).FMap(seq.Join)

thing := entity().ID
it.Ok(t).
If(err).Should().Equal(nil).
If(seq).Should().Equal(dynamo.IDs{thing, thing})
}

//-----------------------------------------------------------------------------
//
// Mock Dynamo DB
Expand Down
22 changes: 22 additions & 0 deletions id.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,28 @@ func (id ID) Unwrap() *IRI {

/*
IDs sequence of Identities
*/
type IDs []ID

/*
Join lifts sequence of matched objects to seq of IDs
seq := dynamo.IDs{}
dynamo.Match(...).FMap(seq.Join)
*/
func (seq *IDs) Join(gen Gen) (Thing, error) {
id, err := gen.ID()
if err != nil {
return nil, err
}

*seq = append(*seq, *id)
return id, nil
}

/*
Thing is the most generic type of item. The interfaces declares anything with
unique identifier. Embedding CURIE ID into struct makes it Thing compatible.
Expand Down
20 changes: 20 additions & 0 deletions s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package dynamo
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
"reflect"
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -136,6 +138,24 @@ type s3Gen struct {
key *string
}

// ID lifts generic representation to its Identity
func (gen s3Gen) ID() (*ID, error) {
if gen.key == nil {
return nil, errors.New("End Of Stream")
}

var id ID
seq := strings.SplitN(*gen.key, "/", 2)
switch {
case len(seq) == 2:
id = MkID(curie.New(strings.Join(seq, ":")))
default:
id = MkID(curie.New(*gen.key))
}

return &id, nil
}

// Lifts generic representation to Thing
func (gen s3Gen) To(thing Thing) error {
req := &s3.GetObjectInput{
Expand Down
10 changes: 10 additions & 0 deletions s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ func TestS3MatchWithFMap(t *testing.T) {
If(pseq).Should().Equal(persons{thing, thing})
}

func TestS3MatchIDsWithFMap(t *testing.T) {
seq := dynamo.IDs{}
_, err := apiS3().Match(dynamo.NewID("dead:")).FMap(seq.Join)

thing := entity().ID
it.Ok(t).
If(err).Should().Equal(nil).
If(seq).Should().Equal(dynamo.IDs{thing, thing})
}

//-----------------------------------------------------------------------------
//
// Mock S3
Expand Down

0 comments on commit e54ab6e

Please sign in to comment.