pb

package
v0.93.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 16, 2020 License: Apache-2.0 Imports: 9 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var StdProtobufDecoder = rel.NewNativeFunction("decode", func(param rel.Value) (rel.Value, error) {
	bytes, isBytes := param.(rel.Bytes)
	if !isBytes {
		return nil, fmt.Errorf("//encoding.proto.decode: param not bytes")
	}

	if bytes.Bytes() == nil {

		return rel.NewNativeFunction("decode$1", func(definition rel.Value) (rel.Value, error) {
			_, is := tools.ValueAsBytes(definition)
			if !is {
				return nil, fmt.Errorf("//encoding.proto.decode: definition not bytes")
			}

			return definition, nil
		}), nil
	}

	definitionBytes, is := tools.ValueAsBytes(bytes)
	if !is {
		return nil, fmt.Errorf("//encoding.proto.decode: fileDescriptor not bytes")
	}

	return rel.NewNativeFunction("decode$2", func(rootMessageName rel.Value) (rel.Value, error) {
		fd, err := decodeFileDescriptor(definitionBytes)
		if err != nil {
			return nil, err
		}

		nameStr, isStr := tools.ValueAsString(rootMessageName)
		if !isStr {
			return nil, fmt.Errorf("//encoding.proto.decode: rootMessageName not string")
		}
		rootMessageDesc := fd.Messages().ByName(protoreflect.Name(nameStr))
		message := dynamicpb.NewMessage(rootMessageDesc)

		return rel.NewNativeFunction("decode$3", func(data rel.Value) (rel.Value, error) {
			dataBytes, is := tools.ValueAsBytes(data)
			if !is {
				return nil, fmt.Errorf("//encoding.proto.decode: data not bytes")
			}

			err = proto.Unmarshal(dataBytes, message)
			if err != nil {
				return nil, err
			}

			tuple, err := convertProtoValToSyslVal(protoreflect.ValueOf(message.ProtoReflect()))
			if err != nil {
				return nil, err
			}

			return tuple, nil
		}), nil
	}), nil
})

StdProtobufDecoder transforms the protocol buffer message to a tuple. Sample code to call this method: let sysl = //encoding.proto.decode(//encoding.proto.proto, //os.file('sysl.pb')); let decodeSyslPb = //encoding.proto.decode(sysl); let shop = decodeSyslPb('Module', //os.file("petshop.pb")); petshop.apps("PetShopApi")

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL