Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
WIP: protobuf state
Browse files Browse the repository at this point in the history
  • Loading branch information
vitiko committed Feb 14, 2019
1 parent 67280e0 commit 32503f4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
17 changes: 10 additions & 7 deletions examples/payment/cc_enc_context_with_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/s7techlab/cckit/extensions/encryption"
"github.com/s7techlab/cckit/router"
p "github.com/s7techlab/cckit/router/param"
"github.com/s7techlab/cckit/state"
m "github.com/s7techlab/cckit/state/mapping"
)

Expand Down Expand Up @@ -45,14 +46,16 @@ func invokePaymentCreateWithDefaultContext(c router.Context) (interface{}, error

func queryPaymentsWithDefaultContext(c router.Context) (interface{}, error) {

paymentType := c.ParamString(`type`)
namespace, err := c.State().(m.MappedState).MappingNamespace(&schema.Payment{})
if err != nil {
return nil, err
}
//paymentType := c.ParamString(`type`)
//namespace, err := c.State().(m.MappedState).MappingNamespace(&schema.Payment{})
//if err != nil {
// return nil, err
//}
//return c.State().List(namespace.Append(state.Key { paymentType }), &schema.Payment{})

// State use encryption setting from context
return c.State().List(namespace.Add(paymentType), &schema.Payment{})
// some sugar to previous

return c.State().(m.MappedState).ListWith(&schema.Payment{}, state.Key{c.ParamString(`type`)})
}

func queryPaymentWithDefaultContext(c router.Context) (interface{}, error) {
Expand Down
1 change: 0 additions & 1 deletion examples/payment/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ var (
// State mappings
StateMappings = m.StateMappings{}.
Add(&schema.Payment{}, m.StatePKeyFromAttrs(`Type`, `Id`)) //key will be <'Payment',Type, Id>

)
11 changes: 11 additions & 0 deletions state/mapping/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import (
type (
MappedState interface {
state.State
// MappingNamespace returns mapping for schema
MappingNamespace(schema interface{}) (state.Key, error)
// ListWith extends schema namespace with key
ListWith(schema interface{}, key state.Key) (result []interface{}, err error)
}

StateImpl struct {
Expand Down Expand Up @@ -93,6 +96,14 @@ func (s *StateImpl) List(namespace interface{}, target ...interface{}) (result [
return s.state.List(namespace, target...)
}

func (s *StateImpl) ListWith(schema interface{}, key state.Key) (result []interface{}, err error) {
namespace, err := s.MappingNamespace(schema)
if err != nil {
return nil, err
}
return s.state.List(namespace.Append(key), schema)
}

func (s *StateImpl) Delete(entry interface{}) (err error) {
if entry, err = s.mapIfMappingExists(entry); err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ type State interface {
UseStatePutTransformer(ToBytesTransformer) State
}

func (k Key) Add(part string) Key {
return append(k, part)
func (k Key) Append(key Key) Key {
return append(k, key...)
}

type StateImpl struct {
Expand Down

0 comments on commit 32503f4

Please sign in to comment.