Documentation
¶
Overview ¶
Package store provides functions to store a protobuf message to different formats: json, bin, and txt.
Index ¶
- func MarshalToBin(msg proto.Message) (out []byte, err error)
- func MarshalToJSON(msg proto.Message, options *MarshalOptions) (out []byte, err error)
- func MarshalToText(msg proto.Message, pretty bool) (out []byte, err error)
- func Store(msg proto.Message, dir string, fmt format.Format, options ...Option) error
- type FilterFunc
- type MarshalOptions
- type Option
- type Options
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MarshalToBin ¶
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 ¶
MarshalToText marshals the given proto.Message in the text (textproto) format. You can depend on the output being stable.
Types ¶
type FilterFunc ¶
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 {
// Location represents the collection of time offsets in use in a geographical area.
// - If the name is "" or "UTC", LoadLocation returns UTC.
// - If the name is "Local", LoadLocation returns Local.
// - Otherwise, the name is taken to be a location name corresponding to a file in the
// IANA Time Zone database, such as "America/New_York", "Asia/Shanghai", and so on.
//
// See https://go.dev/src/time/zoneinfo_abbrs_windows.go.
//
// Default: "Local".
LocationName string `yaml:"locationName"`
// 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
// EmitTimezones specifies whether to emit timestamp in string format with
// timezones (as indicated by an offset).
EmitTimezones 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 EmitTimezones ¶ added in v0.13.0
EmitTimezones specifies whether to emit timestamp in string format with timezones (as indicated by an offset).
func EmitUnpopulated ¶
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 LocationName ¶ added in v0.13.0
LocationName specifies the location name for timezone processing.
func UseEnumNumbers ¶
UseEnumNumbers specifies whether to emit enum values as numbers for JSON field values.
func UseProtoNames ¶
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
// Location represents the collection of time offsets in use in a geographical area.
// - If the name is "" or "UTC", LoadLocation returns UTC.
// - If the name is "Local", LoadLocation returns Local.
// - Otherwise, the name is taken to be a location name corresponding to a file in the
// IANA Time Zone database, such as "America/New_York", "Asia/Shanghai", and so on.
//
// See https://go.dev/src/time/zoneinfo_abbrs_windows.go.
//
// Default: "Local".
LocationName string `yaml:"locationName"`
// 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
// EmitTimezones specifies whether to emit timestamp in string format with
// timezones (as indicated by an offset).
//
// NOTE: use with option "LocationName".
EmitTimezones 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 ¶
ParseOptions parses functional options and merge them to default Options.