luci: go.chromium.org/luci/skylark/skylarkproto Index | Files | Directories

package skylarkproto

import "go.chromium.org/luci/skylark/skylarkproto"

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

Package Files

builtin_with_attrs.go conversions.go default.go doc.go functions.go loader.go message.go type.go

func LoadProtoModule Uses

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 Uses

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.
  """

type Message Uses

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 Uses

func NewMessage(typ *MessageType) *Message

NewMessage instantiates a message of the given type.

func (*Message) Attr Uses

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

Attr implements skylark.HasAttrs.

func (*Message) AttrNames Uses

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

AttrNames implements skylark.HasAttrs.

func (*Message) Freeze Uses

func (m *Message) Freeze()

Freeze implements skylark.Value.

func (*Message) Hash Uses

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

Hash implements skylark.Value.

func (*Message) MessageType Uses

func (m *Message) MessageType() *MessageType

MessageType returns detailed type information about the message.

func (*Message) SetField Uses

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

SetField implements skylark.HasSetField.

func (*Message) String Uses

func (m *Message) String() string

String implements skylark.Value.

func (*Message) ToProto Uses

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 Uses

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

Truth implements skylark.Value.

func (*Message) Type Uses

func (m *Message) Type() string

Type implements skylark.Value.

type MessageType Uses

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 Uses

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 Uses

func (m *MessageType) Name() string

Name returns fully qualified proto message name.

func (*MessageType) Type Uses

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

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

Directories

PathSynopsis
testprotos

Package skylarkproto imports 12 packages (graph) and is imported by 1 packages. Updated 2018-08-19. Refresh now. Tools for package owners.