flagpb

package
v0.0.0-...-e7509a3 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2024 License: Apache-2.0 Imports: 11 Imported by: 3

Documentation

Overview

Package flagpb defines a flag format for protobuf messages, implements a parser and a formatter.

Currently flagpb supports only untyped messages, not proto.Message. Support for the latter could be added too.

Syntax

Flag syntax by example. First line is flagpb, second is jsonpb.

-x=42 -b -s hello
{"x": 42, "b": true, "s": "hello"}

-m.x 3 -m.s world
{"m": {"x": 3, "s": "world"}}

-rx 1 -rx 2
{"rx": [1, 2]}

-rm.x 1 -rm -rm.x 2
{"rm": [{"x": 1}, {"x": 2}]}

where x fields are int32, m are message fields, b are boolean and s are strings. Fields with "r" prefix are repeated.

Bytes field values are decoded from hex, e.g. "FF02AB".

Enum field values can be specified by enum member name or number.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MarshalUntyped

func MarshalUntyped(msg map[string]any) ([]string, error)

MarshalUntyped marshals a key-value map to flags.

func UnmarshalMessage

func UnmarshalMessage(flags []string, resolver Resolver, msg proto.Message) error

UnmarshalMessage unmarshals the proto message from flags.

The descriptor set should be obtained from the `cproto` compiled packages' FileDescriptorSet() method.

func UnmarshalUntyped

func UnmarshalUntyped(flags []string, desc *descriptorpb.DescriptorProto, resolver Resolver) (map[string]any, error)

UnmarshalUntyped unmarshals a key-value map from flags using a protobuf message descriptor.

Types

type Resolver

type Resolver interface {
	// Resolve resolves a type name to
	// *descriptor.DescriptorProto or *descriptor.EnumDescriptorProto.
	Resolve(name string) any
}

Resolver resolves type names.

func NewResolver

func NewResolver(set *descriptorpb.FileDescriptorSet) Resolver

NewResolver creates a resolver for all types in a file descriptor set. Resolving time complexity is linear.

Jump to

Keyboard shortcuts

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