Documentation ¶
Overview ¶
Generic marshalling and unmarshalling interfaces.
Index ¶
- Variables
- func Formats() []string
- func Marshal(path string, value interface{}) ([]byte, error)
- func MarshalDefault(path string, def Marshaller, value interface{}) ([]byte, error)
- func MarshalFile(path string, value interface{}) error
- func Unmarshal(path string, data []byte, value interface{}) error
- func UnmarshalAsset(name string, assets map[string][]byte, value interface{}) error
- func UnmarshalDefault(path string, data []byte, def Marshaller, value interface{}) error
- func UnmarshalFile(path string, value interface{}) error
- func UnmarshalFilePrefix(prefix string, value interface{}) (string, error)
- type FileMarshaller
- type FileMarshallers
- func (fm FileMarshallers) ByExtension(path string) FileMarshaller
- func (fm FileMarshallers) ByFormat(format string) FileMarshaller
- func (fm FileMarshallers) Formats() []string
- func (fm FileMarshallers) Marshal(path string, value interface{}) ([]byte, error)
- func (fm FileMarshallers) MarshalDefault(path string, def Marshaller, value interface{}) ([]byte, error)
- func (fm FileMarshallers) MarshalFile(path string, value interface{}) error
- func (fm FileMarshallers) Unmarshal(path string, data []byte, value interface{}) error
- func (fm FileMarshallers) UnmarshalAsset(name string, assets map[string][]byte, value interface{}) error
- func (fm FileMarshallers) UnmarshalDefault(path string, data []byte, def Marshaller, value interface{}) error
- func (fm FileMarshallers) UnmarshalFile(path string, value interface{}) error
- func (fm FileMarshallers) UnmarshalFilePrefix(prefix string, value interface{}) (string, error)
- type GobEncoder
- type JsonEncoder
- type Marshaller
- type TomlEncoder
- type YamlEncoder
Constants ¶
This section is empty.
Variables ¶
var Gob = &GobEncoder{}
Use marshal.Gob to encode/decode from Gob format.
var Json = &JsonEncoder{}
Use marshal.Json to encode/decode from Json format.
var Known = []FileMarshaller{ Toml, Json, Yaml, Gob, }
Set of known encoders/decoders, in preference order.
var Toml = &TomlEncoder{}
Use marshal.Toml to encode/decode from Toml format.
var Yaml = &YamlEncoder{}
Use marshal.Yaml to encode/decode from Yaml format.
Functions ¶
func Formats ¶
func Formats() []string
Formats is the same as FileMarshallers.Formats, but uses the default list of Marshallers.
func Marshal ¶
Marshal will encode the 'value' object using the best encoder depending on the 'path' extension. Returns the marshalled object, or an error.
func MarshalDefault ¶
func MarshalDefault(path string, def Marshaller, value interface{}) ([]byte, error)
MarshalDefault is the same as FileMarshallers.MarshalDefault, but uses the default list of Marshallers.
func MarshalFile ¶
MarshalFile is the same as FileMarshallers.MarshalFile, but uses the default list of Marshallers.
func Unmarshal ¶
Unmarshal will decode the 'data' into the 'value' object based on the 'path' extension. value must be a pointer to the desired type. Returns error if the byte stream could not be decoded.
func UnmarshalAsset ¶
UnmarshalAsset is the same as FileMarshallers.UnmarshalAsset, but uses the default list of Marshallers.
func UnmarshalDefault ¶
func UnmarshalDefault(path string, data []byte, def Marshaller, value interface{}) error
UnmarshalDefault is the same as FileMarshallers.UnmarshalDefault, but uses the default list of Marshallers.
func UnmarshalFile ¶
UnmarshalFile is the same as FileMarshallers.UnmarshalFile, but uses the default list of Marshallers.
func UnmarshalFilePrefix ¶
UnmarshalFilePrefix is the same as FileMarshallers.UnmarshalFilePrefix, but uses the default list of Marshallers.
Types ¶
type FileMarshaller ¶
type FileMarshaller interface { Marshaller // Returns the typical file extension used by files in this format. Extension() string }
func ByExtension ¶
func ByExtension(path string) FileMarshaller
ByExtension is the same as FileMarshallers.ByExtension, but uses the default list of Marshallers.
func ByFormat ¶
func ByFormat(path string) FileMarshaller
ByFormat is the same as FileMarshallers.ByFormat, but uses the default list of Marshallers.
type FileMarshallers ¶
type FileMarshallers []FileMarshaller
Represents a sorted list of marshallers. Lowest index is the most preferred marshaller.
func (FileMarshallers) ByExtension ¶
func (fm FileMarshallers) ByExtension(path string) FileMarshaller
ByExtension returns the first FileMarshaller based on the extension of the path provided. `path` can be either a local file path or a URL; if `path` is a URL, the Path component of the URL is used for extension-based detection.
func (FileMarshallers) ByFormat ¶
func (fm FileMarshallers) ByFormat(format string) FileMarshaller
ByExtension returns the first FileMarshaller based on the format specified. Format is generally a lowercase string like "json", "yaml", ...
func (FileMarshallers) Formats ¶
func (fm FileMarshallers) Formats() []string
func (FileMarshallers) Marshal ¶
func (fm FileMarshallers) Marshal(path string, value interface{}) ([]byte, error)
Marshal will marshal the specified value based on the extension of the specified path. If the extension is unknown, an error is returned.
Returns a byte array with the marshalled value, or error.
func (FileMarshallers) MarshalDefault ¶
func (fm FileMarshallers) MarshalDefault(path string, def Marshaller, value interface{}) ([]byte, error)
MarshalDefault will marshal the specified value based on the extension of the specified path. If the extension is unknown, the specified default marshaller is used.
Returns a byte array with the marshalled value, or error.
func (FileMarshallers) MarshalFile ¶
func (fm FileMarshallers) MarshalFile(path string, value interface{}) error
MarshalFile invokes Marshal() to then save the content in a file.
func (FileMarshallers) Unmarshal ¶
func (fm FileMarshallers) Unmarshal(path string, data []byte, value interface{}) error
Unmarshal will determine the format of the file based on the extension, and unmarshal it in value.
value is a pointer to the object to be parsed. If the extension is unknown, an error is returned.
func (FileMarshallers) UnmarshalAsset ¶
func (fm FileMarshallers) UnmarshalAsset(name string, assets map[string][]byte, value interface{}) error
UnmarshalAsset tries to find an asset that can be decoded, and decodes it.
UnmarshalAsset expect an 'assets' dict of {'path': '<configuration-blob>'}, generally representing the name of a file, and the corresponding bytes.
'name' is the name of a configuration to parse, without extension. 'value' is the pointer to an object to decode from the configuration file.
UnmarshalAsset will iterate through the known extension, and see if an asset by the name of 'name'.'extension' exists. If it does, it will try to decode the blob of bytes into the value.
For example:
UnmarshalAsset("config", map[string][]byte{"config.yaml": ...}, &config)
Will look for "config.json", "config.toml", "config.yaml", in the assets dict. The value of the first found will be decoded in config.
This function is typically used in conjunction with the go_embed_data rule with the bazel build system.
Similarly to what would happen if a config file was not found on disk, returns os.ErrNotExist if no valid file could be found in the assets.
func (FileMarshallers) UnmarshalDefault ¶
func (fm FileMarshallers) UnmarshalDefault(path string, data []byte, def Marshaller, value interface{}) error
UnmarshalDefault will determine the format of the file based on the extension, and unmarshal it in value.
value is a pointer to the object to be parsed. If the extension is unknown, the specified default marshaller is used.
func (FileMarshallers) UnmarshalFile ¶
func (fm FileMarshallers) UnmarshalFile(path string, value interface{}) error
UnmarshalFile invokes Unarshal() to parse the content of a file.
func (FileMarshallers) UnmarshalFilePrefix ¶
func (fm FileMarshallers) UnmarshalFilePrefix(prefix string, value interface{}) (string, error)
UnmarshalFilePrefix will attempt each FileMarshaller extension in order, and open the first that succeeds.
Returns the full path of the file that succeeded, or error.
type GobEncoder ¶
type GobEncoder struct{}
func (*GobEncoder) Extension ¶
func (j *GobEncoder) Extension() string
func (*GobEncoder) Marshal ¶
func (g *GobEncoder) Marshal(value interface{}) ([]byte, error)
func (*GobEncoder) Unmarshal ¶
func (j *GobEncoder) Unmarshal(data []byte, value interface{}) error
type JsonEncoder ¶
type JsonEncoder struct{}
func (*JsonEncoder) Extension ¶
func (j *JsonEncoder) Extension() string
func (*JsonEncoder) Marshal ¶
func (j *JsonEncoder) Marshal(value interface{}) ([]byte, error)
func (*JsonEncoder) Unmarshal ¶
func (j *JsonEncoder) Unmarshal(data []byte, value interface{}) error
type Marshaller ¶
type Marshaller interface { Marshal(value interface{}) ([]byte, error) Unmarshal(data []byte, value interface{}) error }
Implement the Marshaller interface to provide mechanisms to turn objects into string, and vice-versa.
type TomlEncoder ¶
type TomlEncoder struct{}
func (*TomlEncoder) Extension ¶
func (j *TomlEncoder) Extension() string
func (*TomlEncoder) Marshal ¶
func (j *TomlEncoder) Marshal(value interface{}) ([]byte, error)
func (*TomlEncoder) Unmarshal ¶
func (j *TomlEncoder) Unmarshal(data []byte, value interface{}) error
type YamlEncoder ¶
type YamlEncoder struct{}
func (*YamlEncoder) Extension ¶
func (j *YamlEncoder) Extension() string
func (*YamlEncoder) Marshal ¶
func (j *YamlEncoder) Marshal(value interface{}) ([]byte, error)
func (*YamlEncoder) Unmarshal ¶
func (j *YamlEncoder) Unmarshal(data []byte, value interface{}) error