load

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2025 License: MIT Imports: 20 Imported by: 4

Documentation

Overview

Package load provides functions to load a protobuf message from different formats:

  • output formats: JSON, Bin, Text
  • input formats: Excel, CSV, XML, YAML

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

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

Load loads message's content based on the given dir, format, and load options.

func LoadMessager added in v0.14.0

func LoadMessager(msg proto.Message, path string, fmt format.Format, opts *MessagerOptions) error

LoadMessager is the default LoadFunc which loads the message's content based on the given path, format, and options.

NOTE: only output formats (JSON, Bin, Text) are supported.

func LoadMessagerInDir added in v0.14.0

func LoadMessagerInDir(msg proto.Message, dir string, fmt format.Format, opts *MessagerOptions) error

LoadMessagerInDir loads message's content in the given dir, based on format and messager options.

func Unmarshal added in v0.14.0

func Unmarshal(content []byte, msg proto.Message, path string, fmt format.Format, opts *MessagerOptions) error

Unmarshal unmarshals the message based on the given content, format, and options.

NOTE: only output formats (JSON, Bin, Text) are supported.

Types

type BaseOptions added in v0.14.0

type BaseOptions struct {
	// Location represents the collection of time offsets in use in
	// a geographical area.
	//
	// NOTE: only input formats(Excel, CSV, XML, YAML) are supported.
	//
	// If the name is "" or "UTC", LoadLocation returns UTC.
	// If the name is "Local", LoadLocation returns Local.
	//
	// Default: "Local".
	LocationName string

	// IgnoreUnknownFields signifies whether to ignore unknown JSON fields
	// during parsing.
	//
	// NOTE: only JSON format is supported.
	//
	// Default: nil.
	IgnoreUnknownFields *bool

	// PatchDirs specifies the directory paths for config patching.
	//
	// NOTE: only output formats (JSON, Bin, Text) are supported.
	//
	// Default: nil.
	PatchDirs []string

	// Mode specifies the loading mode for config patching.
	//
	// NOTE: only output formats (JSON, Bin, Text) are supported.
	//
	// Default: ModeAll.
	Mode LoadMode

	// ReadFunc reads the config file and returns its content.
	//
	// Default: [os.ReadFile].
	ReadFunc ReadFunc

	// LoadFunc loads a messager's content.
	//
	// NOTE: only output formats (JSON, Bin, Text) are supported.
	//
	// Default: [LoadMessager].
	LoadFunc LoadFunc

	// SubdirRewrites rewrites subdir paths (relative to workbook name option
	// in .proto file).
	//
	// NOTE: only input formats (Excel, CSV, XML, YAML) are supported.
	//
	// Default: nil.
	SubdirRewrites map[string]string
}

BaseOptions is the common struct for both global-level and messager-level options.

func (*BaseOptions) GetIgnoreUnknownFields added in v0.14.0

func (o *BaseOptions) GetIgnoreUnknownFields() bool

GetIgnoreUnknownFields returns whether to ignore unknown fields when loading JSON.

type LoadFunc added in v0.14.0

type LoadFunc func(msg proto.Message, path string, fmt format.Format, opts *MessagerOptions) error

LoadFunc defines a func which can load message's content based on the given path, format, and options.

NOTE: only output formats (JSON, Bin, Text) are supported.

type LoadMode added in v0.12.0

type LoadMode int
const (
	ModeAll       LoadMode // Load all related files
	ModeOnlyMain           // Only load the main file
	ModeOnlyPatch          // Only load the patch files
)

type MessagerOptions added in v0.14.0

type MessagerOptions struct {
	BaseOptions
	// Path specifies messager's config file path.
	// If specified, then the main messager will be parsed directly,
	// other than the specified load dir.
	//
	// NOTE: only output formats(JSON, Bin, Text) are supported.
	//
	// Default: nil.
	Path string

	// PatchPaths specifies one or multiple corresponding patch file paths.
	// If specified, then main messager will be patched.
	//
	// NOTE: only output formats (JSON, Bin, Text) are supported.
	//
	// Default: nil.
	PatchPaths []string
}

MessagerOptions is the options struct for a messager.

func ParseMessagerOptionsFromOptions added in v0.14.0

func ParseMessagerOptionsFromOptions(o *Options, name string) *MessagerOptions

ParseMessagerOptionsFromOptions parses messager options with both global-level and messager-level options taken into consideration.

type Option added in v0.9.16

type Option func(*Options)

Option is the functional option type.

func IgnoreUnknownFields added in v0.10.6

func IgnoreUnknownFields() Option

IgnoreUnknownFields ignores unknown JSON fields during parsing.

func LocationName added in v0.10.5

func LocationName(name string) Option

LocationName sets TZ location name for parsing datetime format.

func Mode added in v0.12.0

func Mode(mode LoadMode) Option

Mode specifies the loading mode for config patching.

NOTE: only JSON, Bin, and Text formats are supported.

func PatchDirs added in v0.12.0

func PatchDirs(dirs ...string) Option

PatchDirs specifies the directory paths for config patching.

func PatchPaths deprecated added in v0.11.0

func PatchPaths(paths map[string][]string) Option

PatchPaths maps each messager name to one or multiple corresponding patch file paths. If specified, then main messager will be patched.

NOTE: only JSON, Bin, and Text formats are supported.

Deprecated: use WithMessagerOptions instead.

func Paths deprecated added in v0.11.0

func Paths(paths map[string]string) Option

Paths maps each messager name to a corresponding config file path. If specified, then the main messager will be parsed from the file directly, other than the specified load dir.

NOTE: only JSON, Bin, and Text formats are supported.

Deprecated: use WithMessagerOptions instead.

func SubdirRewrites added in v0.9.16

func SubdirRewrites(subdirRewrites map[string]string) Option

SubdirRewrites rewrites subdir paths (relative to workbook name option in .proto file).

func WithLoadFunc added in v0.14.0

func WithLoadFunc(loadFunc LoadFunc) Option

WithLoadFunc sets a custom load func.

func WithMessagerOptions added in v0.14.0

func WithMessagerOptions(options map[string]*MessagerOptions) Option

WithMessagerOptions sets the messager options.

func WithReadFunc added in v0.12.0

func WithReadFunc(readFunc ReadFunc) Option

WithReadFunc sets a custom read func.

type Options added in v0.9.16

type Options struct {
	BaseOptions
	// MessagerOptions maps each messager name to a MessageOptions.
	// If specified, then the messager will be parsed with the given options
	// directly.
	//
	// Default: empty.
	MessagerOptions map[string]*MessagerOptions
}

Options is the options struct, which contains both global-level and messager-level options.

func ParseOptions added in v0.9.16

func ParseOptions(setters ...Option) *Options

ParseOptions parses functional options and merge them to default Options.

type ReadFunc added in v0.12.0

type ReadFunc func(name string) ([]byte, error)

ReadFunc reads the config file and returns its content.

Jump to

Keyboard shortcuts

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