zdpgo_yaml

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2022 License: MIT Imports: 18 Imported by: 1

README

zdpgo_yaml

Golang处理yaml文件的专用库

项目地址:https://github.com/zhangdapeng520/zdpgo_yaml

版本历史

  • v0.1.0 2022/03/28 基本功能,读取配置和默认配置
  • v0.1.1 2022/04/21 新增:读写配置
  • v0.1.2 2022/07/08 优化:代码优化
  • v0.1.3 2022/08/22 新增:函数级别的读写配置

使用案例

请查询examples下面的使用示例

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FutureLineWrap added in v0.1.3

func FutureLineWrap()

FutureLineWrap globally disables line wrapping when encoding long strings. This is a temporary and thus deprecated method introduced to faciliate migration towards v3, which offers more control of line lengths on individual encodings, and has a default matching the behavior introduced by this function.

The default formatting of v2 was erroneously changed in v2.3.0 and reverted in v2.4.0, at which point this function was introduced to help migration.

func Marshal added in v0.1.3

func Marshal(in interface{}) (out []byte, err error)

Marshal serializes the value provided into a YAML document. The structure of the generated document will reflect the structure of the value itself. Maps and pointers (to struct, string, int, etc) are accepted as the in value.

Struct fields are only marshalled if they are exported (have an upper case first letter), and are marshalled using the field name lowercased as the default key. Custom keys may be defined via the "yaml" name in the field tag: the content preceding the first comma is used as the key, and the following comma-separated options are used to tweak the marshalling process. Conflicting names result in a runtime error.

The field tag format accepted is:

`(...) yaml:"[<key>][,<flag1>[,<flag2>]]" (...)`

The following flags are currently supported:

omitempty    Only include the field if it's not set to the zero
             value for the type or to empty slices or maps.
             Zero valued structs will be omitted if all their public
             fields are zero, unless they implement an IsZero
             method (see the IsZeroer interface type), in which
             case the field will be excluded if IsZero returns true.

flow         Marshal using a flow style (useful for structs,
             sequences and maps).

inline       Inline the field, which must be a struct or a map,
             causing all of its fields or keys to be processed as if
             they were part of the outer struct. For maps, keys must
             not conflict with the yaml keys of other struct fields.

In addition, if the key is "-", the field is ignored.

For example:

type T struct {
    F int `yaml:"a,omitempty"`
    B int
}
yaml.Marshal(&T{B: 2}) // Returns "b: 2\n"
yaml.Marshal(&T{F: 1}} // Returns "a: 1\nb: 0\n"

func Read added in v0.1.5

func Read(configPath string, configObj interface{}) error

Read 读取yaml文件并映射到结构体

func ReadConfig added in v0.1.4

func ReadConfig(configPath string, configObj interface{}) error

ReadConfig 读取yaml文件配置

func Save added in v0.1.5

func Save(filePath string, configObj interface{}) error

Save 保存数据到yaml

func Unmarshal added in v0.1.3

func Unmarshal(in []byte, out interface{}) (err error)

Unmarshal decodes the first document found within the in byte slice and assigns decoded values into the out value.

Maps and pointers (to a struct, string, int, etc) are accepted as out values. If an internal pointer within a struct is not initialized, the yaml package will initialize it if necessary for unmarshalling the provided data. The out parameter must not be nil.

The type of the decoded values should be compatible with the respective values in out. If one or more values cannot be decoded due to a type mismatches, decoding continues partially until the end of the YAML content, and a *yaml.TypeError is returned with details for all missed values.

Struct fields are only unmarshalled if they are exported (have an upper case first letter), and are unmarshalled using the field name lowercased as the default key. Custom keys may be defined via the "yaml" name in the field tag: the content preceding the first comma is used as the key, and the following comma-separated options are used to tweak the marshalling process (see Marshal). Conflicting names result in a runtime error.

For example:

type T struct {
    F int `yaml:"a,omitempty"`
    B int
}
var t T
yaml.Unmarshal([]byte("a: 1\nb: 2"), &t)

See the documentation of Marshal for the format of tags and a list of supported tag options.

func UnmarshalStrict added in v0.1.3

func UnmarshalStrict(in []byte, out interface{}) (err error)

UnmarshalStrict is like Unmarshal except that any fields that are found in the data that do not have corresponding struct members, or mapping keys that are duplicates, will result in an error.

Types

type Decoder added in v0.1.3

type Decoder struct {
	// contains filtered or unexported fields
}

A Decoder reads and decodes YAML values from an input stream.

func NewDecoder added in v0.1.3

func NewDecoder(r io.Reader) *Decoder

NewDecoder returns a new decoder that reads from r.

The decoder introduces its own buffering and may read data from r beyond the YAML values requested.

func (*Decoder) Decode added in v0.1.3

func (dec *Decoder) Decode(v interface{}) (err error)

Decode reads the next YAML-encoded value from its input and stores it in the value pointed to by v.

See the documentation for Unmarshal for details about the conversion of YAML into a Go value.

func (*Decoder) SetStrict added in v0.1.3

func (dec *Decoder) SetStrict(strict bool)

SetStrict sets whether strict decoding behaviour is enabled when decoding items in the data (see UnmarshalStrict). By default, decoding is not strict.

type Encoder added in v0.1.3

type Encoder struct {
	// contains filtered or unexported fields
}

An Encoder writes YAML values to an output stream.

func NewEncoder added in v0.1.3

func NewEncoder(w io.Writer) *Encoder

NewEncoder returns a new encoder that writes to w. The Encoder should be closed after use to flush all data to w.

func (*Encoder) Close added in v0.1.3

func (e *Encoder) Close() (err error)

Close closes the encoder by writing any remaining data. It does not write a stream terminating string "...".

func (*Encoder) Encode added in v0.1.3

func (e *Encoder) Encode(v interface{}) (err error)

Encode writes the YAML encoding of v to the stream. If multiple items are encoded to the stream, the second and subsequent document will be preceded with a "---" document separator, but the first will not.

See the documentation for Marshal for details about the conversion of Go values to YAML.

type IsZeroer added in v0.1.3

type IsZeroer interface {
	IsZero() bool
}

IsZeroer is used to check whether an object is zero to determine whether it should be omitted when marshaling with the omitempty flag. One notable implementation is time.Time.

type MapItem added in v0.1.3

type MapItem struct {
	Key, Value interface{}
}

MapItem is an item in a MapSlice.

type MapSlice added in v0.1.3

type MapSlice []MapItem

MapSlice encodes and decodes as a YAML map. The order of keys is preserved when encoding and decoding.

type Marshaler added in v0.1.3

type Marshaler interface {
	MarshalYAML() (interface{}, error)
}

The Marshaler interface may be implemented by types to customize their behavior when being marshaled into a YAML document. The returned value is marshaled in place of the original value implementing Marshaler.

If an error is returned by MarshalYAML, the marshaling procedure stops and returns with the provided error.

type TypeError added in v0.1.3

type TypeError struct {
	Errors []string
}

A TypeError is returned by Unmarshal when one or more fields in the YAML document cannot be properly decoded into the requested types. When this error is returned, the value is still unmarshaled partially.

func (*TypeError) Error added in v0.1.3

func (e *TypeError) Error() string

type Unmarshaler added in v0.1.3

type Unmarshaler interface {
	UnmarshalYAML(unmarshal func(interface{}) error) error
}

The Unmarshaler interface may be implemented by types to customize their behavior when being unmarshaled from a YAML document. The UnmarshalYAML method receives a function that may be called to unmarshal the original YAML value into a field or variable. It is safe to call the unmarshal function parameter more than once if necessary.

type Yaml

type Yaml struct {
}

func New

func New() *Yaml

func (*Yaml) Dumps added in v0.1.1

func (y *Yaml) Dumps(in interface{}) (result []byte, err error)

func (*Yaml) DumpsString added in v0.1.1

func (y *Yaml) DumpsString(in interface{}) (result string, err error)

DumpsString 将结构体对象序列化为yaml数据,得到的是字符串

func (*Yaml) Loads added in v0.1.1

func (y *Yaml) Loads(in []byte, out interface{}) (err error)

func (*Yaml) LoadsString added in v0.1.1

func (y *Yaml) LoadsString(in string, out interface{}) (err error)

LoadsString 加载yaml字符串为结构体

func (*Yaml) Marshal added in v0.1.1

func (y *Yaml) Marshal(in interface{}) (out []byte, err error)

Marshal 将结构体对象序列化为yaml数据

func (*Yaml) RandomStr added in v0.1.1

func (y *Yaml) RandomStr(n int) string

RandomStr 生成指定长度的随机字符串

func (*Yaml) Read added in v0.1.1

func (y *Yaml) Read(configPath string, configObj interface{}) error

Read 读取yaml文件并映射到结构体

func (*Yaml) ReadConfig

func (y *Yaml) ReadConfig(configPath string, configObj interface{}) error

ReadConfig 读取yaml文件配置

func (*Yaml) ReadDefaultConfig

func (y *Yaml) ReadDefaultConfig(configObj interface{}) error

ReadDefaultConfig 读取默认配置。默认使用config/config.yaml和config/secret/.config.yaml

func (*Yaml) Save added in v0.1.1

func (y *Yaml) Save(filePath string, configObj interface{}) error

Save 保存数据到yaml

func (*Yaml) Unmarshal added in v0.1.1

func (y *Yaml) Unmarshal(in []byte, out interface{}) (err error)

Unmarshal 将yaml数据反序列化为结构体

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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