merge into one file

parent 6afd7b10
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ChangeListManager">
<list default="true" id="de83b181-e854-4be8-81f5-73a2fdb02762" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
</list>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileEditorManager">
<leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
<file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/gongxianghui/deposit.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="95">
<caret line="5" column="49" selection-start-line="5" selection-start-column="40" selection-end-line="5" selection-end-column="49" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/gongxianghui/gongxianghui.go">
<provider selected="true" editor-type-id="text-editor" />
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/gongxianghui/gongxianghui_test.go">
<provider selected="true" editor-type-id="text-editor" />
</entry>
</file>
</leaf>
</component>
<component name="GOROOT" path="/usr/local/go" /> <component name="GOROOT" path="/usr/local/go" />
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="ProjectFrameBounds" extendedState="6"> <component name="ProjectFrameBounds" extendedState="6">
<option name="x" value="120" /> <option name="x" value="1" />
<option name="y" value="27" /> <option name="y" value="25" />
<option name="width" value="1657" /> <option name="width" value="639" />
<option name="height" value="861" /> <option name="height" value="455" />
</component>
<component name="ProjectLevelVcsManager" settingsEditedManually="true" />
<component name="ProjectView">
<navigator proportions="" version="1">
<foldersAlwaysOnTop value="true" />
</navigator>
<panes>
<pane id="ProjectPane">
<subPane>
<expand>
<path>
<item name="chaincode" type="b2602c69:ProjectViewProjectNode" />
<item name="chaincode" type="462c0819:PsiDirectoryNode" />
</path>
<path>
<item name="chaincode" type="b2602c69:ProjectViewProjectNode" />
<item name="chaincode" type="462c0819:PsiDirectoryNode" />
<item name="gongxianghui" type="462c0819:PsiDirectoryNode" />
</path>
</expand>
<select />
</subPane>
</pane>
<pane id="Scope" />
</panes>
</component> </component>
<component name="PropertiesComponent"> <component name="PropertiesComponent">
<property name="SHARE_PROJECT_CONFIGURATION_FILES" value="true" />
<property name="WebServerToolWindowFactoryState" value="false" /> <property name="WebServerToolWindowFactoryState" value="false" />
<property name="go.gopath.indexing.explicitly.defined" value="true" /> <property name="go.gopath.indexing.explicitly.defined" value="true" />
<property name="go.import.settings.migrated" value="true" /> <property name="go.import.settings.migrated" value="true" />
...@@ -16,6 +78,18 @@ ...@@ -16,6 +78,18 @@
<property name="nodejs_interpreter_path.stuck_in_default_project" value="undefined stuck path" /> <property name="nodejs_interpreter_path.stuck_in_default_project" value="undefined stuck path" />
<property name="nodejs_npm_path_reset_for_default_project" value="true" /> <property name="nodejs_npm_path_reset_for_default_project" value="true" />
</component> </component>
<component name="RunDashboard">
<option name="ruleStates">
<list>
<RuleState>
<option name="name" value="ConfigurationTypeDashboardGroupingRule" />
</RuleState>
<RuleState>
<option name="name" value="StatusDashboardGroupingRule" />
</RuleState>
</list>
</option>
</component>
<component name="RunManager"> <component name="RunManager">
<configuration name="TestGXHCC in github.com/hyperledger/chaincode/gongxianghui" type="GoTestRunConfiguration" factoryName="Go Test" temporary="true" nameIsGenerated="true"> <configuration name="TestGXHCC in github.com/hyperledger/chaincode/gongxianghui" type="GoTestRunConfiguration" factoryName="Go Test" temporary="true" nameIsGenerated="true">
<module name="chaincode" /> <module name="chaincode" />
...@@ -34,4 +108,54 @@ ...@@ -34,4 +108,54 @@
</list> </list>
</recent_temporary> </recent_temporary>
</component> </component>
<component name="TestHistory">
<history-entry file="TestGXHCC_in_github_com_hyperledger_chaincode_gongxianghui - 2020.02.18 at 18h 01m 03s.xml">
<configuration name="TestGXHCC in github.com/hyperledger/chaincode/gongxianghui" configurationId="GoTestRunConfiguration" />
</history-entry>
</component>
<component name="ToolWindowManager">
<frame x="67" y="25" width="1853" height="925" extended-state="6" />
<editor active="true" />
<layout>
<window_info content_ui="combo" id="Project" order="0" visible="true" weight="0.25263157" />
<window_info id="Structure" order="1" side_tool="true" weight="0.25" />
<window_info id="Favorites" order="2" side_tool="true" />
<window_info anchor="bottom" id="Message" order="0" />
<window_info anchor="bottom" id="Find" order="1" />
<window_info active="true" anchor="bottom" id="Run" order="2" visible="true" weight="0.3308176" />
<window_info anchor="bottom" id="Debug" order="3" weight="0.4" />
<window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
<window_info anchor="bottom" id="TODO" order="6" />
<window_info anchor="bottom" id="Docker" order="7" show_stripe_button="false" />
<window_info anchor="bottom" id="Version Control" order="8" />
<window_info anchor="bottom" id="Database Changes" order="9" />
<window_info anchor="bottom" id="Statistic" order="10" />
<window_info anchor="bottom" id="Terminal" order="11" />
<window_info anchor="bottom" id="Event Log" order="12" side_tool="true" />
<window_info anchor="right" id="Commander" internal_type="SLIDING" order="0" type="SLIDING" weight="0.4" />
<window_info anchor="right" id="Ant Build" order="1" weight="0.25" />
<window_info anchor="right" content_ui="combo" id="Hierarchy" order="2" weight="0.25" />
<window_info anchor="right" id="PlantUML" order="3" />
<window_info anchor="right" id="Database" order="4" />
</layout>
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="1" />
</component>
<component name="editorHistoryManager">
<entry file="file://$PROJECT_DIR$/gongxianghui/gongxianghui.go">
<provider selected="true" editor-type-id="text-editor" />
</entry>
<entry file="file://$PROJECT_DIR$/gongxianghui/gongxianghui_test.go">
<provider selected="true" editor-type-id="text-editor" />
</entry>
<entry file="file://$PROJECT_DIR$/gongxianghui/deposit.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="95">
<caret line="5" column="49" selection-start-line="5" selection-start-column="40" selection-end-line="5" selection-end-column="49" />
</state>
</provider>
</entry>
</component>
</project> </project>
\ No newline at end of file
package main
import (
"encoding/json"
"fmt"
"github.com/hyperledger/fabric/core/chaincode/shim"
)
/*
put
args 字段说明:
1:args[0]代表我们具体操作的表,
2:args[1:]后面的数据表示要记录到数据库的数据,每一个string 必须是json字符串,并且需要每一个json 字符串都必须包含id 字段
*/
func put(args []string, stub shim.ChaincodeStubInterface) (string, error) {
if len(args) < 2 {
return "", fmt.Errorf("Expected more than 2 parameters to function put!")
}
var mapResult map[string]interface{}
var schemaMap map[string]interface{}
schema := &Schema{}
result, err := schema.get(args[0], stub)
if err != nil {
return "", err
}
json.Unmarshal([]byte(result),&schemaMap)
for k, v := range args[1:] {
if err := json.Unmarshal([]byte(v), &mapResult); err != nil {
return "", fmt.Errorf("parameters Unmarshal fail,args index %d string not json string, err:%s", k, err)
}
Id ,ok:= mapResult["id"]
if !ok{
return "", fmt.Errorf("The id field must exist ")
}
if result,err := stub.GetState(args[0]+Id.(string));err!=nil{
return "", fmt.Errorf("get %s data happen err: ", args[0]+Id.(string))
}else if result!=nil{
return "", fmt.Errorf("%s data already exist", args[0]+Id.(string))
}
if !SchemaCheck(schemaMap,mapResult){
return "", fmt.Errorf("SchemaCheck fail,args index %d string doesn't match %s format", k,args[0])
}
putDate,_:=json.Marshal(mapResult)
if err := stub.PutState(args[0]+Id.(string), putDate); err != nil {
return "", fmt.Errorf("PutState fail, err :%s", err)
}
}
return fmt.Sprintf("%s put data success!", args[0]), nil
}
/*
修改一个表的一条数据的一个或者多个字段值
*/
func update(args []string, stub shim.ChaincodeStubInterface) (string, error) {
if len(args) != 2 {
return "", fmt.Errorf("Expected 2 parameters to function put! ")
}
var mapResult map[string]interface{}
var allMap map[string]interface{}
if err := json.Unmarshal([]byte(args[1]), &mapResult); err != nil {
return "", fmt.Errorf("parameters Unmarshal fail,args index %d string not json string,err: ", err)
}
Id ,ok:= mapResult["id"]
if !ok{
return "", fmt.Errorf("The id field must exist ")
}
result, err := stub.GetState(args[0] + Id.(string))
if err != nil || result == nil {
return "", fmt.Errorf("GetState data fail,please check your parameters ")
}
if err := json.Unmarshal(result, &allMap); err != nil {
return "", fmt.Errorf("result parameters Unmarshal fail err:%s ", err)
}
if err := Deassign(mapResult, allMap); err != nil {
return "", err
} else {
resultByte, _:= json.Marshal(mapResult)
if err := stub.PutState(args[0]+Id.(string), resultByte);err!=nil{
return "", fmt.Errorf("updateState date fail, err :%s ", err)
}
return fmt.Sprintf("%s update data success!", args[0]), nil
}
}
func get(args []string, stub shim.ChaincodeStubInterface) (string, error) {
if len(args) != 2 {
return "", fmt.Errorf("Expected 2 parameters to function put!")
}
var mapResult map[string]interface{}
if err := json.Unmarshal([]byte(args[1]), &mapResult); err != nil {
return "", fmt.Errorf("parameters Unmarshal fail,args index string not json string,err: %s", err)
}
Id := mapResult["id"]
Id ,ok:= mapResult["id"]
if !ok{
return "", fmt.Errorf("The id field must exist ")
}
result, err := stub.GetState(args[0] + Id.(string))
if err != nil || result == nil {
return "", fmt.Errorf("GetState data fail,please check your parameters ")
}
return string(result), nil
}
/*
重新赋值,并检查相应的格式是否正确
*/
func Deassign(upMap ,result map[string]interface{}) error {
if len(upMap) > len(result){
return fmt.Errorf("Deassign fail,paramters format doesn't match! ",)
}
for k, v := range upMap {
if resultV,ok := result[k];ok{
resultMap, resultok := resultV.(map[string]interface{})
if vMap, ok := v.(map[string]interface{});ok {
if resultok{
Deassign(vMap,resultMap)
}else{
return fmt.Errorf("Deassign fail, format doesn't match! ")
}
}else if !resultok{
result[k] = v
}
}else{
return fmt.Errorf("Deassign fail %s key nonentity ",k)
}
}
return nil
}
func SchemaCheck(schema, checkmap map[string]interface{}) bool {
if len(schema) != len(checkmap) {
return false
}
for k, v := range checkmap {
if schemaV, ok := schema[k]; !ok {
return false
} else {
if parameterS, ok := schemaV.(map[string]interface{}); ok {
if parameterC, ok := v.(map[string]interface{}); !ok {
return false
} else {
if !SchemaCheck(parameterS, parameterC) {
return false
}
}
}
}
}
return true
}
package main package main
import ( import (
"encoding/json"
"fmt" "fmt"
"github.com/hyperledger/fabric/core/chaincode/shim" "github.com/hyperledger/fabric/core/chaincode/shim"
pb "github.com/hyperledger/fabric/protos/peer" pb "github.com/hyperledger/fabric/protos/peer"
) )
// EncCC example simple Chaincode implementation of a chaincode that uses encryption/signatures
type GXHCC struct { type GXHCC struct {
} }
type Schema struct {
}
// Init does nothing for this cc // Init does nothing for this cc
func (t *GXHCC) Init(stub shim.ChaincodeStubInterface) pb.Response { func (t *GXHCC) Init(stub shim.ChaincodeStubInterface) pb.Response {
return shim.Success([]byte("GXHCC init successful! ")) return shim.Success([]byte("GXHCC init successful! "))
...@@ -57,6 +63,261 @@ func (t *GXHCC) Invoke(stub shim.ChaincodeStubInterface) pb.Response { ...@@ -57,6 +63,261 @@ func (t *GXHCC) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
return shim.Success([]byte("GXHCC invoke successful ")) return shim.Success([]byte("GXHCC invoke successful "))
} }
/*
put
args 字段说明:
1:args[0]代表我们具体操作的表,
2:args[1:]后面的数据表示要记录到数据库的数据,每一个string 必须是json字符串,并且需要每一个json 字符串都必须包含id 字段
*/
func put(args []string, stub shim.ChaincodeStubInterface) (string, error) {
if len(args) < 2 {
return "", fmt.Errorf("Expected more than 2 parameters to function put!")
}
var mapResult map[string]interface{}
var schemaMap map[string]interface{}
schema := &Schema{}
result, err := schema.get(args[0], stub)
if err != nil {
return "", err
}
json.Unmarshal([]byte(result),&schemaMap)
for k, v := range args[1:] {
if err := json.Unmarshal([]byte(v), &mapResult); err != nil {
return "", fmt.Errorf("parameters Unmarshal fail,args index %d string not json string, err:%s", k, err)
}
Id ,ok:= mapResult["id"]
if !ok{
return "", fmt.Errorf("The id field must exist ")
}
if result,err := stub.GetState(args[0]+Id.(string));err!=nil{
return "", fmt.Errorf("get %s data happen err: ", args[0]+Id.(string))
}else if result!=nil{
return "", fmt.Errorf("%s data already exist", args[0]+Id.(string))
}
if !SchemaCheck(schemaMap,mapResult){
return "", fmt.Errorf("SchemaCheck fail,args index %d string doesn't match %s format", k,args[0])
}
putDate,_:=json.Marshal(mapResult)
if err := stub.PutState(args[0]+Id.(string), putDate); err != nil {
return "", fmt.Errorf("PutState fail, err :%s", err)
}
}
return fmt.Sprintf("%s put data success!", args[0]), nil
}
/*
修改一个表的一条数据的一个或者多个字段值
*/
func update(args []string, stub shim.ChaincodeStubInterface) (string, error) {
if len(args) != 2 {
return "", fmt.Errorf("Expected 2 parameters to function put! ")
}
var mapResult map[string]interface{}
var allMap map[string]interface{}
if err := json.Unmarshal([]byte(args[1]), &mapResult); err != nil {
return "", fmt.Errorf("parameters Unmarshal fail,args index %d string not json string,err: ", err)
}
Id ,ok:= mapResult["id"]
if !ok{
return "", fmt.Errorf("The id field must exist ")
}
result, err := stub.GetState(args[0] + Id.(string))
if err != nil || result == nil {
return "", fmt.Errorf("GetState data fail,please check your parameters ")
}
if err := json.Unmarshal(result, &allMap); err != nil {
return "", fmt.Errorf("result parameters Unmarshal fail err:%s ", err)
}
if err := Deassign(mapResult, allMap); err != nil {
return "", err
} else {
resultByte, _:= json.Marshal(mapResult)
if err := stub.PutState(args[0]+Id.(string), resultByte);err!=nil{
return "", fmt.Errorf("updateState date fail, err :%s ", err)
}
return fmt.Sprintf("%s update data success!", args[0]), nil
}
}
func get(args []string, stub shim.ChaincodeStubInterface) (string, error) {
if len(args) != 2 {
return "", fmt.Errorf("Expected 2 parameters to function put!")
}
var mapResult map[string]interface{}
if err := json.Unmarshal([]byte(args[1]), &mapResult); err != nil {
return "", fmt.Errorf("parameters Unmarshal fail,args index string not json string,err: %s", err)
}
Id := mapResult["id"]
Id ,ok:= mapResult["id"]
if !ok{
return "", fmt.Errorf("The id field must exist ")
}
result, err := stub.GetState(args[0] + Id.(string))
if err != nil || result == nil {
return "", fmt.Errorf("GetState data fail,please check your parameters ")
}
return string(result), nil
}
/*
重新赋值,并检查相应的格式是否正确
*/
func Deassign(upMap ,result map[string]interface{}) error {
if len(upMap) > len(result){
return fmt.Errorf("Deassign fail,paramters format doesn't match! ",)
}
for k, v := range upMap {
if resultV,ok := result[k];ok{
resultMap, resultok := resultV.(map[string]interface{})
if vMap, ok := v.(map[string]interface{});ok {
if resultok{
Deassign(vMap,resultMap)
}else{
return fmt.Errorf("Deassign fail, format doesn't match! ")
}
}else if !resultok{
result[k] = v
}
}else{
return fmt.Errorf("Deassign fail %s key nonentity ",k)
}
}
return nil
}
func SchemaCheck(schema, checkmap map[string]interface{}) bool {
if len(schema) != len(checkmap) {
return false
}
for k, v := range checkmap {
if schemaV, ok := schema[k]; !ok {
return false
} else {
if parameterS, ok := schemaV.(map[string]interface{}); ok {
if parameterC, ok := v.(map[string]interface{}); !ok {
return false
} else {
if !SchemaCheck(parameterS, parameterC) {
return false
}
}
}
}
}
return true
}
func SchemaProcess(args []string, stub shim.ChaincodeStubInterface)(string, error){
if len(args) < 2{
return "", fmt.Errorf("Expect features with 2 or more parameters to play! ")
}
schema := &Schema{}
switch args[0] {
case "put":
return schema.put(args[1:],stub)
case "update":
return schema.update(args[1:],stub)
case "get":
return schema.get(args[1],stub)
default:
return "",fmt.Errorf(fmt.Sprintf("Unsupported schema function of %s", args[0]))
}
}
/*
put
args 字段说明:
1:args[0]代表我们具体操作的表,
2:args[1:]后面的数据表示要记录到数据库的数据,每一个string 必须是json字符串,并且需要每一个json 字符串都必须包含id 字段
我们可以发现json字符串都被我们解析成了map[string]interface{}类型的map,其中key代表表模板的字段,interface可以是一个结构,我们当前将他看做我们表模板字段的初始值。
*/
func (this *Schema) put(args []string, stub shim.ChaincodeStubInterface) (string, error) {
if len(args) != 2 {
return "", fmt.Errorf("Expected 2 parameters to function put!")
}
var mapResult map[string]interface{}
if err := json.Unmarshal([]byte(args[1]), &mapResult); err != nil {
return "", fmt.Errorf("parameters Unmarshal fail,args string not json string,err: %s", err)
}else{
if extrastr,ok := mapResult["extra"];ok{
var extraResult map[string]interface{}
if err := json.Unmarshal([]byte(extrastr.(string)), &extraResult); err != nil {
return "", fmt.Errorf("parameters Unmarshal fail,extra string not json string,err: %s", err)
}
}
}
_ ,ok:= mapResult["id"]
if !ok{
return "", fmt.Errorf("The id field must exist ")
}
if value,_ :=stub.GetState(args[0]);value != nil{
return "", fmt.Errorf("the parameter %s already exists and cannot be added", args[0])
}
putDate,_:= json.Marshal(mapResult) //处理重复字段
if err := stub.PutState(args[0], putDate); err != nil {
return "", fmt.Errorf("PutState fail,err: %s", err)
}
return fmt.Sprintf("%s schema put success!",args[0]), nil
}
/*
if update Schema 那么我们没有办法比较旧有的一个模板,
TODO,增加版本管理
*/
func (this *Schema) update(args []string, stub shim.ChaincodeStubInterface) (string, error) {
if len(args) != 2 {
return "", fmt.Errorf("expected 2 parameters to function put!")
}
var mapResult map[string]interface{}
if err := json.Unmarshal([]byte(args[1]), &mapResult); err != nil {
return "", fmt.Errorf("parameters Unmarshal fail,args string not json string,err: %s", err)
}else{
if extrastr,ok := mapResult["extra"];ok{
var extraResult map[string]interface{}
if err := json.Unmarshal([]byte(extrastr.(string)), &extraResult); err != nil {
return "", fmt.Errorf("parameters Unmarshal fail,extra string not json string,err: %s", err)
}
}
}
_ ,ok:= mapResult["id"]
if !ok{
return "", fmt.Errorf("The id field must exist ")
}
result, err := stub.GetState(args[0])
if err != nil || result == nil {
return "", fmt.Errorf("GetSchema data fail,please check your parameters ")
}
err = stub.PutState(args[0], []byte(args[1]))
return fmt.Sprintf("%s schema update success!",args[0]), err
}
func (this *Schema) get(args string, stub shim.ChaincodeStubInterface) (string, error) {
result, err := stub.GetState(args)
if err != nil || result == nil {
return "", fmt.Errorf("GetSchema data fail,please check your parameters ")
}
return string(result), nil
}
func main() { func main() {
err := shim.Start(&GXHCC{}) err := shim.Start(&GXHCC{})
if err != nil { if err != nil {
......
package main
import (
"encoding/json"
"fmt"
"github.com/hyperledger/fabric/core/chaincode/shim"
)
type Schema struct {
}
func SchemaProcess(args []string, stub shim.ChaincodeStubInterface)(string, error){
if len(args) < 2{
return "", fmt.Errorf("Expect features with 2 or more parameters to play! ")
}
schema := &Schema{}
switch args[0] {
case "put":
return schema.put(args[1:],stub)
case "update":
return schema.update(args[1:],stub)
case "get":
return schema.get(args[1],stub)
default:
return "",fmt.Errorf(fmt.Sprintf("Unsupported schema function of %s", args[0]))
}
}
/*
put
args 字段说明:
1:args[0]代表我们具体操作的表,
2:args[1:]后面的数据表示要记录到数据库的数据,每一个string 必须是json字符串,并且需要每一个json 字符串都必须包含id 字段
我们可以发现json字符串都被我们解析成了map[string]interface{}类型的map,其中key代表表模板的字段,interface可以是一个结构,我们当前将他看做我们表模板字段的初始值。
*/
func (this *Schema) put(args []string, stub shim.ChaincodeStubInterface) (string, error) {
if len(args) != 2 {
return "", fmt.Errorf("Expected 2 parameters to function put!")
}
var mapResult map[string]interface{}
if err := json.Unmarshal([]byte(args[1]), &mapResult); err != nil {
return "", fmt.Errorf("parameters Unmarshal fail,args string not json string,err: %s", err)
}else{
if extrastr,ok := mapResult["extra"];ok{
var extraResult map[string]interface{}
if err := json.Unmarshal([]byte(extrastr.(string)), &extraResult); err != nil {
return "", fmt.Errorf("parameters Unmarshal fail,extra string not json string,err: %s", err)
}
}
}
_ ,ok:= mapResult["id"]
if !ok{
return "", fmt.Errorf("The id field must exist ")
}
if value,_ :=stub.GetState(args[0]);value != nil{
return "", fmt.Errorf("the parameter %s already exists and cannot be added", args[0])
}
putDate,_:= json.Marshal(mapResult) //处理重复字段
if err := stub.PutState(args[0], putDate); err != nil {
return "", fmt.Errorf("PutState fail,err: %s", err)
}
return fmt.Sprintf("%s schema put success!",args[0]), nil
}
/*
if update Schema 那么我们没有办法比较旧有的一个模板,
TODO,增加版本管理
*/
func (this *Schema) update(args []string, stub shim.ChaincodeStubInterface) (string, error) {
if len(args) != 2 {
return "", fmt.Errorf("expected 2 parameters to function put!")
}
var mapResult map[string]interface{}
if err := json.Unmarshal([]byte(args[1]), &mapResult); err != nil {
return "", fmt.Errorf("parameters Unmarshal fail,args string not json string,err: %s", err)
}else{
if extrastr,ok := mapResult["extra"];ok{
var extraResult map[string]interface{}
if err := json.Unmarshal([]byte(extrastr.(string)), &extraResult); err != nil {
return "", fmt.Errorf("parameters Unmarshal fail,extra string not json string,err: %s", err)
}
}
}
_ ,ok:= mapResult["id"]
if !ok{
return "", fmt.Errorf("The id field must exist ")
}
result, err := stub.GetState(args[0])
if err != nil || result == nil {
return "", fmt.Errorf("GetSchema data fail,please check your parameters ")
}
err = stub.PutState(args[0], []byte(args[1]))
return fmt.Sprintf("%s schema update success!",args[0]), err
}
func (this *Schema) get(args string, stub shim.ChaincodeStubInterface) (string, error) {
result, err := stub.GetState(args)
if err != nil || result == nil {
return "", fmt.Errorf("GetSchema data fail,please check your parameters ")
}
return string(result), nil
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment