mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-25 19:15:10 +00:00
Chaincode slowly evolving
This commit is contained in:
parent
9acb406996
commit
ee6954ddac
1 changed files with 50 additions and 42 deletions
|
|
@ -63,19 +63,25 @@ type Schema struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// InitLedger adds a base set of Data entries to the ledger
|
// InitLedger adds a base set of Data entries to the ledger
|
||||||
func (s *SmartContract) InitLedger(ctx contractapi.TransactionContextInterface, pathSchema string, pathFirstJsonFile string) (error) {
|
func (s *SmartContract) InitLedger(ctx contractapi.TransactionContextInterface, pathSchema string, pathFirstJsonFile string) error {
|
||||||
|
|
||||||
// We use the function jsonReader in order to read the content of the shcema Json File. The schema Json file is composed by us and inserted into
|
// We use the function jsonReader in order to read the content of the shcema Json File. The schema Json file is composed by us and inserted into
|
||||||
// the docker container of the commited chaincode (For now)
|
// the docker container of the commited chaincode (For now)
|
||||||
schemaJsonFileContent := jsonReader(ctx, pathSchema)
|
schemaJsonFileContent, error_schema := s.JsonReader(ctx, pathSchema)
|
||||||
firstJsonFileContent := jsonReader(ctx, pathFirstJsonFile)
|
firstJsonFileContent, error_file := s.JsonReader(ctx, pathFirstJsonFile)
|
||||||
|
|
||||||
|
if error_schema != nil {
|
||||||
|
return fmt.Errorf("failed to read shcema.json file or NewJsonFile: %v", error_schema)
|
||||||
|
} else if error_file != nil {
|
||||||
|
return fmt.Errorf("failed to read shcema.json file or NewJsonFile: %v", error_file)
|
||||||
|
} else {
|
||||||
dataEntries := []Data{
|
dataEntries := []Data{
|
||||||
{Contributor: "pepitoperes@email.com", ContributorId: "ABC123", ContentHash: "ZXCVBNM", Id: "00000", Owners: []string{"CIA", "DEA", "FBI"}, firstJsonFileContent},
|
{Contributor: "pepitoperes@email.com", ContributorId: "ABC123", ContentHash: "ZXCVBNM", Id: "00000", Owners: []string{"CIA", "DEA", "FBI"}, JsonFileContent: firstJsonFileContent},
|
||||||
}
|
}
|
||||||
|
|
||||||
schema := Schema{
|
//This is the definition of the Schema that we should use for validate all the JSON files from now on.
|
||||||
{Contributor: "pepitoperes@email.com", ContributorId: "ABC123", ContentHash: "ZXCVBNM", Id: "00000", Owners: []string{"CIA", "DEA", "FBI"}, schemaJsonFileContent},
|
schema := []Schema{
|
||||||
|
{Contributor: "pepitoperes@email.com", ContributorId: "ABC123", ContentHash: "ZXCVBNM", Id: "00000", Owners: []string{"CIA", "DEA", "FBI"}, JsonFileContent: schemaJsonFileContent},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, data := range schema {
|
for _, data := range schema {
|
||||||
|
|
@ -102,7 +108,8 @@ func (s *SmartContract) InitLedger(ctx contractapi.TransactionContextInterface,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return schema, nil
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SmartContract) JsonReader(ctx contractapi.TransactionContextInterface, path string) (map[string]interface{}, error) {
|
func (s *SmartContract) JsonReader(ctx contractapi.TransactionContextInterface, path string) (map[string]interface{}, error) {
|
||||||
|
|
@ -118,7 +125,7 @@ func (s *SmartContract) JsonReader(ctx contractapi.TransactionContextInterface,
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Error during Unmarshal(): ", err)
|
log.Fatal("Error during Unmarshal(): ", err)
|
||||||
}
|
}
|
||||||
return payload != nil, nil
|
return payload, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAllAssets returns all assets found in world state
|
// GetAllAssets returns all assets found in world state
|
||||||
|
|
@ -169,9 +176,8 @@ func (s *SmartContract) ValidJson(ctx contractapi.TransactionContextInterface, A
|
||||||
//schemaLoader := gojsonschema.NewReferenceLoader("file:///home/chaincode/Schema.json")
|
//schemaLoader := gojsonschema.NewReferenceLoader("file:///home/chaincode/Schema.json")
|
||||||
//documentLoader := gojsonschema.NewReferenceLoader("file:////home/chaincode/testFile.json")
|
//documentLoader := gojsonschema.NewReferenceLoader("file:////home/chaincode/testFile.json")
|
||||||
|
|
||||||
```
|
// PATH Needs to be absolute path (From root '/'). Add something that takes care of that.
|
||||||
PATH Needs to be absolute path (From root '/'). Add something that takes care of that.
|
|
||||||
```
|
|
||||||
m := schema.JsonFileContent.JsonFileContent
|
m := schema.JsonFileContent.JsonFileContent
|
||||||
schemaLoader := gojsonschema.NewGoLoader(m)
|
schemaLoader := gojsonschema.NewGoLoader(m)
|
||||||
documentLoader := gojsonschema.NewReferenceLoader("file://" + AbsolutePathToJsonFile)
|
documentLoader := gojsonschema.NewReferenceLoader("file://" + AbsolutePathToJsonFile)
|
||||||
|
|
@ -212,13 +218,15 @@ func (s *SmartContract) CreateDataSample(ctx contractapi.TransactionContextInter
|
||||||
if !valid {
|
if !valid {
|
||||||
return fmt.Errorf("the json file provided is not valid")
|
return fmt.Errorf("the json file provided is not valid")
|
||||||
} else {
|
} else {
|
||||||
|
jsonFileContent := s.JsonReader(ctx, AbsolutePathToJsonFile)
|
||||||
data := Data{
|
data := Data{
|
||||||
Contributor: Contributor,
|
Contributor: Contributor,
|
||||||
ContributorId: ContributorId,
|
ContributorId: ContributorId,
|
||||||
ContentHash: ContentHash,
|
ContentHash: ContentHash,
|
||||||
Id: Id,
|
Id: Id,
|
||||||
Owners: []string{"DOE", "DOS", "DOJ"}},
|
Owners: []string{"DOE", "DOS", "DOJ"},
|
||||||
JsonFileContent:
|
JsonFileContent: jsonFileContent,
|
||||||
|
}
|
||||||
|
|
||||||
assetJSON, err := json.Marshal(data)
|
assetJSON, err := json.Marshal(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue