Documentation ¶
Overview ¶
Package datatype manages datatypes and their relationships. Each asset has a list of datatypes. For example, this design allows all assets of the datatype "Medical Records," to be shared through a single consent.
Index ¶
- Constants
- func AddDatatypeSymKey(stub cached_stub.CachedStubInterface, caller data_model.User, ...) (data_model.Key, error)
- func GetAllDatatypes(stub cached_stub.CachedStubInterface, caller data_model.User, args []string) ([]byte, error)
- func GetDatatype(stub cached_stub.CachedStubInterface, caller data_model.User, args []string) ([]byte, error)
- func GetDatatypeKeyID(datatypeID string, ownerID string) string
- func GetDatatypeSymKey(stub cached_stub.CachedStubInterface, caller data_model.User, ...) (data_model.Key, error)
- func GetDatatypeWithParams(stub cached_stub.CachedStubInterface, datatypeID string) (datatype_interface.DatatypeInterface, error)
- func GetParentDatatype(stub cached_stub.CachedStubInterface, datatypeID string) (string, error)
- func Init(stub cached_stub.CachedStubInterface, logLevel ...shim.LoggingLevel) ([]byte, error)
- func NormalizeDatatypes(stub cached_stub.CachedStubInterface, datatypeIDs []string) ([]string, error)
- func RegisterDatatype(stub cached_stub.CachedStubInterface, caller data_model.User, args []string) ([]byte, error)
- func RegisterDatatypeWithParams(stub cached_stub.CachedStubInterface, datatypeID, description string, ...) (datatype_interface.DatatypeInterface, error)
- func UpdateDatatype(stub cached_stub.CachedStubInterface, caller data_model.User, args []string) ([]byte, error)
Examples ¶
Constants ¶
const ROOT_DATATYPE_ID = global.ROOT_DATATYPE_ID
ROOT_DATATYPE_ID is the ID of default ROOT datatype. All other datatypes are children of ROOT.
Variables ¶
This section is empty.
Functions ¶
func AddDatatypeSymKey ¶
func AddDatatypeSymKey(stub cached_stub.CachedStubInterface, caller data_model.User, datatypeID, ownerID string, keyPathForOwnerSymkey ...[]string) (data_model.Key, error)
AddDatatypeSymKey adds a sym key for the given datatypeID and ownerID and returns the DatatypeSymKey. It will also make sure that all its parent datatypes will get new sym key for the given owner (if it does not exist already). If the datatype sym key already exists, it will return success.
func GetAllDatatypes ¶
func GetAllDatatypes(stub cached_stub.CachedStubInterface, caller data_model.User, args []string) ([]byte, error)
GetAllDatatypes returns all datatypes, not including the ROOT datatype.
func GetDatatype ¶
func GetDatatype(stub cached_stub.CachedStubInterface, caller data_model.User, args []string) ([]byte, error)
GetDatatype returns a datatype with the given datatypeID. Returns an empty datatype if the passed in datatypeID does not match an existing datatype's ID.
args = [ datatypeID ]
func GetDatatypeKeyID ¶
GetDatatypeKeyID returns the datatype key ID associated with a datatype owner pair.
Example ¶
/******************************************************************************* * * * (c) Copyright Merative US L.P. and others 2020-2022 * * SPDX-Licence-Identifier: Apache 2.0 * *******************************************************************************/ package main import ( "common/bchcls/cached_stub" "common/bchcls/data_model" //"common/bchcls/internal/datatype_i/datatype_interface" "common/bchcls/test_utils" "encoding/json" "fmt" "testing" ) func main() { datatypeKeyId := GetDatatypeKeyID("datatype1", "owner1") fmt.Println(datatypeKeyId) } func main(t *testing.T) { mstub := test_utils.CreateNewMockStub(t) caller := test_utils.CreateTestUser("caller") datatype1 := data_model.Datatype{DatatypeID: "datatype1", Description: "description", IsActive: true} datatype1Bytes, _ := json.Marshal(&datatype1) parentDatatypeID := "datatype0" // registers datatype mstub.MockTransactionStart("t1") stub := cached_stub.NewCachedStub(mstub) RegisterDatatype(stub, caller, []string{string(datatype1Bytes), parentDatatypeID}) mstub.MockTransactionEnd("t1") } func main(t *testing.T) { mstub := test_utils.CreateNewMockStub(t) datatype1 := data_model.Datatype{DatatypeID: "datatype1", Description: "description", IsActive: true} parentDatatypeID := "datatype0" // registers datatype mstub.MockTransactionStart("t1") stub := cached_stub.NewCachedStub(mstub) RegisterDatatypeWithParams(stub, datatype1.DatatypeID, datatype1.Description, datatype1.IsActive, parentDatatypeID) mstub.MockTransactionEnd("t1") } func main(t *testing.T) { mstub := test_utils.CreateNewMockStub(t) caller := test_utils.CreateTestUser("caller") mstub.MockTransactionStart("t1") stub := cached_stub.NewCachedStub(mstub) var datatype1 data_model.Datatype datatype1Bytes, _ := GetDatatype(stub, caller, []string{"datatype1"}) json.Unmarshal(datatype1Bytes, &datatype1) datatype1.Description = "updated description" datatype1Bytes, _ = json.Marshal(datatype1) mstub.MockTransactionEnd("t1") // updates datatype mstub.MockTransactionStart("t1") stub = cached_stub.NewCachedStub(mstub) UpdateDatatype(stub, caller, []string{string(datatype1Bytes)}) mstub.MockTransactionEnd("t1") } func main(t *testing.T) { mstub := test_utils.CreateNewMockStub(t) caller := test_utils.CreateTestUser("caller") stub := cached_stub.NewCachedStub(mstub) // returns datatype GetDatatype(stub, caller, []string{"datatype1"}) } func main(t *testing.T) { mstub := test_utils.CreateNewMockStub(t) stub := cached_stub.NewCachedStub(mstub) // returns datatype GetDatatypeWithParams(stub, "datatype1") } func main(t *testing.T) { mstub := test_utils.CreateNewMockStub(t) caller := test_utils.CreateTestUser("caller") stub := cached_stub.NewCachedStub(mstub) // returns all datatypes GetAllDatatypes(stub, caller, []string{}) } func main(t *testing.T) { mstub := test_utils.CreateNewMockStub(t) caller := test_utils.CreateTestUser("caller") stub := cached_stub.NewCachedStub(mstub) // add datatype sym key for owner1 AddDatatypeSymKey(stub, caller, "datatype1", "owner1") } func main(t *testing.T) { mstub := test_utils.CreateNewMockStub(t) caller := test_utils.CreateTestUser("caller") stub := cached_stub.NewCachedStub(mstub) // returns datatype sym key for owner1 GetDatatypeSymKey(stub, caller, "datatype1", "owner1") } func main(t *testing.T) { mstub := test_utils.CreateNewMockStub(t) stub := cached_stub.NewCachedStub(mstub) // returns datatypeID of the parent datatype GetParentDatatype(stub, "datatype1") } func main(t *testing.T) { mstub := test_utils.CreateNewMockStub(t) mstub.MockTransactionStart("t1") stub := cached_stub.NewCachedStub(mstub) RegisterDatatypeWithParams(stub, "datatype1", "datatype1 description", true, ROOT_DATATYPE_ID) mstub.MockTransactionEnd("t1") mstub.MockTransactionStart("t1") stub = cached_stub.NewCachedStub(mstub) RegisterDatatypeWithParams(stub, "datatype2", "datatype2 description", true, "datatype1") mstub.MockTransactionEnd("t1") mstub.MockTransactionStart("t1") stub = cached_stub.NewCachedStub(mstub) RegisterDatatypeWithParams(stub, "datatype3", "datatype3 description", true, "datatype2") mstub.MockTransactionEnd("t1") mstub.MockTransactionStart("t1") stub = cached_stub.NewCachedStub(mstub) RegisterDatatypeWithParams(stub, "datatype4", "datatype4 description", true, ROOT_DATATYPE_ID) mstub.MockTransactionEnd("t1") mstub.MockTransactionStart("t1") stub = cached_stub.NewCachedStub(mstub) datatypes := []string{"datatype1", "datatype2", "datatype3", "datatype4"} // returns []string{"datatype3", "datatype4"} NormalizeDatatypes(stub, datatypes) mstub.MockTransactionEnd("t1") } func main(t *testing.T) { mstub := test_utils.CreateNewMockStub(t) stub := cached_stub.NewCachedStub(mstub) datatype1, _ := GetDatatypeWithParams(stub, "datatype1") // returns Datatype object datatype1.GetDatatypeStruct() } func main(t *testing.T) { mstub := test_utils.CreateNewMockStub(t) stub := cached_stub.NewCachedStub(mstub) datatype1, _ := GetDatatypeWithParams(stub, "datatype1") // returns DatatypeID datatype1.GetDatatypeID() } func main(t *testing.T) { mstub := test_utils.CreateNewMockStub(t) stub := cached_stub.NewCachedStub(mstub) datatype1, _ := GetDatatypeWithParams(stub, "datatype1") // returns Description datatype1.GetDescription() } func main(t *testing.T) { mstub := test_utils.CreateNewMockStub(t) stub := cached_stub.NewCachedStub(mstub) datatype1, _ := GetDatatypeWithParams(stub, "datatype1") // sets Description datatype1.SetDescription("new description") } func main(t *testing.T) { mstub := test_utils.CreateNewMockStub(t) stub := cached_stub.NewCachedStub(mstub) datatype1, _ := GetDatatypeWithParams(stub, "datatype1") // changes state to active datatype1.Activate() } func main(t *testing.T) { mstub := test_utils.CreateNewMockStub(t) stub := cached_stub.NewCachedStub(mstub) datatype1, _ := GetDatatypeWithParams(stub, "datatype1") // changes state to inactive datatype1.Deactivate() } func main(t *testing.T) { mstub := test_utils.CreateNewMockStub(t) stub := cached_stub.NewCachedStub(mstub) datatype1, _ := GetDatatypeWithParams(stub, "datatype1") // returns child datatypes of datatype1 datatype1.GetChildDatatypes(stub) } func main(t *testing.T) { mstub := test_utils.CreateNewMockStub(t) stub := cached_stub.NewCachedStub(mstub) datatype1, _ := GetDatatypeWithParams(stub, "datatype1") // returns child datatypes of datatype1 datatype1.GetParentDatatypeID(stub) } func main(t *testing.T) { mstub := test_utils.CreateNewMockStub(t) stub := cached_stub.NewCachedStub(mstub) datatype1, _ := GetDatatypeWithParams(stub, "datatype1") // returns parent datatypes of datatype1 datatype1.GetParentDatatypes(stub) } func main(t *testing.T) { mstub := test_utils.CreateNewMockStub(t) mstub.MockTransactionStart("t1") stub := cached_stub.NewCachedStub(mstub) datatype1, _ := RegisterDatatypeWithParams(stub, "datatype1", "datatype1 description", true, ROOT_DATATYPE_ID) mstub.MockTransactionEnd("t1") mstub.MockTransactionStart("t1") stub = cached_stub.NewCachedStub(mstub) datatype2, _ := RegisterDatatypeWithParams(stub, "datatype2", "datatype2 description", true, "datatype1") mstub.MockTransactionEnd("t1") mstub.MockTransactionStart("t1") stub = cached_stub.NewCachedStub(mstub) // returns false datatype1.IsChildOf(stub, "datatype2") // returns true datatype2.IsChildOf(stub, "datatype1") mstub.MockTransactionEnd("t1") } func main(t *testing.T) { mstub := test_utils.CreateNewMockStub(t) mstub.MockTransactionStart("t1") stub := cached_stub.NewCachedStub(mstub) datatype1, _ := RegisterDatatypeWithParams(stub, "datatype1", "datatype1 description", true, ROOT_DATATYPE_ID) mstub.MockTransactionEnd("t1") mstub.MockTransactionStart("t1") stub = cached_stub.NewCachedStub(mstub) datatype2, _ := RegisterDatatypeWithParams(stub, "datatype2", "datatype2 description", true, "datatype1") mstub.MockTransactionEnd("t1") mstub.MockTransactionStart("t1") // returns true datatype1.IsParentOf(stub, "datatype2") // returns false datatype2.IsParentOf(stub, "datatype1") mstub.MockTransactionEnd("t1") } func main(t *testing.T) { mstub := test_utils.CreateNewMockStub(t) mstub.MockTransactionStart("t1") stub := cached_stub.NewCachedStub(mstub) datatype1, _ := RegisterDatatypeWithParams(stub, "datatype1", "datatype1 description", true, ROOT_DATATYPE_ID) mstub.MockTransactionEnd("t1") // returns sym-owner1-datatype1 datatype1.GetDatatypeKeyID("owner1") }
Output: sym-owner1-datatype1
func GetDatatypeSymKey ¶
func GetDatatypeSymKey(stub cached_stub.CachedStubInterface, caller data_model.User, datatypeID string, ownerID string, keyPath ...[]string) (data_model.Key, error)
GetDatatypeSymKey composes and returns a sym key for the given datatypeID and ownerID. Returns an empty key if the datatype is not found or if there is an error.
func GetDatatypeWithParams ¶
func GetDatatypeWithParams(stub cached_stub.CachedStubInterface, datatypeID string) (datatype_interface.DatatypeInterface, error)
GetDatatypeWithParams function gets a datatype from the ledger. "WithParams" functions should only be called from within the chaincode.
Returns an empty datatype if the passed in datatypeID does not match an existing datatype's ID.
func GetParentDatatype ¶
func GetParentDatatype(stub cached_stub.CachedStubInterface, datatypeID string) (string, error)
GetParentDatatype returns the datatypeID of a given datatypeID's direct parent.
func Init ¶
func Init(stub cached_stub.CachedStubInterface, logLevel ...shim.LoggingLevel) ([]byte, error)
Init sets up the datatype package by registering a default ROOT datatype. Called by init_common.Init function. All solutions must call init_common.Init during solution set up time.
func NormalizeDatatypes ¶
func NormalizeDatatypes(stub cached_stub.CachedStubInterface, datatypeIDs []string) ([]string, error)
NormalizeDatatypes returns a list of normalized child datatype IDs.
func RegisterDatatype ¶
func RegisterDatatype(stub cached_stub.CachedStubInterface, caller data_model.User, args []string) ([]byte, error)
RegisterDatatype registers a new datatype to the ledger. Maintains datatype tree structure. Assumes a ROOT datatype exists. Creates datatypeSymKey and maintains key relationship with parent datatypes. If parentDatatypeID is not provided or does not exist, the datatype will be automatically added as a child of ROOT.
args = [ datatype, parentDatatypeID ]
func RegisterDatatypeWithParams ¶
func RegisterDatatypeWithParams(stub cached_stub.CachedStubInterface, datatypeID, description string, isActive bool, parentDatatypeID string) (datatype_interface.DatatypeInterface, error)
RegisterDatatypeWithParams saves a new datatype to the ledger and maintains datatype tree structure. "WithParams" functions should only be called from within the chaincode.
Caller must pass in datatype and can optionally pass in parentDatatypeID. If parentDatatypeID is not provided, the datatype will be added as a child of ROOT. If the parentDatatypeID passed in does not exist, an error will be thrown. It returns a registered DatatypeInterface instance.
func UpdateDatatype ¶
func UpdateDatatype(stub cached_stub.CachedStubInterface, caller data_model.User, args []string) ([]byte, error)
UpdateDatatype updates an existing datatype's description. Caller must be a system admin.
args = [ datatype ]
Types ¶
This section is empty.
Directories ¶
Path | Synopsis |
---|---|
Package datatype_interface provides an interface for datatype methods.
|
Package datatype_interface provides an interface for datatype methods. |