codec

package
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// WeaklyIgnoreSeperator is used to ignore the seperator in the tag name.
	WeaklyIgnoreSeperator = true
	// WeaklyDashUnderscore is used to convert dash to underscore in the tag name.
	// If WeaklyIgnoreSeperator is set to true then this will be ignored.
	WeaklyDashUnderscore = false

	// HooksDecode functions to convert value before decoding.
	HooksDecode = []struct2.HookDecodeFunc{

		func(t1, t2 reflect.Type, data interface{}) (interface{}, error) {
			if t2 != reflect.TypeOf(time.Duration(0)) {
				return data, nil
			}

			switch t1.Kind() {
			case reflect.String:
				return str2duration.ParseDuration(data.(string))
			case reflect.Int:
				return time.Duration(data.(int)), nil
			case reflect.Int64:
				return time.Duration(data.(int64)), nil
			case reflect.Float64:
				return time.Duration(data.(float64)), nil
			default:
				return data, nil
			}
		},
	}
)
View Source
var BackupTagName = "cfg"

BackupTagName is the tag name used if tagname not found in the struct tags.

Functions

func LoadReaderWithDecoder

func LoadReaderWithDecoder(r io.Reader, to interface{}, decoder Decoder, tag string) error

LoadReaderWithDecoder will decode input in `r` into `to` by using `decoder`.

func MapDecoder

func MapDecoder(input, output interface{}, tag string) error

MapDecoder implements the reformat package, it exposes functionality to convert an arbitrary map[string]interface{} into a native Go structure with given tag name.

Types

type Decoder

type Decoder interface {
	Decode(r io.Reader, to interface{}) error
}

Decoder is a interface that will decode reader `r` into `to`.

`to` will already be pointer to struct, so no need to further prepare it for decoding.

type JSON

type JSON struct {
	Strict bool
}

JSON is a json decoder.

func (JSON) Decode

func (c JSON) Decode(r io.Reader, to interface{}) error

Decode is a decoder function for json.

type TOML

type TOML struct{}

TOML is a toml decoder.

func (TOML) Decode

func (c TOML) Decode(r io.Reader, to interface{}) error

Decode is a decoder function for yaml.

type YAML

type YAML struct {
	Strict bool
}

YAML is a yaml decoder.

func (YAML) Decode

func (c YAML) Decode(r io.Reader, to interface{}) error

Decode is a decoder function for yaml.

Jump to

Keyboard shortcuts

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