store

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2025 License: MIT Imports: 13 Imported by: 2

Documentation

Overview

Package store provides functions to store a protobuf message to different formats: json, bin, and txt.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MarshalToBin

func MarshalToBin(msg proto.Message) (out []byte, err error)

MarshalToBin marshals the given proto.Message in the wire (binary) format. You can depend on the output being stable.

func MarshalToJSON

func MarshalToJSON(msg proto.Message, options *MarshalOptions) (out []byte, err error)

MarshalToJSON marshals the given proto.Message in the JSON format. You can depend on the output being stable.

func MarshalToText

func MarshalToText(msg proto.Message, pretty bool) (out []byte, err error)

MarshalToText marshals the given proto.Message in the text (textproto) format. You can depend on the output being stable.

func Store

func Store(msg proto.Message, dir string, fmt format.Format, options ...Option) error

Store stores protobuf message to file in the specified directory and format. Available formats: JSON, Bin, and Text.

Types

type FilterFunc

type FilterFunc func(name string) bool

FilterFunc filter in messagers if returned value is true.

NOTE: name is the protobuf message name, e.g.: "message ItemConf{...}".

FilterFunc is redefined here (also defined in "load" package) to avoid "import cycle" problem.

type MarshalOptions

type MarshalOptions struct {
	// Output pretty format of JSON and Text, with multiline and indent.
	//
	// Default: false.
	Pretty bool

	// EmitUnpopulated specifies whether to emit unpopulated fields. It does not
	// emit unpopulated oneof fields or unpopulated extension fields.
	// The JSON value emitted for unpopulated fields are as follows:
	//  ╔═══════╤════════════════════════════╗
	//  ║ JSON  │ Protobuf field             ║
	//  ╠═══════╪════════════════════════════╣
	//  ║ false │ proto3 boolean fields      ║
	//  ║ 0     │ proto3 numeric fields      ║
	//  ║ ""    │ proto3 string/bytes fields ║
	//  ║ null  │ proto2 scalar fields       ║
	//  ║ null  │ message fields             ║
	//  ║ []    │ list fields                ║
	//  ║ {}    │ map fields                 ║
	//  ╚═══════╧════════════════════════════╝
	//
	// NOTE: worksheet with FieldPresence set as true ignore this option.
	//
	// Refer: https://github.com/protocolbuffers/protobuf/blob/main/docs/field_presence.md
	//
	// Default: false.
	EmitUnpopulated bool

	// UseProtoNames uses proto field name instead of lowerCamelCase name in JSON
	// field names.
	UseProtoNames bool

	// UseEnumNumbers emits enum values as numbers.
	UseEnumNumbers bool
}

type Option

type Option func(*Options)

Option is the functional option type.

func EmitUnpopulated

func EmitUnpopulated(v bool) Option

EmitUnpopulated specifies whether to emit unpopulated fields.

func Filter

func Filter(filter FilterFunc) Option

Filter can only filter in certain specific messagers based on the condition that you provide.

NOTE: only used in https://github.com/tableauio/loader.

func Name

func Name(v string) Option

Name specifies the output file name (without file extension).

func Pretty

func Pretty(v bool) Option

Pretty specifies whether to prettify JSON and Text output with multiline and indent.

func UseEnumNumbers

func UseEnumNumbers(v bool) Option

UseEnumNumbers specifies whether to emit enum values as numbers for JSON field values.

func UseProtoNames

func UseProtoNames(v bool) Option

UseProtoNames specifies whether to use proto field name instead of lowerCamelCase name in JSON field names.

type Options

type Options struct {
	// Filter can only filter in certain specific messagers based on the
	// condition that you provide.
	//
	// NOTE: only used in https://github.com/tableauio/loader.
	//
	// Default: nil.
	Filter FilterFunc

	// Specify output file name (without file extension).
	//
	// Default: "".
	Name string
	// Output pretty format of JSON and Text, with multiline and indent.
	//
	// Default: false.
	Pretty bool
	// EmitUnpopulated specifies whether to emit unpopulated fields. It does not
	// emit unpopulated oneof fields or unpopulated extension fields.
	// The JSON value emitted for unpopulated fields are as follows:
	//  ╔═══════╤════════════════════════════╗
	//  ║ JSON  │ Protobuf field             ║
	//  ╠═══════╪════════════════════════════╣
	//  ║ false │ proto3 boolean fields      ║
	//  ║ 0     │ proto3 numeric fields      ║
	//  ║ ""    │ proto3 string/bytes fields ║
	//  ║ null  │ proto2 scalar fields       ║
	//  ║ null  │ message fields             ║
	//  ║ []    │ list fields                ║
	//  ║ {}    │ map fields                 ║
	//  ╚═══════╧════════════════════════════╝
	//
	// NOTE: worksheet with FieldPresence set as true ignore this option.
	//
	// Refer: https://github.com/protocolbuffers/protobuf/blob/main/docs/field_presence.md
	//
	// Default: false.
	EmitUnpopulated bool
	// UseProtoNames uses proto field name instead of lowerCamelCase name in JSON
	// field names.
	UseProtoNames bool
	// UseEnumNumbers emits enum values as numbers.
	UseEnumNumbers bool
}

func ParseOptions

func ParseOptions(setters ...Option) *Options

ParseOptions parses functional options and merge them to default Options.

Jump to

Keyboard shortcuts

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