github.com/s7techlab/cckit@v0.10.5/state/mapping/testdata/cc_withconfig.go (about)

     1  package testdata
     2  
     3  import (
     4  	"github.com/s7techlab/cckit/extensions/owner"
     5  	"github.com/s7techlab/cckit/router"
     6  	"github.com/s7techlab/cckit/router/param/defparam"
     7  	m "github.com/s7techlab/cckit/state/mapping"
     8  	"github.com/s7techlab/cckit/state/mapping/testdata/schema"
     9  )
    10  
    11  func NewCCWithConfig() *router.Chaincode {
    12  	r := router.New(`withConfig`)
    13  
    14  	// Mappings for chaincode state
    15  	r.Use(m.MapStates(m.StateMappings{}.
    16  		//key will be <`config`>
    17  		Add(&schema.Config{}, m.WithConstPKey())))
    18  
    19  	r.Init(owner.InvokeSetFromCreator)
    20  
    21  	r.Group(`config`).
    22  		Invoke(`Set`, configSet, defparam.Proto(&schema.Config{})).
    23  		Invoke(`Get`, configGet)
    24  
    25  	return router.NewChaincode(r)
    26  }
    27  
    28  func configGet(c router.Context) (interface{}, error) {
    29  	return c.State().Get(&schema.Config{}, &schema.Config{})
    30  }
    31  
    32  func configSet(c router.Context) (interface{}, error) {
    33  	conf := c.Param()
    34  	return conf, c.State().Put(conf)
    35  }