skylarkproto

package
v0.0.0-...-1643519 Latest Latest
Warning

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

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

Documentation

Overview

Package skylarkproto exposes protobuf messages as skylark types.

It is geared towards emitting messages, not reading or parsing them. Thus it provides only one-way bridge from Skylark to Go (but not vice-versa), i.e. Go programs can use Skylark scripts that return protobuf messages, but not accept them.

Internally a message is stored as a tree of Skylark native values, with some type checking done when manipulating fields. For example, reading or assigning to a field not defined in a message will cause a runtime error. Similarly, trying to assign a value of a wrong type to a non-repeated field will fail.

Repeated fields currently have more lax type checks: they just have to be lists or tuples. It is possible to put wrong values inside them, which will cause runtime error at a later stage, when trying to serialize the proto message (or materialize it as proto.Message on the Go side).

Instantiating messages and default field values

Each proto message in a loaded package is exposed via constructor function that takes optional keyword arguments and produces a new object of *Message type.

All unassigned fields are implicitly set to their default zero values on first access, including message-typed fields. It means, for example, if a message 'a' has a singular field 'b', that has a field 'c', it is always fine to write 'a.b.c' to read or set 'c' value, without explicitly checking that 'b' is set.

To clear a field, assign None to it (regardless of its type).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadProtoModule

func LoadProtoModule(name string) (skylark.StringDict, error)

LoadProtoModule loads a protobuf module, specified by its full path, for example "a/b/c.proto". The module should be registered in the process's protobuf descriptors set. Returns a dict with single struct named after the proto package. It has all message constructors defined inside.

func ProtoLib

func ProtoLib() skylark.StringDict

ProtoLib() returns a dict with single struct named "proto" that holds helper functions to manipulate protobuf messages (in particular serialize them).

Exported functions:

def to_pbtext(msg):
  """Serializes a protobuf message to text proto.

  Args:
    msg: a *Message to serialize.

  Returns:
    An str representing msg in text format.
  """

Types

type Message

type Message struct {
	// contains filtered or unexported fields
}

Message is a skylark value that implements a struct-like type structured like a protobuf message.

Implements skylark.Value, skylark.HasAttrs and skylark.HasSetField interfaces.

func NewMessage

func NewMessage(typ *MessageType) *Message

NewMessage instantiates a message of the given type.

func (*Message) Attr

func (m *Message) Attr(name string) (skylark.Value, error)

Attr implements skylark.HasAttrs.

func (*Message) AttrNames

func (m *Message) AttrNames() []string

AttrNames implements skylark.HasAttrs.

func (*Message) Freeze

func (m *Message) Freeze()

Freeze implements skylark.Value.

func (*Message) Hash

func (m *Message) Hash() (uint32, error)

Hash implements skylark.Value.

func (*Message) MessageType

func (m *Message) MessageType() *MessageType

MessageType returns detailed type information about the message.

func (*Message) SetField

func (m *Message) SetField(name string, val skylark.Value) error

SetField implements skylark.HasSetField.

func (*Message) String

func (m *Message) String() string

String implements skylark.Value.

func (*Message) ToProto

func (m *Message) ToProto() (proto.Message, error)

ToProto returns a new populated proto message of an appropriate type.

Returns an error if the data inside the skylark representation of the message has a wrong type.

func (*Message) Truth

func (m *Message) Truth() skylark.Bool

Truth implements skylark.Value.

func (*Message) Type

func (m *Message) Type() string

Type implements skylark.Value.

type MessageType

type MessageType struct {
	// contains filtered or unexported fields
}

MessageType contains information about the structure of a proto message.

It is extracted via reflection from a proto message struct type.

func GetMessageType

func GetMessageType(typ reflect.Type) (*MessageType, error)

GetMessageType extracts type description for protobuf message of given type.

'typ' is expected to represent a pointer to a protobuf struct, as returned by proto.MessageType(...). Returns an error otherwise.

func (*MessageType) Name

func (m *MessageType) Name() string

Name returns fully qualified proto message name.

func (*MessageType) Type

func (m *MessageType) Type() reflect.Type

Type returns proto message type (pointer to a proto struct).

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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