本文整理匯總了Golang中github.com/FactomProject/factomd/common/interfaces.BinaryMarshallable類的典型用法代碼示例。如果您正苦於以下問題:Golang BinaryMarshallable類的具體用法?Golang BinaryMarshallable怎麽用?Golang BinaryMarshallable使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了BinaryMarshallable類的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Get
func (db *BoltDB) Get(bucket []byte, key []byte, destination interfaces.BinaryMarshallable) (interfaces.BinaryMarshallable, error) {
db.Sem.RLock()
defer db.Sem.RUnlock()
var v []byte
db.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket(bucket)
if b == nil {
return nil
}
v = b.Get(key)
if v == nil {
return nil
}
return nil
})
if v == nil { // If the value is undefined, return nil
return nil, nil
}
_, err := destination.UnmarshalBinaryData(v)
if err != nil {
return nil, err
}
return destination, nil
}
示例2: Unmarshall
// Make this stuff easier to read.
func Unmarshall(thing interfaces.BinaryMarshallable, err error, data []byte) ([]byte, error) {
if err != nil {
return nil, err
}
newdata, err := thing.UnmarshalBinaryData(data)
return newdata, err
}
示例3: Get
func (db *MapDB) Get(bucket, key []byte, destination interfaces.BinaryMarshallable) (interfaces.BinaryMarshallable, error) {
db.createCache(bucket)
db.Sem.RLock()
defer db.Sem.RUnlock()
if db.Cache == nil {
db.Cache = map[string]map[string][]byte{}
}
_, ok := db.Cache[string(bucket)]
if ok == false {
db.Cache[string(bucket)] = map[string][]byte{}
}
v, ok := db.Cache[string(bucket)][string(key)]
if ok == false {
return nil, nil
}
if v == nil {
return nil, nil
}
_, err := destination.UnmarshalBinaryData(v)
if err != nil {
return nil, err
}
return destination, nil
}
示例4: TestCommitMarshalUnmarshal
func TestCommitMarshalUnmarshal(t *testing.T) {
blocks := testHelper.CreateFullTestBlockSet()
for _, block := range blocks {
for _, tx := range block.ECBlock.GetEntries() {
h1, err := tx.MarshalBinary()
if err != nil {
t.Errorf("Error marshalling - %v", err)
}
var h2 []byte
var e interfaces.BinaryMarshallable
switch tx.ECID() {
case ECIDChainCommit:
e = new(CommitChain)
break
case ECIDEntryCommit:
e = new(CommitEntry)
break
case ECIDBalanceIncrease:
e = new(IncreaseBalance)
break
case ECIDMinuteNumber:
e = new(MinuteNumber)
break
case ECIDServerIndexNumber:
e = new(ServerIndexNumber)
break
default:
t.Error("Wrong ECID")
break
}
h2, err = e.UnmarshalBinaryData(h1)
if err != nil {
t.Logf("Tried to unmarshal %x", h1)
t.Errorf("Error unmarshalling - %v", err)
continue
}
if len(h2) > 0 {
t.Errorf("Leftovers from unmarshalling - %x", h2)
}
h2, err = e.MarshalBinary()
if err != nil {
t.Errorf("Error marshalling2 - %v", err)
continue
}
if primitives.AreBytesEqual(h1, h2) == false {
t.Error("ECEntries are not identical - %x vs %x", h1, h2)
}
}
}
}
示例5: Put
func (d *BoltDB) Put(bucket []byte, key []byte, data interfaces.BinaryMarshallable) error {
hex, err := data.MarshalBinary()
if err != nil {
return err
}
err = d.db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists(bucket)
if err != nil {
return err
}
b := tx.Bucket(bucket)
err = b.Put(key, hex)
return err
})
return err
}
示例6: rawPut
func (db *MapDB) rawPut(bucket, key []byte, data interfaces.BinaryMarshallable) error {
if db.Cache == nil {
db.Cache = map[string]map[string][]byte{}
}
_, ok := db.Cache[string(bucket)]
if ok == false {
db.Cache[string(bucket)] = map[string][]byte{}
}
var hex []byte
var err error
if data != nil {
hex, err = data.MarshalBinary()
if err != nil {
return err
}
}
db.Cache[string(bucket)][string(key)] = hex
return nil
}
示例7: Put
func (db *LevelDB) Put(bucket []byte, key []byte, data interfaces.BinaryMarshallable) error {
if db.lbatch == nil {
db.lbatch = new(leveldb.Batch)
}
defer db.lbatch.Reset()
ldbKey := append(bucket, key...)
hex, err := data.MarshalBinary()
if err != nil {
return err
}
db.lbatch.Put(ldbKey, hex)
err = db.lDB.Write(db.lbatch, db.wo)
if err != nil {
return err
}
return nil
}
示例8: Get
func (db *LevelDB) Get(bucket []byte, key []byte, destination interfaces.BinaryMarshallable) (interfaces.BinaryMarshallable, error) {
db.dbLock.Lock()
defer db.dbLock.Unlock()
ldbKey := append(bucket, key...)
data, err := db.lDB.Get(ldbKey, db.ro)
if err != nil {
if strings.Contains(err.Error(), "not found") {
return nil, nil
}
return nil, err
}
_, err = destination.UnmarshalBinaryData(data)
if err != nil {
return nil, err
}
return destination, nil
}
示例9: AreBinaryMarshallablesEqual
func AreBinaryMarshallablesEqual(b1, b2 interfaces.BinaryMarshallable) (bool, error) {
if b1 == nil {
if b2 == nil {
return true, nil
}
return false, nil
}
if b2 == nil {
return false, nil
}
bytes1, err := b1.MarshalBinary()
if err != nil {
return false, err
}
bytes2, err := b2.MarshalBinary()
if err != nil {
return false, err
}
return AreBytesEqual(bytes1, bytes2), nil
}
示例10: HandleV2RawData
func HandleV2RawData(state interfaces.IState, params interface{}) (interface{}, *primitives.JSONError) {
hashkey := new(HashRequest)
err := MapToObject(params, hashkey)
if err != nil {
panic(reflect.TypeOf(params))
return nil, NewInvalidParamsError()
}
h, err := primitives.HexToHash(hashkey.Hash)
if err != nil {
return nil, NewInvalidHashError()
}
var block interfaces.BinaryMarshallable
var b []byte
if block, _ = state.FetchECTransactionByHash(h); block != nil {
b, _ = block.MarshalBinary()
} else if block, _ = state.FetchFactoidTransactionByHash(h); block != nil {
b, _ = block.MarshalBinary()
} else if block, _ = state.FetchEntryByHash(h); block != nil {
b, _ = block.MarshalBinary()
}
if b == nil {
dbase := state.GetAndLockDB()
defer state.UnlockDB()
// try to find the block data in db and return the first one found
if block, _ = dbase.FetchFBlock(h); block != nil {
b, _ = block.MarshalBinary()
} else if block, _ = dbase.FetchDBlock(h); block != nil {
b, _ = block.MarshalBinary()
} else if block, _ = dbase.FetchABlock(h); block != nil {
b, _ = block.MarshalBinary()
} else if block, _ = dbase.FetchEBlock(h); block != nil {
b, _ = block.MarshalBinary()
} else if block, _ = dbase.FetchECBlock(h); block != nil {
b, _ = block.MarshalBinary()
} else if block, _ = dbase.FetchFBlock(h); block != nil {
b, _ = block.MarshalBinary()
} else if block, _ = dbase.FetchEntry(h); block != nil {
b, _ = block.MarshalBinary()
} else {
return nil, NewEntryNotFoundError()
}
}
d := new(RawDataResponse)
d.Data = hex.EncodeToString(b)
return d, nil
}
示例11: MarshalSign
// Sign signs msg with PrivateKey and return Signature
func (pk *PrivateKey) MarshalSign(msg interfaces.BinaryMarshallable) (sig interfaces.IFullSignature) {
data, _ := msg.MarshalBinary()
return pk.Sign(data)
}