當前位置: 首頁>>代碼示例>>Golang>>正文


Golang encoding.Cbor函數代碼示例

本文整理匯總了Golang中github.com/cronosun/buranv1/ares/encoding.Cbor函數的典型用法代碼示例。如果您正苦於以下問題:Golang Cbor函數的具體用法?Golang Cbor怎麽用?Golang Cbor使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了Cbor函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: directoryGet

func directoryGet(ctx *restContext, dirHash []byte, filename string) (
	entry *directoryEntry,
	found bool,
	status retcode.Status) {

	getEntry := &operations.BucketGetIn{
		BucketOperation: operations.BucketOperation{
			BucketId: ctx.caprica.GlobalDirectoryId(),
		},
		Key: [][]byte{dirHash, []byte(filename)},
	}
	var out interface{}
	out, status = ctx.execute(getEntry, operations.BucketGet)

	if !status.IsOk() {
		return
	}
	if status.Code == retcode.OkNotFound {
		return
	}
	found = true

	getEntryReturn := out.(operations.BucketGetOut)
	value := getEntryReturn.Value.([][]byte)

	var targetHash []byte
	err := encoding.Cbor().Decode(value[0], &targetHash)
	if err != nil {
		status = retcode.NewStatusFmt(retcode.ErrorServer, "Unable to decode hash")
		return
	}
	entry = new(directoryEntry)
	entry.targetHash = targetHash
	if len(value) == 1 {
		// Directory
		entry.isDirectory = true
	} else {
		entry.isDirectory = false
		// Mime
		var mime string
		err := encoding.Cbor().Decode(value[1], &mime)
		if err != nil {
			status = retcode.NewStatusFmt(retcode.ErrorServer, "Unable to decode mime")
			return
		}
		entry.mimeType = mime
	}

	status = retcode.NewStatusOk()
	return
}
開發者ID:cronosun,項目名稱:buranv1,代碼行數:51,代碼來源:dirget.go

示例2: indexDirectory

func (self *directoryBucketInstance) indexDirectory(operation *operations.Put,
	state *minos.OperationState,
	hash []byte,
	directory *directoryStruct) (ret bucket.BucketReturn) {

	var status retcode.Status

	mimeTypes := directory.mimeEntries
	for _, dirEntry := range directory.fileEntries {
		key := types.Key{hash, []byte(dirEntry.name)}

		cborTargetHash, err := encoding.Cbor().Encode(dirEntry.hashPointer)
		if err != nil {
			status = retcode.NewStatusFmt(retcode.ErrorServer, "Error cbor encoding: %v",
				err)
			ret = &operations.GenericReturn{status}
			return
		}

		var value types.Array
		if dirEntry.mimePointer == -1 {
			// It's a directory
			value = types.Array{cborTargetHash}
		} else {
			mimeEntry := mimeTypes[dirEntry.mimePointer]
			cborMimeType, err := encoding.Cbor().Encode(mimeEntry.typeName)
			if err != nil {
				status = retcode.NewStatusFmt(retcode.ErrorServer, "Error cbor encoding: %v",
					err)
				ret = &operations.GenericReturn{status}
				return
			}
			value = types.Array{cborTargetHash, cborMimeType}
		}

		opPut := &operations.Put{
			Key:   key,
			Value: value,
		}
		putReturn := self.cstore.Op_put(opPut)
		if !putReturn.GetCode().IsOk() {
			// Error put-ing
			ret = &operations.GenericReturn{putReturn.GetStatus()}
			return
		}
	}
	return
}
開發者ID:cronosun,項目名稱:buranv1,代碼行數:48,代碼來源:opput.go

示例3: writeInformation

func (self *incubator) writeInformation(state *minos.OperationState,
	id string, incState *incubationState) (ret retcode.Status) {

	key := self.createInformationKey(state, id)

	encodedLength, err := encoding.Cbor().Encode(incState.length)
	if err != nil {
		ret = retcode.NewStatusError(retcode.ErrorServer, err)
		return
	}
	value := types.Array{incState.hashState, encodedLength}

	apiOperation := &operations.Put{
		BucketId: self.bucketId,
		Key:      key,
		Value:    value,
	}
	opRet := state.Dispatcher.Perform(state.Context, apiOperation)
	if !opRet.GetCode().IsOk() {
		// Unable to put
		ret = opRet.GetStatus()
		return
	}

	return opRet.GetStatus()
}
開發者ID:cronosun,項目名稱:buranv1,代碼行數:26,代碼來源:incubator.go

示例4: CborArray

func CborArray(payload string, forwarding ForwardingFunction) (data []byte, err error) {
	if !strings.HasPrefix(payload, cborArrayPrefix) {
		err = errors.New("A cbor array has to start with " + cborArrayPrefix)
		return
	}
	if !strings.HasSuffix(payload, cborArraySuffix) {
		err = errors.New("A cbor array has to end with " + cborArraySuffix)
		return
	}

	payload = strings.TrimPrefix(payload, cborArrayPrefix)
	payload = strings.TrimSuffix(payload, cborArraySuffix)
	splitElements := strings.SplitN(payload, cborArraySeparator, cborArrayMaxElements+1)
	if len(splitElements) > cborArrayMaxElements {
		err = errors.New("The cbor array can process at max 32 elements. Input has more")
		return
	}

	array := make([][]byte, len(splitElements))
	for index, element := range splitElements {
		array[index], err = forwarding(element)
		if err != nil {
			return
		}
	}

	// Now cbor encode that
	encodedCbor, err := encoding.Cbor().Encode(array)
	if err != nil {
		return
	}
	data = encodedCbor
	return
}
開發者ID:cronosun,項目名稱:buranv1,代碼行數:34,代碼來源:cbor.go

示例5: PutReceivers

func (self *TypedGetterSetter) PutReceivers() (receivers []bucket.Id, err error) {
	binaryValue, err := self.Get(putReceiversKey)
	if err != nil {
		return
	}
	if binaryValue == nil || len(binaryValue) == 0 {
		return []bucket.Id{}, nil
	}

	var bucketIdsAsBinaryInterface interface{}
	err = encoding.Cbor().Decode(binaryValue, &bucketIdsAsBinaryInterface)
	if err != nil {
		return nil, errors.New(fmt.Sprintf("Error cbor decoding: %v\n", err))
	}

	av := bucketIdsAsBinaryInterface.([]interface{})

	bucketIds := make([]bucket.Id, len(av))
	for index, singleBucket := range av {
		typeOf := reflect.TypeOf(singleBucket)
		if !reflect.TypeOf([]byte{}).AssignableTo(typeOf) {
			return nil, errors.New("Expecting slices of bytes in put receivers")
		}
		bucketIds[index] = singleBucket.([]byte)
	}
	return bucketIds, nil
}
開發者ID:cronosun,項目名稱:buranv1,代碼行數:27,代碼來源:typedgettersetter.go

示例6: writeInformation

func (self *public) writeInformation(state *minos.OperationState,
	hash []byte, pubState *publicState) (ret retcode.Status) {

	key := self.clreatePublicKey(state, hash)

	encodedLength, err := encoding.Cbor().Encode(pubState.length)
	if err != nil {
		ret = retcode.NewStatusError(retcode.ErrorServer, err)
		return
	}
	if pubState.data == nil {
		pubState.data = []byte{}
	}
	value := types.Array{pubState.data, encodedLength}
	apiOperation := &operations.Put{
		BucketId: self.bucketId,
		Key:      key,
		Value:    value,
	}
	opRet := state.Dispatcher.Perform(state.Context, apiOperation)
	if !opRet.GetCode().IsOk() {
		// Unable to put
		ret = opRet.GetStatus()
		return
	}

	return opRet.GetStatus()
}
開發者ID:cronosun,項目名稱:buranv1,代碼行數:28,代碼來源:public.go

示例7: information

func (self *incubator) information(state *minos.OperationState,
	id string) (incState incubationState, ret retcode.Status) {

	key := self.createInformationKey(state, id)

	apiOperation := &operations.Get{
		BucketId: self.bucketId,
		Key:      key,
	}
	opRet := state.Dispatcher.Perform(state.Context, apiOperation)
	if opRet.GetCode() != retcode.Ok {
		// Not found or a different error
		ret = opRet.GetStatus()
		return
	}

	// Ok, found it, deserialize
	retCast := opRet.(*operations.GetReturn)
	value := retCast.Value

	incState = incubationState{}
	incState.hashState = value[0]
	err := encoding.Cbor().Decode(value[1], &incState.length)
	if err != nil {
		ret = retcode.NewStatusError(retcode.ErrorServer, err)
		return
	}

	return
}
開發者ID:cronosun,項目名稱:buranv1,代碼行數:30,代碼來源:incubator.go

示例8: information

func (self *public) information(state *minos.OperationState,
	hash []byte) (pubState publicState, ret retcode.Status) {

	key := self.clreatePublicKey(state, hash)

	apiOperation := &operations.Get{
		BucketId: self.bucketId,
		Key:      key,
	}
	opRet := state.Dispatcher.Perform(state.Context, apiOperation)
	if opRet.GetCode() != retcode.Ok {
		// Not found or a different error
		ret = opRet.GetStatus()
		return
	}

	// Ok, found it, deserialize
	retCast := opRet.(*operations.GetReturn)
	value := retCast.Value

	pubState = publicState{}
	pubState.data = value[0]
	err := encoding.Cbor().Decode(value[1], &pubState.length)
	if err != nil {
		ret = retcode.NewStatusError(retcode.ErrorServer, err)
		return
	}

	return
}
開發者ID:cronosun,項目名稱:buranv1,代碼行數:30,代碼來源:public.go

示例9: PrepareAndEncode

func (self *EncodableDirectory) PrepareAndEncode() (
	data []byte, err error) {

	if !self.prepared {
		self.prepare()
	}

	// Entries
	var dirEntries []interface{}
	for _, srcEntry := range self.Entries {
		var mimeValue int16
		if srcEntry.TargetIsDirectory {
			mimeValue = -1
		} else {
			mimeValue = self.mimeTypeToArrayEntry[srcEntry.MimeType]
		}
		targetEntry := []interface{}{mimeValue, srcEntry.TargetHash, srcEntry.Name}
		dirEntries = append(dirEntries, targetEntry)
	}

	topLevelArray := []interface{}{encodableDirectoryMagicNumber,
		dirEntries, self.mimeTypes}

	data, err = encoding.Cbor().Encode(topLevelArray)
	if err != nil {
		return
	}

	return
}
開發者ID:cronosun,項目名稱:buranv1,代碼行數:30,代碼來源:directoryencoder.go

示例10: CborSignedInt

func CborSignedInt(payload string) (data []byte, err error) {
	u, err := strconv.ParseInt(payload, 10, 64)
	if err != nil {
		return
	}

	return encoding.Cbor().Encode(u)
}
開發者ID:cronosun,項目名稱:buranv1,代碼行數:8,代碼來源:cbor.go

示例11: initializeGlobalDirectoryStore

func (self *capricaStruct) initializeGlobalDirectoryStore() (err error) {
	sysstorage := self.buran.SystemStorage()
	valuePtr, err := sysstorage.Get(globaldirectory_key)
	if err != nil {
		return
	}
	if valuePtr == nil {
		// First get the global blob
		globalBlobId := self.globalBlobStoreId
		if globalBlobId == nil || len(globalBlobId) == 0 {
			err = errors.New("The global directory depends on the global blob store. So need " +
				"to create that first.")
			return
		}

		var globalBlobIdCbor []byte
		globalBlobIdCbor, err = encoding.Cbor().Encode(globalBlobId)
		if err != nil {
			return
		}

		// Need to create the global blob store
		operation := operations.CreateBucket{
			TypeId: bucket.TypeId_Directory,
			Metadata: metadata.MemoryMetadata{
				"const.forwarder.blob": globalBlobIdCbor,
			},
		}
		// TODO: Real context
		context := ares.Context{
			UserId: user.Id([]byte("Test")),
		}
		ret := self.buran.Perform(&context, &operation)
		if !ret.GetCode().IsOk() {
			// Error
			err = errors.New(fmt.Sprintf("Error creating "+
				"the global directory: %v", ret.GetText()))
			return
		}
		retCast := ret.(*operations.CreateBucketReturn)
		self.globalDirectoryId = retCast.Id

		// Store it - so we have the same on next start
		err = self.buran.SystemStorage().Put(globaldirectory_key, retCast.Id)
		if err != nil {
			return
		}
	} else {
		// Already have a global blob store
		self.globalDirectoryId = bucket.Id(*valuePtr)
	}
	return
}
開發者ID:cronosun,項目名稱:buranv1,代碼行數:53,代碼來源:globaldirectory.go

示例12: SetCreator

func (self *TypedGetterSetter) SetCreator(creator user.Id) (err error) {
	userIdAsBytes := []byte(creator)
	userIdEncoded, err := encoding.Cbor().Encode(userIdAsBytes)
	if err != nil {
		return
	}

	err = self.Set(creatorKey, userIdEncoded)
	if err != nil {
		return
	}
	return
}
開發者ID:cronosun,項目名稱:buranv1,代碼行數:13,代碼來源:typedgettersetter.go

示例13: SetTypeId

func (self *TypedGetterSetter) SetTypeId(typeId bucket.TypeId) (err error) {
	var typeIdAsUint8 uint8
	typeIdAsUint8 = uint8(typeId)
	encodedTypeId, err := encoding.Cbor().Encode(typeIdAsUint8)
	if err != nil {
		return
	}
	err = self.Set(typeIdKey, encodedTypeId)
	if err != nil {
		return
	}
	return
}
開發者ID:cronosun,項目名稱:buranv1,代碼行數:13,代碼來源:typedgettersetter.go

示例14: Download

func Download(t btesting.T, bucketId typing.BucketId, hash []byte,
	writer io.Writer) (entireLength uint64) {

	var skip int = 0
	var limit int = readerBlockSize
	var entireLengthProcessed bool

	for {
		var err error
		skipEncoded := encoding.UIntToUVarInt(uint64(skip))
		limitEncoded := encoding.UIntToUVarInt(uint64(limit))
		//hash/[HASH]/content/VUI(skip_optional)/VUI(limit_optional)
		key := typing.Key{[]byte("hash"), hash, []byte("content"), skipEncoded, limitEncoded}
		value := operations.Get(t, bucketId, key, true)
		// value = [data, CBOR(entire_length)]
		if len(value) != 2 {
			t.Errorf("Got invalid get from bucket / expecting 2 elements in value. Have %v",
				len(value))
			return
		}
		data := value[0]

		// Set entire length
		if !entireLengthProcessed {
			entireLengthEncoded := value[1]
			err = encoding.Cbor().Decode(entireLengthEncoded, &entireLength)
			entireLengthProcessed = true
			if err != nil {
				t.Errorf("Error decoding entire length %v", err)
				return
			}
		}

		_, err = writer.Write(data)
		if err != nil {
			t.Errorf("Unable to write to writer: %v", err)
			return
		}

		skip += readerBlockSize

		// Next one? End if we got less than requested or would exceed entire length
		if uint64(len(data)) < readerBlockSize || uint64(skip) >= entireLength {
			// No, end here
			return
		}
	}
	return
}
開發者ID:cronosun,項目名稱:buranv1,代碼行數:49,代碼來源:blob.go

示例15: SetForwarder

func (self *TypedGetterSetter) SetForwarder(constant bool,
	key string, bucketId bucket.Id) (err error) {

	var bucketIdAsBinary []byte
	bucketIdAsBinary = []byte(bucketId)
	bucketIdEncoded, err := encoding.Cbor().Encode(bucketIdAsBinary)
	if err != nil {
		return
	}

	err = self.Set(generateKeyForForwarder(constant, key), bucketIdEncoded)
	if err != nil {
		return
	}
	return
}
開發者ID:cronosun,項目名稱:buranv1,代碼行數:16,代碼來源:typedgettersetter.go


注:本文中的github.com/cronosun/buranv1/ares/encoding.Cbor函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。