github.com/s7techlab/cckit@v0.10.5/extensions/debug/debug_state.go (about)

     1  package debug
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/golang/protobuf/proto"
     7  
     8  	"github.com/s7techlab/cckit/convert"
     9  	"github.com/s7techlab/cckit/router"
    10  	"github.com/s7techlab/cckit/state"
    11  )
    12  
    13  type (
    14  	StateService struct {
    15  		State StateFn
    16  	}
    17  
    18  	// StateFn function can add mappings to state, for correct convertation in StateGet
    19  	StateFn func(router.Context) state.State
    20  )
    21  
    22  var _ DebugStateServiceChaincode = &StateService{}
    23  
    24  func StateAsIs(ctx router.Context) state.State {
    25  	return ctx.State()
    26  }
    27  
    28  func NewStateService() *StateService {
    29  	return &StateService{
    30  		State: StateAsIs,
    31  	}
    32  }
    33  
    34  func (s *StateService) ListKeys(ctx router.Context, prefix *Prefix) (*CompositeKeys, error) {
    35  	keys, err := s.State(ctx).Keys(prefix.GetKey())
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  
    40  	cKeys := &CompositeKeys{}
    41  	for _, keyStr := range keys {
    42  		key, err := state.NormalizeKey(ctx.Stub(), keyStr)
    43  		if err != nil {
    44  			return nil, err
    45  		}
    46  		cKeys.Keys = append(cKeys.Keys, &CompositeKey{Key: key})
    47  	}
    48  
    49  	return cKeys, nil
    50  }
    51  
    52  func (s *StateService) GetState(ctx router.Context, key *CompositeKey) (*Value, error) {
    53  	val, err := s.State(ctx).Get(key.Key)
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  
    58  	bb, err := convert.ToBytes(val)
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  
    63  	var jsonVal []byte
    64  	switch val.(type) {
    65  	case proto.Message:
    66  		jsonVal, _ = json.Marshal(val)
    67  	}
    68  
    69  	return &Value{
    70  		Key:   key.Key,
    71  		Value: bb,
    72  		Json:  string(jsonVal),
    73  	}, nil
    74  }
    75  
    76  func (s *StateService) PutState(ctx router.Context, val *Value) (*Value, error) {
    77  	if err := s.State(ctx).Put(val.Key, val.Value); err != nil {
    78  		return nil, err
    79  	}
    80  	return val, nil
    81  }
    82  
    83  func (s *StateService) DeleteState(ctx router.Context, key *CompositeKey) (*Value, error) {
    84  	val, err := s.GetState(ctx, key)
    85  	if err != nil {
    86  		return nil, err
    87  	}
    88  
    89  	if err = s.State(ctx).Delete(key.Key); err != nil {
    90  		return nil, err
    91  	}
    92  
    93  	return val, nil
    94  }
    95  
    96  func (s *StateService) DeleteStates(ctx router.Context, prefixes *Prefixes) (*PrefixesMatchCount, error) {
    97  	var (
    98  		keys []string
    99  		key  string
   100  		err  error
   101  	)
   102  	for _, p := range prefixes.Prefixes {
   103  		if len(p.Key) > 0 {
   104  			key, err = state.KeyToString(ctx.Stub(), p.Key)
   105  			if err != nil {
   106  				return nil, err
   107  			}
   108  		}
   109  
   110  		keys = append(keys, key)
   111  	}
   112  	matches, err := DeleteStateByPrefixes(s.State(ctx), keys)
   113  	if err != nil {
   114  		return nil, err
   115  	}
   116  
   117  	return &PrefixesMatchCount{Matches: matches}, nil
   118  }